/** * (non-PHPdoc) * @see common_user_auth_Adapter::authenticate() */ public function authenticate() { $userClass = new core_kernel_classes_Class(CLASS_GENERIS_USER); $filters = array(PROPERTY_USER_LOGIN => $this->username); $options = array('like' => false, 'recursive' => true); $users = $userClass->searchInstances($filters, $options); if (count($users) > 1) { // Multiple users matching throw new common_exception_InconsistentData("Multiple Users found with the same login '" . $this->username . "'."); } if (empty($users)) { // fake code execution to prevent timing attacks $label = new core_kernel_classes_Property(RDFS_LABEL); $hash = $label->getUniquePropertyValue($label); if (!core_kernel_users_Service::getPasswordHash()->verify($this->password, $hash)) { throw new core_kernel_users_InvalidLoginException(); } // should never happen, added for integrity throw new core_kernel_users_InvalidLoginException(); } $userResource = current($users); $hash = $userResource->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_USER_PASSWORD)); if (!core_kernel_users_Service::getPasswordHash()->verify($this->password, $hash)) { throw new core_kernel_users_InvalidLoginException(); } return new core_kernel_users_GenerisUser($userResource); }
/** * * @param \core_kernel_classes_Property $property * @param mixed $value * @param string $userMessage */ public function __construct(\core_kernel_classes_Property $property, $value, $userMessage) { parent::__construct($userMessage . ' ' . $property->getUri() . ' ' . $value); $this->property = $property; $this->value = $value; $this->userMessage = $userMessage; }
public function setReverseValues() { if (!tao_helpers_Request::isAjax()) { throw new common_exception_IsAjaxAction(__FUNCTION__); } $values = tao_helpers_form_GenerisTreeForm::getSelectedInstancesFromPost(); $resource = new core_kernel_classes_Resource($this->getRequestParameter('resourceUri')); $property = new core_kernel_classes_Property($this->getRequestParameter('propertyUri')); $currentValues = array(); foreach ($property->getDomain() as $domain) { $instances = $domain->searchInstances(array($property->getUri() => $resource), array('recursive' => true, 'like' => false)); $currentValues = array_merge($currentValues, array_keys($instances)); } $toAdd = array_diff($values, $currentValues); $toRemove = array_diff($currentValues, $values); $success = true; foreach ($toAdd as $uri) { $subject = new core_kernel_classes_Resource($uri); $success = $success && $subject->setPropertyValue($property, $resource); } foreach ($toRemove as $uri) { $subject = new core_kernel_classes_Resource($uri); $success = $success && $subject->removePropertyValue($property, $resource); } echo json_encode(array('saved' => $success)); }
public static function getIndexes(\core_kernel_classes_Property $property) { $indexUris = $property->getPropertyValues(new \core_kernel_classes_Property('http://www.tao.lu/Ontologies/TAO.rdf#PropertyIndex')); $indexes = array(); foreach ($indexes as $indexUri) { $indexes[] = new Index($indexUri); } }
/** * Get all indexes of a property * * @param \core_kernel_classes_Property $property * @return multitype:\oat\tao\model\search\Index */ public static function getIndexes(\core_kernel_classes_Property $property) { $indexUris = $property->getPropertyValues(new \core_kernel_classes_Property(INDEX_PROPERTY)); $indexes = array(); foreach ($indexUris as $indexUri) { $indexes[] = new Index($indexUri); } return $indexes; }
/** * Get groups to which delivery is assigned. * @param \core_kernel_classes_Resource $delivery * @return \core_kernel_classes_Resource[] */ public function getGroups(\core_kernel_classes_Resource $delivery) { $result = array(); $groupsProperty = new \core_kernel_classes_Property(PROPERTY_GROUP_DELVIERY); $domainCollection = $groupsProperty->getDomain(); if (!$domainCollection->isEmpty()) { $domain = $domainCollection->get(0); $result = $domain->searchInstances(array($groupsProperty->getUri() => $delivery), array('recursive' => false, 'like' => false)); } return $result; }
protected function getIndexes(\core_kernel_classes_Property $property) { if (!isset($this->indexMap[$property->getUri()])) { $this->indexMap[$property->getUri()] = array(); $indexes = $property->getPropertyValues($this->getProperty(INDEX_PROPERTY)); foreach ($indexes as $indexUri) { $this->indexMap[$property->getUri()][] = new Index($indexUri); } } return $this->indexMap[$property->getUri()]; }
/** * remove a class property * * @param core_kernel_classes_Property $property * @return bool */ public function deleteClassProperty(core_kernel_classes_Property $property) { $indexes = $property->getPropertyValues(new core_kernel_classes_Property(INDEX_PROPERTY)); //delete property and the existing values of this property if ($returnValue = $property->delete(true)) { //delete index linked to the property foreach ($indexes as $indexUri) { $index = new core_kernel_classes_Resource($indexUri); $returnValue = $this->deletePropertyIndex($index); } } return $returnValue; }
/** * Short description of method createProperty * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @param Class clazz * @param string label * @param string comment * @param boolean isLgDependent * @param string uri * @return core_kernel_classes_Property */ public static function createProperty(core_kernel_classes_Class $clazz, $label = '', $comment = '', $isLgDependent = false, $uri = '') { $returnValue = null; $property = new core_kernel_classes_Class(RDF_PROPERTY); $propertyInstance = self::createInstance($property, $label, $comment, $uri); $returnValue = new core_kernel_classes_Property($propertyInstance->getUri()); if (!$returnValue->setDomain($clazz)) { throw new common_Exception('An error occured during Property creation.'); } else { $returnValue->setLgDependent($isLgDependent); } return $returnValue; }
public function getValidators(\core_kernel_classes_Property $property) { $validationProp = $this->getProperty(self::PROPERTY_VALIDATION_RULE); $rules = []; foreach ($property->getPropertyValues($validationProp) as $ruleId) { $rule = $this->get($ruleId); if ($rule == '') { throw new \common_exception_NotFound('No validation rule found with id "' . $ruleId . '"'); } $rules[] = $rule; } return $rules; }
/** * Add column to export * Check if core_kernel_classes_Property exists into $config * * @param $column * @param array $config * @throws ExtractorException * @return Extractor $this */ public function addColumn($column, array $config) { if (!isset($config['property'])) { throw new ExtractorException('Property config is missing.'); } $property = new \core_kernel_classes_Property($config['property']); if (!$property->exists()) { throw new ExtractorException('Property config is not a valid property uri.'); } $config['property'] = $property; $this->columns[$column] = $config; return $this; }
/** * Initialize the form elements * * @access protected * @author Bertrand Chevrier, <*****@*****.**> * @return mixed */ protected function initElements() { parent::initElements(); $elements = $this->form->getElements(); $this->form->setElements(array()); $currentLangElt = tao_helpers_form_FormFactory::getElement('current_lang', 'Textbox'); $currentLangElt->setDescription(__('Current language')); $currentLangElt->setAttributes(array('readonly' => 'true')); $currentLangElt->setValue(\common_session_SessionManager::getSession()->getDataLanguage()); //API lang /data lang $this->form->addElement($currentLangElt); $dataLangElement = tao_helpers_form_FormFactory::getElement('translate_lang', 'Combobox'); $dataLangElement->setDescription(__('Translate to')); $dataLangElement->setOptions(tao_helpers_I18n::getAvailableLangsByUsage(new core_kernel_classes_Resource(INSTANCE_LANGUAGE_USAGE_DATA))); $dataLangElement->setEmptyOption(__('Select a language')); $dataLangElement->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $this->form->addElement($dataLangElement); $this->form->createGroup('translation_info', __('Translation parameters'), array('current_lang', 'translate_lang')); $dataGroup = array(); foreach ($elements as $element) { if ($element instanceof tao_helpers_form_elements_Hidden || $element->getName() == 'uri' || $element->getName() == 'classUri') { $this->form->addElement($element); } else { $propertyUri = tao_helpers_Uri::decode($element->getName()); $property = new core_kernel_classes_Property($propertyUri); //translate only language dependent properties or Labels //supported widget are: Textbox, TextArea, HtmlArea //@todo support other widgets if ($property->isLgDependent() && ($element instanceof tao_helpers_form_elements_Textbox || $element instanceof tao_helpers_form_elements_TextArea || $element instanceof tao_helpers_form_elements_HtmlArea) || $propertyUri == RDFS_LABEL) { $translatedElt = clone $element; $viewElt = tao_helpers_form_FormFactory::getElement('view_' . $element->getName(), 'Label'); $viewElt->setDescription($element->getDescription()); $viewElt->setValue($element->getValue()); $viewElt->setAttribute('no-format', true); if ($element instanceof tao_helpers_form_elements_HtmlArea) { $viewElt->setAttribute('htmlentities', false); } $this->form->addElement($viewElt); $dataGroup[] = $viewElt->getName(); $translatedElt->setDescription(' '); $translatedElt->setValue(''); if ($propertyUri == RDFS_LABEL) { $translatedElt->setForcedValid(); } $this->form->addElement($translatedElt); $dataGroup[] = $translatedElt->getName(); } } } $this->form->createGroup('translation_form', __('Translate'), $dataGroup); }
protected function data_dump($data, $sortProperty = null) { if ($data instanceof core_kernel_classes_Resource) { $propertyValues = array(); $epoch = ''; foreach ($data->getRdfTriples()->getIterator() as $triple) { $key = $triple->predicate; if (!is_null($sortProperty) && $sortProperty->getUri() == $key) { $epoch = $triple->epoch; } if ($this->diplayPropertyLabels) { if (!isset($this->propertyLabels[$key])) { $property = new core_kernel_classes_Property($key); $this->propertyLabels[$key] = trim(strip_tags($property->getLabel())); } if (!empty($this->propertyLabels[$key])) { $key = $this->propertyLabels[$key]; } } $value = $triple->object; if ($this->unserialize && preg_match('/^a:[0-9]{1,2}:{/i', $value)) { $unserializedValue = unserialize($value); if (!is_null($unserializedValue)) { $desc = is_array($unserializedValue) ? 'array ' . count($unserializedValue) . ': ' . implode(', ', array_keys($unserializedValue)) : 'object'; if ($this->unserialize == 2) { $value = array("unserialized ({$desc})" => $unserializedValue); } else { $value = array("unserialized ({$desc})" => $value); } } } if (isset($propertyValues[$key])) { if (is_array($propertyValues[$key])) { $propertyValues[$key][] = $value; } else { $propertyValues[$key] = array($propertyValues[$key], $value); } } else { $propertyValues[$key] = $value; } } $returnValue = array('resource' => "{$data->getLabel()} ({$data->getUri()})", 'properties' => $propertyValues); if (!empty($epoch)) { $returnValue['epoch'] = $epoch; } var_dump($returnValue); } else { var_dump($data); } }
/** * Short description of method createSPX * * @access public * @author firstname and lastname of author, <*****@*****.**> * @param Resource $subject * @param Property $predicate * @return core_kernel_rules_Term */ public static function createSPX(core_kernel_classes_Resource $subject, core_kernel_classes_Property $predicate) { $returnValue = null; $termSPXClass = new core_kernel_classes_Class(CLASS_TERM_SUJET_PREDICATE_X, __METHOD__); $label = 'Def Term SPX Label : ' . $subject->getLabel() . ' - ' . $predicate->getLabel(); $comment = 'Def Term SPX Label : ' . $subject->getUri() . ' ' . $predicate->getUri(); $SPXResource = core_kernel_classes_ResourceFactory::create($termSPXClass, $label, $comment); $returnValue = new core_kernel_rules_Term($SPXResource->getUri()); $subjectProperty = new core_kernel_classes_Property(PROPERTY_TERM_SPX_SUBJET, __METHOD__); $predicateProperty = new core_kernel_classes_Property(PROPERTY_TERM_SPX_PREDICATE, __METHOD__); $returnValue->setPropertyValue($subjectProperty, $subject->getUri()); $returnValue->setPropertyValue($predicateProperty, $predicate->getUri()); return $returnValue; }
/** * Enable you to map an rdf property to a form element using the Widget * * @access public * @author Bertrand Chevrier, <*****@*****.**> * @param Property property * @return tao_helpers_form_FormElement */ public static function elementMap(core_kernel_classes_Property $property) { $returnValue = null; //create the element from the right widget $property->feed(); $widgetResource = $property->getWidget(); if (is_null($widgetResource)) { return null; } //authoring widget is not used in standalone mode if ($widgetResource->getUri() == 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#Authoring' && tao_helpers_Context::check('STANDALONE_MODE')) { return null; } // horrible hack to fix file widget if ($widgetResource->getUri() == 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#AsyncFile') { $widgetResource = new core_kernel_classes_Resource('http://www.tao.lu/datatypes/WidgetDefinitions.rdf#GenerisAsyncFile'); } $element = tao_helpers_form_FormFactory::getElementByWidget(tao_helpers_Uri::encode($property->getUri()), $widgetResource); if (!is_null($element)) { if ($element->getWidget() != $widgetResource->getUri()) { common_Logger::w('Widget definition differs from implementation: ' . $element->getWidget() . ' != ' . $widgetResource->getUri()); return null; } //use the property label as element description $propDesc = strlen(trim($property->getLabel())) > 0 ? $property->getLabel() : str_replace(LOCAL_NAMESPACE, '', $property->getUri()); $element->setDescription($propDesc); //multi elements use the property range as options if (method_exists($element, 'setOptions')) { $range = $property->getRange(); if ($range != null) { $options = array(); if ($element instanceof tao_helpers_form_elements_Treeview) { if ($property->getUri() == RDFS_RANGE) { $options = self::rangeToTree(new core_kernel_classes_Class(RDFS_RESOURCE)); } else { $options = self::rangeToTree($range); } } else { foreach ($range->getInstances(true) as $rangeInstance) { $options[tao_helpers_Uri::encode($rangeInstance->getUri())] = $rangeInstance->getLabel(); } //set the default value to an empty space if (method_exists($element, 'setEmptyOption')) { $element->setEmptyOption(' '); } } //complete the options listing $element->setOptions($options); } } $returnValue = $element; } return $returnValue; }
/** * @return array */ protected function getResource($relPath) { $dir = $this->getTempDirectory(); $file = $dir->getFile($relPath); $file->put(file_get_contents(__DIR__ . $relPath)); /** @var FileReferenceSerializer $serializer */ $serializer = ServiceManager::getServiceManager()->get(FileReferenceSerializer::SERVICE_ID); $fileUri = $serializer->serialize($file); $class = new \core_kernel_classes_Class('fakeClass'); $resource = $class->createInstance('fakeInstance'); $prop = new \core_kernel_classes_Property('fakeProp'); $prop->setRange(new \core_kernel_classes_Class(CLASS_GENERIS_FILE)); $resource->editPropertyValues($prop, new \core_kernel_classes_Resource($fileUri)); return array($file, $resource); }
/** * Test the service factory: dynamical instantiation and single instance serving */ public function testMultiple() { $propClass = new core_kernel_classes_Class(RDF_PROPERTY); $q = "SELECT subject, count(object)\n FROM statements\n WHERE predicate = ?\n GROUP BY subject\n HAVING (count(object) > 1)"; foreach ($propClass->getInstances(true) as $property) { $property = new core_kernel_classes_Property($property); if (!$property->isMultiple() && !$property->isLgDependent()) { // bypass generis $result = core_kernel_classes_DbWrapper::singleton()->query($q, array($property->getUri())); while ($statement = $result->fetch()) { $this->fail($property->getUri() . ' has multiple values but is not multiple.'); } } } }
/** * @param \core_kernel_classes_Triple $triple * @return bool */ public static function isFileReference(\core_kernel_classes_Triple $triple) { $prop = new \core_kernel_classes_Property($triple->predicate); $range = $prop->getRange(); $rangeUri = is_null($range) ? '' : $range->getUri(); switch ($rangeUri) { case CLASS_GENERIS_FILE: return true; case RDFS_RESOURCE: $object = new \core_kernel_classes_Resource($triple->object); return $object->hasType(new \core_kernel_classes_Class(CLASS_GENERIS_FILE)); default: return false; } }
/** * Get items of a specific model * @param string|core_kernel_classes_Resource $itemModel - the item model URI * @return core_kernel_classes_Resource[] the found items */ public function getAllByModel($itemModel) { if (!empty($itemModel)) { $uri = $itemModel instanceof core_kernel_classes_Resource ? $itemModel->getUri() : $itemModel; return $this->itemClass->searchInstances(array($this->itemModelProperty->getUri() => $uri), array('recursive' => true)); } return array(); }
/** * 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()); }
public function lookup(array $metadataValues) { $lookup = false; foreach ($metadataValues as $metadataValue) { $path = $metadataValue->getPath(); $expectedPath = array(RDFS_LABEL); if ($path === $expectedPath) { // Check for such a value in database... $prop = new \core_kernel_classes_Property(RDFS_LABEL); $class = new \core_kernel_classes_Class(RDFS_CLASS); $instances = $class->searchInstances(array($prop->getUri() => $metadataValue->getValue()), array('like' => false, 'recursive' => true)); if (count($instances) > 0) { $lookup = new \core_kernel_classes_Class(reset($instances)); break; } } } return $lookup; }
/** * Short description of method initElements * * @access public * @author Bertrand Chevrier, <*****@*****.**> * @return mixed */ public 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(PROPERTY_WIDGET), new core_kernel_classes_Property(RDFS_RANGE))); $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("property_{$index}_{$element->getName()}"); $element->addClass('property'); if ($propertyProperty->getUri() == RDFS_LABEL) { $element->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); } $this->form->addElement($element); $elementNames[] = $element->getName(); } } $encodedUri = tao_helpers_Uri::encode($property->getUri()); $modeElt = tao_helpers_form_FormFactory::getElement("{$index}_uri", 'Hidden'); $modeElt->setValue($encodedUri); $modeElt->addClass('property'); $this->form->addElement($modeElt); $elementNames[] = $modeElt->getName(); if (count($elementNames) > 0) { $groupTitle = $this->getGroupTitle($property); $this->form->createGroup("property_{$encodedUri}", $groupTitle, $elementNames); } }
public function testRightModels() { $namespaces = common_ext_NamespaceManager::singleton()->getAllNamespaces(); $localNamespace = $namespaces[LOCAL_NAMESPACE . '#']; // In tao context, the only one model which is updatable $updatableModels = core_kernel_persistence_smoothsql_SmoothModel::getUpdatableModelIds(); $this->assertEquals(1, count($updatableModels)); $this->assertEquals(1, $localNamespace->getModelId()); $readableModels = core_kernel_persistence_smoothsql_SmoothModel::getReadableModelIds(); $this->assertTrue(count($readableModels) > 3); $this->assertTrue(array_search(1, $readableModels) !== false); $this->assertTrue(array_search(2, $readableModels) !== false); $this->assertTrue(array_search(3, $readableModels) !== false); $this->assertTrue(array_search(4, $readableModels) !== false); // Try to delete a resource of a locked model $property = new core_kernel_classes_Property(RDFS_LABEL); $domain = new core_kernel_classes_Property(RDFS_DOMAIN, __METHOD__); $this->assertFalse($property->removePropertyValues($domain, array('pattern' => RDFS_LABEL))); // Try to remove a property value which is lg dependent of a locked model $clazz = new core_kernel_classes_Class('http://www.tao.lu/middleware/Rules.rdf#And'); $this->assertFalse($clazz->removePropertyValueByLg($property, 'EN')); }
public function guard(array $metadataValues) { $guard = false; // Search for a metadataValue with path: // http://www.imsglobal.org/xsd/imsmd_v1p2#lom // http://www.imsglobal.org/xsd/imsmd_v1p2#general // http://www.imsglobal.org/xsd/imsmd_v1p2#identifier foreach ($metadataValues as $metadataValue) { $path = $metadataValue->getPath(); $expectedPath = array('http://www.imsglobal.org/xsd/imsmd_v1p2#lom', 'http://www.imsglobal.org/xsd/imsmd_v1p2#general', 'http://www.imsglobal.org/xsd/imsmd_v1p2#identifier'); if ($path === $expectedPath) { // Check for such a value in database... $prop = new \core_kernel_classes_Property('http://www.imsglobal.org/xsd/imsmd_v1p2#identifier'); $class = new \core_kernel_classes_Class(TAO_ITEM_CLASS); $instances = $class->searchInstances(array($prop->getUri() => $metadataValue->getValue()), array('like' => false, 'recursive' => true)); if (count($instances) > 0) { //var_dump($instances); $guard = reset($instances); break; } } } return $guard; }
public function testSetPropertiesValues() { $this->hardify(); $instance = $this->targetMovieClass->createInstance("Hard Sub Movie (Unit Test)"); $instance2 = $this->targetMovieClass->createInstance("Hard Sub Movie2 (Unit Test)"); $instance->setPropertiesValues(array($this->targetRelatedMoviesProperty->getUri() => $instance2, $this->targetActorsProperty->getUri() => array('I\'m special"! !', 'So special"! !'), RDFS_LABEL => 'I\'m special"! !')); $actors = $instance->getPropertyValues($this->targetActorsProperty); $this->assertEquals(2, count($actors)); $this->assertTrue(in_array('I\'m special"! !', $actors)); $this->assertTrue(in_array('So special"! !', $actors)); $relateds = $instance->getPropertyValues($this->targetRelatedMoviesProperty); $this->assertEquals(1, count($relateds)); $related = current($relateds); $this->assertEquals($instance2->getUri(), $related); $labels = $instance->getPropertyValues(new core_kernel_classes_Property(RDFS_LABEL)); $this->assertEquals(2, count($labels)); $this->assertTrue(in_array('I\'m special"! !', $labels)); $this->assertTrue(in_array("Hard Sub Movie (Unit Test)", $labels)); $instance->delete(); $instance2->delete(); }
/** * Add a process variable in the model * @param string $code * @return boolean */ private function addProcessVariable($code) { $code = preg_replace("/^\\^/", '', $code); if (!$this->processVarExists($code)) { $processVar = $this->processVarClass->createInstance(self::unCamelize($code)); if (!is_null($processVar)) { //set the new instance of process variable as a property of the class process instance: if ($processVar->setType(new core_kernel_classes_Class(RDF_PROPERTY))) { $newProcessInstanceProperty = new core_kernel_classes_Property($processVar->getUri()); $newProcessInstanceProperty->setDomain(new core_kernel_classes_Class(CLASS_TOKEN)); $newProcessInstanceProperty->setRange($this->rdfLiteralClass); } return $processVar->setPropertyValue($this->processVarCodeProp, $code); } } return false; }
/** * Expose or not a property * * @param RdfProperty $property the property to check * @param bool $value true if exposed * * @return void */ public function exposeCategory(RdfProperty $property, $value) { $exposeProperty = new RdfProperty(self::EXPOSE_PROP_URI); if ($value == true) { $property->setPropertyValue($exposeProperty, GENERIS_TRUE); } else { $property->removePropertyValue($exposeProperty, GENERIS_TRUE); } }
/** * Short description of method attachResource * * @access public * @author Jerome Bogaerts, <*****@*****.**> * @param core_kernel_classes_Property $targetProperty * @param core_kernel_classes_Resource $targetResource * @param string $value * @return mixed */ public function attachResource(core_kernel_classes_Property $targetProperty, core_kernel_classes_Resource $targetResource, $value) { // We have to check if the resource identified by value exists in the Ontology. $resource = new core_kernel_classes_Resource($value); if ($resource->exists()) { // Is the range correct ? $targetPropertyRanges = $targetProperty->getPropertyValuesCollection(new core_kernel_classes_Property(RDFS_RANGE)); $rangeCompliance = true; // If $targetPropertyRange->count = 0, we consider that the resouce // may be attached because $rangeCompliance = true. foreach ($targetPropertyRanges->getIterator() as $range) { // Check all classes in target property's range. if ($resource->hasType(new core_kernel_classes_Class($range))) { $rangeCompliance = false; break; } } if (true == $rangeCompliance) { $targetResource->setPropertyValue($targetProperty, $resource->getUri()); } } }
/** * Enable you to map an rdf property to a form element using the Widget * * @access public * @author Bertrand Chevrier, <*****@*****.**> * @param core_kernel_classes_Property $property * @return tao_helpers_form_FormElement */ public static function elementMap(core_kernel_classes_Property $property) { $returnValue = null; //create the element from the right widget $property->feed(); $widgetResource = $property->getWidget(); if (is_null($widgetResource)) { return null; } //authoring widget is not used in standalone mode if ($widgetResource->getUri() === 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#Authoring' && tao_helpers_Context::check('STANDALONE_MODE')) { return null; } // horrible hack to fix file widget if ($widgetResource->getUri() === 'http://www.tao.lu/datatypes/WidgetDefinitions.rdf#AsyncFile') { $widgetResource = new core_kernel_classes_Resource('http://www.tao.lu/datatypes/WidgetDefinitions.rdf#GenerisAsyncFile'); } $element = tao_helpers_form_FormFactory::getElementByWidget(tao_helpers_Uri::encode($property->getUri()), $widgetResource); if (!is_null($element)) { if ($element->getWidget() !== $widgetResource->getUri()) { common_Logger::w('Widget definition differs from implementation: ' . $element->getWidget() . ' != ' . $widgetResource->getUri()); return null; } //use the property label as element description $propDesc = strlen(trim($property->getLabel())) > 0 ? $property->getLabel() : str_replace(LOCAL_NAMESPACE, '', $property->getUri()); $element->setDescription($propDesc); //multi elements use the property range as options if (method_exists($element, 'setOptions')) { $range = $property->getRange(); if ($range !== null) { $options = array(); if ($element instanceof TreeAware) { $sortedOptions = $element->rangeToTree($property->getUri() === RDFS_RANGE ? new core_kernel_classes_Class(RDFS_RESOURCE) : $range); } else { /** @var core_kernel_classes_Resource $rangeInstance */ foreach ($range->getInstances(true) as $rangeInstance) { $level = $rangeInstance->getOnePropertyValue(new core_kernel_classes_Property(TAO_LIST_LEVEL_PROP)); if (is_null($level)) { $options[tao_helpers_Uri::encode($rangeInstance->getUri())] = array(tao_helpers_Uri::encode($rangeInstance->getUri()), $rangeInstance->getLabel()); } else { $level = $level instanceof core_kernel_classes_Resource ? $level->getUri() : (string) $level; $options[$level] = array(tao_helpers_Uri::encode($rangeInstance->getUri()), $rangeInstance->getLabel()); } } ksort($options); $sortedOptions = array(); foreach ($options as $id => $values) { $sortedOptions[$values[0]] = $values[1]; } //set the default value to an empty space if (method_exists($element, 'setEmptyOption')) { $element->setEmptyOption(' '); } } //complete the options listing $element->setOptions($sortedOptions); } } foreach (ValidationRuleRegistry::getRegistry()->getValidators($property) as $validator) { $element->addValidator($validator); } $returnValue = $element; } return $returnValue; }
/** * 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); } }