From 473fa2671d56d0dac7c0e5ecc16e2a5a183bb9c4 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Tue, 17 Jun 2025 19:00:56 -0400 Subject: [PATCH] Migrate Document templates to its own table --- admin_document_template.php | 57 ++++++++----------- admin_document_template_details.php | 24 ++++---- ajax/ajax_document_template_edit.php | 20 +++---- client_documents.php | 3 - database_updates.php | 54 +++++++++++++++++- db.sql | 24 +++++++- includes/database_version.php | 2 +- ...lient_document_add_from_template_modal.php | 6 +- post/admin/admin_document_template.php | 31 +++++++--- post/user/document.php | 33 ++--------- 10 files changed, 150 insertions(+), 104 deletions(-) diff --git a/admin_document_template.php b/admin_document_template.php index c2181e93..eec30ac8 100644 --- a/admin_document_template.php +++ b/admin_document_template.php @@ -1,27 +1,19 @@ "> - - Template Name + + Template Name - - Created + + Created - - Updated + + Updated @@ -78,27 +70,26 @@ - -
+ +
- -
+ +
- + diff --git a/admin_document_template_details.php b/admin_document_template_details.php index 355641c2..0d638d55 100644 --- a/admin_document_template_details.php +++ b/admin_document_template_details.php @@ -11,19 +11,19 @@ $purifier_config->set('Cache.DefinitionImpl', null); // Disable cache by setting $purifier_config->set('URI.AllowedSchemes', ['data' => true, 'src' => true, 'http' => true, 'https' => true]); $purifier = new HTMLPurifier($purifier_config); -if (isset($_GET['document_id'])) { - $document_id = intval($_GET['document_id']); +if (isset($_GET['document_template_id'])) { + $document_template_id = intval($_GET['document_template_id']); } -$sql_document = mysqli_query($mysqli, "SELECT * FROM documents WHERE document_template = 1 AND document_id = $document_id"); +$sql_document = mysqli_query($mysqli, "SELECT * FROM document_templates WHERE document_template_id = $document_template_id"); $row = mysqli_fetch_array($sql_document); -$document_name = nullable_htmlentities($row['document_name']); -$document_description = nullable_htmlentities($row['document_description']); -$document_content = $purifier->purify($row['document_content']); -$document_created_at = nullable_htmlentities($row['document_created_at']); -$document_updated_at = nullable_htmlentities($row['document_updated_at']); +$document_template_name = nullable_htmlentities($row['document_template_name']); +$document_template_description = nullable_htmlentities($row['document_template_description']); +$document_template_content = $purifier->purify($row['document_template_content']); +$document_template_created_at = nullable_htmlentities($row['document_template_created_at']); +$document_template_updated_at = nullable_htmlentities($row['document_template_updated_at']); ?> @@ -37,27 +37,27 @@ $document_updated_at = nullable_htmlentities($row['document_updated_at']); - +
-

+

