From 77d7e7ba0d7bfc991d13fe0a7dbe20459069654e Mon Sep 17 00:00:00 2001 From: Marcus Hill Date: Wed, 5 Jan 2022 21:58:10 +0000 Subject: [PATCH] Add tag functionality to documents --- add_document_modal.php | 26 ++++++++++++++ client_documents.php | 64 +++++++++++++++++++++++++++++++--- db.sql | 24 +++++++++++++ edit_document_modal.php | 28 +++++++++++++++ manage_document_tags_modal.php | 60 +++++++++++++++++++++++++++++++ post.php | 61 ++++++++++++++++++++++++++++++-- 6 files changed, 256 insertions(+), 7 deletions(-) create mode 100644 manage_document_tags_modal.php diff --git a/add_document_modal.php b/add_document_modal.php index cc962216..8c57423d 100644 --- a/add_document_modal.php +++ b/add_document_modal.php @@ -14,6 +14,32 @@
+ + +
+
+ + +
+
+ +
diff --git a/client_documents.php b/client_documents.php index ae258d0d..a4e2582c 100644 --- a/client_documents.php +++ b/client_documents.php @@ -36,14 +36,42 @@ if(isset($_GET['o'])){ $disp = "DESC"; } +# Tag from GET +if (isset($_GET['tag'])) { + $tag = intval($_GET['tag']); + # Avoid doubling up + unset($_GET['tag']); +} +else { + $tag = ''; +} + //Rebuild URL $url_query_strings_sb = http_build_query(array_merge($_GET,array('sb' => $sb, 'o' => $o))); - -$sql = mysqli_query($mysqli,"SELECT SQL_CALC_FOUND_ROWS * FROM documents + +# Currently using two separate queries: one with and one without tags +# If we use a query with tags with no tags set (or even %), then documents appear twice + +$sql_no_tag = "SELECT SQL_CALC_FOUND_ROWS * FROM documents WHERE document_client_id = $client_id AND documents.company_id = $session_company_id - AND (document_name LIKE '%$q%' OR document_content LIKE '%$q%') - ORDER BY $sb $o LIMIT $record_from, $record_to"); + AND (document_name LIKE '%$q%' OR document_content LIKE '%$q%') + ORDER BY $sb $o LIMIT $record_from, $record_to"; + +$sql_with_tag = "SELECT SQL_CALC_FOUND_ROWS * FROM documents + LEFT JOIN documents_tagged ON documents.document_id = documents_tagged.document_id + WHERE document_client_id = $client_id + AND documents.company_id = $session_company_id + AND (document_name LIKE '%$q%' OR document_content LIKE '%$q%') + AND documents_tagged.tag_id LIKE '%$tag%' + ORDER BY $sb $o LIMIT $record_from, $record_to"; + +if (empty($tag)) { + $sql = mysqli_query($mysqli, $sql_no_tag); +} +else { + $sql = mysqli_query($mysqli, $sql_with_tag); +} $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()")); @@ -54,9 +82,24 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()"));

Documents

