Skip to content

Commit

Permalink
feat(targetticket): set type from question
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jan 23, 2020
1 parent 19d1e71 commit d23759a
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 23 deletions.
198 changes: 198 additions & 0 deletions inc/fields/requesttypefield.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2019 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/

class PluginFormcreatorRequestTypeField extends PluginFormcreatorField
{
public function isPrerequisites() {
return true;
}

public function getDesignSpecializationField() {
$rand = mt_rand();

$label = '';
$field = '';

$additions = '<tr class="plugin_formcreator_question_specific">';
$additions .= '<td>';
$additions .= '<label for="dropdown_default_values'.$rand.'">';
$additions .= __('Default values');
$additions .= '</label>';
$additions .= '</td>';
$additions .= '<td>';
$additions .= Ticket::dropdownType('default_values',
[
'value' => $this->value,
'rand' => $rand,
'display' => false,
]
);
$additions .= '</td>';
$additions .= '<td>';
$additions .= '</td>';
$additions .= '<td>';
$additions .= '</td>';
$additions .= '</tr>';

$common = $common = PluginFormcreatorField::getDesignSpecializationField();
$additions .= $common['additions'];

return [
'label' => $label,
'field' => $field,
'additions' => $additions,
'may_be_empty' => false,
'may_be_required' => true,
];
}

public function displayField($canEdit = true) {
if ($canEdit) {
$id = $this->question->getID();
$rand = mt_rand();
$fieldName = 'formcreator_field_' . $id;
Ticket::dropdownType($fieldName, [
'value' => $this->value,
'rand' => $rand,
]);
echo PHP_EOL;
echo Html::scriptBlock("$(function() {
pluginFormcreatorInitializeRequestType('$fieldName', '$rand');
});");
} else {
echo Ticket::getTicketTypeName($this->value);
}
}

public static function getName() {
return __('Request type', 'formcreator');
}

public function prepareQuestionInputForSave($input) {
$this->value = $input['default_values'] != ''
? (int) $input['default_values']
: '3';
return $input;
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->question->getID();
if (!isset($input[$key])) {
$input[$key] = '3';
} else {
if (!is_string($input[$key])) {
return false;
}
}

$this->value = $input[$key];
return true;
}

public static function canRequire() {
return true;
}

public function getAvailableValues() {
return Ticket::getTypes();
}

public function serializeValue() {
if ($this->value === null || $this->value === '') {
return '2';
}

return $this->value;
}

public function deserializeValue($value) {
$this->value = ($value !== null && $value !== '')
? $value
: '2';
}

public function getValueForDesign() {
if ($this->value === null) {
return '';
}

return $this->value;
}

public function getValueForTargetText($richText) {
$available = $this->getAvailableValues();
return $available[$this->value];
}

public function getDocumentsForTarget() {
return [];
}

public function isValid() {
// If the field is required it can't be empty
if ($this->isRequired() && $this->value == '0') {
Session::addMessageAfterRedirect(
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
false,
ERROR);
return false;
}

// All is OK
return true;
}

public function equals($value) {
$available = $this->getAvailableValues();
return strcasecmp($available[$this->value], $value) === 0;
}

public function notEquals($value) {
return !$this->equals($value);
}

public function greaterThan($value) {
$available = $this->getAvailableValues();
return strcasecmp($available[$this->value], $value) > 0;
}

public function lessThan($value) {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function isAnonymousFormCompatible() {
return true;
}

public function getHtmlIcon() {
return '<i class="fa fa-exclamation" aria-hidden="true"></i>';
}
}
100 changes: 82 additions & 18 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class PluginFormcreatorTargetTicket extends PluginFormcreatorTargetBase
const ASSOCIATE_RULE_SPECIFIC = 2;
const ASSOCIATE_RULE_ANSWER = 3;

const REQUESTTYPE_SPECIFIC = 1;
const REQUESTTYPE_ANSWER = 2;


public static function getTypeName($nb = 1) {
return _n('Target ticket', 'Target tickets', $nb, 'formcreator');
}
Expand Down Expand Up @@ -86,6 +90,12 @@ public static function getEnumAssociateRule() {
];
}

