/**
  * 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;
 }
 /**
  * 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;
 }