/**
  * @Flow\After("method(TYPO3\TYPO3CR\Domain\Repository\NodeDataRepository->remove())")
  * @return void
  */
 public function indexNodeBeforeRemovingNodeData(JoinPointInterface $joinPoint)
 {
     $object = $joinPoint->getMethodArgument('object');
     if ($object instanceof NodeData) {
         $nodeData = $object;
     }
     if ($object instanceof Node) {
         $nodeData = $object->getNodeData();
     }
     if ($nodeData instanceof NodeData) {
         $this->indexService->removeIndex($nodeData);
     }
 }
 /**
  * @param integer $itemsPerPage items per site
  * @param NodeInterface $node The node of the list element. Optional, will be resolved from the TypoScript context by default.
  * @param integer $nodescount the count of indexed node selection. Optional, will be resolved from the TypoScript context by default.
  * @param boolean $absoluteUrl
  * @return string The rendered property with a wrapping tag. In the user workspace this adds some required attributes for the RTE to work
  * @throws ViewHelperException
  */
 public function render($itemsPerPage, NodeInterface $node = null, $nodescount = null, $absoluteUrl = false)
 {
     $view = $this->viewHelperVariableContainer->getView();
     if (!$view instanceof TypoScriptAwareViewInterface) {
         throw new ViewHelperException('This ViewHelper can only be used in a TypoScript content element. You have to specify the "node" argument if it cannot be resolved from the TypoScript context.', 1385737102);
     }
     $typoScriptObject = $view->getTypoScriptObject();
     $currentContext = $typoScriptObject->getTsRuntime()->getCurrentContext();
     if ($node === null) {
         $node = $currentContext['node'];
     }
     if ($nodescount === null) {
         $nodescount = $currentContext['nodescount'];
     }
     $configuration = $this->indexService->prepareNodeSelectionFromNode($node);
     $this->itemsPerPage = $itemsPerPage;
     $this->total = $nodescount;
     $this->limit = $configuration['limit'];
     $this->limitParamName = $configuration['limit_param_name'];
     $this->offsetParamName = $configuration['offset_param_name'];
     $this->numberOfPages = ceil($this->total / (int) $this->itemsPerPage);
     $this->absoluteUrl = $absoluteUrl;
     if ($configuration['offset'] > 0) {
         $this->currentPage = $configuration['offset'] / $this->itemsPerPage + 1;
     }
     // set default requested arguments
     if (!$this->controllerContext->getRequest()->hasArgument($this->offsetParamName)) {
         $_GET[$this->offsetParamName] = '0';
     }
     if (!$this->controllerContext->getRequest()->hasArgument($this->limitParamName)) {
         $_GET[$this->limitParamName] = $this->itemsPerPage;
     }
     // override nodes array from earlier eol query
     if ($this->templateVariableContainer->exists('nodes')) {
         $this->templateVariableContainer->remove('nodes');
         $this->templateVariableContainer->add('nodes', $this->nodeHelper->get($node));
     }
     // build pagination array
     $this->templateVariableContainer->add('pagination', $this->buildPagination());
     return $this->renderChildren();
 }
 /**
  * Finds nodes index by its filter
  *
  *
  * @param array $selection
  * @param boolean $countonly
  * @return \TYPO3\Flow\Persistence\QueryResultInterface The query result
  * @see \TYPO3\Flow\Persistence\QueryInterface::execute()
  */
 public function getFilteredNodes($selection, $countonly = false)
 {
     $query = $this->createQuery();
     $nodeMatcherConditions = array();
     // set workspace query
     if (isset($selection['workspace'])) {
         $or = array();
         if ($selection['workspace']->getName() != 'live') {
             $or[] = $query->equals('nodeData.workspace', $selection['workspace']->getName());
         }
         $or[] = $query->equals('nodeData.workspace', 'live');
         $nodeMatcherConditions[] = $query->logicalOr($or);
     }
     // set nodetype query
     if (isset($selection['nodetype'])) {
         if (isset($selection['nodetypeisabstract']) && $selection['nodetypeisabstract']) {
             // find nodes that inherit from abstract nodetype
             $nodeMatcherConditions[] = $query->equals('nodeTypeAbstract', $selection['nodetype']);
         } else {
             $nodeMatcherConditions[] = $query->equals('nodeData.nodeType', $selection['nodetype']);
         }
     }
     // set entrypoint query
     if (isset($selection['entryNodes']) && is_array($selection['entryNodes'])) {
         $entrypointConditions = array();
         foreach ($selection['entryNodes'] as $key => $val) {
             if (isset($val['path'])) {
                 if (isset($val['recursive']) == FALSE) {
                     $val['recursive'] = FALSE;
                 }
                 if (isset($val['recursive']) && $val['recursive'] == TRUE) {
                     $and = array();
                     $and[] = $query->like('nodeData.path', $val['path'] . '%');
                     if ($val['childNodePath']) {
                         $and[] = $query->like('nodeData.path', '%/' . $val['childNodePath'] . '/node-%');
                     }
                     $entrypointConditions[] = $query->logicalAnd($and);
                 } else {
                     if (isset($val['childNodes'])) {
                         foreach ($val['childNodes'] as $path => $data) {
                             $and = array();
                             $and[] = $query->like('nodeData.parentPath', $val['path'] . '/' . $path);
                             if ($val['childNodePath']) {
                                 $and[] = $query->like('nodeData.path', '%/' . $val['childNodePath'] . '/node-%');
                             }
                             $entrypointConditions[] = $query->logicalAnd($and);
                         }
                     } else {
                         // get only nodes on self path
                         $and = array();
                         $and[] = $query->like('nodeData.path', $val['path'] . '%');
                         $entrypointConditions[] = $query->logicalAnd($and);
                     }
                 }
             } else {
                 $entrypointConditions[] = $query->like('nodeData.parentpath', '');
             }
         }
         if (count($entrypointConditions) > 0) {
             $nodeMatcherConditions[] = $query->logicalOr($entrypointConditions);
         }
     }
     // set filter query
     if (isset($selection['nodetype']) && isset($selection['filter']) && is_array($selection['filter'])) {
         foreach ($selection['filter'] as $property => $filter) {
             if (isset($filter['operand'])) {
                 $filterCondition = array();
                 $oProperty = 'valueRaw';
                 $oOperator = 'like';
                 if (isset($filter['type']) == false) {
                     $filter['type'] = 'string';
                 }
                 if (isset($filter['operator']) == false) {
                     $filter['operator'] = 'like';
                 }
                 $oIndex = $this->indexService->getOrderingIndex($this->nodeTypeManager->getNodeType($selection['nodetype']), $property);
                 if ($oIndex >= 0) {
                     // filter by indexed nodes
                     $filterCondition[] = $query->equals('indexData.property', $property);
                     switch ($filter['type']) {
                         case 'datetime':
                             $oProperty = 'indexData.valueDateTime';
                             $filter['operand'] = new \DateTime(strtotime($filter['operand']));
                             break;
                         case 'integer':
                             $oProperty = 'indexData.valueInteger';
                             break;
                         default:
                             $oProperty = 'indexData.valueRaw';
                             break;
                     }
                 } else {
                     //try to sort by default nodedata properties
                     switch ($filter['type']) {
                         case 'datetime':
                             $filter['operand'] = $this->calculateDateFromString($filter['operand']);
                             break;
                     }
                     switch (strtolower($property)) {
                         case 'parentpath':
                             $oProperty = 'nodeData.parentPath';
                             break;
                         case 'index':
                             $oProperty = 'nodeData.index';
                             break;
                         case 'sortingindex':
                             $oProperty = 'nodeData.index';
                             break;
                         case 'lastmodificationdatetime':
                             $oProperty = 'nodeData.lastModificationDateTime';
                             break;
                         case 'lastpublicationdatetime':
                             $oProperty = 'nodeData.lastPublicationDateTime';
                             break;
                         case 'creationdatetime':
                             $oProperty = 'nodeData.creationDateTime';
                             break;
                         case 'identifier':
                             $oProperty = 'nodeData.identifier';
                             break;
                     }
                 }
                 // apply filter condition
                 switch ($filter['operator']) {
                     case 'like':
                         $filterCondition[] = $query->like($oProperty, $filter['operand'] . "%");
                         break;
                     case 'equals':
                         $filterCondition[] = $query->equals($oProperty, $filter['operand']);
                         break;
                     case 'greaterThanOrEqual':
                         $filterCondition[] = $query->greaterThanOrEqual($oProperty, $filter['operand']);
                         break;
                     case 'greaterThan':
                         $filterCondition[] = $query->greaterThan($oProperty, $filter['operand']);
                         break;
                     case 'lessThan':
                         $filterCondition[] = $query->lessThan($oProperty, $filter['operand']);
                         break;
                     case 'lessThanOrEqual':
                         $filterCondition[] = $query->lessThanOrEqual($oProperty, $filter['operand']);
                         break;
                     case 'in':
                         $filterCondition[] = $query->in($oProperty, $filter['operand']);
                         break;
                     case 'notequal':
                         $filterCondition[] = $query->logicalNot($query->matching($oProperty, $filter['operand']));
                         break;
                 }
                 if (count($filterCondition) > 0) {
                     $nodeMatcherConditions[] = $query->logicalAnd($filterCondition);
                 }
             }
         }
         // set orderings
         $orderingArray = array('orderWorkspaces' => 'DESC');
         if (isset($selection['nodetype']) && isset($selection['sort'])) {
             foreach ($selection['sort'] as $propertyName => $ordering) {
                 $oIndex = $this->indexService->getOrderingIndex($this->nodeTypeManager->getNodeType($selection['nodetype']), $ordering['value']);
                 if (isset($ordering['direction']) && $ordering['direction'] == QueryInterface::ORDER_DESCENDING) {
                     $oDirection = QueryInterface::ORDER_DESCENDING;
                 } else {
                     $oDirection = QueryInterface::ORDER_ASCENDING;
                 }
                 if ($oIndex >= 0) {
                     if (isset($ordering['type']) == false) {
                         $ordering['type'] = 'string';
                     }
                     switch ($ordering['type']) {
                         case 'datetime':
                             $oProperty = 'valueDateTime';
                             break;
                         case 'integer':
                             $oProperty = 'valueInteger';
                             break;
                         default:
                             $oProperty = 'valueRaw';
                             break;
                     }
                     $orderingArray['orderIndex' . $oIndex . '.' . $oProperty] = $oDirection;
                 } else {
                     // try to sort by default nodedata properties
                     switch (strtolower($ordering['value'])) {
                         case 'parentpath':
                             $orderingArray['nodeData.parentPath'] = $oDirection;
                             break;
                         case 'index':
                             $orderingArray['nodeData.index'] = $oDirection;
                             break;
                         case 'sortingindex':
                             $orderingArray['nodeData.index'] = $oDirection;
                             break;
                         case 'lastmodificationdatetime':
                             $orderingArray['nodeData.lastModificationDateTime'] = $oDirection;
                             break;
                         case 'lastpublicationdatetime':
                             $orderingArray['nodeData.lastPublicationDateTime'] = $oDirection;
                             break;
                         case 'creationdatetime':
                             $orderingArray['nodeData.creationDateTime'] = $oDirection;
                             break;
                         default:
                             break;
                     }
                 }
             }
         }
         if (count($orderingArray)) {
             $query->setOrderings($orderingArray);
         }
     }
     // set node matcher
     if (count($nodeMatcherConditions) > 0) {
         $query->matching($query->logicalAnd($nodeMatcherConditions));
     }
     // set limit
     if (isset($selection['limit'])) {
         $query->setLimit($selection['limit']);
     }
     if ($countonly) {
         // count only query
         return $query->execute()->count();
     } else {
         // set offset
         if (isset($selection['offset']) && $selection['offset'] != false) {
             $query->setOffset($selection['offset']);
         }
         $data = array();
         foreach ($query->execute()->toArray() as $index) {
             if (isset($data[$index->getNodeData()->getIdentifier()]) && $index->getNodeData()->getWorkspace()->getName() == 'live') {
                 // skip double nodes
             } else {
                 $data[$index->getNodeData()->getIdentifier()] = $index->getNodeData();
             }
         }
         return $data;
     }
 }
 /**
  * Count indexed nodes
  *
  * @param Node $node
  * @return integer
  */
 public function count(Node $node)
 {
     $indexService = new IndexService();
     return $indexService->countNodes($node);
 }