public static function getEnumRequestTypeRule() {
return [
self::REQUESTTYPE_SPECIFIC => __('Specific type', 'formcreator'),
self::REQUESTTYPE_ANSWER => __('Equals to the answer to the question', 'formcreator'),
];
}
/**
* Show the Form for the adminsitrator to edit in the config page
*
Expand Down Expand Up @@ -373,6 +383,16 @@ public function prepareInputForUpdate($input) {
$input['urgency_question'] = '0';
}

$input['type_question'] = '0';
switch ($input['type_rule']) {
case self::REQUESTTYPE_ANSWER:
$input['type_question'] = $input['_type_question'];
break;
case self::REQUESTTYPE_SPECIFIC:
$input['type_question'] = $input['_type_specific'];
break;
}

switch ($input['category_rule']) {
case self::CATEGORY_RULE_ANSWER:
$input['category_question'] = $input['_category_question'];
Expand Down Expand Up @@ -745,14 +765,72 @@ protected function setTargetLocation($data, $formanswer) {
return $data;
}

protected function setTargetType($data, $formanswer) {
global $DB;

$type = null;
switch ($this->fields['type_rule']) {
case self::REQUESTTYPE_ANSWER:
$type = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->getID(),
'plugin_formcreator_questions_id' => $this->fields['type_question']
]
])->next();
$type = $type['answer'];
break;
case self::REQUESTTYPE_SPECIFIC:
$type = $this->fields['type_question'];
break;
default:
$type = null;
}
if (!is_null($type)) {
$data['type'] = $type;
}

return $data;
}

protected function showTypeSettings($rand) {
echo '<tr class="line0">';
echo '<td width="15%">' . __('Type') . '</td>';
echo '<td width="15%">' . __('Request type') . '</td>';
echo '<td width="25%">';
Ticket::dropdownType('type', ['value' => $this->fields['type'], 'rand' => $rand]);
Dropdown::showFromArray('type_rule', static::getEnumRequestTypeRule(), [
'value' => $this->fields['type_rule'],
'rand' => $rand,
'on_change' => "plugin_formcreator_changeRequestType($rand)",
]
);
echo Html::scriptBlock("plugin_formcreator_changeRequestType($rand);");
echo '</td>';
echo '<td width="15%">';
echo '<span id="requesttype_question_title" style="display: none">' . __('Question', 'formcreator') . '</span>';
echo '<span id="requesttype_specific_title" style="display: none">' . __('Type ', 'formcreator') . '</span>';
echo '</td>';
echo '<td width="25%">';
echo '<div id="requesttype_specific_value" style="display: none">';
Ticket::dropdownType('_type_specific',
[
'value' => $this->fields['type_question'],
]
);
echo '</div>';
echo '<div id="requesttype_question_value" style="display: none">';
PluginFormcreatorQuestion::dropdownForForm(
$this->getForm()->getID(),
[
'fieldtype' => ['requesttype'],
],
'_type_question',
[
'value' => $this->fields['type_question']
]
);
echo '</div>';
echo '</td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
}

Expand Down Expand Up @@ -858,20 +936,6 @@ protected function setTargetAssociatedItem($data, $formanswer) {
return $data;
}

protected function setTargetType($data, $formanswer) {
switch ($this->fields['type']) {
case Ticket::INCIDENT_TYPE:
$data['type'] = Ticket::INCIDENT_TYPE;
break;

case Ticket::DEMAND_TYPE:
$data['type'] = Ticket::DEMAND_TYPE;
break;
}

return $data;
}

public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) {
global $DB;

Expand Down
3 changes: 2 additions & 1 deletion install/mysql/plugin_formcreator_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` (
`name` varchar(255) NOT NULL DEFAULT '',
`plugin_formcreator_forms_id` int(11) NOT NULL DEFAULT '0',
`target_name` varchar(255) NOT NULL DEFAULT '',
`type` int(11) NOT NULL DEFAULT '1',
`type_rule` int(11) NOT NULL DEFAULT '1',
`type_question` int(11) NOT NULL DEFAULT '0',
`tickettemplates_id` int(11) DEFAULT NULL,
`content` longtext,
`due_date_rule` int(11) NOT NULL DEFAULT '1',
Expand Down
6 changes: 6 additions & 0 deletions install/upgrade_to_2.10.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,11 @@ public function upgrade(Migration $migration) {
'comment' => 'Conditions setting to show the submit button'
]
);

// Add request type specific question
$table = 'glpi_plugin_formcreator_targettickets';
$migration->changeField($table, 'type', 'type_question', 'integer', ['after' => 'target_name', 'value' => '0']);
$migration->migrationOneTable($table);
$migration->addField($table, 'type_rule', 'integer', ['after' => 'target_name', 'value' => '1']);
}
}
Loading

0 comments on commit d23759a

Please sign in to comment.