/** * Calculate the resulting list. * * @return void * * @throws \RuntimeException When no sorting property has been defined. */ protected function calculate() { if (isset($this->results) || $this->models->length() == 0) { return; } if (!$this->getSortingProperty()) { throw new \RuntimeException('No sorting property defined for ' . $this->models->get(0)->getProviderName()); } $this->results = clone $this->models; $this->siblingsCopy = clone $this->siblings; $this->scanToDesiredPosition(); $this->updateSorting(); }
/** * Calculate the resulting list. * * @return void */ protected function calculate() { if (isset($this->results) || $this->models->length() == 0) { return; } $ids = $this->getModelIds(); $this->results = clone $this->models; $this->siblingsCopy = clone $this->siblings; $this->scanToDesiredPosition(); // If no "next" sibling, simply increment the sorting as we are at the end of the list. if (!$this->marker) { foreach ($this->results as $model) { $this->position += 128; /** @var ModelInterface $model */ $model->setProperty($this->getSortingProperty(), $this->position); } return; } // Determine delta value: ((next sorting - current sorting) / amount of insert models). $delta = ($this->marker->getProperty($this->getSortingProperty()) - $this->position) / $this->results->length(); // If delta too narrow, we need to make room. if ($delta < 2) { $delta = 128; } // Loop over all models and increment sorting value. foreach ($this->results as $model) { $this->position += $delta; /** @var ModelInterface $model */ $model->setProperty($this->getSortingProperty(), $this->position); } // When the sorting exceeds the sorting of the "next" sibling, we need to push the remaining siblings to the // end of the list. if ($this->marker->getProperty($this->getSortingProperty()) <= $this->position) { do { // Skip models about to be pasted. if (in_array($this->marker->getId(), $ids)) { $this->marker = $this->siblingsCopy->shift(); continue; } $this->position += $delta; $this->marker->setProperty($this->getSortingProperty(), $this->position); $this->results->push($this->marker); $this->marker = $this->siblingsCopy->shift(); } while ($this->marker); } }
/** * {@inheritDoc} * * @throws \RuntimeException When no models have been passed. */ public function pasteAfter(ModelInterface $previousModel, CollectionInterface $models, $sortedBy) { if ($models->length() == 0) { throw new \RuntimeException('No models passed to pasteAfter().'); } $environment = $this->getEnvironment(); if (in_array($environment->getDataDefinition()->getBasicDefinition()->getMode(), array(BasicDefinitionInterface::MODE_HIERARCHICAL, BasicDefinitionInterface::MODE_PARENTEDLIST))) { $parentModel = null; $parentModel = null; if (!$this->isRootModel($previousModel)) { $parentModel = $this->searchParentOf($previousModel); } foreach ($models as $model) { /** @var ModelInterface $model */ $this->setSameParent($model, $previousModel, $parentModel ? $parentModel->getProviderName() : null); } } // Enforce proper sorting now. $siblings = $this->assembleSiblingsFor($previousModel, $sortedBy); $sortManager = new SortingManager($models, $siblings, $sortedBy, $previousModel); $newList = $sortManager->getResults(); $environment->getDataProvider($previousModel->getProviderName())->saveEach($newList); }