+
+
+ 0) { + foreach($document_tags as $document_tag) { + echo ""; echo htmlentities($document_tag['tag_name']); echo ""; + } + } + else { + $document_tags = FALSE; + } + ?> +
@@ -97,6 +140,16 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()")); $document_created_at = $row['document_created_at']; $document_updated_at = $row['document_updated_at']; + // Get the tags set for the current document, store them as $document_tags_set + // There is probably a nicer way to do this, but this works. + $query_tags_set = mysqli_query($mysqli, "SELECT tag_id FROM documents_tagged WHERE document_id = $document_id"); + $document_tags_set = array(); + if ($query_tags_set){ + foreach($query_tags_set as $query_tag) { + array_push($document_tags_set, $query_tag['tag_id']); + } + } + ?> @@ -134,4 +187,5 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()"));
- \ No newline at end of file + + diff --git a/db.sql b/db.sql index 9d7d7bdd..0ca09b42 100644 --- a/db.sql +++ b/db.sql @@ -354,6 +354,30 @@ CREATE TABLE `documents` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `documents_tagged` +-- + +DROP TABLE IF EXISTS `documents_tagged`; +CREATE TABLE IF NOT EXISTS `documents_tagged` ( + `document_id` int(11) NOT NULL, + `tag_id` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +COMMIT; + +-- +-- Table structure for table `document_tags` +-- + +DROP TABLE IF EXISTS `document_tags`; +CREATE TABLE IF NOT EXISTS `document_tags` ( + `tag_id` int(11) NOT NULL AUTO_INCREMENT, + `client_id` int(11) NOT NULL, + `tag_name` varchar(15) NOT NULL, + PRIMARY KEY (`tag_id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +COMMIT; + -- -- Table structure for table `domains` -- diff --git a/edit_document_modal.php b/edit_document_modal.php index a9ef3fe5..8fc1043e 100644 --- a/edit_document_modal.php +++ b/edit_document_modal.php @@ -14,6 +14,34 @@
+ + + +
+
+ + +
+
+ +
diff --git a/manage_document_tags_modal.php b/manage_document_tags_modal.php new file mode 100644 index 00000000..76f9a79e --- /dev/null +++ b/manage_document_tags_modal.php @@ -0,0 +1,60 @@ + \ No newline at end of file diff --git a/post.php b/post.php index 56501d18..5904cbcc 100644 --- a/post.php +++ b/post.php @@ -5036,13 +5036,23 @@ if(isset($_POST['add_document'])){ $client_id = intval($_POST['client_id']); $name = trim(strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']))); + $tags_ids = $_POST['tags_ids']; $content = trim(mysqli_real_escape_string($mysqli,$_POST['content'])); - mysqli_query($mysqli,"INSERT INTO documents SET document_name = '$name', document_content = '$content', document_created_at = NOW(), document_client_id = $client_id, company_id = $session_company_id"); + // Document add query + $add_document = mysqli_query($mysqli,"INSERT INTO documents SET document_name = '$name', document_content = '$content', document_created_at = NOW(), document_client_id = $client_id, company_id = $session_company_id"); + $document_id = $mysqli->insert_id; - //Logging + // Logging mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Document', log_action = 'Created', log_description = '$details', log_created_at = NOW(), company_id = $session_company_id, log_user_id = $session_user_id"); + // Add tags + foreach($tags_ids as $tag_id) { + if (intval($tag_id)) { + mysqli_query($mysqli, "INSERT INTO documents_tagged SET document_id = '$document_id', tag_id = '$tag_id'"); + } + } + $_SESSION['alert_message'] = "Document added"; header("Location: " . $_SERVER["HTTP_REFERER"]); @@ -5053,13 +5063,25 @@ if(isset($_POST['edit_document'])){ $document_id = intval($_POST['document_id']); $name = trim(strip_tags(mysqli_real_escape_string($mysqli,$_POST['name']))); + $tags_ids = $_POST['tags_ids']; $content = trim(mysqli_real_escape_string($mysqli,$_POST['content'])); + // Document edit query mysqli_query($mysqli,"UPDATE documents SET document_name = '$name', document_content = '$content', document_updated_at = NOW() WHERE document_id = $document_id AND company_id = $session_company_id"); //Logging mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Note', log_action = 'Modified', log_description = '$name', log_created_at = NOW(), company_id = $session_company_id, log_user_id = $session_user_id"); + // Remove any old tags + mysqli_query($mysqli, "DELETE FROM documents_tagged WHERE document_id = $document_id"); + + // Add tags + foreach($tags_ids as $tag_id) { + if (intval($tag_id)) { + mysqli_query($mysqli, "INSERT INTO documents_tagged SET document_id = '$document_id', tag_id = '$tag_id'"); + } + } + $_SESSION['alert_message'] = "Document updated"; header("Location: " . $_SERVER["HTTP_REFERER"]); @@ -5080,6 +5102,41 @@ if(isset($_GET['delete_document'])){ } +if (isset($_POST['add_document_tag'])) { + $client_id = intval($_POST['client_id']); + $tag_name = trim(strip_tags(mysqli_real_escape_string($mysqli,$_POST['tag_name']))); + + mysqli_query($mysqli,"INSERT INTO document_tags SET client_id = '$client_id', tag_name = '$tag_name'"); + + $_SESSION['alert_message'] = "Document tag added"; + header("Location: " . $_SERVER["HTTP_REFERER"]); +} + +if (isset($_POST['delete_document_tag'])) { + $tag_id = intval($_POST['tag_id']); + + // Delete the tag ID + mysqli_query($mysqli, "DELETE FROM document_tags WHERE tag_id = '$tag_id'"); + + // Delete the associations to documents + mysqli_query($mysqli, "DELETE FROM documents_tagged WHERE tag_id = '$tag_id'"); + + $_SESSION['alert_message'] = "Document tag deleted"; + header("Location: " . $_SERVER["HTTP_REFERER"]); +} + +if (isset($_POST['rename_document_tag'])) { + $tag_id = intval($_POST['tag_id']); + $tag_new_name = trim(strip_tags(mysqli_real_escape_string($mysqli,$_POST['tag_new_name']))); + + // Rename tag in db + mysqli_query($mysqli, "UPDATE document_tags SET tag_name = '$tag_new_name' WHERE tag_id = '$tag_id'"); + + $_SESSION['alert_message'] = "Document tag updated"; + header("Location: " . $_SERVER["HTTP_REFERER"]); + +} + if(isset($_GET['force_recurring'])){ $recurring_id = intval($_GET['force_recurring']);