Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_elements_xhtml_Treeview | |
0.00% |
0 / 55 |
|
0.00% |
0 / 6 |
380 | |
0.00% |
0 / 1 |
| feed | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
90 | |||
| getOptions | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| setValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| getEvaluatedValue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| rangeToTree | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| 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\model\GenerisTreeFactory; |
| 26 | use oat\tao\helpers\form\elements\xhtml\XhtmlRenderingTrait; |
| 27 | |
| 28 | /** |
| 29 | * Short description of class tao_helpers_form_elements_xhtml_Treeview |
| 30 | * |
| 31 | * @access public |
| 32 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 33 | * @package tao |
| 34 | */ |
| 35 | class tao_helpers_form_elements_xhtml_Treeview extends tao_helpers_form_elements_Treeview |
| 36 | { |
| 37 | use XhtmlRenderingTrait; |
| 38 | |
| 39 | public const NO_TREEVIEW_INTERACTION_IDENTIFIER = 'x-tao-no-treeview-interaction'; |
| 40 | |
| 41 | /** |
| 42 | * Short description of method feed |
| 43 | * |
| 44 | * @access public |
| 45 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 46 | * @return mixed |
| 47 | */ |
| 48 | public function feed() |
| 49 | { |
| 50 | $expression = "/^" . preg_quote($this->name, "/") . "(.)*[0-9]+$/"; |
| 51 | $foundIndexes = []; |
| 52 | foreach ($_POST as $key => $value) { |
| 53 | if (preg_match($expression, $key)) { |
| 54 | $foundIndexes[] = $key; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if ( |
| 59 | (count($foundIndexes) > 0 && $_POST[$foundIndexes[0]] !== self::NO_TREEVIEW_INTERACTION_IDENTIFIER) |
| 60 | || count($foundIndexes) === 0 |
| 61 | ) { |
| 62 | $this->setValues([]); |
| 63 | } elseif ((count($foundIndexes) > 0 && $_POST[$foundIndexes[0]] === self::NO_TREEVIEW_INTERACTION_IDENTIFIER)) { |
| 64 | array_shift($foundIndexes); |
| 65 | } |
| 66 | |
| 67 | foreach ($foundIndexes as $index) { |
| 68 | $this->addValue(tao_helpers_Uri::decode($_POST[$index])); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Short description of method getOptions |
| 74 | * |
| 75 | * @access public |
| 76 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 77 | * @param string format |
| 78 | * @return array |
| 79 | */ |
| 80 | public function getOptions($format = 'flat') |
| 81 | { |
| 82 | switch ($format) { |
| 83 | case 'structured': |
| 84 | $returnValue = parent::getOptions(); |
| 85 | break; |
| 86 | case 'flat': |
| 87 | default: |
| 88 | $returnValue = tao_helpers_form_GenerisFormFactory::extractTreeData(parent::getOptions()); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | return (array) $returnValue; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Short description of method setValue |
| 97 | * |
| 98 | * @access public |
| 99 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 100 | * @param string value |
| 101 | * @return mixed |
| 102 | */ |
| 103 | public function setValue($value) |
| 104 | { |
| 105 | $this->addValue($value); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Short description of method render |
| 110 | * |
| 111 | * @access public |
| 112 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 113 | * @return string |
| 114 | */ |
| 115 | public function render() |
| 116 | { |
| 117 | $widgetTreeName = $this->name . '-TreeBox'; |
| 118 | $widgetValueName = $this->name . '-TreeValues'; |
| 119 | |
| 120 | $returnValue = $this->renderLabel(); |
| 121 | |
| 122 | $returnValue .= "<div class='form-elt-container' style='min-height:50px; overflow-y:auto;'>"; |
| 123 | $returnValue .= "<div id='{$widgetValueName}'>"; |
| 124 | $returnValue .= '<input type="hidden" value="' . self::NO_TREEVIEW_INTERACTION_IDENTIFIER . '" name="' |
| 125 | . $this->name . '_0"/>'; |
| 126 | $returnValue .= "</div>"; |
| 127 | |
| 128 | $returnValue .= "<div id='{$widgetTreeName}'></div>"; |
| 129 | |
| 130 | // initialize the AsyncFileUpload Js component |
| 131 | // phpcs:disable Generic.Files.LineLength |
| 132 | $returnValue .= ' |
| 133 | <script type="text/javascript"> |
| 134 | $(function(){ |
| 135 | require([\'require\', \'jquery\', \'generis.tree.select\', \'context\'], function(req, $, GenerisTreeSelectClass, context) { |
| 136 | $("div[id=\'' . $widgetTreeName . '\']").tree({ |
| 137 | data: { |
| 138 | type : "json", |
| 139 | async: false, |
| 140 | opts : {static : '; |
| 141 | $returnValue .= json_encode($this->getOptions('structured')); |
| 142 | $returnValue .= '} |
| 143 | }, |
| 144 | callback:{ |
| 145 | onload: function(TREE_OBJ) { |
| 146 | checkedElements = ' . json_encode($this->values) . '; |
| 147 | var tree = $("#' . $widgetTreeName . '"); |
| 148 | $.each(checkedElements, function(i, elt){ |
| 149 | NODE = $("li[id=\'"+elt+"\']", tree); |
| 150 | if(NODE.length > 0){ |
| 151 | parent = TREE_OBJ.parent(NODE); |
| 152 | TREE_OBJ.open_branch(parent); |
| 153 | while(parent != -1){ |
| 154 | parent = TREE_OBJ.parent(parent); |
| 155 | TREE_OBJ.open_branch(parent); |
| 156 | } |
| 157 | $.tree.plugins.checkbox.check(NODE); |
| 158 | } |
| 159 | }); |
| 160 | }, |
| 161 | onchange: function(NODE, TREE_OBJ){ |
| 162 | var valueContainer = $("div[id=\'' . $widgetValueName . '\']"); |
| 163 | valueContainer.empty(); |
| 164 | $.each($.tree.plugins.checkbox.get_checked(TREE_OBJ), function(i, myNODE){ |
| 165 | valueContainer.append( |
| 166 | "<input type=\'hidden\' name=\'' . $this->name . '_"+i+"\' value=\'"+$(myNODE).attr("id")+"\' />" |
| 167 | ); |
| 168 | }); |
| 169 | } |
| 170 | }, |
| 171 | types: { |
| 172 | "default" : { |
| 173 | renameable : false, |
| 174 | deletable : false, |
| 175 | creatable : false, |
| 176 | draggable : false |
| 177 | } |
| 178 | }, |
| 179 | ui: { |
| 180 | "theme_name" : "checkbox", |
| 181 | "theme_path" : context.taobase_www + \'js/lib/jsTree/themes/css/style.css\' |
| 182 | }, |
| 183 | plugins : { checkbox : { three_state : false} } |
| 184 | }); |
| 185 | }); |
| 186 | }); |
| 187 | </script>'; |
| 188 | // phpcs:enable Generic.Files.LineLength |
| 189 | $returnValue .= "</div><br />"; |
| 190 | |
| 191 | return (string) $returnValue; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Short description of method getEvaluatedValue |
| 196 | * |
| 197 | * @access public |
| 198 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 199 | * @return mixed |
| 200 | */ |
| 201 | public function getEvaluatedValue() |
| 202 | { |
| 203 | $values = array_map("tao_helpers_Uri::decode", $this->getValues()); |
| 204 | if (count($values) == 1) { |
| 205 | return $values[0]; |
| 206 | } else { |
| 207 | return $values; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public function rangeToTree(core_kernel_classes_Class $range, $recursive = true) |
| 212 | { |
| 213 | $openNodes = array_reduce($range->getSubClasses(true), function ($carry, $item) { |
| 214 | if (! $carry) { |
| 215 | $carry = []; |
| 216 | } |
| 217 | $carry[] = $item->getUri(); |
| 218 | return $carry; |
| 219 | }); |
| 220 | $openNodes[] = $range->getUri(); |
| 221 | $factory = new GenerisTreeFactory(true, $openNodes, 10, 0); |
| 222 | $array = $factory->buildTree($range); |
| 223 | return $array; |
| 224 | } |
| 225 | } |