From 7f840dfd1692d8d48b2a69189a9a17c9b10841ff Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Tue, 12 Feb 2019 16:23:07 +0100 Subject: [PATCH 01/29] fix(targetchange): category not assigned also refactor entity assignation for methods consistency Signed-off-by: Thierry Bugier --- inc/targetbase.class.php | 49 +++++-- inc/targetchange.class.php | 5 +- inc/targetticket.class.php | 122 +----------------- .../PluginFormcreatorTargetChangeDummy.php | 4 +- .../PluginFormcreatorTargetTicketDummy.php | 4 +- .../PluginFormcreatorTargetChange.php | 29 ++--- .../PluginFormcreatorTargetTicket.php | 28 ++-- 7 files changed, 75 insertions(+), 166 deletions(-) diff --git a/inc/targetbase.class.php b/inc/targetbase.class.php index b76b9df23..dfc83db18 100644 --- a/inc/targetbase.class.php +++ b/inc/targetbase.class.php @@ -203,26 +203,28 @@ public function getForm() { } /** - * Undocumented function + * Set the entity of the target * + * @param array $data input data of the target * @param PluginFormcreatorFormAnswer $formanswer * @param integer $requesters_id ID of the requester of the answers * @return integer ID of the entity where the target must be generated */ - protected function getTargetEntity(PluginFormcreatorFormAnswer $formanswer, $requesters_id) { + protected function setTargetEntity($data, PluginFormcreatorFormAnswer $formanswer, $requesters_id) { global $DB; $entityId = 0; + $entityFk = Entity::getForeignKeyField(); switch ($this->fields['destination_entity']) { // Requester's entity case 'current' : - $entityId = $formanswer->fields[Entity::getForeignKeyField()]; + $entityId = $formanswer->fields[$entityFk]; break; case 'requester' : $userObj = new User(); $userObj->getFromDB($requesters_id); - $entityId = $userObj->fields[Entity::getForeignKeyField()]; + $entityId = $userObj->fields[$entityFk]; break; // Requester's first dynamic entity @@ -244,7 +246,7 @@ protected function getTargetEntity(PluginFormcreatorFormAnswer $formanswer, $req $data_entities[] = $entity; } $first_entity = array_shift($data_entities); - $entityId = $first_entity[Entity::getForeignKeyField()]; + $entityId = $first_entity[$entityFk]; break; // Specific entity @@ -254,14 +256,14 @@ protected function getTargetEntity(PluginFormcreatorFormAnswer $formanswer, $req // The form entity case 'form' : - $entityId = $formanswer->getForm()->fields[Entity::getForeignKeyField()]; + $entityId = $formanswer->getForm()->fields[$entityFk]; break; // The validator entity case 'validator' : $userObj = new User(); $userObj->getFromDB($formanswer->fields['users_id_validator']); - $entityId = $userObj->fields[Entity::getForeignKeyField()]; + $entityId = $userObj->fields[$entityFk]; break; // Default entity of a user from the answer of a user's type question @@ -279,7 +281,7 @@ protected function getTargetEntity(PluginFormcreatorFormAnswer $formanswer, $req if ($user_id > 0) { $userObj = new User(); $userObj->getFromDB($user_id); - $entityId = $userObj->fields[Entity::getForeignKeyField()]; + $entityId = $userObj->fields[$entityFk]; } break; @@ -297,7 +299,36 @@ protected function getTargetEntity(PluginFormcreatorFormAnswer $formanswer, $req break; } - return $entityId; + $data[$entityFk] = $entityId; + return $data; + } + + protected function setTargetCategory($data, $formanswer) { + global $DB; + + switch ($this->fields['category_rule']) { + case 'answer': + $category = $DB->request([ + 'SELECT' => ['answer'], + 'FROM' => PluginFormcreatorAnswer::getTable(), + 'WHERE' => [ + 'plugin_formcreator_formanswers_id' => $formanswer->fields['id'], + 'plugin_formcreator_questions_id' => $this->fields['category_question'] + ] + ])->next(); + $category = $category['answer']; + break; + case 'specific': + $category = $this->fields['category_question']; + break; + default: + $category = null; + } + if ($category !== null) { + $data['itilcategories_id'] = $category; + } + + return $data; } /** diff --git a/inc/targetchange.class.php b/inc/targetchange.class.php index 368234322..65a211a97 100644 --- a/inc/targetchange.class.php +++ b/inc/targetchange.class.php @@ -1022,10 +1022,9 @@ public function save(PluginFormcreatorFormAnswer $formanswer) { } } - // Computation of the entity - $data['entities_id'] = $this->getTargetEntity($formanswer, $requesters_id); - + $data = $this->setTargetEntity($data, $formanswer, $requesters_id); $data = $this->setTargetDueDate($data, $formanswer); + $data = $this->setTargetCategory($data, $formanswer); $data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data; $data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data; diff --git a/inc/targetticket.class.php b/inc/targetticket.class.php index 9812726b5..f1ed3953e 100644 --- a/inc/targetticket.class.php +++ b/inc/targetticket.class.php @@ -1177,99 +1177,7 @@ public function save(PluginFormcreatorFormAnswer $formanswer) { } } - // Computation of the entity - switch ($this->fields['destination_entity']) { - // Requester's entity - case 'current' : - $data['entities_id'] = $formanswer->getField('entities_id'); - break; - - case 'requester' : - $userObj = new User(); - $userObj->getFromDB($requesters_id); - $data['entities_id'] = $userObj->fields['entities_id']; - break; - - // Requester's first dynamic entity - case 'requester_dynamic_first' : - $order_entities = "`glpi_profiles`.`name` ASC"; - case 'requester_dynamic_last' : - if (!isset($order_entities)) { - $order_entities = "`glpi_profiles`.`name` DESC"; - } - $query_entities = "SELECT `glpi_profiles_users`.`entities_id` - FROM `glpi_profiles_users` - LEFT JOIN `glpi_profiles` - ON `glpi_profiles`.`id` = `glpi_profiles_users`.`profiles_id` - WHERE `glpi_profiles_users`.`users_id` = $requesters_id - ORDER BY `glpi_profiles_users`.`is_dynamic` DESC, $order_entities"; - $res_entities = $DB->query($query_entities); - $data_entities = []; - while ($entity = $DB->fetch_array($res_entities)) { - $data_entities[] = $entity; - } - $first_entity = array_shift($data_entities); - $data['entities_id'] = $first_entity['entities_id']; - break; - - // Specific entity - case 'specific' : - $data['entities_id'] = $this->fields['destination_entity_value']; - break; - - // The form entity - case 'form' : - $data['entities_id'] = $form->fields['entities_id']; - break; - - // The validator entity - case 'validator' : - $userObj = new User(); - $userObj->getFromDB($formanswer->fields['users_id_validator']); - $data['entities_id'] = $userObj->fields['entities_id']; - break; - - // Default entity of a user from the answer of a user's type question - case 'user' : - $user = $DB->request([ - 'SELECT' => ['answer'], - 'FROM' => $answer::getTable(), - 'WHERE' => [ - 'plugin_formcreator_formanswers_id' => $formanswer->fields['id'], - 'plugin_formcreator_questions_id' => $this->fields['destination_entity_value'] - ] - ])->next(); - $user_id = $user['answer']; - - if ($user_id > 0) { - $userObj = new User(); - $userObj->getFromDB($user_id); - $data['entities_id'] = $userObj->fields['entities_id']; - } else { - $data['entities_id'] = 0; - } - break; - - // Entity from the answer of an entity's type question - case 'entity' : - $entity = $DB->request([ - 'SELECT' => ['answer'], - 'FROM' => $answer::getTable(), - 'WHERE' => [ - 'plugin_formcreator_formanswers_id' => $formanswer->fields['id'], - 'plugin_formcreator_questions_id' => $this->fields['destination_entity_value'] - ] - ])->next(); - - $data['entities_id'] = $entity['answer']; - break; - - // Requester current entity - default : - $data['entities_id'] = 0; - break; - } - + $data = $this->setTargetEntity($data, $formanswer, $requesters_id); $data = $this->setTargetDueDate($data, $formanswer); $data = $this->setTargetUrgency($data, $formanswer); $data = $this->setTargetCategory($data, $formanswer); @@ -1401,34 +1309,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer) { return $ticket; } - protected function setTargetCategory($data, $formanswer) { - global $DB; - - switch ($this->fields['category_rule']) { - case 'answer': - $category = $DB->request([ - 'SELECT' => ['answer'], - 'FROM' => PluginFormcreatorAnswer::getTable(), - 'WHERE' => [ - 'plugin_formcreator_formanswers_id' => $formanswer->fields['id'], - 'plugin_formcreator_questions_id' => $this->fields['category_question'] - ] - ])->next(); - $category = $category['answer']; - break; - case 'specific': - $category = $this->fields['category_question']; - break; - default: - $category = null; - } - if ($category !== null) { - $data['itilcategories_id'] = $category; - } - - return $data; - } - protected function setTargetUrgency($data, $formanswer) { global $DB; diff --git a/tests/src/PluginFormcreatorTargetChangeDummy.php b/tests/src/PluginFormcreatorTargetChangeDummy.php index 274da38cb..8a9b15780 100644 --- a/tests/src/PluginFormcreatorTargetChangeDummy.php +++ b/tests/src/PluginFormcreatorTargetChangeDummy.php @@ -39,8 +39,8 @@ public static function getTable($classname = null) { return \PluginFormcreatorTargetChange::getTable(); } - public function publicGetTargetEntity(\PluginFormcreatorFormAnswer $formanswer, $requesters_id) { - return $this->getTargetEntity($formanswer, $requesters_id); + public function publicSetTargetEntity($data, \PluginFormcreatorFormAnswer $formanswer, $requesters_id) { + return $this->setTargetEntity($data, $formanswer, $requesters_id); } public function publicPrepareTemplate($template, \PluginFormcreatorFormAnswer $formAnswer, $disableRichText = false) { diff --git a/tests/src/PluginFormcreatorTargetTicketDummy.php b/tests/src/PluginFormcreatorTargetTicketDummy.php index 36bd1a26d..d7a5e1e22 100644 --- a/tests/src/PluginFormcreatorTargetTicketDummy.php +++ b/tests/src/PluginFormcreatorTargetTicketDummy.php @@ -39,8 +39,8 @@ public static function getTable($classname = null) { return \PluginFormcreatorTargetTicket::getTable(); } - public function publicGetTargetEntity(\PluginFormcreatorFormAnswer $formanswer, $requesters_id) { - return $this->getTargetEntity($formanswer, $requesters_id); + public function publicSetTargetEntity($data, \PluginFormcreatorFormAnswer $formanswer, $requesters_id) { + return $this->SetTargetEntity($data, $formanswer, $requesters_id); } public function publicPrepareTemplate($template, \PluginFormcreatorFormAnswer $formAnswer, $disableRichText = false) { diff --git a/tests/suite-unit/PluginFormcreatorTargetChange.php b/tests/suite-unit/PluginFormcreatorTargetChange.php index 6edd2dafc..927e392fa 100644 --- a/tests/suite-unit/PluginFormcreatorTargetChange.php +++ b/tests/suite-unit/PluginFormcreatorTargetChange.php @@ -39,18 +39,17 @@ class PluginFormcreatorTargetChange extends CommonTestCase { public function beforeTestMethod($method) { parent::beforeTestMethod($method); switch ($method) { - case 'testGetTargetEntity': + case 'testSetTargetEntity': $this->boolean($this->login('glpi', 'glpi'))->isTrue(); break; } } /** - * @engine inline * * @return void */ - public function testGetTargetEntity() { + public function testSetTargetEntity() { $form = $this->getForm(); $formFk = \PluginFormcreatorForm::getForeignKeyField(); $targetChange = $this->getTargetChange([ @@ -81,8 +80,8 @@ public function testGetTargetEntity() { ]); $formAnswer->getFromDB($formAnswer->getID()); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); // Test requester's entity $targetChange->update([ @@ -98,8 +97,8 @@ public function testGetTargetEntity() { ]); \Session::changeActiveEntities($entityId); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo(0); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo(0); // Test requester's first entity (alphanumeric order) $targetChange->update([ @@ -134,8 +133,8 @@ public function testGetTargetEntity() { ]); $this->boolean($this->login($user->fields['name'], 'passwd'))->isTrue(); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); // Test requester's last entity (alphanumeric order) $targetChange->update([ @@ -151,8 +150,8 @@ public function testGetTargetEntity() { ]); $this->boolean($this->login($user->fields['name'], 'passwd'))->isTrue(); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo(0); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo(0); // Test specific entity $this->boolean($this->login('glpi', 'glpi'))->isTrue(); @@ -172,8 +171,8 @@ public function testGetTargetEntity() { 'entities_id' => 0, ]); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); // Test form's entity $entityId = $entity->import([ @@ -196,7 +195,7 @@ public function testGetTargetEntity() { 'entities_id' => 0, ]); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); } } diff --git a/tests/suite-unit/PluginFormcreatorTargetTicket.php b/tests/suite-unit/PluginFormcreatorTargetTicket.php index c270356a0..e4bf9ebc6 100644 --- a/tests/suite-unit/PluginFormcreatorTargetTicket.php +++ b/tests/suite-unit/PluginFormcreatorTargetTicket.php @@ -39,7 +39,7 @@ class PluginFormcreatorTargetTicket extends CommonTestCase { public function beforeTestMethod($method) { parent::beforeTestMethod($method); switch ($method) { - case 'testGetTargetEntity': + case 'testSetTargetEntity': $this->boolean($this->login('glpi', 'glpi'))->isTrue(); break; } @@ -128,7 +128,7 @@ public function testDeleteLinkedTickets() { * * @return void */ - public function testGetTargetEntity() { + public function testSetTargetEntity() { $form = $this->getForm(); $formFk = \PluginFormcreatorForm::getForeignKeyField(); $targetTicket = $this->getTargetTicket([ @@ -159,8 +159,8 @@ public function testGetTargetEntity() { ]); $formAnswer->getFromDB($formAnswer->getID()); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); // Test requester's entity $targetTicket->update([ @@ -176,8 +176,8 @@ public function testGetTargetEntity() { ]); \Session::changeActiveEntities($entityId); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo(0); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo(0); // Test requester's first entity (alphanumeric order) $targetTicket->update([ @@ -212,8 +212,8 @@ public function testGetTargetEntity() { ]); $this->boolean($this->login($user->fields['name'], 'passwd'))->isTrue(); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); // Test requester's last entity (alphanumeric order) $targetTicket->update([ @@ -229,8 +229,8 @@ public function testGetTargetEntity() { ]); $this->boolean($this->login($user->fields['name'], 'passwd'))->isTrue(); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo(0); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo(0); // Test specific entity $this->boolean($this->login('glpi', 'glpi'))->isTrue(); @@ -250,8 +250,8 @@ public function testGetTargetEntity() { 'entities_id' => 0, ]); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); // Test form's entity $entityId = $entity->import([ @@ -274,8 +274,8 @@ public function testGetTargetEntity() { 'entities_id' => 0, ]); $requesterId = \Session::getLoginUserID(); - $output = $instance->publicGetTargetEntity($formAnswer, $requesterId); - $this->integer((int) $output)->isEqualTo($entityId); + $output = $instance->publicSetTargetEntity([], $formAnswer, $requesterId); + $this->integer((int) $output['entities_id'])->isEqualTo($entityId); } public function providerPrepareTemplate() { From 2462ca46e9aa2da95c88a9d12fbb1f637b32b7cd Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Mon, 18 Feb 2019 10:42:42 +0100 Subject: [PATCH 02/29] fix(wizard): compatibility accross versions od font awesome GLPI 9.3 and GLPI 9.4 use different versions and some icons broke Signed-off-by: Thierry Bugier --- inc/wizard.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/wizard.class.php b/inc/wizard.class.php index 54b8918bd..0d7fb45e3 100644 --- a/inc/wizard.class.php +++ b/inc/wizard.class.php @@ -96,7 +96,7 @@ public static function header($title) { echo '