/**
  * Add discriminator values to class MetaData when a content class is loaded
  * Occur on classcontent.include events.
  *
  * @param Event $event
  */
 public function onInclude(Event $event)
 {
     $shortClassname = AbstractClassContent::getShortClassname($event->getTarget());
     $fullClassname = AbstractClassContent::getFullClassname($event->getTarget());
     foreach (class_parents($fullClassname) as $classname) {
         //            @gvf todo container injection
         $this->serviceContainer->get('doctrine.orm.default_entity_manager')->getClassMetadata($classname)->addDiscriminatorMapClass($shortClassname, $fullClassname);
         if ('BackBee\\CoreDomain\\ClassContent\\AbstractClassContent' === $classname) {
             break;
         }
     }
 }
 /**
  * Get a selection of ClassContent.
  *
  * @param array   $selector
  * @param boolean $multipage
  * @param boolean $recursive
  * @param int     $start
  * @param int     $limit
  * @param boolean $limitToOnline
  * @param boolean $excludedFromSelection
  * @param array   $classnameArr
  * @param int     $delta
  *
  * @return array|Paginator
  */
 public function getSelection($selector, $multipage = false, $recursive = true, $start = 0, $limit = null, $limitToOnline = true, $excludedFromSelection = false, $classnameArr = array(), $delta = 0)
 {
     $query = 'SELECT c.uid FROM bb_content c';
     $join = array();
     $where = array();
     $orderby = array();
     $limit = $limit ? $limit : (array_key_exists('limit', $selector) ? $selector['limit'] : 10);
     $offset = $start + $delta;
     if (true === is_array($classnameArr) && 0 < count($classnameArr)) {
         foreach ($classnameArr as &$classname) {
             // ensure Doctrine already known these classname
             class_exists($classname);
             $classname = AbstractClassContent::getShortClassname($classname);
         }
         unset($classname);
         $where[] = str_replace('\\', '\\\\', 'c.classname IN ("' . implode('","', $classnameArr) . '")');
     }
     if (true === array_key_exists('content_uid', $selector)) {
         $uids = (array) $selector['content_uid'];
         if (false === empty($uids)) {
             $where[] = 'c.uid IN ("' . implode('","', $uids) . '")';
         }
     }
     if (true === array_key_exists('criteria', $selector)) {
         $criteria = (array) $selector['criteria'];
         foreach ($criteria as $field => $crit) {
             $crit = (array) $crit;
             if (1 == count($crit)) {
                 $crit[1] = '=';
             }
             $alias = uniqid('i' . rand());
             $join[] = 'LEFT JOIN bb_indexation ' . $alias . ' ON c.uid  = ' . $alias . '.content_uid';
             $where[] = $alias . '.field = "' . $field . '" AND ' . $alias . '.value ' . $crit[1] . ' "' . $crit[0] . '"';
         }
     }
     if (true === array_key_exists('indexedcriteria', $selector) && true === is_array($selector['indexedcriteria'])) {
         foreach ($selector['indexedcriteria'] as $field => $values) {
             $values = array_filter((array) $values);
             if (0 < count($values)) {
                 $alias = md5($field);
                 $join[] = 'LEFT JOIN bb_indexation ' . $alias . ' ON c.uid  = ' . $alias . '.content_uid';
                 $where[] = $alias . '.field = "' . $field . '" AND ' . $alias . '.value IN ("' . implode('","', $values) . '")';
             }
         }
     }
     if (true === array_key_exists('keywordsselector', $selector)) {
         $keywordInfos = $selector['keywordsselector'];
         if (true === is_array($keywordInfos)) {
             if (true === array_key_exists('selected', $keywordInfos)) {
                 $selectedKeywords = $keywordInfos['selected'];
                 if (true === is_array($selectedKeywords)) {
                     $selectedKeywords = array_filter($selectedKeywords);
                     if (false === empty($selectedKeywords)) {
                         $contentIds = $this->_em->getRepository('BackBee\\CoreDomain\\NestedNode\\KeyWord')->getContentsIdByKeyWords($selectedKeywords, false);
                         if (true === is_array($contentIds) && false === empty($contentIds)) {
                             $where[] = 'c.uid IN ("' . implode('","', $contentIds) . '")';
                         } else {
                             return array();
                         }
                     }
                 }
             }
         }
     }
     if (false === array_key_exists('orderby', $selector)) {
         $selector['orderby'] = array('created', 'desc');
     } else {
         $selector['orderby'] = (array) $selector['orderby'];
     }
     $hasPageJoined = false;
     if (array_key_exists('parentnode', $selector) && true === is_array($selector['parentnode'])) {
         $parentnode = array_filter($selector['parentnode']);
         if (false === empty($parentnode)) {
             $nodes = $this->_em->getRepository('BackBee\\CoreDomain\\NestedNode\\Page')->findBy(array('_uid' => $parentnode));
             if (count($nodes) != 0) {
                 $subquery = $this->getEntityManager()->getRepository('BackBee\\CoreDomain\\NestedNode\\Section')->createQueryBuilder('s')->select('s._uid');
                 $qOR = $subquery->expr()->orX();
                 if (true === $recursive) {
                     foreach ($nodes as $node) {
                         $qAND = $subquery->expr()->andX();
                         $qAND->add($subquery->expr()->eq('s._root', $subquery->expr()->literal($node->getSection()->getRoot()->getUid())));
                         $qAND->add($subquery->expr()->between('s._leftnode', $node->getLeftnode(), $node->getRightnode()));
                         $qOR->add($qAND);
                     }
                 } else {
                     foreach ($nodes as $node) {
                         $qOR->add($subquery->expr()->eq('s._parent', $subquery->expr()->literal($node->getSection()->getUid())));
                     }
                 }
                 $subquery->andWhere($qOR);
                 $query = 'SELECT c.uid FROM bb_page p LEFT JOIN bb_content c ON c.node_uid = p.uid';
                 $where[] = 'p.section_uid IN (' . $subquery->getQuery()->getSQL() . ')';
                 if (true === $limitToOnline) {
                     $where[] = 'p.state IN (1, 3)';
                     $where[] = '(p.publishing IS NULL OR p.publishing <= "' . date('Y-m-d H:i:00', time()) . '")';
                     $where[] = '(p.archiving IS NULL OR p.archiving >"' . date('Y-m-d H:i:00', time()) . '")';
                 } else {
                     $where[] = 'p.state < 4';
                 }
                 $hasPageJoined = true;
             }
         }
     }
     if (0 === count($orderby)) {
         if (true === property_exists('BackBee\\CoreDomain\\ClassContent\\AbstractClassContent', '_' . $selector['orderby'][0])) {
             $orderby[] = 'c.' . $selector['orderby'][0] . ' ' . (count($selector['orderby']) > 1 ? $selector['orderby'][1] : 'desc');
         } else {
             $join[] = 'LEFT JOIN indexation isort ON c.uid  = isort.content_uid';
             $where[] = 'isort.field = "' . $selector['orderby'][0] . '"';
             $orderby[] = 'isort.value' . ' ' . (count($selector['orderby']) > 1 ? $selector['orderby'][1] : 'desc');
         }
     }
     if (0 < count($join)) {
         $query .= ' ' . implode(' ', $join);
     }
     if (0 < count($where)) {
         $query .= sprintf(' WHERE %s', implode(' AND ', $where));
     }
     //Optimize multipage query
     if (true === $multipage) {
         $query = str_replace('SELECT c.uid', 'SELECT SQL_CALC_FOUND_ROWS c.uid', $query);
         $query = str_replace('USE INDEX(IDX_SELECT_PAGE)', ' ', $query);
     }
     $uids = $this->getEntityManager()->getConnection()->executeQuery(str_replace('JOIN bb_content c', 'JOIN bb_opt_content_modified c', $query) . ' ORDER BY ' . implode(', ', $orderby) . ' LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(\PDO::FETCH_COLUMN);
     if (count($uids) < $limit) {
         $uids = $this->getEntityManager()->getConnection()->executeQuery($query . ' ORDER BY ' . implode(', ', $orderby) . ' LIMIT ' . $limit . ' OFFSET ' . $offset)->fetchAll(\PDO::FETCH_COLUMN);
     }
     $q = $this->createQueryBuilder('c')->select()->where('c._uid IN (:uids)')->setParameter('uids', $uids);
     if (true === $hasPageJoined && true === property_exists('BackBee\\CoreDomain\\NestedNode\\Page', '_' . $selector['orderby'][0])) {
         $q->join('c._mainnode', 'p')->orderBy('p._' . $selector['orderby'][0], count($selector['orderby']) > 1 ? $selector['orderby'][1] : 'desc');
     } elseif (true === property_exists('BackBee\\CoreDomain\\ClassContent\\AbstractClassContent', '_' . $selector['orderby'][0])) {
         $q->orderBy('c._' . $selector['orderby'][0], count($selector['orderby']) > 1 ? $selector['orderby'][1] : 'desc');
     } else {
         $q->leftJoin('c._indexation', 'isort')->andWhere('isort._field = :sort')->setParameter('sort', $selector['orderby'][0])->orderBy('isort._value', count($selector['orderby']) > 1 ? $selector['orderby'][1] : 'desc');
     }
     if (true === $multipage) {
         $numResults = $this->getEntityManager()->getConnection()->executeQuery('SELECT FOUND_ROWS()')->fetch(\PDO::FETCH_COLUMN);
         $result = $q->getQuery()->getResult();
         $q->setFirstResult($offset)->setMaxResults($limit);
         $paginator = new SettablePaginator($q);
         $paginator->setCount($numResults)->setResult($result);
         return $paginator;
     }
     $result = $q->getQuery()->getResult();
     return $result;
 }
 /**
  * Replaces content in optimized tables.
  *
  * @param  AbstractClassContent $content
  *
  * @return IndexationRepository
  */
 public function replaceOptContentTable(AbstractClassContent $content)
 {
     if (null === $content->getMainNode()) {
         return $this;
     }
     $command = 'REPLACE';
     if (!$this->replaceSupported) {
         // REPLACE command not supported, remove first then insert
         $this->removeOptContentTable($content);
         $command = 'INSERT';
     }
     $meta = $this->_em->getClassMetadata('BackBee\\ClassContent\\Indexes\\OptContentByModified');
     $query = $command . ' INTO ' . $meta->getTableName() . ' (' . $meta->getColumnName('_uid') . ', ' . $meta->getColumnName('_label') . ', ' . $meta->getColumnName('_classname') . ', ' . $meta->getColumnName('_node_uid') . ', ' . $meta->getColumnName('_modified') . ', ' . $meta->getColumnName('_created') . ')' . ' VALUES (:uid, :label, :classname, :node_uid, :modified, :created)';
     $params = array('uid' => $content->getUid(), 'label' => $content->getLabel(), 'classname' => AbstractClassContent::getShortClassname($content), 'node_uid' => $content->getMainNode()->getUid(), 'modified' => date('Y-m-d H:i:s', $content->getModified()->getTimestamp()), 'created' => date('Y-m-d H:i:s', $content->getCreated()->getTimestamp()));
     $types = array(\Doctrine\DBAL\Connection::PARAM_STR_ARRAY, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY);
     return $this->_executeQuery($query, $params, $types);
 }