- +
diff --git a/ajax/ajax_document_template_edit.php b/ajax/ajax_document_template_edit.php index 2c3fe1bc..fb69d1c1 100644 --- a/ajax/ajax_document_template_edit.php +++ b/ajax/ajax_document_template_edit.php @@ -2,38 +2,38 @@ require_once '../includes/ajax_header.php'; -$document_id = intval($_GET['id']); +$document_template_id = intval($_GET['id']); -$sql = mysqli_query($mysqli, "SELECT * FROM documents WHERE document_id = $document_id LIMIT 1"); +$sql = mysqli_query($mysqli, "SELECT * FROM document_templates WHERE document_template_id = $document_template_id LIMIT 1"); $row = mysqli_fetch_array($sql); -$document_name = nullable_htmlentities($row['document_name']); -$document_description = nullable_htmlentities($row['document_description']); -$document_content = nullable_htmlentities($row['document_content']); +$document_template_name = nullable_htmlentities($row['document_template_name']); +$document_template_description = nullable_htmlentities($row['document_template_description']); +$document_template_content = nullable_htmlentities($row['document_template_content']); // Generate the HTML form content using output buffering. ob_start(); ?>
- + diff --git a/client_documents.php b/client_documents.php index c9220134..06ed1c3e 100644 --- a/client_documents.php +++ b/client_documents.php @@ -41,8 +41,6 @@ if ($get_folder_id == 0 && $_GET["q"]) { "SELECT SQL_CALC_FOUND_ROWS * FROM documents LEFT JOIN users ON document_created_by = user_id WHERE document_client_id = $client_id - AND document_template = 0 - AND document_archived_at IS NULL $query_snippet ORDER BY $sort $order LIMIT $record_from, $record_to" @@ -53,7 +51,6 @@ if ($get_folder_id == 0 && $_GET["q"]) { "SELECT SQL_CALC_FOUND_ROWS * FROM documents LEFT JOIN users ON document_created_by = user_id WHERE document_client_id = $client_id - AND document_template = 0 AND document_folder_id = $folder AND document_archived_at IS NULL $query_snippet diff --git a/database_updates.php b/database_updates.php index 601a5ae8..6da12e04 100644 --- a/database_updates.php +++ b/database_updates.php @@ -3497,10 +3497,58 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) { mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.1.6'"); } - // if (CURRENT_DATABASE_VERSION == '2.1.6') { - // // Insert queries here required to update to DB version 2.1.7 + if (CURRENT_DATABASE_VERSION == '2.1.6') { + mysqli_query($mysqli, "CREATE TABLE `document_templates` ( + `document_template_id` INT(11) NOT NULL AUTO_INCREMENT, + `document_template_name` VARCHAR(200) NOT NULL, + `document_template_description` TEXT DEFAULT NULL, + `document_template_content` LONGTEXT NOT NULL, + `document_template_created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + `document_template_updated_at` DATETIME NULL ON UPDATE CURRENT_TIMESTAMP, + `document_template_archived_at` DATETIME NULL DEFAULT NULL, + `document_template_created_by` INT(11) NOT NULL DEFAULT 0, + `document_template_updated_by` INT(11) NOT NULL DEFAULT 0, + PRIMARY KEY (`document_template_id`) + )"); + + // Copy Document Templates over to new document templates table + mysqli_query($mysqli, " + INSERT INTO document_templates ( + document_template_name, + document_template_description, + document_template_content, + document_template_created_at, + document_template_updated_at, + document_template_archived_at, + document_template_created_by, + document_template_updated_by + ) + SELECT + document_name, + document_description, + document_content, + document_created_at, + document_updated_at, + document_archived_at, + document_created_by, + document_updated_by + FROM + documents + WHERE + document_template = 1 + "); + + mysqli_query($mysqli, "DELETE FROM documents WHERE document_template = 1"); + + mysqli_query($mysqli, "ALTER TABLE `documents` DROP `document_template`"); + + mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.1.7'"); + } + + // if (CURRENT_DATABASE_VERSION == '2.1.7') { + // // Insert queries here required to update to DB version 2.1.8 // // Then, update the database to the next sequential version - // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.1.7'"); + // mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.1.8'"); // } } else { diff --git a/db.sql b/db.sql index 22e6409f..36c642bc 100644 --- a/db.sql +++ b/db.sql @@ -830,6 +830,27 @@ CREATE TABLE `document_files` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `document_templates` +-- + +DROP TABLE IF EXISTS `document_templates`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8mb4 */; +CREATE TABLE `document_templates` ( + `document_template_id` int(11) NOT NULL AUTO_INCREMENT, + `document_template_name` varchar(200) NOT NULL, + `document_template_description` text DEFAULT NULL, + `document_template_content` longtext NOT NULL, + `document_template_created_at` datetime NOT NULL DEFAULT current_timestamp(), + `document_template_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(), + `document_template_archived_at` datetime DEFAULT NULL, + `document_template_created_by` int(11) NOT NULL DEFAULT 0, + `document_template_updated_by` int(11) NOT NULL DEFAULT 0, + PRIMARY KEY (`document_template_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `document_versions` -- @@ -868,7 +889,6 @@ CREATE TABLE `documents` ( `document_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(), `document_archived_at` datetime DEFAULT NULL, `document_accessed_at` datetime DEFAULT NULL, - `document_template` tinyint(1) NOT NULL DEFAULT 0, `document_folder_id` int(11) NOT NULL DEFAULT 0, `document_created_by` int(11) NOT NULL DEFAULT 0, `document_updated_by` int(11) NOT NULL DEFAULT 0, @@ -2522,4 +2542,4 @@ CREATE TABLE `vendors` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2025-06-17 17:56:40 +-- Dump completed on 2025-06-17 19:00:19 diff --git a/includes/database_version.php b/includes/database_version.php index 017876ac..c8f06cff 100644 --- a/includes/database_version.php +++ b/includes/database_version.php @@ -5,4 +5,4 @@ * It is used in conjunction with database_updates.php */ -DEFINE("LATEST_DATABASE_VERSION", "2.1.6"); +DEFINE("LATEST_DATABASE_VERSION", "2.1.7"); diff --git a/modals/client_document_add_from_template_modal.php b/modals/client_document_add_from_template_modal.php index ceb7a5f9..2ccb5a3d 100644 --- a/modals/client_document_add_from_template_modal.php +++ b/modals/client_document_add_from_template_modal.php @@ -20,10 +20,10 @@