예제 #1
0
 /**
  *
  * Determine the title of the entity to be created, following this logic:
  *
  * 1. is there a getTitle method with a value set
  * 2. is there a property containing "title" in rdf definition
  * 3. take the first property defined in rdf and take first 50 characters
  * 4. give up
  *
  * @param EntityInterface $entity
  * @throws \RuntimeException
  * @return mixed|string
  */
 private function determineEntityTitle(EntityInterface $entity)
 {
     $object = $entity->getObject();
     //is there a getTitle method?
     if (method_exists($object, 'getTitle') && $object->getTitle()) {
         return $object->getTitle();
     } else {
         //try to get a property containing title in the rdf description
         foreach ($entity->getChildDefinitions() as $node) {
             if (!$node instanceof \Midgard\CreatePHP\Entity\PropertyInterface || strpos($node->getProperty(), "title") === false) {
                 continue;
             }
             return $entity->getMapper()->getPropertyValue($object, $node);
         }
         //try to get the first property to create a guessed title of max 50 characters
         foreach ($entity->getChildDefinitions() as $node) {
             if (!$node instanceof \Midgard\CreatePHP\Entity\PropertyInterface) {
                 continue;
             }
             return substr($entity->getMapper()->getPropertyValue($object, $node), 0, 49);
         }
         //give up
         throw new RuntimeException('No title could be found four your new node.');
     }
 }
예제 #2
0
 /**
  * Never call this method directly, but use createWithParent on your CollectionDefinition
  * to get a collection tied to concrete data.
  *
  * @private
  *
  * @param EntityInterface $parent
  */
 public function loadFromParent(EntityInterface $parent)
 {
     $this->_children = array();
     $object = $parent->getObject();
     $parentMapper = $parent->getMapper();
     $children = $parentMapper->getChildren($object, $this);
     // create entities for children
     foreach ($children as $child) {
         if (count($this->_typenames) === 1) {
             $type = $this->_typeFactory->getTypeByRdf(current($this->_typenames));
         } else {
             $type = $this->_typeFactory->getTypeByObject($child);
         }
         foreach ($type->getVocabularies() as $prefix => $uri) {
             $this->setAttribute('xmlns:' . $prefix, $uri);
         }
         $this->_children[] = $type->createWithObject($child);
     }
     if ($this->_parent->isEditable($object) && sizeof($this->_children) == 0 && count($this->_typenames) == 1) {
         // create an empty element to allow adding new elements to an empty editable collection
         $type = $this->_typeFactory->getTypeByRdf(reset($this->_typenames));
         $mapper = $type->getMapper();
         $object = $mapper->prepareObject($type, $object);
         $entity = $type->createWithObject($object);
         if ($entity instanceof NodeInterface) {
             /** @var $entity NodeInterface */
             $entity->setAttribute('style', 'display:none');
         }
         $this->_children[] = $entity;
     }
 }