Skip to content

Commit 349ad17

Browse files
committed
refactor(targetticket,targetchange,targetproblem): code factorization
1 parent 9f524eb commit 349ad17

4 files changed

Lines changed: 111 additions & 78 deletions

File tree

inc/abstractitiltarget.class.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ abstract class PluginFormcreatorAbstractItilTarget extends PluginFormcreatorAbst
4545
/** @var array $requesters requester actors of the target */
4646
protected $requesters;
4747

48+
/** @var array $requesters requester actors of the target */
49+
protected $firstRequester;
50+
4851
/** @var array $observers watcher actors of the target */
4952
protected $observers;
5053

@@ -2543,4 +2546,97 @@ public static function getMailImage() {
25432546
public static function getNoMailImage() {
25442547
return '<i class="fas fa-envelope pointer" title="' . __('Email followup') . ' ' . __('No') . '" width="20"></i>';
25452548
}
2549+
2550+
/**
2551+
* Undocumented function
2552+
*
2553+
* @param array $data
2554+
* @param PluginFormcreatorFormAnswer $formanswer
2555+
* @return array
2556+
*/
2557+
protected function setTargetRequesters(array $data, PluginFormcreatorFormAnswer $formanswer): array {
2558+
if (count($this->requesters['_users_id_requester']) == 0) {
2559+
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, $formanswer->fields['requester_id'], true);
2560+
$requesters_id = $formanswer->fields['requester_id'];
2561+
} else {
2562+
$requesterAccounts = array_filter($this->requesters['_users_id_requester'], function($v) {
2563+
return ($v != 0);
2564+
});
2565+
$requesters_id = array_shift($requesterAccounts);
2566+
if ($requesters_id === null) {
2567+
// No account for requesters, then fallback on the account used to fill the answers
2568+
$requesters_id = $formanswer->fields['requester_id'];
2569+
}
2570+
2571+
// If only one requester, revert array of requesters into a scalar
2572+
// This is needed to process business rule affecting location of a ticket with the location of the user
2573+
if (count($this->requesters['_users_id_requester']) == 1) {
2574+
$this->requesters['_users_id_requester'] = array_pop($this->requesters['_users_id_requester']);
2575+
}
2576+
}
2577+
2578+
// There is always at least one requester
2579+
$data = $this->requesters + $data;
2580+
$this->firstRequester = $requesters_id;
2581+
2582+
if (count($this->requesterGroups['_groups_id_requester']) > 0) {
2583+
$data = $this->requesterGroups + $data;
2584+
}
2585+
2586+
return $data;
2587+
}
2588+
2589+
/**
2590+
* Undocumented function
2591+
*
2592+
* @param array $data
2593+
* @param PluginFormcreatorFormAnswer $formanswer
2594+
* @return array
2595+
*/
2596+
protected function setTargetObservers(array $data, PluginFormcreatorFormAnswer $formanswer): array {
2597+
if (count($this->observers['_users_id_observer']) > 0) {
2598+
$data = $this->observers + $data;
2599+
}
2600+
2601+
if (count($this->observerGroups['_groups_id_observer']) > 0) {
2602+
$data = $this->observerGroups + $data;
2603+
}
2604+
2605+
return $data;
2606+
}
2607+
2608+
/**
2609+
* Undocumented function
2610+
*
2611+
* @param array $data
2612+
* @param PluginFormcreatorFormAnswer $formanswer
2613+
* @return array
2614+
*/
2615+
protected function setTargeAssigned(array $data, PluginFormcreatorFormAnswer $formanswer): array {
2616+
if (count($this->assigned['_users_id_assign']) > 0) {
2617+
$data = $this->assigned + $data;
2618+
}
2619+
2620+
if (count($this->assignedGroups['_groups_id_assign']) > 0) {
2621+
$data = $this->assignedGroups + $data;
2622+
}
2623+
2624+
return $data;
2625+
}
2626+
2627+
/**
2628+
* Undocumented function
2629+
*
2630+
* @param array $data
2631+
* @param PluginFormcreatorFormAnswer $formanswer
2632+
* @return array
2633+
*/
2634+
protected function setTargetSuppliers(array $data, PluginFormcreatorFormAnswer $formanswer): array {
2635+
if (count($this->assignedSuppliers['_suppliers_id_assign']) > 0) {
2636+
$data = $this->assignedSuppliers + $data;
2637+
}
2638+
2639+
return $data;
2640+
}
2641+
25462642
}

inc/targetchange.class.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -680,30 +680,17 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
680680

681681
$this->prepareActors($form, $formanswer);
682682

683-
if (count($this->requesters['_users_id_requester']) == 0) {
684-
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, $formanswer->fields['requester_id'], true);
685-
$requesters_id = $formanswer->fields['requester_id'];
686-
} else {
687-
$requesterAccounts = array_filter($this->requesters['_users_id_requester'], function($v) {
688-
return ($v != 0);
689-
});
690-
$requesters_id = array_shift($requesterAccounts);
691-
if ($requesters_id === null) {
692-
// No account for requesters, then fallback on the account used to fill the answers
693-
$requesters_id = $formanswer->fields['requester_id'];
694-
}
695-
}
696-
697-
$data = $this->setTargetEntity($data, $formanswer, $requesters_id);
683+
$data = $this->setTargetRequesters($data, $formanswer);
684+
$data = $this->setTargetEntity($data, $formanswer, $this->firstRequester);
698685
$data = $this->setTargetDueDate($data, $formanswer);
699686
$data = $this->setSLA($data, $formanswer);
700687
$data = $this->setOLA($data, $formanswer);
701688
$data = $this->setTargetUrgency($data, $formanswer);
702689
$data = $this->setTargetPriority($data, $formanswer);
703690
$data = $this->setTargetValidation($data, $formanswer);
704-
705-
$data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data;
706-
$data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data;
691+
$data = $this->setTargetObservers($data, $formanswer);
692+
$data = $this->setTargeAssigned($data, $formanswer);
693+
$data = $this->setTargetSuppliers($data, $formanswer);
707694

