Esempio n. 1
0
 /**
  * Updates every indexed value assocatied to the contents flushed.
  *
  * @param \BackBee\Event\Event $event
  */
 public static function onFlushContent(Event $event)
 {
     if (null === self::_getEntityManager($event)) {
         return;
     }
     $contents_inserted = ScheduledEntities::getScheduledAClassContentInsertions(self::$_em, true, true);
     $contents_updated = ScheduledEntities::getScheduledAClassContentUpdates(self::$_em, true, true);
     $contents_deleted = ScheduledEntities::getSchedulesAClassContentDeletions(self::$_em, true);
     // Updates content-content indexes
     self::_updateIdxContentContents(array_merge($contents_inserted, $contents_updated), $contents_deleted);
     // Updates site-content indexes
     self::_updateIdxSiteContents($contents_inserted, $contents_deleted);
     $content = $event->getTarget();
     if (!$content instanceof AbstractClassContent) {
         return;
     }
     $dispatcher = $event->getDispatcher();
     $application = $dispatcher->getApplication();
     if (null === $application) {
         return;
     }
     $em = $application->getEntityManager();
     $uow = $em->getUnitOfWork();
     if ($uow->isScheduledForInsert($content) || $uow->isScheduledForUpdate($content)) {
         self::$_em->getRepository('BackBee\\ClassContent\\Indexes\\OptContentByModified')->replaceOptContentTable($content);
         if (is_array($content->getProperty()) && array_key_exists('indexation', $content->getProperty())) {
             foreach ($content->getProperty('indexation') as $indexedElement) {
                 $indexedElement = (array) $indexedElement;
                 $callback = array_key_exists(1, $indexedElement) ? $indexedElement[1] : null;
                 if ('@' === substr($indexedElement[0], 0, 1)) {
                     // parameter indexation
                     $param = substr($indexedElement[0], 1);
                     $value = $content->getParam($param);
                     $owner = $content;
                 } else {
                     $elements = explode('->', $indexedElement[0]);
                     $owner = null;
                     $element = null;
                     $value = $content;
                     foreach ($elements as $element) {
                         $owner = $value;
                         if (!$value instanceof AbstractClassContent) {
                             continue;
                         }
                         if (null !== $value) {
                             $value = $value->getData($element);
                             if ($value instanceof AbstractClassContent && !$em->contains($value)) {
                                 $value = $em->find(get_class($value), $value->getUid());
                             }
                         }
                     }
                 }
                 if (null !== $callback) {
                     $callback = (array) $callback;
                     foreach ($callback as $func) {
                         $value = call_user_func($func, $value);
                     }
                 }
                 if (null !== $owner && null !== $value) {
                     $index = $em->getRepository('BackBee\\ClassContent\\Indexation')->find(['_content' => $content, '_field' => $indexedElement[0]]);
                     if (null === $index) {
                         $index = new Indexation($content, $indexedElement[0], $owner, $value, serialize($callback));
                         $em->persist($index);
                     }
                     $index->setValue($value);
                     $em->getUnitOfWork()->computeChangeSet($em->getClassMetadata('BackBee\\ClassContent\\Indexation'), $index);
                 }
             }
             $alreadyIndex = $em->getRepository('BackBee\\ClassContent\\Indexation')->findBy(['_owner' => $content]);
             foreach ($alreadyIndex as $index) {
                 $field = $index->getField();
                 $callback = unserialize($index->getCallback());
                 if (null !== $field) {
                     $tmp = explode('->', $field);
                     $field = array_pop($tmp);
                     try {
                         $value = $content->{$field};
                         if (null !== $callback) {
                             $callback = (array) $callback;
                             foreach ($callback as $func) {
                                 $value = call_user_func($func, $value);
                             }
                         }
                         if ($value != $index->getValue() && null !== $value) {
                             $index->setValue($value);
                             $em->getUnitOfWork()->computeChangeSet($em->getClassMetadata('BackBee\\ClassContent\\Indexation'), $index);
                         }
                     } catch (\Exception $e) {
                         // Nothing to do
                     }
                 }
             }
         }
     } elseif ($uow->isScheduledForDelete($content)) {
         self::$_em->getRepository('BackBee\\ClassContent\\Indexes\\OptContentByModified')->removeOptContentTable($content);
         foreach ($content->getIndexation() as $index) {
             $em->remove($index);
             $em->getUnitOfWork()->computeChangeSet($em->getClassMetadata('BackBee\\ClassContent\\Indexation'), $index);
         }
     }
 }
 /**
  * Updates every indexed value assocatied to the contents flushed.
  *
  * @param Event $event
  */
 public static function onFlushContent(Event $event)
 {
     if (null === self::getEntityManager($event)) {
         return;
     }
     $contents_inserted = ScheduledEntities::getScheduledAClassContentInsertions(self::$em, true, true);
     $contents_updated = ScheduledEntities::getScheduledAClassContentUpdates(self::$em, true, true);
     $contents_deleted = ScheduledEntities::getSchedulesAClassContentDeletions(self::$em, true);
     // Updates content-content indexes
     self::updateIdxContentContents(array_merge($contents_inserted, $contents_updated), $contents_deleted);
     // Updates site-content indexes
     self::updateIdxSiteContents($contents_inserted, $contents_deleted);
     $content = $event->getTarget();
     if (!$content instanceof AbstractClassContent) {
         return;
     }
     $dispatcher = $event->getDispatcher();
     $application = $dispatcher->getApplication();
     if (null === $application) {
         return;
     }
     $uow = self::$em->getUnitOfWork();
     if ($uow->isScheduledForInsert($content) || $uow->isScheduledForUpdate($content)) {
         self::$em->getRepository('BackBee\\ClassContent\\Indexes\\OptContentByModified')->replaceOptContentTable($content);
         self::indexContent($content);
         self::updateSuperContentIndexes($content);
     } elseif ($uow->isScheduledForDelete($content)) {
         self::$em->getRepository('BackBee\\ClassContent\\Indexes\\OptContentByModified')->removeOptContentTable($content);
         foreach ($content->getIndexation() as $index) {
             self::$em->remove($index);
             self::$em->getUnitOfWork()->computeChangeSet(self::$em->getClassMetadata('BackBee\\ClassContent\\Indexation'), $index);
         }
     }
 }