<?php
namespace Moto\Application\MediaLibrary\Filter; use Moto\Filter\AbstractFilter; use Moto\Application\MediaLibrary\InputFilter; use Moto\Application\MediaLibrary\Validator; use Moto\Application\MediaLibrary\MediaFoldersTable; use Zend\Db\Sql\Select; use Moto; class FoldersTree extends AbstractFilter { public function filter($items) { if (!is_array($items) || empty($items)) { return $items; } $inputFilter = new InputFilter\FoldersTreeItem(); $result = array(); $table = new MediaFoldersTable(); $table->useResultAsModel(true); foreach($items as $item) { $inputFilter->setData($item); $filtered = $inputFilter->getValues(); $item = $table->getById($filtered['id']); $uniqueTreeItemValidator = new Validator\UniqueTreeItem(array( 'table' => Moto\Config::get('database.prefix') . 'media_folders', 'field' => 'name', 'adapter' => Moto\Config::get('databaseAdapter') )); $uniqueTreeItemValidator->setParentId($filtered['parent_id']); $uniqueTreeItemValidator->setExclude(array( 'field' => 'id', 'value' => $filtered['id'] )); if ($item && !$uniqueTreeItemValidator->isValid($item->name)) { $select = new Select(Moto\Config::get('database.prefix') . 'media_folders'); $select->columns(array('name')); $select->where->equalTo('name', null); $select->where->equalTo('parent_id', $filtered['parent_id']); $duplicateFilter = new Moto\Filter\Duplicate(array( 'duplicator' => array( 'name' => 'db', 'options' => array( 'suffix' => '_', 'select' => $select, 'start_index' => 1 ) ) )); $filtered['name'] = $duplicateFilter->filter($item->name); } $result[] = $filtered; } return $result; } } 