Skip to content

Commit

Permalink
fix(issue): normalize columns
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jul 5, 2021
1 parent c421d64 commit f793115
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 105 deletions.
32 changes: 16 additions & 16 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function plugin_formcreator_getDropdown() {
function plugin_formcreator_addDefaultSelect($itemtype) {
switch ($itemtype) {
case PluginFormcreatorIssue::class:
return "`glpi_plugin_formcreator_issues`.`sub_itemtype`, ";
return "`glpi_plugin_formcreator_issues`.`itemtype`, ";
}
return "";
}
Expand All @@ -87,7 +87,7 @@ function plugin_formcreator_addDefaultJoin($itemtype, $ref_table, &$already_link
$join = Search::addDefaultJoin(Ticket::getType(), Ticket::getTable(), $already_link_tables);
$join .= Search::addLeftJoin($itemtype, $ref_table, $already_link_tables, Group::getTable(), 'groups_id_validator');
// but we want to join in issues
$join = str_replace('`glpi_tickets`.`id`', '`glpi_plugin_formcreator_issues`.`sub_itemtype` = "Ticket" AND `glpi_plugin_formcreator_issues`.`original_id`', $join);
$join = str_replace('`glpi_tickets`.`id`', '`glpi_plugin_formcreator_issues`.`itemtype` = "Ticket" AND `glpi_plugin_formcreator_issues`.`items_id`', $join);
$join = str_replace('`glpi_tickets`', '`glpi_plugin_formcreator_issues`', $join);
$join = str_replace('`users_id_recipient`', '`requester_id`', $join);
}
Expand Down Expand Up @@ -359,8 +359,8 @@ function plugin_formcreator_hook_add_ticket(CommonDBTM $item) {

$issue = new PluginFormcreatorIssue();
$issue->add([
'original_id' => $item->getID(),
'sub_itemtype' => 'Ticket',
'items_id' => $item->getID(),
'itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'status' => $validationStatus['status'],
'date_creation' => $item->fields['date'],
Expand All @@ -385,15 +385,15 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
$issue = new PluginFormcreatorIssue();
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => Ticket::class,
'original_id' => $id
'itemtype' => Ticket::class,
'items_id' => $id
]
]);
$issue->update([
'id' => $issue->getID(),
'original_id' => $id,
'items_id' => $id,
'display_id' => "t_$id",
'sub_itemtype' => 'Ticket',
'itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'status' => $validationStatus['status'],
'date_creation' => $item->fields['date'],
Expand Down Expand Up @@ -436,7 +436,7 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
$issue = new PluginFormcreatorIssue();
$issue->deleteByCriteria([
'display_id' => "t_$id",
'sub_itemtype' => 'Ticket'
'itemtype' => 'Ticket'
], 1);
}

Expand All @@ -451,7 +451,7 @@ function plugin_formcreator_hook_purge_ticket(CommonDBTM $item) {
$issue = new PluginFormcreatorIssue();
$issue->deleteByCriteria([
'display_id' => "t_$id",
'sub_itemtype' => 'Ticket'
'itemtype' => 'Ticket'
], 1);
}
}
Expand All @@ -476,8 +476,8 @@ function plugin_formcreator_hook_update_ticketvalidation(CommonDBTM $item) {

$issue = new PluginFormcreatorIssue();
$issue->getFromDBByCrit([
'sub_itemtype' => Ticket::getType(),
'original_id' => $item->fields['tickets_id']
'itemtype' => Ticket::getType(),
'items_id' => $item->fields['tickets_id']
]);
if ($issue->isNewItem()) {
return;
Expand All @@ -500,17 +500,17 @@ function plugin_formcreator_hook_update_itilFollowup($followup) {
$issue = new PluginFormcreatorIssue();
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => $itemtype,
'original_id' => $item->getID(),
'itemtype' => $itemtype,
'items_id' => $item->getID(),
]
]);
if ($issue->isNewItem()) {
return;
}
$issue->update([
'id' => $issue->getID(),
'sub_itemtype' => $itemtype,
'original_id' => $item->getID(),
'itemtype' => $itemtype,
'items_id' => $item->getID(),
'status' => $validationStatus['status'],
'date_mod' => $item->fields['date_mod'],
]);
Expand Down
36 changes: 18 additions & 18 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ public function prepareInputForUpdate($input) {
public function pre_deleteItem() {
$issue = new PluginFormcreatorIssue();
$issue->deleteByCriteria([
'original_id' => $this->getID(),
'sub_itemtype' => self::getType(),
'items_id' => $this->getID(),
'itemtype' => self::getType(),
]);

return true;
Expand Down Expand Up @@ -1238,8 +1238,8 @@ private function createIssue() {
// There are several tickets for this form answer
// The issue must be created from this form answer
$issue->add([
'original_id' => $this->getID(),
'sub_itemtype' => PluginFormcreatorFormAnswer::class,
'items_id' => $this->getID(),
'itemtype' => PluginFormcreatorFormAnswer::class,
'name' => addslashes($this->fields['name']),
'status' => $this->fields['status'],
'date_creation' => $this->fields['request_date'],
Expand Down Expand Up @@ -1276,8 +1276,8 @@ private function createIssue() {
);
$ticketUserRow = array_pop($ticketUserRow);
$issue->add([
'original_id' => $ticketId,
'sub_itemtype' => Ticket::class,
'items_id' => $ticketId,
'itemtype' => Ticket::class,
'name' => addslashes($ticket->fields['name']),
'status' => $ticket->fields['status'],
'date_creation' => $ticket->fields['date'],
Expand All @@ -1298,14 +1298,14 @@ private function updateIssue() {
if ($this->input['status'] == self::STATUS_REFUSED) {
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => PluginFormcreatorFormAnswer::class,
'original_id' => $this->getID()
'itemtype' => PluginFormcreatorFormAnswer::class,
'items_id' => $this->getID()
]
]);
$issue->update([
'id' => $issue->getID(),
'sub_itemtype' => PluginFormcreatorFormAnswer::class,
'original_id' => $this->getID(),
'itemtype' => PluginFormcreatorFormAnswer::class,
'items_id' => $this->getID(),
'status' => $this->fields['status'],
]);
return;
Expand All @@ -1324,14 +1324,14 @@ private function updateIssue() {
// The issue must be updated from this form answer
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => PluginFormcreatorFormAnswer::class,
'original_id' => $this->getID()
'itemtype' => PluginFormcreatorFormAnswer::class,
'items_id' => $this->getID()
]
]);
$issue->update([
'id' => $issue->getID(),
'original_id' => $this->getID(),
'sub_itemtype' => PluginFormcreatorFormAnswer::class,
'items_id' => $this->getID(),
'itemtype' => PluginFormcreatorFormAnswer::class,
'name' => addslashes($this->fields['name']),
'status' => $this->fields['status'],
'date_creation' => $this->fields['request_date'],
Expand Down Expand Up @@ -1368,14 +1368,14 @@ private function updateIssue() {
$ticketUserRow = array_pop($ticketUserRow);
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => PluginFormcreatorFormAnswer::class,
'original_id' => $this->getID()
'itemtype' => PluginFormcreatorFormAnswer::class,
'items_id' => $this->getID()
]
]);
$issue->update([
'id' => $issue->getID(),
'original_id' => $ticketId,
'sub_itemtype' => Ticket::class,
'items_id' => $ticketId,
'itemtype' => Ticket::class,
'name' => addslashes($ticket->fields['name']),
'status' => $ticket->fields['status'],
'date_creation' => $ticket->fields['date'],
Expand Down
66 changes: 33 additions & 33 deletions inc/issue.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public static function cronSyncIssues(CronTask $task) {
*/
public static function getSyncIssuesRequest() : AbstractQuery {
// Request which merges tickets and formanswers
// 1 ticket not linked to a formanswer => 1 issue which is the ticket sub_itemtype
// 1 form_answer not linked to a ticket => 1 issue which is the formanswer sub_itemtype
// 1 ticket linked to 1 form_answer => 1 issue which is the ticket sub_itemtype
// several tickets linked to the same form_answer => 1 issue which is the form_answer sub_itemtype
// 1 ticket not linked to a formanswer => 1 issue which is the ticket itemtype
// 1 form_answer not linked to a ticket => 1 issue which is the formanswer itemtype
// 1 ticket linked to 1 form_answer => 1 issue which is the ticket itemtype
// several tickets linked to the same form_answer => 1 issue which is the form_answer itemtype
$formTable = PluginFormcreatorForm::getTable();
$formAnswerTable = PluginFormcreatorFormAnswer::getTable();
$itemTicketTable = Item_Ticket::getTable();
Expand All @@ -91,8 +91,8 @@ public static function getSyncIssuesRequest() : AbstractQuery {
new QueryExpression('NULL as `id`'),
$formTable => ['name as name'],
new QueryExpression("CONCAT('f_', `$formAnswerTable`.`id`) as `display_id`"),
"$formAnswerTable.id as original_id",
new QueryExpression("'" . PluginFormcreatorFormAnswer::getType() . "' as `sub_itemtype`"),
"$formAnswerTable.id as items_id",
new QueryExpression("'" . PluginFormcreatorFormAnswer::getType() . "' as `itemtype`"),
$formAnswerTable => [
'status as status',
'request_date as date_creation',
Expand Down Expand Up @@ -124,7 +124,7 @@ public static function getSyncIssuesRequest() : AbstractQuery {
]
]
],
'GROUPBY' => ['original_id'],
'GROUPBY' => ['items_id'],
'HAVING' => new QueryExpression("COUNT(`$itemTicketTable`.`$ticketFk`) != 1"),
]);

Expand All @@ -136,8 +136,8 @@ public static function getSyncIssuesRequest() : AbstractQuery {
new QueryExpression('NULL as `id`'),
"$ticketTable.name as name",
new QueryExpression("CONCAT('t_', `$ticketTable`.`id`) as `display_id`"),
"$ticketTable.id as original_id",
new QueryExpression("'" . Ticket::getType() . "' as `sub_itemtype`"),
"$ticketTable.id as items_id",
new QueryExpression("'" . Ticket::getType() . "' as `itemtype`"),
new QueryExpression("IF(`$ticketValidationTable`.`status` IS NULL,
`$ticketTable`.`status`,
IF(`$ticketTable`.`global_validation` IN ('" . CommonITILValidation::NONE . "', '" . CommonITILValidation::ACCEPTED . "'),
Expand Down Expand Up @@ -203,7 +203,7 @@ public static function getSyncIssuesRequest() : AbstractQuery {
'WHERE' => [
"$ticketTable.is_deleted" => 0,
],
'GROUPBY' => ['original_id'],
'GROUPBY' => ["$ticketTable.id"],
'HAVING' => new QueryExpression("COUNT(`$itemTicketTable`.`items_id`) <= 1")
]);

Expand Down Expand Up @@ -261,9 +261,9 @@ public function display($options = []) {
}

public function displayExtended($options = []) {
$itemtype = $this->fields['sub_itemtype'];
$itemtype = $this->fields['itemtype'];
$item = new $itemtype();
if (!$item->getFromDB($this->fields['original_id'])) {
if (!$item->getFromDB($this->fields[''])) {
Html::displayNotFoundError();
}

Expand All @@ -286,17 +286,17 @@ public function displayExtended($options = []) {
public function displaySimplified($options = []) {
global $CFG_GLPI;

$itemtype = $this->fields['sub_itemtype'];
$itemtype = $this->fields['itemtype'];
$item = new $itemtype();
if (!$item->getFromDB($this->fields['original_id'])) {
if (!$item->getFromDB($this->fields['items_id'])) {
Html::displayNotFoundError();
}

// in case of left tab layout, we couldn't see "right error" message
if ($item->get_item_to_display_tab) {
if (isset($this->fields['original_id'])
&& $this->fields['original_id']
&& !$item->can($this->fields['original_id'], READ)) {
if (isset($this->fields['items_id'])
&& $this->fields['items_id']
&& !$item->can($this->fields['items_id'], READ)) {
// This triggers from a profile switch.
// If we don't have right, redirect instead to central page
if (isset($_SESSION['_redirected_from_profile_selector'])
Expand All @@ -309,7 +309,7 @@ public function displaySimplified($options = []) {
}
}

if (!isset($this->fields['original_id'])) {
if (!isset($this->fields['items_id'])) {
$options['id'] = 0;
} else {
$options['id'] = $item->getID();
Expand Down Expand Up @@ -433,7 +433,7 @@ public function rawSearchOptions() {
$tab[] = [
'id' => '3',
'table' => $this::getTable(),
'field' => 'sub_itemtype',
'field' => 'itemtype',
'name' => __('Type'),
'searchtype' => [
'0' => 'equals',
Expand Down Expand Up @@ -541,7 +541,7 @@ public function rawSearchOptions() {
'joinparams' => [
'jointype' => 'itemtypeonly',
'specific_itemtype' => PluginFormcreatorFormAnswer::class,
'condition' => 'AND `REFTABLE`.`original_id` = `NEWTABLE`.`items_id`'
'condition' => 'AND `REFTABLE`.`items_id` = `NEWTABLE`.`items_id`'
]
],
'1' => [
Expand Down Expand Up @@ -623,7 +623,7 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
$values = [$field => $values];
}
switch ($field) {
case 'sub_itemtype':
case 'itemtype':
return Dropdown::showFromArray($name,
[Ticket::class => __('Ticket'),
PluginFormcreatorFormAnswer::class => __('Form answer', 'formcreator')],
Expand Down Expand Up @@ -670,7 +670,7 @@ public static function giveItem($itemtype, $option_id, $data, $num) {
switch ("$table.$field") {
case "glpi_plugin_formcreator_issues.name":
$name = $data[$num][0]['name'];
$subItemtype = $data['raw']['sub_itemtype'];
$subItemtype = $data['raw']['itemtype'];
switch ($subItemtype) {
case Ticket::class:
$ticket = new Ticket();
Expand All @@ -687,7 +687,7 @@ public static function giveItem($itemtype, $option_id, $data, $num) {
default:
$content = '';
}
$link = self::getFormURLWithID($id) . "&sub_itemtype=".$data['raw']['sub_itemtype'];
$link = self::getFormURLWithID($id) . "&itemtype=".$data['raw']['itemtype'];
$link = self::getFormURLWithID($data['id']);
$key = 'id';
$tooltip = Html::showToolTip(nl2br(Html::Clean($content)), [
Expand Down Expand Up @@ -884,14 +884,14 @@ static function getTicketSummary() {
*
*/
public function prepareInputForAdd($input) {
if (!isset($input['original_id']) || !isset($input['sub_itemtype'])) {
if (!isset($input['items_id']) || !isset($input['itemtype'])) {
return false;
}

if ($input['sub_itemtype'] == PluginFormcreatorFormAnswer::class) {
$input['display_id'] = 'f_' . $input['original_id'];
} else if ($input['sub_itemtype'] == 'Ticket') {
$input['display_id'] = 't_' . $input['original_id'];
if ($input['itemtype'] == PluginFormcreatorFormAnswer::class) {
$input['display_id'] = 'f_' . $input['items_id'];
} else if ($input['itemtype'] == 'Ticket') {
$input['display_id'] = 't_' . $input['items_id'];
} else {
return false;
}
Expand All @@ -900,14 +900,14 @@ public function prepareInputForAdd($input) {
}

public function prepareInputForUpdate($input) {
if (!isset($input['original_id']) || !isset($input['sub_itemtype'])) {
if (!isset($input['items_id']) || !isset($input['itemtype'])) {
return false;
}

if ($input['sub_itemtype'] == PluginFormcreatorFormAnswer::class) {
$input['display_id'] = 'f_' . $input['original_id'];
} else if ($input['sub_itemtype'] == 'Ticket') {
$input['display_id'] = 't_' . $input['original_id'];
if ($input['itemtype'] == PluginFormcreatorFormAnswer::class) {
$input['display_id'] = 'f_' . $input['items_id'];
} else if ($input['itemtype'] == 'Ticket') {
$input['display_id'] = 't_' . $input['items_id'];
} else {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_issues` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`display_id` varchar(255) NOT NULL,
`original_id` int(11) NOT NULL DEFAULT '0',
`sub_itemtype` varchar(100) NOT NULL DEFAULT '',
`items_id` int(11) NOT NULL DEFAULT '0',
`itemtype` varchar(255) NOT NULL DEFAULT '',
`status` varchar(255) NOT NULL DEFAULT '',
`date_creation` timestamp NULL,
`date_mod` timestamp NULL,
Expand All @@ -255,7 +255,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_issues` (
`groups_id_validator` int(11) NOT NULL DEFAULT '0',
`comment` longtext,
PRIMARY KEY (`id`),
INDEX `original_id_sub_itemtype` (`original_id`, `sub_itemtype`),
INDEX `item` (`itemtype`, `items_id`),
INDEX `entities_id` (`entities_id`),
INDEX `requester_id` (`requester_id`),
INDEX `users_id_validator` (`users_id_validator`),
Expand Down
Loading

0 comments on commit f793115

Please sign in to comment.