Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.49% |
43 / 61 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_FormFactory | |
70.49% |
43 / 61 |
|
0.00% |
0 / 6 |
26.32 | |
0.00% |
0 / 1 |
| setRenderMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getForm | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
2.00 | |||
| getElement | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
7.46 | |||
| getElementByWidget | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| getValidator | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| getCommonActions | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | use oat\tao\helpers\form\WidgetRegistry; |
| 26 | |
| 27 | /** |
| 28 | * The FormFactory enable you to create ready-to-use instances of the Form |
| 29 | * It helps you to get the commonly used instances for the default rendering |
| 30 | * etc. |
| 31 | * |
| 32 | * @access public |
| 33 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 34 | * @package tao |
| 35 | */ |
| 36 | class tao_helpers_form_FormFactory |
| 37 | { |
| 38 | // --- ASSOCIATIONS --- |
| 39 | |
| 40 | |
| 41 | // --- ATTRIBUTES --- |
| 42 | |
| 43 | /** |
| 44 | * The rendering mode of the form. |
| 45 | * |
| 46 | * @access protected |
| 47 | * @var string |
| 48 | */ |
| 49 | protected static $renderMode = 'xhtml'; |
| 50 | |
| 51 | // --- OPERATIONS --- |
| 52 | |
| 53 | /** |
| 54 | * Define the rendering mode |
| 55 | * |
| 56 | * @access public |
| 57 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 58 | * @param string $renderMode |
| 59 | */ |
| 60 | public static function setRenderMode($renderMode) |
| 61 | { |
| 62 | self::$renderMode = $renderMode; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Factors an instance of Form |
| 67 | * |
| 68 | * @access public |
| 69 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 70 | * @param string $name |
| 71 | * @param array $options |
| 72 | * @return tao_helpers_form_Form |
| 73 | * @throws common_Exception |
| 74 | */ |
| 75 | public static function getForm($name = '', array $options = []) |
| 76 | { |
| 77 | $returnValue = null; |
| 78 | |
| 79 | //use the right implementation (depending the render mode) |
| 80 | //@todo refactor this and use a FormElementFactory |
| 81 | if (self::$renderMode === 'xhtml') { |
| 82 | $myForm = new tao_helpers_form_xhtml_Form($name, $options); |
| 83 | |
| 84 | $myForm->setDecorators([ |
| 85 | 'element' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div']), |
| 86 | 'group' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div', 'cssClass' => 'form-group']), |
| 87 | 'error' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div', 'cssClass' => 'form-error']), |
| 88 | 'actions-bottom' => new tao_helpers_form_xhtml_TagWrapper([ |
| 89 | 'tag' => 'div', |
| 90 | 'cssClass' => 'form-toolbar' |
| 91 | ]) |
| 92 | ]); |
| 93 | |
| 94 | $myForm->setActions(self::getCommonActions()); |
| 95 | } else { |
| 96 | throw new common_Exception(sprintf('render mode {%s} not yet supported', self::$renderMode)); |
| 97 | } |
| 98 | |
| 99 | $returnValue = $myForm; |
| 100 | |
| 101 | return $returnValue; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create dynamically a Form Element instance of the defined type |
| 106 | * |
| 107 | * @access public |
| 108 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 109 | * @param string $name |
| 110 | * @param string $widgetId |
| 111 | * @return tao_helpers_form_FormElement |
| 112 | * @throws common_Exception |
| 113 | * @throws Exception |
| 114 | */ |
| 115 | public static function getElement($name = '', $widgetId = '') |
| 116 | { |
| 117 | $eltClass = null; |
| 118 | $definition = WidgetRegistry::getWidgetDefinitionById($widgetId); |
| 119 | |
| 120 | if ($definition === null || !isset($definition['renderers'][self::$renderMode])) { |
| 121 | // could be a "pseudo" widget that has not been registered |
| 122 | $candidates = [ |
| 123 | 'tao_helpers_form_elements_xhtml_' . $widgetId, |
| 124 | $widgetId |
| 125 | ]; |
| 126 | |
| 127 | foreach ($candidates as $candidate) { |
| 128 | if (class_exists($candidate)) { |
| 129 | $eltClass = $candidate; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } else { |
| 134 | $eltClass = $definition['renderers'][self::$renderMode]; |
| 135 | } |
| 136 | |
| 137 | if ($eltClass !== null) { |
| 138 | $returnValue = new $eltClass($name); |
| 139 | if (!$returnValue instanceof tao_helpers_form_FormElement) { |
| 140 | throw new common_Exception(sprintf('%s must be a tao_helpers_form_FormElement', $eltClass)); |
| 141 | } |
| 142 | } else { |
| 143 | $returnValue = null; |
| 144 | common_Logger::w(sprintf('Widget type with id %s not yet supported', $widgetId), ['FORM']); |
| 145 | } |
| 146 | |
| 147 | return $returnValue; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param $name |
| 152 | * @param core_kernel_classes_Resource $widget |
| 153 | * @return tao_helpers_form_FormElement |
| 154 | * @throws common_Exception |
| 155 | * @throws common_exception_Error |
| 156 | */ |
| 157 | public static function getElementByWidget($name, core_kernel_classes_Resource $widget) |
| 158 | { |
| 159 | $definition = WidgetRegistry::getWidgetDefinition($widget); |
| 160 | if ($definition === null || !isset($definition['renderers'][self::$renderMode])) { |
| 161 | throw new common_exception_Error( |
| 162 | sprintf('Widget %s not supported in render mode %s', $widget->getUri(), self::$renderMode) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | $eltClass = $definition['renderers'][self::$renderMode]; |
| 167 | $returnValue = new $eltClass($name); |
| 168 | |
| 169 | if (!$returnValue instanceof tao_helpers_form_FormElement) { |
| 170 | throw new common_Exception("$eltClass must be a tao_helpers_form_FormElement"); |
| 171 | } |
| 172 | return $returnValue; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get an instance of a Validator |
| 177 | * |
| 178 | * @access public |
| 179 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 180 | * @param string $name |
| 181 | * @param array $options |
| 182 | * @return tao_helpers_form_Validator |
| 183 | */ |
| 184 | public static function getValidator($name, $options = []) |
| 185 | { |
| 186 | $returnValue = null; |
| 187 | |
| 188 | $class = 'tao_helpers_form_validators_' . $name; |
| 189 | if (class_exists($class)) { |
| 190 | $returnValue = new $class($options); |
| 191 | } else { |
| 192 | common_Logger::w('Unknown validator ' . $name, ['TAO', 'FORM']); |
| 193 | } |
| 194 | |
| 195 | return $returnValue; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get the common actions: save and revert |
| 200 | * |
| 201 | * @access public |
| 202 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 203 | * @param string $context deprecated |
| 204 | * @param bool $save |
| 205 | * @return array |
| 206 | * @throws common_Exception |
| 207 | */ |
| 208 | public static function getCommonActions($context = 'bottom', $save = true) |
| 209 | { |
| 210 | if (!$save) { |
| 211 | return [tao_helpers_form_FormFactory::getElement('save', 'Free')]; |
| 212 | } |
| 213 | |
| 214 | $action = tao_helpers_form_FormFactory::getElement('Save', 'Button'); |
| 215 | $action->setIcon('icon-save'); |
| 216 | $action->setValue(__('Save')); |
| 217 | $action->setType('submit'); |
| 218 | $action->setTestId('save'); |
| 219 | $action->addClass('form-submitter btn-success small'); |
| 220 | |
| 221 | return [$action]; |
| 222 | } |
| 223 | } |