Created new table to allow many to many relationship for Ticket and Project Templates

This commit is contained in:
johnnyq 2024-04-06 18:37:37 -04:00
parent c17957fe0a
commit f6b87553ac
7 changed files with 102 additions and 26 deletions

View File

@ -21,21 +21,24 @@ if (isset($_GET['project_template_id'])) {
$row = mysqli_fetch_array($sql_project_templates);
$project_template_id = intval($row['project_template_id']);
$project_template_name = nullable_htmlentities($row['project_template_name']);
$project_template_description = nullable_htmlentities($row['project_template_description']);
$project_template_created_at = date("Y-m-d", strtotime($row['project_template_created_at']));
$project_template_updated_at = nullable_htmlentities($row['project_template_updated_at']);
// Get Associated Ticket Templates
$sql_ticket_templates = mysqli_query($mysqli, "SELECT * FROM ticket_templates WHERE ticket_template_project_template_id = $project_template_id ORDER BY ticket_template_order ASC, ticket_template_name ASC");
$sql_ticket_templates = mysqli_query($mysqli, "SELECT * FROM ticket_templates, project_template_ticket_templates
WHERE ticket_templates.ticket_template_id = project_template_ticket_templates.ticket_template_id
AND project_template_ticket_templates.project_template_id = $project_template_id
ORDER BY ticket_template_order ASC, ticket_template_name ASC");
$ticket_template_count = mysqli_num_rows($sql_ticket_templates);
// Get All Task Templates
$sql_task_templates = mysqli_query($mysqli,
"SELECT * FROM ticket_templates, task_templates
WHERE ticket_template_id = task_template_ticket_template_id
AND ticket_template_project_template_id = $project_template_id
"SELECT * FROM ticket_templates, task_templates, project_template_ticket_templates
WHERE ticket_templates.ticket_template_id = project_template_ticket_templates.ticket_template_id
AND project_template_ticket_templates.project_template_id = $project_template_id
AND ticket_templates.ticket_template_id = task_template_ticket_template_id
ORDER BY task_template_created_at ASC"
);
$task_template_count = mysqli_num_rows($sql_task_templates);
@ -154,7 +157,8 @@ if (isset($_GET['project_template_id'])) {
<tr>
<td class="pr-0">
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="edit_ticket_template_order" value="1">
<input type="hidden" name="edit_ticket_template_order">
<input type="hidden" name="project_template_id" value="<?php echo $project_template_id; ?>">
<input type="hidden" name="ticket_template_id" value="<?php echo $ticket_template_id; ?>">
<input type="text" class="form-control pr-0" onchange="this.form.submit()" name="order" value="<?php echo $ticket_template_order; ?>">
</form>
@ -167,9 +171,14 @@ if (isset($_GET['project_template_id'])) {
<td><?php echo $ticket_template_description; ?></td>
<td><?php echo $ticket_template_subject; ?></td>
<td>
<a href="post.php?remove_ticket_template_from_project_template=<?php echo $ticket_template_id; ?>" class="btn btn-default btn-sm confirm-link">
<i class="fa fa-fw fa-times"></i>
</a>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="project_template_id" value="<?php echo $project_template_id; ?>">
<input type="hidden" name="ticket_template_id" value="<?php echo $ticket_template_id; ?>">
<button type="submit" class="btn btn-default btn-sm confirm-link"
name="remove_ticket_template_from_project_template">
<i class="fa fa-fw fa-times"></i>
</button>
</form>
</td>
</tr>

View File

@ -12,7 +12,7 @@
<div class="modal-body bg-white">
<div class="form-group">
<label>Ticket Template<strong class="text-danger">*</strong></label>
<label>Ticket Template <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-life-ring"></i></span>
@ -21,7 +21,14 @@
<option value="">- Select a Ticket Template -</option>
<?php
$sql_ticket_templates_select = mysqli_query($mysqli, "SELECT * FROM ticket_templates WHERE ticket_template_project_template_id != $project_template_id AND ticket_template_archived_at IS NULL");
$sql_ticket_templates_select = mysqli_query($mysqli, "SELECT * FROM ticket_templates
WHERE ticket_template_id NOT IN (
SELECT ticket_template_id FROM project_template_ticket_templates
WHERE project_template_id = $project_template_id
)
AND ticket_template_archived_at IS NULL
ORDER BY ticket_template_name ASC"
);
while ($row = mysqli_fetch_array($sql_ticket_templates_select)) {
$ticket_template_id_select = intval($row['ticket_template_id']);
$ticket_template_subject_select = nullable_htmlentities($row['ticket_template_subject']);
@ -35,6 +42,16 @@
</div>
</div>
<div class="form-group">
<label>Order</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-sort-numeric-down"></i></span>
</div>
<input type="text" class="form-control" name="order" value="1">
</div>
</div>
</div>
<div class="modal-footer bg-white">

View File

@ -67,6 +67,23 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$project_template_description = nullable_htmlentities($row['project_template_description']);
$project_template_created_at = nullable_htmlentities($row['project_template_created_at']);
// Get Ticket Template Count
$sql_ticket_templates = mysqli_query($mysqli, "SELECT * FROM ticket_templates, project_template_ticket_templates
WHERE ticket_templates.ticket_template_id = project_template_ticket_templates.ticket_template_id
AND project_template_ticket_templates.project_template_id = $project_template_id
ORDER BY ticket_template_order ASC, ticket_template_name ASC");
$ticket_template_count = mysqli_num_rows($sql_ticket_templates);
// Get Tasks Template Count
$sql_task_templates = mysqli_query($mysqli,
"SELECT * FROM ticket_templates, task_templates, project_template_ticket_templates
WHERE ticket_templates.ticket_template_id = project_template_ticket_templates.ticket_template_id
AND project_template_ticket_templates.project_template_id = $project_template_id
AND ticket_templates.ticket_template_id = task_template_ticket_template_id
ORDER BY task_template_created_at ASC"
);
$task_template_count = mysqli_num_rows($sql_task_templates);
?>
<tr>
<td>
@ -84,8 +101,8 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
</div>
</a>
</td>
<td>0</td>
<td>0</td>
<td><?php echo $ticket_template_count; ?></td>
<td><?php echo $task_template_count; ?></td>
<td>
<div class="dropdown dropleft text-center">
<button class="btn btn-secondary btn-sm" data-toggle="dropdown">

View File

@ -1818,10 +1818,27 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) {
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.2.1'");
}
// if (CURRENT_DATABASE_VERSION == '1.2.1') {
// // Insert queries here required to update to DB version 1.2.2
if (CURRENT_DATABASE_VERSION == '1.2.1') {
// Ticket Templates can have many project templates and Project Template can have have many ticket template, so instead create a many to many table relationship
mysqli_query($mysqli, "ALTER TABLE `ticket_templates` DROP `ticket_template_order`");
mysqli_query($mysqli, "ALTER TABLE `ticket_templates` DROP `ticket_template_project_template_id`");
mysqli_query($mysqli,
"CREATE TABLE `project_template_ticket_templates` (
`ticket_template_id` INT(11) NOT NULL,
`project_template_id` INT(11) NOT NULL,
`ticket_template_order` INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`ticket_template_id`,`project_template_id`)
)");
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.2.2'");
}
// if (CURRENT_DATABASE_VERSION == '1.2.2') {
// // Insert queries here required to update to DB version 1.2.3
// // Then, update the database to the next sequential version
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.2.2");
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '1.2.3");
// }
} else {

View File

@ -5,4 +5,4 @@
* It is used in conjunction with database_updates.php
*/
DEFINE("LATEST_DATABASE_VERSION", "1.2.1");
DEFINE("LATEST_DATABASE_VERSION", "1.2.2");

19
db.sql
View File

@ -931,6 +931,21 @@ CREATE TABLE `products` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `project_template_ticket_templates`
--
DROP TABLE IF EXISTS `project_template_ticket_templates`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `project_template_ticket_templates` (
`ticket_template_id` int(11) NOT NULL,
`project_template_id` int(11) NOT NULL,
`ticket_template_order` int(11) NOT NULL,
PRIMARY KEY (`ticket_template_id`,`project_template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `project_templates`
--
@ -1630,11 +1645,9 @@ CREATE TABLE `ticket_templates` (
`ticket_template_description` text DEFAULT NULL,
`ticket_template_subject` varchar(200) DEFAULT NULL,
`ticket_template_details` longtext DEFAULT NULL,
`ticket_template_order` int(11) NOT NULL DEFAULT 0,
`ticket_template_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`ticket_template_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`ticket_template_archived_at` datetime DEFAULT NULL,
`ticket_template_project_template_id` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`ticket_template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
@ -1883,4 +1896,4 @@ CREATE TABLE `vendors` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-04-05 18:19:29
-- Dump completed on 2024-04-06 18:36:53

View File

@ -46,9 +46,10 @@ if (isset($_POST['edit_ticket_template_order'])) {
validateTechRole();
$ticket_template_id = intval($_POST['ticket_template_id']);
$project_template_id = intval($_POST['project_template_id']);
$order = intval($_POST['order']);
mysqli_query($mysqli, "UPDATE ticket_templates SET ticket_template_order = $order WHERE ticket_template_id = $ticket_template_id");
mysqli_query($mysqli, "UPDATE project_template_ticket_templates SET ticket_template_order = $order WHERE ticket_template_id = $ticket_template_id AND project_template_id = $project_template_id");
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
@ -58,8 +59,9 @@ if (isset($_POST['add_ticket_template_to_project_template'])) {
validateTechRole();
$project_template_id = intval($_POST['project_template_id']);
$ticket_template_id = intval($_POST['ticket_template_id']);
$order = intval($_POST['order']);
mysqli_query($mysqli, "UPDATE ticket_templates SET ticket_template_project_template_id = $project_template_id WHERE ticket_template_id = $ticket_template_id");
mysqli_query($mysqli, "INSERT INTO project_template_ticket_templates SET project_template_id = $project_template_id, ticket_template_id = $ticket_template_id, ticket_template_order = $order");
// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Project Template', log_action = 'Edit', log_description = '$session_name added a ticket template to project template', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $project_template_id");
@ -69,15 +71,16 @@ if (isset($_POST['add_ticket_template_to_project_template'])) {
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
if (isset($_GET['remove_ticket_template_from_project_template'])) {
if (isset($_POST['remove_ticket_template_from_project_template'])) {
validateTechRole();
$ticket_template_id = intval($_GET['remove_ticket_template_from_project_template']);
$ticket_template_id = intval($_POST['ticket_template_id']);
$project_template_id = intval($_POST['project_template_id']);
mysqli_query($mysqli, "UPDATE ticket_templates SET ticket_template_project_template_id = 0 WHERE ticket_template_id = $ticket_template_id");
mysqli_query($mysqli, "DELETE FROM project_template_ticket_templates WHERE project_template_id = $project_template_id AND ticket_template_id = $ticket_template_id");
// Logging
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Project Template', log_action = 'Edit', log_description = '$session_name removed a ticket template from a project template', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $ticket_template_id");
mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'Project Template', log_action = 'Edit', log_description = '$session_name removed a ticket template from a project template', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $project_template_id");
$_SESSION['alert_message'] = "You removed ticket template from the project template";