Beispiel #1
0
 /**
  * Adjusts the order of the document and its siblings.
  *
  * @param ReorderEvent $event
  */
 public function handleReorder(ReorderEvent $event)
 {
     $document = $event->getDocument();
     if (!$document instanceof OrderBehavior) {
         return;
     }
     $parentDocument = $this->documentInspector->getParent($document);
     if (null === $parentDocument) {
         return;
     }
     $count = 1;
     foreach ($this->documentInspector->getChildren($parentDocument) as $childDocument) {
         if (!$childDocument instanceof OrderBehavior) {
             continue;
         }
         $order = $count * 10;
         $childDocument->setSuluOrder($order);
         // TODO move to NodeHelper once integrated in sulu/sulu?
         $childNode = $this->documentInspector->getNode($childDocument);
         $childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $order);
         ++$count;
     }
 }
Beispiel #2
0
 /**
  * TODO: Move this logic to the DocumentManager
  * {@inheritDoc}
  */
 public function orderAt($uuid, $position, $userId, $webspaceKey, $locale)
 {
     $document = $this->documentManager->find($uuid, $locale);
     $parentDocument = $this->inspector->getParent($document);
     $siblingDocuments = $this->inspector->getChildren($parentDocument);
     $siblings = array_values($siblingDocuments->toArray());
     // get indexed array
     $countSiblings = count($siblings);
     $currentPosition = array_search($document, $siblings) + 1;
     if ($countSiblings < $position || $position <= 0) {
         throw new InvalidOrderPositionException(sprintf('Cannot order node "%s" at out-of-range position "%s", must be >= 0 && < %d"', $this->inspector->getPath($document), $position, $countSiblings));
     }
     if ($position === $countSiblings) {
         // move to the end
         $this->documentManager->reorder($document, null);
     } else {
         if ($currentPosition < $position) {
             $targetSibling = $siblings[$position];
         } elseif ($currentPosition > $position) {
             $targetSibling = $siblings[$position - 1];
         }
         $this->documentManager->reorder($document, $targetSibling->getPath());
     }
     $this->documentManager->persist($document, $locale);
     $this->documentManager->flush();
     return $this->documentToStructure($document);
 }