Skip to content

Commit

Permalink
feat(targetticket,targetcvhange,targetproblem): update actors array
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jun 27, 2022
1 parent be417f5 commit 44465cb
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 25 deletions.
58 changes: 58 additions & 0 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,64 @@ protected function initializeActors() {
];
}

/**
* Cleanup invalid actors or emoty keys on actors of the target
*
* @param array $data
* @return array
*/
public function cleanActors(array $data): array {
$actorTypes = [
'_users_id_requester',
'_users_id_observer',
'_users_id_assign',
'_suppliers_id_assign',
];

foreach ($actorTypes as $actorType) {
if (isset($data["$actorType"])) {
if (is_array($data["$actorType"])) {
if (count($data["$actorType"]) < 1) {
unset($data["$actorType"]);
unset($data["${actorType}_notif"]);
} else {
$cleaned = [];
$cleaned_notif = [];
foreach ($data["$actorType"] as $key => $actor) {
if ($actor == 0) {
continue;
}
$cleaned[] = $actor;
$cleaned_notif['use_notification'][] = $data["${actorType}_notif"]['use_notification'][$key];
$cleaned_notif['alternative_email'][] = $data["${actorType}_notif"]['alternative_email'][$key];
}
$data["$actorType"] = $cleaned;
$data["${actorType}_notif"] = $cleaned_notif;
}
} else {
if ($data["$actorType"] == 0) {
unset($data["$actorType"]);
unset($data["${actorType}_notif"]);
}
}
}
}

if (isset($data['_groups_id_requester']) && $data['_groups_id_requester'] == 0) {
unset($data['_groups_id_requester']);
}

if (isset($data['_groups_id_observer']) && $data['_groups_id_observer'] == 0) {
unset($data['_groups_id_observer']);
}

if (isset($data['_groups_id_assign']) && $data['_groups_id_assign'] == 0) {
unset($data['_groups_id_assign']);
}

return $data;
}

/**
* Set default values for the item to create
*
Expand Down
3 changes: 3 additions & 0 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,9 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {

$this->appendFieldsData($formanswer, $data);

// Cleanup actors array
$data = $this->cleanActors($data);

// Create the target change
if (!$changeID = $change->add($data)) {
return null;
Expand Down
3 changes: 3 additions & 0 deletions inc/targetproblem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {

$this->appendFieldsData($formanswer, $data);

// Cleanup actors array
$data = $this->cleanActors($data);

// Create the target problem
if (!$problemID = $problem->add($data)) {
return null;
Expand Down
3 changes: 3 additions & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,9 @@ public function save(PluginFormcreatorFormAnswer $formanswer): ?CommonDBTM {

$this->appendFieldsData($formanswer, $data);

// Cleanup actors array
$data = $this->cleanActors($data);

// Create the target ticket
$data['_auto_import'] = true;
if (!$ticketID = $ticket->add($data)) {
Expand Down
85 changes: 60 additions & 25 deletions tests/3-unit/PluginFormcreatorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,18 @@ public function providerGetTicketStatusForIssue() {
$this->boolean($ticket->isNewItem())->isFalse();
// Creating a ticket directly with status solved or closed
// will prevent credation of ticketvalidation item
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => ($ticketStatus > \CommonITILObject::INCOMING) ? 4 /* Tech */ : 0,
]);
if ($ticketStatus > \CommonITILObject::INCOMING) {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => 4,
]);
} else {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
]);
}
$this->integer((int) $ticket->fields['status'])->isEqualTo($ticketStatus);
$ticket->fields['global_validation'] = \CommonITILValidation::NONE;
$dataSet = [
Expand Down Expand Up @@ -233,11 +240,18 @@ public function providerGetTicketStatusForIssue() {
$this->boolean($ticket->isNewItem())->isFalse();
// Creating a ticket directly with status solved or closed
// will prevent credation of ticketvalidation item
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => ($ticketStatus > \CommonITILObject::INCOMING) ? 4 /* Tech */ : 0,
]);
if ($ticketStatus > \CommonITILObject::INCOMING) {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => 4 // Tech,
]);
} else {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
]);
}
$this->integer((int) $ticket->fields['status'])->isEqualTo($ticketStatus);
$ticket->fields['global_validation'] = \CommonITILValidation::ACCEPTED;
$dataSet = [
Expand Down Expand Up @@ -269,11 +283,18 @@ public function providerGetTicketStatusForIssue() {
$this->boolean($ticket->isNewItem())->isFalse();
// Creating a ticket directly with status solved or closed
// will prevent credation of ticketvalidation item
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => ($ticketStatus > \CommonITILObject::INCOMING) ? 4 /* Tech */ : 0,
]);
if ($ticketStatus > \CommonITILObject::INCOMING) {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => 4, /* Tech */
]);
} else {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
]);
}
$this->integer((int) $ticket->fields['status'])->isEqualTo($ticketStatus);
$ticket->fields['global_validation'] = \CommonITILValidation::WAITING;
$dataSet = [
Expand Down Expand Up @@ -336,11 +357,18 @@ public function providerGetTicketStatusForIssue() {
$this->boolean($ticket->isNewItem())->isFalse();
// Creating a ticket directly with status solved or closed
// will prevent credation of ticketvalidation item
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => ($ticketStatus > \CommonITILObject::INCOMING) ? 4 /* Tech */ : 0,
]);
if ($ticketStatus > \CommonITILObject::INCOMING) {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => 4, /* Tech */
]);
} else {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
]);
}
$this->integer((int) $ticket->fields['status'])->isEqualTo($ticketStatus);
$ticket->fields['global_validation'] = \CommonITILValidation::REFUSED;
$dataSet = [
Expand All @@ -367,11 +395,18 @@ public function providerGetTicketStatusForIssue() {
$this->boolean($ticket->isNewItem())->isFalse();
// Creating a ticket directly with status solved or closed
// will prevent credation of ticketvalidation item
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => ($ticketStatus > \CommonITILObject::INCOMING) ? 4 /* Tech */ : 0,
]);
if ($ticketStatus > \CommonITILObject::INCOMING) {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
'_users_id_assign' => 4, /* Tech */
]);
} else {
$ticket->update([
'id' => $ticket->getID(),
'status' => $ticketStatus,
]);
}
$this->integer((int) $ticket->fields['status'])->isEqualTo($ticketStatus);
$ticket->fields['global_validation'] = \CommonITILValidation::REFUSED;
$dataSet = [
Expand Down

0 comments on commit 44465cb

Please sign in to comment.