public function build(ObjectModel\DataObject $do, array $display = null, $identifier = false) { if ($identifier === true) { $identifier = new FieldElement(ObjectUri::IDENTIFIER); $identifier->setTitle("Identifiant unique")->setConstraint(Property::CONSTRAINT_MANDATORY, true)->setConstraint(Property::CONSTRAINT_MAXLENGTH, 10); $this->addElement($identifier); } if (is_null($display)) { $display = array_keys($do->getProperties()); } foreach ($display as $element) { $property = $do->getRecursiveProperty($element); if ($property instanceof AbstractProperty) { /* convert property to form element */ $this->addElementFromProperty($property, $element, (count($this->_elements) + 1) * 100); } } }
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); }