/** * Initialize the form elements * * @access protected * @author Bertrand Chevrier, <*****@*****.**> * @return mixed */ protected function initElements() { $property = new core_kernel_classes_Property($this->instance->getUri()); isset($this->options['index']) ? $index = $this->options['index'] : ($index = 1); $propertyProperties = array_merge(tao_helpers_form_GenerisFormFactory::getDefaultProperties(), array(new core_kernel_classes_Property(PROPERTY_IS_LG_DEPENDENT), new core_kernel_classes_Property(TAO_GUIORDER_PROP))); $elementNames = array(); foreach ($propertyProperties as $propertyProperty) { //map properties widgets to form elements $element = tao_helpers_form_GenerisFormFactory::elementMap($propertyProperty); if (!is_null($element)) { //take property values to populate the form $values = $property->getPropertyValuesCollection($propertyProperty); foreach ($values->getIterator() as $value) { if (!is_null($value)) { if ($value instanceof core_kernel_classes_Resource) { $element->setValue($value->getUri()); } if ($value instanceof core_kernel_classes_Literal) { $element->setValue((string) $value); } } } $element->setName("{$index}_{$element->getName()}"); $element->addClass('property'); if ($propertyProperty->getUri() == TAO_GUIORDER_PROP) { $element->addValidator(tao_helpers_form_FormFactory::getValidator('Integer')); } if ($propertyProperty->getUri() == RDFS_LABEL) { $element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); } $this->form->addElement($element); $elementNames[] = $element->getName(); } } //build the type list from the "widget/range to type" map $typeElt = tao_helpers_form_FormFactory::getElement("{$index}_type", 'Combobox'); $typeElt->setDescription(__('Type')); $typeElt->addAttribute('class', 'property-type property'); $typeElt->setEmptyOption(' --- ' . __('select') . ' --- '); $options = array(); $checkRange = false; foreach (tao_helpers_form_GenerisFormFactory::getPropertyMap() as $typeKey => $map) { $options[$typeKey] = $map['title']; $widget = $property->getWidget(); if ($widget instanceof core_kernel_classes_Resource) { if ($widget->getUri() == $map['widget']) { $typeElt->setValue($typeKey); $checkRange = is_null($map['range']); } } } $typeElt->setOptions($options); $this->form->addElement($typeElt); $elementNames[] = $typeElt->getName(); //list drop down $listService = tao_models_classes_ListService::singleton(); $listElt = tao_helpers_form_FormFactory::getElement("{$index}_range", 'Combobox'); $listElt->setDescription(__('List values')); $listElt->addAttribute('class', 'property-listvalues property'); $listElt->setEmptyOption(' --- ' . __('select') . ' --- '); $listOptions = array(); foreach ($listService->getLists() as $list) { $listOptions[tao_helpers_Uri::encode($list->getUri())] = $list->getLabel(); $range = $property->getRange(); if (!is_null($range)) { if ($range->getUri() == $list->getUri()) { $listElt->setValue($list->getUri()); } } } $listOptions['new'] = ' + ' . __('Add / Edit lists'); $listElt->setOptions($listOptions); if ($checkRange) { $listElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); } $this->form->addElement($listElt); $elementNames[] = $listElt->getName(); //index part $indexes = $property->getPropertyValues(new \core_kernel_classes_Property(INDEX_PROPERTY)); foreach ($indexes as $i => $indexUri) { $indexProperty = new \oat\tao\model\search\Index($indexUri); $indexFormContainer = new tao_actions_form_IndexProperty($this->getClazz(), $indexProperty, array('property' => $property->getUri(), 'propertyindex' => $index, 'index' => $i)); /** @var tao_helpers_form_Form $indexForm */ $indexForm = $indexFormContainer->getForm(); foreach ($indexForm->getElements() as $element) { $this->form->addElement($element); $elementNames[] = $element->getName(); } } //add this element only when the property is defined (type) if (!is_null($property->getRange())) { $addIndexElt = tao_helpers_form_FormFactory::getElement("index_{$index}_add", 'Free'); $addIndexElt->setValue("<a href='#' class='btn-info index-adder small index'><span class='icon-add'></span> " . __('Add index') . "</a><div class='clearfix'></div>"); $this->form->addElement($addIndexElt); $elementNames[] = $addIndexElt; } else { $addIndexElt = tao_helpers_form_FormFactory::getElement("index_{$index}_p", 'Free'); $addIndexElt->setValue("<p class='index' >" . __('Choose a type for your property first') . "</p>"); $this->form->addElement($addIndexElt); $elementNames[] = $addIndexElt; } //add an hidden elt for the property uri $encodedUri = tao_helpers_Uri::encode($property->getUri()); $propUriElt = tao_helpers_form_FormFactory::getElement("{$index}_uri", 'Hidden'); $propUriElt->addAttribute('class', 'property-uri property'); $propUriElt->setValue($encodedUri); $this->form->addElement($propUriElt); $elementNames[] = $propUriElt; if (count($elementNames) > 0) { $groupTitle = $this->getGroupTitle($property); $this->form->createGroup("property_{$encodedUri}", $groupTitle, $elementNames); } }
/** * Render the add index sub form. * @throws Exception * @return void */ public function addPropertyIndex() { if (!tao_helpers_Request::isAjax()) { throw new Exception("wrong request mode"); } if (!$this->hasRequestParameter('uri')) { throw new Exception("wrong request Parameter"); } $uri = $this->getRequestParameter('uri'); $clazz = $this->getCurrentClass(); $index = 1; if ($this->hasRequestParameter('index')) { $index = $this->getRequestParameter('index'); } $propertyIndex = 1; if ($this->hasRequestParameter('propertyIndex')) { $propertyIndex = $this->getRequestParameter('propertyIndex'); } //create and attach the new index property to the property $property = new core_kernel_classes_Property(tao_helpers_Uri::decode($uri)); $class = new \core_kernel_classes_Class("http://www.tao.lu/Ontologies/TAO.rdf#Index"); //get property range to select a default tokenizer /** @var core_kernel_classes_Class $range */ $range = $property->getRange(); //range is empty select item content $tokenizer = null; if (is_null($range)) { $tokenizer = new core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAO.rdf#RawValueTokenizer'); } else { $tokenizer = $range->getUri() === RDFS_LITERAL ? new core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAO.rdf#RawValueTokenizer') : new core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAO.rdf#LabelTokenizer'); } $indexClass = new core_kernel_classes_Class('http://www.tao.lu/Ontologies/TAO.rdf#Index'); $i = 0; $indexIdentifierBackup = preg_replace('/[^a-z_0-9]/', '_', strtolower($property->getLabel())); $indexIdentifierBackup = ltrim(trim($indexIdentifierBackup, '_'), '0..9'); $indexIdentifier = $indexIdentifierBackup; do { if ($i !== 0) { $indexIdentifier = $indexIdentifierBackup . '_' . $i; } $resources = $indexClass->searchInstances(array(INDEX_PROPERTY_IDENTIFIER => $indexIdentifier), array('like' => false)); $count = count($resources); $i++; } while ($count !== 0); $indexProperty = $class->createInstanceWithProperties(array(RDFS_LABEL => preg_replace('/_/', ' ', ucfirst($indexIdentifier)), INDEX_PROPERTY_IDENTIFIER => $indexIdentifier, INDEX_PROPERTY_TOKENIZER => $tokenizer, INDEX_PROPERTY_FUZZY_MATCHING => GENERIS_TRUE, INDEX_PROPERTY_DEFAULT_SEARCH => GENERIS_FALSE)); $property->setPropertyValue(new core_kernel_classes_Property(INDEX_PROPERTY), $indexProperty); //generate form $indexFormContainer = new tao_actions_form_IndexProperty($clazz, $indexProperty, array('index' => $index, 'propertyindex' => $propertyIndex)); $myForm = $indexFormContainer->getForm(); $form = trim(preg_replace('/\\s+/', ' ', $myForm->renderElements())); echo json_encode(array('form' => $form)); }