708695
$data = $this->prepareUploadedFiles($data, $formanswer);
709696

inc/targetproblem.class.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,13 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
199199

200200
$this->prepareActors($form, $formanswer);
201201

202-
if (count($this->requesters['_users_id_requester']) == 0) {
203-
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, $formanswer->fields['requester_id'], true);
204-
$requesters_id = $formanswer->fields['requester_id'];
205-
} else {
206-
$requesterAccounts = array_filter($this->requesters['_users_id_requester'], function($v) {
207-
return ($v != 0);
208-
});
209-
$requesters_id = array_shift($requesterAccounts);
210-
if ($requesters_id === null) {
211-
// No account for requesters, then fallback on the account used to fill the answers
212-
$requesters_id = $formanswer->fields['requester_id'];
213-
}
214-
}
215-
216-
$data = $this->setTargetEntity($data, $formanswer, $requesters_id);
202+
$data = $this->setTargetRequesters($data, $formanswer);
203+
$data = $this->setTargetEntity($data, $formanswer, $this->firstRequester);
217204
$data = $this->setTargetUrgency($data, $formanswer);
218205
$data = $this->setTargetPriority($data, $formanswer);
219-
220-
$data = $this->requesters + $this->observers + $this->assigned + $this->assignedSuppliers + $data;
221-
$data = $this->requesterGroups + $this->observerGroups + $this->assignedGroups + $data;
206+
$data = $this->setTargetObservers($data, $formanswer);
207+
$data = $this->setTargeAssigned($data, $formanswer);
208+
$data = $this->setTargetSuppliers($data, $formanswer);
222209

223210
$data = $this->prepareUploadedFiles($data, $formanswer);
224211

inc/targetticket.class.php

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -856,33 +856,14 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
856856

857857
$this->prepareActors($form, $formanswer);
858858

859-
if (count($this->requesters['_users_id_requester']) == 0) {
860-
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, $formanswer->fields['requester_id'], true);
861-
$requesters_id = $formanswer->fields['requester_id'];
862-
} else {
863-
$requesterAccounts = array_filter($this->requesters['_users_id_requester'], function($v) {
864-
return ($v != 0);
865-
});
866-
$requesters_id = array_shift($requesterAccounts);
867-
if ($requesters_id === null) {
868-
// No account for requesters, then fallback on the account used to fill the answers
869-
$requesters_id = $formanswer->fields['requester_id'];
870-
}
871-
872-
// If only one requester, revert array of requesters into a scalar
873-
// This is needed to process business rule affecting location of a ticket with the location of the user
874-
if (count($this->requesters['_users_id_requester']) == 1) {
875-
$this->requesters['_users_id_requester'] = array_pop($this->requesters['_users_id_requester']);
876-
}
877-
}
878-
879859
$data['users_id_recipient'] = $formanswer->fields['requester_id'];
880860
$lastUpdater = Session::getLoginUserID();
881861
$data['users_id_lastupdater'] = $lastUpdater != '' ? $lastUpdater : 0;
882862

863+
$data = $this->setTargetRequesters($data, $formanswer);
883864
$data = $this->setTargetType($data, $formanswer);
884865
$data = $this->setTargetSource($data, $formanswer);
885-
$data = $this->setTargetEntity($data, $formanswer, $requesters_id);
866+
$data = $this->setTargetEntity($data, $formanswer, $this->firstRequester);
886867
$data = $this->setTargetDueDate($data, $formanswer);
887868
$data = $this->setSLA($data, $formanswer);
888869
$data = $this->setOLA($data, $formanswer);
@@ -893,28 +874,10 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {
893874
$data = $this->setTargetAssociatedItem($data, $formanswer);
894875
$data = $this->setTargetValidation($data, $formanswer);
895876

896-
// There is always at least one requester
897-
$data = $this->requesters + $data;
898-
899877
// Overwrite default actors only if populated
900-
if (count($this->observers['_users_id_observer']) > 0) {
901-
$data = $this->observers + $data;
902-
}
903-
if (count($this->assigned['_users_id_assign']) > 0) {
904-
$data = $this->assigned + $data;
905-
}
906-
if (count($this->assignedSuppliers['_suppliers_id_assign']) > 0) {
907-
$data = $this->assignedSuppliers + $data;
908-
}
909-
if (count($this->requesterGroups['_groups_id_requester']) > 0) {
910-
$data = $this->requesterGroups + $data;
911-
}
912-
if (count($this->observerGroups['_groups_id_observer']) > 0) {
913-
$data = $this->observerGroups + $data;
914-
}
915-
if (count($this->assignedGroups['_groups_id_assign']) > 0) {
916-
$data = $this->assignedGroups + $data;
917-
}
878+
$data = $this->setTargetObservers($data, $formanswer);
879+
$data = $this->setTargeAssigned($data, $formanswer);
880+
$data = $this->setTargetSuppliers($data, $formanswer);
918881

919882
$data = $this->prepareUploadedFiles($data, $formanswer);
920883

0 commit comments

Comments
 (0)