Skip to content

Commit

Permalink
feat(targetchange): change template support
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 19, 2021
1 parent 6cc01b9 commit 0ea4079
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 36 deletions.
28 changes: 19 additions & 9 deletions inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ abstract protected function getItem_Supplier();
*/
abstract protected function getItem_Item();

/**
* Get the class name of the target itemtype's template
*
* @return string
*/
abstract protected function getTemplateItemtypeName();

/**
* Get the class name of the target itemtype
*
Expand Down Expand Up @@ -928,16 +935,19 @@ protected function showDestinationEntitySetings($rand) {
}

protected function showTemplateSettings($rand) {
echo '<td width="15%">' . _n('Ticket template', 'Ticket templates', 1) . '</td>';
$templateType = $this->getTemplateItemtypeName();
$templateFk = $templateType::getForeignKeyField();

echo '<td width="15%">' . $templateType::getTypeName(1) . '</td>';
echo '<td width="25%">';
Dropdown::show('TicketTemplate', [
'name' => 'tickettemplates_id',
'value' => $this->fields['tickettemplates_id']
Dropdown::show($templateType, [
'name' => $templateFk,
'value' => $this->fields[$templateFk]
]);
echo '</td>';
}

protected function showDueDateSettings(PluginFormcreatorForm $form, $rand) {
protected function showDueDateSettings($rand) {
echo '<td width="15%">' . __('Time to resolve') . '</td>';
echo '<td width="45%">';

Expand Down Expand Up @@ -1143,7 +1153,7 @@ protected function showOLASettings() {
echo '</tr>';
}

protected function showCategorySettings(PluginFormcreatorForm $form, $rand) {
protected function showCategorySettings($rand) {
echo '<tr>';
echo '<td width="15%">' . __('Category', 'formcreator') . '</td>';
echo '<td width="25%">';
Expand Down Expand Up @@ -1184,7 +1194,7 @@ protected function showCategorySettings(PluginFormcreatorForm $form, $rand) {
echo '</tr>';
}

protected function showUrgencySettings(PluginFormcreatorForm $form, $rand) {
protected function showUrgencySettings($rand) {
echo '<tr>';
echo '<td width="15%">' . __('Urgency') . '</td>';
echo '<td width="45%">';
Expand Down Expand Up @@ -1221,7 +1231,7 @@ protected function showUrgencySettings(PluginFormcreatorForm $form, $rand) {
echo '</tr>';
}

protected function showPluginTagsSettings(PluginFormcreatorForm $form, $rand) {
protected function showPluginTagsSettings($rand) {
global $DB;

$plugin = new Plugin();
Expand Down Expand Up @@ -1387,7 +1397,7 @@ protected function showActorsSettings() {
echo '</table>';
}

protected function showLocationSettings(PluginFormcreatorForm $form, $rand) {
protected function showLocationSettings($rand) {
global $DB;

echo '<tr>';
Expand Down
111 changes: 90 additions & 21 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ static function getEnumUrgencyRule() {
];
}

static function getEnumCategoryRule() {
return [
PluginFormcreatorTargetChange::CATEGORY_RULE_NONE => __('None', 'formcreator'),
PluginFormcreatorTargetChange::CATEGORY_RULE_SPECIFIC => __('Specific category', 'formcreator'),
PluginFormcreatorTargetChange::CATEGORY_RULE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
];
}

protected function getItem_User() {
return new Change_User();
}
Expand All @@ -78,6 +70,10 @@ protected function getTargetItemtypeName() {
return Change::class;
}

protected function getTemplateItemtypeName() {
return ChangeTemplate::class;
}

protected function getCategoryFilter() {
return ['is_change' => 1];
}
Expand Down Expand Up @@ -463,8 +459,8 @@ public function showForm($ID, $options = []) {
$this->showDestinationEntitySetings($rand);

echo '<tr>';
$this->showDueDateSettings($form, $rand);
echo '<td colspan="2"></td>';
$this->showTemplateSettings($rand);
$this->showDueDateSettings($rand);
echo '</tr>';

$this->showSLASettings();
Expand All @@ -473,17 +469,17 @@ public function showForm($ID, $options = []) {
// -------------------------------------------------------------------------------------------
// category of the target
// -------------------------------------------------------------------------------------------
$this->showCategorySettings($form, $rand);
$this->showCategorySettings($rand);

// -------------------------------------------------------------------------------------------
// Urgency selection
// -------------------------------------------------------------------------------------------
$this->showUrgencySettings($form, $rand);
$this->showUrgencySettings($rand);

// -------------------------------------------------------------------------------------------
// Tags
// -------------------------------------------------------------------------------------------
$this->showPluginTagsSettings($form, $rand);
$this->showPluginTagsSettings($rand);

// -------------------------------------------------------------------------------------------
// Conditions to generate the target
Expand Down Expand Up @@ -673,14 +669,15 @@ public function post_updateItem($history = 1) {
}

/**
* Save form data to the target
*
* @param PluginFormcreatorFormAnswer $formanswer Answers previously saved
* Set default values for the ticket to create
*
* @return Change|null generated change
* @param PluginFormcreatorFormAnswer $formanswer
* @return array
*/
public function save(PluginFormcreatorFormAnswer $formanswer) {
// Prepare actors structures for creation of the ticket
public function getDefaultData(PluginFormcreatorFormAnswer $formanswer) : array {
global $DB;

// Prepare actors structures for creation of the change
$this->requesters = [
'_users_id_requester' => [],
'_users_id_requester_notif' => [
Expand Down Expand Up @@ -723,11 +720,84 @@ public function save(PluginFormcreatorFormAnswer $formanswer) {
'_groups_id_assign' => [],
];

$data = Change::getDefaultValues();

$data['requesttypes_id'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();

$data = $this->setTargetCategory($data, $formanswer);

// Set template change from itilcategory when template change is not set in the target (=0)
$itilCategory = new ITILCategory();
$targetItemtype = $this->getTemplateItemtypeName();
/** @var ITILTemplate $targetItem */
$targetItem = new $targetItemtype();
$targetTemplateFk = $targetItemtype::getForeignKeyField();
if ($targetItem->isNewID($this->fields[$targetTemplateFk]) && !$itilCategory->isNewID($data['itilcategories_id'])) {
$rows = $DB->request([
'SELECT' => [$targetTemplateFk],
'FROM' => ITILCategory::getTable(),
'WHERE' => ['id' => $data['itilcategories_id']]
]);
if ($row = $rows->next()) { // assign change template according to resulting change category
$this->fields[$targetTemplateFk] = $row[$targetTemplateFk];
}

}

// Get predefined Fields
$ttp = new ChangeTemplatePredefinedField();
$predefined_fields = $ttp->getPredefinedFields($this->fields['tickettemplates_id'], true);

if (isset($predefined_fields['_users_id_requester'])) {
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, $predefined_fields['_users_id_requester'], true);
unset($predefined_fields['_users_id_requester']);
}
if (isset($predefined_fields['_users_id_observer'])) {
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER, $predefined_fields['_users_id_observer'], true);
unset($predefined_fields['_users_id_observer']);
}
if (isset($predefined_fields['_users_id_assign'])) {
$this->addActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_ASSIGNED, $predefined_fields['_users_id_assign'], true);
unset($predefined_fields['_users_id_assign']);
}

if (isset($predefined_fields['_groups_id_requester'])) {
$this->addGroupActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, $predefined_fields['_groups_id_requester']);
unset($predefined_fields['_groups_id_requester']);
}
if (isset($predefined_fields['_groups_id_observer'])) {
$this->addGroupActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER, $predefined_fields['_groups_id_observer']);
unset($predefined_fields['_groups_id_observer']);
}
if (isset($predefined_fields['_groups_id_assign'])) {
$this->addGroupActor(PluginFormcreatorTarget_Actor::ACTOR_ROLE_ASSIGNED, $predefined_fields['_groups_id_assign']);
unset($predefined_fields['_groups_id_assign']);
}

