Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_form_GenerisTreeForm | |
0.00% |
0 / 46 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
| buildTree | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| buildReverseTree | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| setHiddenNodes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSelectedInstancesFromPost | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | use oat\tao\helpers\Template; |
| 4 | use oat\tao\helpers\TreeHelper; |
| 5 | |
| 6 | /* |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; under version 2 |
| 10 | * of the License (non-upgradable). |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 22 | * (under the project TAO-TRANSFER); |
| 23 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 24 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Helper to generate simple tree forms, that allow a user |
| 30 | * to modify properties that have a resource range or domain |
| 31 | * |
| 32 | * @author Joel Bout, <joel@taotesting.com> |
| 33 | * @package tao |
| 34 | * @see core_kernel_classes_* packages |
| 35 | |
| 36 | */ |
| 37 | class tao_helpers_form_GenerisTreeForm extends Renderer |
| 38 | { |
| 39 | /** |
| 40 | * Generates a form to define the values of a specific property for a resource |
| 41 | * |
| 42 | * @param core_kernel_classes_Resource $resource |
| 43 | * @param core_kernel_classes_Property $property |
| 44 | * @return tao_helpers_form_GenerisTreeForm |
| 45 | */ |
| 46 | public static function buildTree(core_kernel_classes_Resource $resource, core_kernel_classes_Property $property) |
| 47 | { |
| 48 | $tree = new self($resource, $property); |
| 49 | |
| 50 | $range = $property->getRange(); |
| 51 | $tree->setData('rootNode', $range->getUri()); |
| 52 | $tree->setData('dataUrl', _url('getData', 'GenerisTree', 'tao')); |
| 53 | $tree->setData('saveUrl', _url('setValues', 'GenerisTree', 'tao')); |
| 54 | |
| 55 | $values = $resource->getPropertyValues($property); |
| 56 | $tree->setData('values', $values); |
| 57 | $openNodeUris = TreeHelper::getNodesToOpen($values, $range); |
| 58 | $tree->setData('openNodes', $openNodeUris); |
| 59 | return $tree; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Generates a form to define the reverse values of a specific property for a resource |
| 64 | * This allows to set/remove multiple triples that share the same object |
| 65 | * |
| 66 | * @param core_kernel_classes_Resource $resource |
| 67 | * @param core_kernel_classes_Property $property |
| 68 | * @return tao_helpers_form_GenerisTreeForm |
| 69 | */ |
| 70 | public static function buildReverseTree( |
| 71 | core_kernel_classes_Resource $resource, |
| 72 | core_kernel_classes_Property $property |
| 73 | ) { |
| 74 | $tree = new self($resource, $property); |
| 75 | |
| 76 | $domainCollection = $property->getDomain(); |
| 77 | if (!$domainCollection->isEmpty()) { |
| 78 | $domain = $domainCollection->get(0); |
| 79 | $tree->setData('rootNode', $domain->getUri()); |
| 80 | $tree->setData('dataUrl', _url('getData', 'GenerisTree', 'tao')); |
| 81 | $tree->setData('saveUrl', _url('setReverseValues', 'GenerisTree', 'tao')); |
| 82 | |
| 83 | $values = array_keys($domain->searchInstances([ |
| 84 | $property->getUri() => $resource |
| 85 | ], ['recursive' => true, 'like' => false])); |
| 86 | |
| 87 | $tree->setData('values', $values); |
| 88 | $openNodeUris = TreeHelper::getNodesToOpen($values, $domain); |
| 89 | $tree->setData('openNodes', $openNodeUris); |
| 90 | } |
| 91 | return $tree; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Should not be called directly but is public |
| 96 | * since Renderer is public |
| 97 | * |
| 98 | * @param core_kernel_classes_Resource $resource |
| 99 | * @param core_kernel_classes_Property $property |
| 100 | */ |
| 101 | public function __construct(core_kernel_classes_Resource $resource, core_kernel_classes_Property $property) |
| 102 | { |
| 103 | $tpl = Template::getTemplate('form' . DIRECTORY_SEPARATOR . 'generis_tree_form.tpl', 'tao'); |
| 104 | parent::__construct($tpl); |
| 105 | |
| 106 | $this->setData('id', 'uid' . md5($property->getUri() . $resource->getUri())); |
| 107 | $this->setData('title', $property->getLabel()); |
| 108 | |
| 109 | $this->setData('resourceUri', $resource->getUri()); |
| 110 | $this->setData('propertyUri', $property->getUri()); |
| 111 | |
| 112 | $this->setHiddenNodes([]); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Set list of nodes id to be hidden |
| 117 | * |
| 118 | * @param Array $hiddenNodes |
| 119 | */ |
| 120 | public function setHiddenNodes($hiddenNodes) |
| 121 | { |
| 122 | $this->setData('hiddenNodes', $hiddenNodes); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set the title of the tree widget |
| 127 | * |
| 128 | * @param string $title |
| 129 | */ |
| 130 | public function setTitle($title) |
| 131 | { |
| 132 | $this->setData('title', $title); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * (non-PHPdoc) |
| 137 | * @see Renderer::render() |
| 138 | */ |
| 139 | public function render() |
| 140 | { |
| 141 | return parent::render(); |
| 142 | } |
| 143 | |
| 144 | public static function getSelectedInstancesFromPost() |
| 145 | { |
| 146 | $values = []; |
| 147 | if (isset($_POST['instances'])) { |
| 148 | $json = json_decode($_POST['instances']); |
| 149 | if (!is_null($json)) { |
| 150 | foreach ($json as $coded) { |
| 151 | $val = tao_helpers_Uri::decode($coded); |
| 152 | if (!empty($val)) { |
| 153 | $values[] = $val; |
| 154 | } else { |
| 155 | common_Logger::w('Empty URI in json array in ' . __FUNCTION__); |
| 156 | } |
| 157 | } |
| 158 | } else { |
| 159 | common_Logger::w('json string could not be decoded in ' . __FUNCTION__); |
| 160 | } |
| 161 | } else { |
| 162 | common_Logger::w('No post parameter instances in ' . __FUNCTION__); |
| 163 | } |
| 164 | // Make sure we return a set of unique URIs. |
| 165 | return array_unique($values); |
| 166 | } |
| 167 | } |