public function render() { if (($value = $this->_obj->getValue()) !== false) { if (($value instanceof ObjectModel\BaseObject || $value instanceof ObjectModel\DataObject) && $value->getUri()) { $value = $this->_obj->getParameter('altkey') ? $value->getProperty($this->_obj->getParameter('altkey'))->getValue() : $value->getUri()->getIdentifier(); } else { if ($value instanceof ObjectModel\ObjectUri) { $value = $value->getIdentifier(); } else { $value = null; } } } $name = $this->_obj->getId(); // set correct name for field name value depending on 'mode' parameter value if ($this->getParameter('mode') == View\FormComponent::SEARCH_MODE) { $name = ViewUri::getUriAdapter()->getIdentifier('search') . '[' . $name . ']'; } View::addCoreLib(array('core.js', 'locale.js', 'view.js', 'view:table.js', 'view:action:autocomplete.js')); $acfield = new View\FormComponent\Element\FieldElement('_' . $this->_nametoDomId($name)); $acfield->setValue($value); $action = new AutocompleteAction($this->_obj->getCollection()); $objsearchprops = $this->_obj->getParameter('search') ? $this->_obj->getParameter('search') : $this->_obj->getParameter('display'); $action->setParameter('searchprops', explode(',', $this->getParameter('searchprops') ? $this->getParameter('searchprops') : $objsearchprops)); $action->setParameter('searchmode', $this->getParameter('searchmode')); $action->setParameter('display', explode(',', $this->_obj->getParameter('display'))); $action->setParameter('sdisplay', explode(',', $this->_obj->getParameter('sdisplay') ? $this->_obj->getParameter('sdisplay') : $this->_obj->getParameter('display'))); $action->setParameter('event', 'keyup'); // if a list of properties to be returned exists, pass it to the action if ($this->getParameter('retprops')) { $action->setParameter('member_reduce_params', array('props' => explode(',', $this->getParameter('retprops')))); } $action->setContextData('onclick', 't41.view.element.autocomplete.close'); $action->setContextData('target', $this->_nametoDomId($name)); $action->bind($acfield); $deco = View\Decorator::factory($acfield); $html = $deco->render(); $deco = View\Decorator::factory($action); $deco->render(); $html .= sprintf('<input type="hidden" name="%s" id="%s" value="%s"/>', $name, $this->_nametoDomId($name), $value); return $html . "\n"; }
public function addElementFromProperty(AbstractProperty $property, $fname, $position = null) { $class = get_class($property); $class = substr($class, strrpos($class, '\\') + 1); switch ($class) { case 'EnumProperty': if ($property->getParameter('constraints.multiple') !== false) { $element = new Element\MultipleElement(); } else { $element = new Element\EnumElement(); } $element->setEnumValues($property->getValues()); break; case 'DateProperty': $element = new Element\DateElement(); break; case 'TimeProperty': $element = new Element\TimeElement(); break; case 'CurrencyProperty': $element = new Element\CurrencyElement(); break; case 'StringProperty': if ($property->getParameter('multilines')) { $element = new Element\TextElement(); } else { $element = new Element\FieldElement(); } break; case 'ObjectProperty': // @todo join this code and the one in CollectionProperty::getValue if ($property->getParameter('instanceof') == 't41\\ObjectModel\\MediaObject') { $element = new Element\MediaElement(); } else { $element = new Element\ListElement(); $element->setParameter('display', $property->getParameter('display')); $collection = new ObjectModel\Collection($property->getParameter('instanceof')); /* inject the condition that allows to find collection members */ if ($property->getParameter('keyprop')) { $collection->having($property->getParameter('keyprop'))->equals($property->getParent()); } if ($property->getParameter('depends')) { $element->setParameter('dependency', $property->getParameter('depends')); } if ($property->getParameter('morekeyprop')) { foreach ($property->getParameter('morekeyprop') as $value) { if (strstr($value, ' ') !== false) { // if value contains spaces, it's a pattern $parts = explode(' ', $value); if (count($parts) == 3) { if (strstr($parts[2], ',') !== false) { $parts[2] = explode(',', $parts[2]); } $collection->having($parts[0])->{$parts}[1]($parts[2]); } else { if (strstr($parts[1], ',') !== false) { $parts[1] = explode(',', $parts[1]); } $collection->having($parts[0])->equals($parts[1]); } } else { // default case, we expect the member to hold a property // with the same name and value as the current object $collection->having($value)->equals($property->getParent()->getProperty($value)->getValue()); } } } if ($property->getParameter('sorting')) { $element->setParameter('sorting', $property->getParameter('sorting')); } if ($property->getParameter('search')) { $element->setParameter('search', $property->getParameter('search')); } if ($property->getParameter('sdisplay')) { $element->setParameter('sdisplay', $property->getParameter('sdisplay')); } $element->setCollection($collection); } break; case 'CollectionProperty': $element = new Element\GridElement(); $element->setCollection($property->getValue()); break; case 'MediaProperty': $element = new Element\MediaElement(); break; default: $element = new Element\FieldElement(); break; } $element->setId(str_replace('.', '-', $fname)); $element->setTitle($property->getLabel()); $element->setDefaultValue($property->getDefaultValue()); $element->setHelp($property->getParameter('help')); if ($property instanceof MetaProperty) { $value = $property->getDisplayValue(); } else { $value = $property->getValue(); if ($value instanceof ObjectModel\ObjectUri) { $value = $value->__toString(); } else { if ($value instanceof ObjectModel\BaseObject) { $value = $value->getUri() ? $value->getUri()->__toString() : null; } } } $element->setValue($value); $constraints = $property->getParameter('constraints'); foreach (self::$constraintsList as $key) { if (isset($constraints[$key])) { $element->setConstraint($key, $constraints[$key] != '0' && empty($constraints[$key]) ? true : $constraints[$key]); } } // property uses a special format for which we should have a decorator if (isset($constraints['format'])) { $element->setDecorator($constraints['format']); } return $this->addElement($element, $position); }