mirror of https://github.com/itflow-org/itflow
Add tag functionality to documents
This commit is contained in:
parent
e6c2f90e02
commit
77d7e7ba0d
|
|
@ -14,6 +14,32 @@
|
|||
<div class="form-group">
|
||||
<input type="text" class="form-control" name="name" placeholder="Name" required autofocus>
|
||||
</div>
|
||||
<?php
|
||||
if($document_tags) {
|
||||
foreach($document_tags as $document_tag) {
|
||||
?>
|
||||
<!-- Document Tags select start -->
|
||||
<div class="form-group">
|
||||
<div class="button-group">
|
||||
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
|
||||
<span class="fa fa-fw fa-tag"></span> <span class="caret"></span>
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
<li>
|
||||
<div class="form-check">
|
||||
<label>
|
||||
<input class="form-check-input" type="checkbox" value="<?php echo $document_tag['tag_id'] ?>" name="tags_ids[<?php echo $document_tag['tag_id']; ?>]"> <?php echo htmlentities($document_tag['tag_name']); ?>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Document tags select end -->
|
||||
<?php
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<div class="form-group">
|
||||
<textarea class="form-control summernote" name="content"></textarea>
|
||||
|
|
|
|||
|
|
@ -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()"));
|
|||
<h3 class="card-title mt-2"><i class="fa fa-fw fa-file-alt"></i> Documents</h3>
|
||||
<div class="card-tools">
|
||||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addDocumentModal"><i class="fas fa-fw fa-plus"></i> New Document</button>
|
||||
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#manageTagsModal"><i class="fas fa-fw fa-tags"></i> Tags</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="form-group">
|
||||
<?php
|
||||
# Show client document tags from database, allow user to filter documents using these
|
||||
$document_tags = mysqli_query($mysqli, "SELECT tag_id, tag_name FROM document_tags WHERE client_id = $client_id ORDER BY tag_name");
|
||||
if (mysqli_num_rows($document_tags) > 0) {
|
||||
foreach($document_tags as $document_tag) {
|
||||
echo "<a href='?$url_query_strings_sb&tag=$document_tag[tag_id]' class=\"btn btn-outline-primary btn-lg mr-1\">"; echo htmlentities($document_tag['tag_name']); echo "</a>";
|
||||
}
|
||||
}
|
||||
else {
|
||||
$document_tags = FALSE;
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<form autocomplete="off">
|
||||
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>">
|
||||
<input type="hidden" name="tab" value="<?php echo $_GET['tab']; ?>">
|
||||
|
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<tr>
|
||||
|
|
@ -134,4 +187,5 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()"));
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<?php include("add_document_modal.php"); ?>
|
||||
<?php include("add_document_modal.php"); ?>
|
||||
<?php include("manage_document_tags_modal.php"); ?>
|
||||
|
|
|
|||
24
db.sql
24
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`
|
||||
--
|
||||
|
|
|
|||
|
|
@ -14,6 +14,34 @@
|
|||
<div class="form-group">
|
||||
<input type="text" class="form-control" name="name" value="<?php echo $document_name; ?>" placeholder="Name" required>
|
||||
</div>
|
||||
|
||||
<!-- Document Tags select start -->
|
||||
<?php
|
||||
if($document_tags) { ?>
|
||||
<div class="form-group">
|
||||
<div class="button-group">
|
||||
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
|
||||
<span class="fa fa-fw fa-tag"></span> <span class="caret"></span>
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
<?php
|
||||
foreach($document_tags as $document_tag) {
|
||||
?>
|
||||
<li>
|
||||
<div class="form-check">
|
||||
<label>
|
||||
<input class="form-check-input" type="checkbox" value="<?php echo $document_tag['tag_id'] ?>" name="tags_ids[<?php echo $document_tag['tag_id']; ?>]" <?php if(in_array($document_tag['tag_id'],$document_tags_set)) {echo "checked";} ?>> <?php echo htmlentities($document_tag['tag_name']); ?>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
<?php
|
||||
} ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
} ?>
|
||||
<!-- Document tags select end -->
|
||||
|
||||
<div class="form-group">
|
||||
<textarea class="form-control summernote" name="content"><?php echo $document_content; ?></textarea>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
<div class="modal" id="manageTagsModal" tabindex="-1">
|
||||
<div class="modal-dialog modal-md">
|
||||
<div class="modal-content bg-dark">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title"><i class="fa fa-fw fa-tags"></i> Manage Tags</h5>
|
||||
<button type="button" class="close text-white" data-dismiss="modal">
|
||||
<span>×</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="modal-body bg-white">
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>">
|
||||
<div class="form-group">
|
||||
<input type="text" class="form-control" name="tag_name" placeholder="Tag Name" autofocus>
|
||||
</div>
|
||||
<button type="submit" name="add_document_tag" class="btn btn-primary">Add Tag</button>
|
||||
</form>
|
||||
<?php
|
||||
// Only show the edit/update tags if we have tags to work with
|
||||
if ($document_tags) { ?>
|
||||
<hr>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>">
|
||||
<div class="form-group">
|
||||
<select class="form-select" name="tag_id">
|
||||
<?php
|
||||
foreach($document_tags as $document_tag) {
|
||||
echo "<option value='$document_tag[tag_id]'>"; echo htmlentities($document_tag['tag_name']); echo "</option>";
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</div>
|
||||
<button type="submit" name="delete_document_tag" class="btn btn-danger">Delete Tag</button>
|
||||
</form>
|
||||
<hr>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>">
|
||||
<div class="form-group">
|
||||
<select class="form-select" name="tag_id">
|
||||
<?php
|
||||
foreach($document_tags as $document_tag) {
|
||||
echo "<option value='$document_tag[tag_id]'>"; echo htmlentities($document_tag['tag_name']); echo "</option>";
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="text" class="form-control" name="tag_new_name" placeholder="Tag Name" autofocus>
|
||||
</div>
|
||||
<button type="submit" name="rename_document_tag" class="btn btn-primary">Rename Tag</button>
|
||||
</form>
|
||||
</div>
|
||||
<?php }
|
||||
?>
|
||||
<div class="modal-footer bg-white">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
61
post.php
61
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']);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue