Пример #1
0
 /**
  * Takes an entity and works out whether it has a relation to CMF's URL Model. If so,
  * it updates the properties of the associated URL object.
  * 
  * @param object $entity
  * @param \Doctrine\ORM\EntityManager $em
  * @param \Doctrine\ORM\UnitOfWork|null $uow
  * @return void
  */
 protected function process(&$entity, &$em, &$uow)
 {
     $entity_class = get_class($entity);
     $entity_namespace = trim(\CMF::slug(str_replace('\\', '/', \Inflector::get_namespace($entity_class))), '/');
     $metadata = $em->getClassMetadata($entity_class);
     // Ignore URL entities themselves
     if ($metadata->name == 'CMF\\Model\\URL') {
         return;
     }
     $url_associations = $metadata->getAssociationsByTargetClass('CMF\\Model\\URL');
     if (!empty($url_associations)) {
         // A bit hacky, but if this is a root tree item don't bother
         if (property_exists($entity, 'is_root') && $entity->is_root === true) {
             return;
         }
         $url_field = null;
         foreach ($url_associations as $key => $association) {
             if ($association['type'] == ClassMetadataInfo::ONE_TO_ONE && $association['orphanRemoval']) {
                 $url_field = $key;
                 break;
             }
         }
         if ($url_field == null) {
             return;
         }
         $settings = $entity->settings();
         $url_settings = isset($settings[$url_field]) ? $settings[$url_field] : array();
         $url_item = $entity->get($url_field);
         if ($new_url = is_null($url_item)) {
             $url_item = new \CMF\Model\URL();
         }
         // Don't run if this is an alias...
         $alias = $url_item->alias;
         if (!is_null($alias) && !empty($alias)) {
             return;
         }
         // Don't run if this is an external link...
         if ($url_item->isExternal()) {
             return;
         }
         $prefix = $this->getPrefix($entity);
         $slug = '';
         if (isset($url_settings['keep_updated']) && !$url_settings['keep_updated']) {
             $slug = \CMF::slug($url_item->slug);
         } else {
             $slug = $entity->urlSlug();
         }
         $url = $prefix . $slug;
         if ($url != '/') {
             $url = rtrim($url, '/');
         }
         $current_url = $url_item->url;
         $entity_id = $entity->id;
         $url_id = $url_item->get('id');
         // Check for duplicates, only if this is an already existing item
         if (!empty($entity_id) && !is_null($entity_id)) {
             // Set data from the entity if the prefix is null
             if (is_null($prefix)) {
                 $prefix = $this->getPrefix($entity);
                 $url = $prefix . $slug;
             }
             // Set data from the entity if the slug is null
             if (is_null($slug)) {
                 $slug = $entity->urlSlug();
                 $url = $prefix . $slug;
             }
             // Set it to the item's ID if empty
             if (is_null($slug)) {
                 $slug = $entity_id . "";
                 $url = $prefix . $slug;
             }
             $slug_orig = $slug;
             $unique = $this->checkUnique($url, $entity_id, $url_id);
             $counter = 2;
             while (!$unique) {
                 $slug = $slug_orig . '-' . $counter;
                 $url = $prefix . $slug;
                 $unique = $this->checkUnique($url, $entity_id, $url_id);
                 $counter++;
             }
             // Add it to the list of saved URLs
             $this->savedUrls[$url] = $entity_id;
         }
         $url_item->set('item_id', $entity->get('id'));
         $url_item->set('prefix', $prefix);
         $url_item->set('slug', $slug);
         $url_item->set('url', $url);
         $url_item->set('type', $metadata->name);
         $entity->set($url_field, $url_item);
         $em->persist($url_item);
         // Skip this if the url hasn't changed
         if (!$new_url && $current_url == $url) {
             return;
         }
         $url_metadata = $em->getClassMetadata('CMF\\Model\\URL');
         $url_changeset = $uow->getEntityChangeSet($url_item);
         if (!empty($url_changeset)) {
             $uow->recomputeSingleEntityChangeSet($url_metadata, $url_item);
         } else {
             $uow->computeChangeSet($url_metadata, $url_item);
         }
         $uow->recomputeSingleEntityChangeSet($metadata, $entity);
         $associations = $metadata->getAssociationMappings();
         foreach ($associations as $association_name => $association) {
             // Only do it if it's the inverse side, to prevent the dreaded infinite recursion
             if (!$association['isOwningSide']) {
                 $items = $entity->{$association_name};
                 if (!empty($items)) {
                     foreach ($items as $item) {
                         $this->process($item, $em, $uow);
                     }
                 }
             }
         }
     }
 }