// Manage special values
if (isset($predefined_fields['date']) && $predefined_fields['date'] == 'NOW') {
$predefined_fields['date'] = $_SESSION['glpi_currenttime'];
}

$data = array_merge($data, $predefined_fields);
return $data;

}

/**
* Save form data to the target
*
* @param PluginFormcreatorFormAnswer $formanswer Answers previously saved
*
* @return Change|null generated change
*/
public function save(PluginFormcreatorFormAnswer $formanswer) {

$data = [];
$change = new Change();
$form = $formanswer->getForm();
$data = $this->getDefaultData($formanswer);

$data['requesttypes_id'] = PluginFormcreatorCommon::getFormcreatorRequestTypeId();

// Parse data
$data['name'] = $this->prepareTemplate(
Expand Down Expand Up @@ -778,7 +848,6 @@ public function save(PluginFormcreatorFormAnswer $formanswer) {
$data = $this->setSLA($data, $formanswer);
$data = $this->setOLA($data, $formanswer);
$data = $this->setTargetUrgency($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;
Expand Down
16 changes: 10 additions & 6 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ protected function getTargetItemtypeName() {
return Ticket::class;
}

protected function getTemplateItemtypeName() {
return TicketTemplate::class;
}

protected function getCategoryFilter() {
return [
'OR' => [
Expand Down Expand Up @@ -195,7 +199,7 @@ public function showForm($ID, $options = []) {

echo '<tr>';
$this->showTemplateSettings($rand);
$this->showDueDateSettings($form, $rand);
$this->showDueDateSettings($rand);
echo '</tr>';

$this->showSLASettings();
Expand All @@ -210,21 +214,21 @@ public function showForm($ID, $options = []) {
// -------------------------------------------------------------------------------------------
// category of the target
// -------------------------------------------------------------------------------------------
$this->showCategorySettings($form, $rand);
$this->showCategorySettings($rand);

// -------------------------------------------------------------------------------------------
// Urgency selection
// -------------------------------------------------------------------------------------------
$this->showUrgencySettings($form, $rand);
$this->showUrgencySettings($rand);

// -------------------------------------------------------------------------------------------
// Location selection
// -------------------------------------------------------------------------------------------
$this->showLocationSettings($form, $rand);
$this->showLocationSettings($rand);
// -------------------------------------------------------------------------------------------
// Tags
// -------------------------------------------------------------------------------------------
$this->showPluginTagsSettings($form, $rand);
$this->showPluginTagsSettings($rand);

// -------------------------------------------------------------------------------------------
// Composite tickets
Expand Down Expand Up @@ -700,7 +704,7 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer) : array
$data = $this->setTargetCategory($data, $formanswer);
$data = $this->setTargetType($data, $formanswer);

// Set template ticket from itilcategorie when template ticket is not set in the target (=0)
// Set template ticket from itilcategory when template ticket is not set in the target (=0)
$itilCategory = new ITILCategory();
$ticket = new Ticket();
if ($ticket->isNewID($this->fields['tickettemplates_id']) && !$itilCategory->isNewID($data['itilcategories_id'])) {
Expand Down
1 change: 1 addition & 0 deletions install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetchanges` (
`name` varchar(255) NOT NULL DEFAULT '',
`plugin_formcreator_forms_id` int(11) NOT NULL DEFAULT '0',
`target_name` varchar(255) NOT NULL DEFAULT '',
`changetemplates_id` int(11) DEFAULT NULL,
`content` longtext,
`impactcontent` longtext,
`controlistcontent` longtext,
Expand Down
3 changes: 3 additions & 0 deletions install/upgrade_to_2.12.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function upgrade(Migration $migration) {
$this->migration->changeField($table, 'language', 'language', 'string', ['value' => '', 'after' => 'is_active']);

$this->normalizeIssues();

$table = 'glpi_plugin_formcreator_targetchanges';
$this->migration->addField($table, 'changetemplates_id', 'integer', ['after' => 'target_name']);
}

/**
Expand Down

0 comments on commit 0ea4079

Please sign in to comment.