Skip to content

Commit d23759a

Browse files
committed
feat(targetticket): set type from question
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 19d1e71 commit d23759a

File tree

7 files changed

+437
-23
lines changed

7 files changed

+437
-23
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
<?php
2+
/**
3+
* ---------------------------------------------------------------------
4+
* Formcreator is a plugin which allows creation of custom forms of
5+
* easy access.
6+
* ---------------------------------------------------------------------
7+
* LICENSE
8+
*
9+
* This file is part of Formcreator.
10+
*
11+
* Formcreator is free software; you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation; either version 2 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* Formcreator is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
23+
* ---------------------------------------------------------------------
24+
* @copyright Copyright © 2011 - 2019 Teclib'
25+
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
26+
* @link https://github.com/pluginsGLPI/formcreator/
27+
* @link https://pluginsglpi.github.io/formcreator/
28+
* @link http://plugins.glpi-project.org/#/plugin/formcreator
29+
* ---------------------------------------------------------------------
30+
*/
31+
32+
class PluginFormcreatorRequestTypeField extends PluginFormcreatorField
33+
{
34+
public function isPrerequisites() {
35+
return true;
36+
}
37+
38+
public function getDesignSpecializationField() {
39+
$rand = mt_rand();
40+
41+
$label = '';
42+
$field = '';
43+
44+
$additions = '<tr class="plugin_formcreator_question_specific">';
45+
$additions .= '<td>';
46+
$additions .= '<label for="dropdown_default_values'.$rand.'">';
47+
$additions .= __('Default values');
48+
$additions .= '</label>';
49+
$additions .= '</td>';
50+
$additions .= '<td>';
51+
$additions .= Ticket::dropdownType('default_values',
52+
[
53+
'value' => $this->value,
54+
'rand' => $rand,
55+
'display' => false,
56+
]
57+
);
58+
$additions .= '</td>';
59+
$additions .= '<td>';
60+
$additions .= '</td>';
61+
$additions .= '<td>';
62+
$additions .= '</td>';
63+
$additions .= '</tr>';
64+
65+
$common = $common = PluginFormcreatorField::getDesignSpecializationField();
66+
$additions .= $common['additions'];
67+
68+
return [
69+
'label' => $label,
70+
'field' => $field,
71+
'additions' => $additions,
72+
'may_be_empty' => false,
73+
'may_be_required' => true,
74+
];
75+
}
76+
77+
public function displayField($canEdit = true) {
78+
if ($canEdit) {
79+
$id = $this->question->getID();
80+
$rand = mt_rand();
81+
$fieldName = 'formcreator_field_' . $id;
82+
Ticket::dropdownType($fieldName, [
83+
'value' => $this->value,
84+
'rand' => $rand,
85+
]);
86+
echo PHP_EOL;
87+
echo Html::scriptBlock("$(function() {
88+
pluginFormcreatorInitializeRequestType('$fieldName', '$rand');
89+
});");
90+
} else {
91+
echo Ticket::getTicketTypeName($this->value);
92+
}
93+
}
94+
95+
public static function getName() {
96+
return __('Request type', 'formcreator');
97+
}
98+
99+
public function prepareQuestionInputForSave($input) {
100+
$this->value = $input['default_values'] != ''
101+
? (int) $input['default_values']
102+
: '3';
103+
return $input;
104+
}
105+
106+
public function parseAnswerValues($input, $nonDestructive = false) {
107+
$key = 'formcreator_field_' . $this->question->getID();
108+
if (!isset($input[$key])) {
109+
$input[$key] = '3';
110+
} else {
111+
if (!is_string($input[$key])) {
112+
return false;
113+
}
114+
}
115+
116+
$this->value = $input[$key];
117+
return true;
118+
}
119+
120+
public static function canRequire() {
121+
return true;
122+
}
123+
124+
public function getAvailableValues() {
125+
return Ticket::getTypes();
126+
}
127+
128+
public function serializeValue() {
129+
if ($this->value === null || $this->value === '') {
130+
return '2';
131+
}
132+
133+
return $this->value;
134+
}
135+
136+
public function deserializeValue($value) {
137+
$this->value = ($value !== null && $value !== '')
138+
? $value
139+
: '2';
140+
}
141+
142+
public function getValueForDesign() {
143+
if ($this->value === null) {
144+
return '';
145+
}
146+
147+
return $this->value;
148+
}
149+
150+
public function getValueForTargetText($richText) {
151+
$available = $this->getAvailableValues();
152+
return $available[$this->value];
153+
}
154+
155+
public function getDocumentsForTarget() {
156+
return [];
157+
}
158+
159+
public function isValid() {
160+
// If the field is required it can't be empty
161+
if ($this->isRequired() && $this->value == '0') {
162+
Session::addMessageAfterRedirect(
163+
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
164+
false,
165+
ERROR);
166+
return false;
167+
}
168+
169+
// All is OK
170+
return true;
171+
}
172+
173+
public function equals($value) {
174+
$available = $this->getAvailableValues();
175+
return strcasecmp($available[$this->value], $value) === 0;
176+
}
177+
178+
public function notEquals($value) {
179+
return !$this->equals($value);
180+
}
181+
182+
public function greaterThan($value) {
183+
$available = $this->getAvailableValues();
184+
return strcasecmp($available[$this->value], $value) > 0;
185+
}
186+
187+
public function lessThan($value) {
188+
return !$this->greaterThan($value) && !$this->equals($value);
189+
}
190+
191+
public function isAnonymousFormCompatible() {
192+
return true;
193+
}
194+
195+
public function getHtmlIcon() {
196+
return '<i class="fa fa-exclamation" aria-hidden="true"></i>';
197+
}
198+
}

