|
| 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 | +} |
0 commit comments