/** * Checks if the position changed and update the position of the old and new level. * If the parent changed the old and next level must updated. * OldLevel => from OldPosition to MAX -1 <br> * NewLevel => from newPosition to MAX +1 * * @param Shopware\Models\Media\Album $album * @param $newPosition * @param $newParent */ private function changePosition(Album $album, $newPosition, $newParent) { //position or level not changed? if ($newParent == $album->getParentId() && $newPosition == $album->getPosition()) { return; } //0 can not be used, because Doctrine would otherwise check if the album with the id 0 exists. $newParent = ($newParent == 0) ? null : $newParent; //moved up or down? down => -1 / up = +1 $step = ($newPosition < $album->getPosition()) ? 1 : -1; //Start and end values determined. $fromPosition = ($step == 1) ? $newPosition - 1 : $album->getPosition(); $toPosition = ($step == 1) ? $album->getPosition() : $newPosition + 1; //same parent? Then change only the position of the current album level. if ($newParent == $album->getParentId()) { $this->changePositionBetween($fromPosition, $toPosition, $step, $album->getParentId()); } else { //change the position of the old level $this->changePositionBetween($album->getPosition(), 99999, -1, $album->getParentId()); //change the position of the new level $this->changePositionBetween($newPosition - 1, 99999, +1, $newParent); } }
/** * {@inheritDoc} */ public function getPosition() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getPosition', array()); return parent::getPosition(); }
/** * Converts the album properties into tree node properties. * If the album has sub-albums, iterates the children recursively. * * @param Shopware\Models\Media\Album $album * @return array */ private function getAlbumNodeProperties(\Shopware\Models\Media\Album $album) { /** @var $repository \Shopware\Models\Media\Repository */ $repository = Shopware()->Models()->Media(); $query = $repository->getAlbumMediaQuery($album->getId()); $paginator = $this->getModelManager()->createPaginator($query); //returns the total count of the query $totalResult = $paginator->count(); $parentId = null; if ($album->getParent()) { $parentId = $album->getParent()->getId(); } $node = array('id' => $album->getId(), 'text' => $album->getName(), 'position' => $album->getPosition(), 'mediaCount' => $totalResult, 'parentId' => $parentId); //to get fresh album settings from new albums too $settingsQuery = $repository->getAlbumWithSettingsQuery($album->getId()); $albumData = $settingsQuery->getOneOrNullResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY); $settings = $albumData["settings"]; if (!empty($settings) && $settings !== null) { $node["iconCls"] = $settings["icon"]; $node["createThumbnails"] = $settings["createThumbnails"]; $node["thumbnailHighDpi"] = $settings["thumbnailHighDpi"]; $node["thumbnailQuality"] = $settings["thumbnailQuality"]; $node["thumbnailHighDpiQuality"] = $settings["thumbnailHighDpiQuality"]; $thumbnails = explode(";", $settings["thumbnailSize"]); $node["thumbnailSize"] = array(); $count = count($thumbnails); //convert the thumbnail to an array width the index and value for ($i = 0; $i <= $count; $i++) { if ($thumbnails[$i] === '' || $thumbnails[$i] === null) { continue; } $node["thumbnailSize"][] = array('id' => $i, 'index' => $i, 'value' => $thumbnails[$i]); } } //has sub-albums, then iterate and add the media count to the parent album. if (count($album->getChildren()) > 0) { $node['data'] = $this->toTree($album->getChildren(), $node); $node['leaf'] = false; } else { $node['leaf'] = true; } return $node; }