inc/targetticket.class.php

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class PluginFormcreatorTargetTicket extends PluginFormcreatorTargetBase
4141
const ASSOCIATE_RULE_SPECIFIC = 2;
4242
const ASSOCIATE_RULE_ANSWER = 3;
4343

44+
const REQUESTTYPE_SPECIFIC = 1;
45+
const REQUESTTYPE_ANSWER = 2;
46+
47+
4448
public static function getTypeName($nb = 1) {
4549
return _n('Target ticket', 'Target tickets', $nb, 'formcreator');
4650
}
@@ -86,6 +90,12 @@ public static function getEnumAssociateRule() {
8690
];
8791
}
8892

93+
public static function getEnumRequestTypeRule() {
94+
return [
95+
self::REQUESTTYPE_SPECIFIC => __('Specific type', 'formcreator'),
96+
self::REQUESTTYPE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
97+
];
98+
}
8999
/**
90100
* Show the Form for the adminsitrator to edit in the config page
91101
*
@@ -373,6 +383,16 @@ public function prepareInputForUpdate($input) {
373383
$input['urgency_question'] = '0';
374384
}
375385

386+
$input['type_question'] = '0';
387+
switch ($input['type_rule']) {
388+
case self::REQUESTTYPE_ANSWER:
389+
$input['type_question'] = $input['_type_question'];
390+
break;
391+
case self::REQUESTTYPE_SPECIFIC:
392+
$input['type_question'] = $input['_type_specific'];
393+
break;
394+
}
395+
376396
switch ($input['category_rule']) {
377397
case self::CATEGORY_RULE_ANSWER:
378398
$input['category_question'] = $input['_category_question'];
@@ -745,14 +765,72 @@ protected function setTargetLocation($data, $formanswer) {
745765
return $data;
746766
}
747767

768+
protected function setTargetType($data, $formanswer) {
769+
global $DB;
770+
771+
$type = null;
772+
switch ($this->fields['type_rule']) {
773+
case self::REQUESTTYPE_ANSWER:
774+
$type = $DB->request([
775+
'SELECT' => ['answer'],
776+
'FROM' => PluginFormcreatorAnswer::getTable(),
777+
'WHERE' => [
778+
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
779+
'plugin_formcreator_questions_id' => $this->fields['type_question']
780+
]
781+
])->next();
782+
$type = $type['answer'];
783+
break;
784+
case self::REQUESTTYPE_SPECIFIC:
785+
$type = $this->fields['type_question'];
786+
break;
787+
default:
788+
$type = null;
789+
}
790+
if (!is_null($type)) {
791+
$data['type'] = $type;
792+
}
793+
794+
return $data;
795+
}
796+
748797
protected function showTypeSettings($rand) {
749798
echo '<tr class="line0">';
750-
echo '<td width="15%">' . __('Type') . '</td>';
799+
echo '<td width="15%">' . __('Request type') . '</td>';
751800
echo '<td width="25%">';
752-
Ticket::dropdownType('type', ['value' => $this->fields['type'], 'rand' => $rand]);
801+
Dropdown::showFromArray('type_rule', static::getEnumRequestTypeRule(), [
802+
'value' => $this->fields['type_rule'],
803+
'rand' => $rand,
804+
'on_change' => "plugin_formcreator_changeRequestType($rand)",
805+
]
806+
);
807+
echo Html::scriptBlock("plugin_formcreator_changeRequestType($rand);");
808+
echo '</td>';
809+
echo '<td width="15%">';
810+
echo '<span id="requesttype_question_title" style="display: none">' . __('Question', 'formcreator') . '</span>';
811+
echo '<span id="requesttype_specific_title" style="display: none">' . __('Type ', 'formcreator') . '</span>';
812+
echo '</td>';
813+
echo '<td width="25%">';
814+
echo '<div id="requesttype_specific_value" style="display: none">';
815+
Ticket::dropdownType('_type_specific',
816+
[
817+
'value' => $this->fields['type_question'],
818+
]
819+
);
820+
echo '</div>';
821+
echo '<div id="requesttype_question_value" style="display: none">';
822+
PluginFormcreatorQuestion::dropdownForForm(
823+
$this->getForm()->getID(),
824+
[
825+
'fieldtype' => ['requesttype'],
826+
],
827+
'_type_question',
828+
[
829+
'value' => $this->fields['type_question']
830+
]
831+
);
832+
echo '</div>';
753833
echo '</td>';
754-
echo '<td></td>';
755-
echo '<td></td>';
756834
echo '</tr>';
757835
}
758836

@@ -858,20 +936,6 @@ protected function setTargetAssociatedItem($data, $formanswer) {
858936
return $data;
859937
}
860938

861-
protected function setTargetType($data, $formanswer) {
862-
switch ($this->fields['type']) {
863-
case Ticket::INCIDENT_TYPE:
864-
$data['type'] = Ticket::INCIDENT_TYPE;
865-
break;
866-
867-
case Ticket::DEMAND_TYPE:
868-
$data['type'] = Ticket::DEMAND_TYPE;
869-
break;
870-
}
871-
872-
return $data;
873-
}
874-
875939
public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) {
876940
global $DB;
877941

install/mysql/plugin_formcreator_empty.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
190190
`name` varchar(255) NOT NULL DEFAULT '',
191191
`plugin_formcreator_forms_id` int(11) NOT NULL DEFAULT '0',
192192
`target_name` varchar(255) NOT NULL DEFAULT '',
193-
`type` int(11) NOT NULL DEFAULT '1',
193+
`type_rule` int(11) NOT NULL DEFAULT '1',
194+
`type_question` int(11) NOT NULL DEFAULT '0',
194195
`tickettemplates_id` int(11) DEFAULT NULL,
195196
`content` longtext,
196197
`due_date_rule` int(11) NOT NULL DEFAULT '1',

install/upgrade_to_2.10.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,11 @@ public function upgrade(Migration $migration) {
5252
'comment' => 'Conditions setting to show the submit button'
5353
]
5454
);
55+
56+
// Add request type specific question
57+
$table = 'glpi_plugin_formcreator_targettickets';
58+
$migration->changeField($table, 'type', 'type_question', 'integer', ['after' => 'target_name', 'value' => '0']);
59+
$migration->migrationOneTable($table);
60+
$migration->addField($table, 'type_rule', 'integer', ['after' => 'target_name', 'value' => '1']);
5561
}
5662
}

0 commit comments

Comments
 (0)