-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(targetticket): set type from question
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
- Loading branch information
Showing
7 changed files
with
437 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.