* @package tao * @see core_kernel_classes_* packages */ class tao_helpers_form_GenerisTreeForm extends Renderer { /** * Generates a form to define the values of a specific property for a resource * * @param core_kernel_classes_Resource $resource * @param core_kernel_classes_Property $property * @return tao_helpers_form_GenerisTreeForm */ public static function buildTree(core_kernel_classes_Resource $resource, core_kernel_classes_Property $property) { $tree = new self($resource, $property); $range = $property->getRange(); $tree->setData('rootNode', $range->getUri()); $tree->setData('dataUrl', _url('getData', 'GenerisTree', 'tao')); $tree->setData('saveUrl', _url('setValues', 'GenerisTree', 'tao')); $values = $resource->getPropertyValues($property); $tree->setData('values', $values); $openNodeUris = TreeHelper::getNodesToOpen($values, $range); $tree->setData('openNodes', $openNodeUris); return $tree; } /** * Generates a form to define the reverse values of a specific property for a resource * This allows to set/remove multiple triples that share the same object * * @param core_kernel_classes_Resource $resource * @param core_kernel_classes_Property $property * @return tao_helpers_form_GenerisTreeForm */ public static function buildReverseTree(core_kernel_classes_Resource $resource, core_kernel_classes_Property $property) { $tree = new self($resource, $property); $domainCollection = $property->getDomain(); if (!$domainCollection->isEmpty()) { $domain = $domainCollection->get(0); $tree->setData('rootNode', $domain->getUri()); $tree->setData('dataUrl', _url('getData', 'GenerisTree', 'tao')); $tree->setData('saveUrl', _url('setReverseValues', 'GenerisTree', 'tao')); $values = array_keys($domain->searchInstances([ $property->getUri() => $resource ], ['recursive' => true, 'like' => false])); $tree->setData('values', $values); $openNodeUris = TreeHelper::getNodesToOpen($values, $domain); $tree->setData('openNodes', $openNodeUris); } return $tree; } /** * Should not be called directly but is public * since Renderer is public * * @param core_kernel_classes_Resource $resource * @param core_kernel_classes_Property $property */ public function __construct(core_kernel_classes_Resource $resource, core_kernel_classes_Property $property) { $tpl = Template::getTemplate('form' . DIRECTORY_SEPARATOR . 'generis_tree_form.tpl', 'tao'); parent::__construct($tpl); $this->setData('id', 'uid' . md5($property->getUri() . $resource->getUri())); $this->setData('title', $property->getLabel()); $this->setData('resourceUri', $resource->getUri()); $this->setData('propertyUri', $property->getUri()); $this->setHiddenNodes([]); } /** * Set list of nodes id to be hidden * * @param Array $hiddenNodes */ public function setHiddenNodes($hiddenNodes) { $this->setData('hiddenNodes', $hiddenNodes); } /** * Set the title of the tree widget * * @param string $title */ public function setTitle($title) { $this->setData('title', $title); } /** * (non-PHPdoc) * @see Renderer::render() */ public function render() { return parent::render(); } public static function getSelectedInstancesFromPost() { $values = []; if (isset($_POST['instances'])) { $json = json_decode($_POST['instances']); if (!is_null($json)) { foreach ($json as $coded) { $val = tao_helpers_Uri::decode($coded); if (!empty($val)) { $values[] = $val; } else { common_Logger::w('Empty URI in json array in ' . __FUNCTION__); } } } else { common_Logger::w('json string could not be decoded in ' . __FUNCTION__); } } else { common_Logger::w('No post parameter instances in ' . __FUNCTION__); } // Make sure we return a set of unique URIs. return array_unique($values); } }