<commit_before><?php

namespace Application\Controller\Api;

use Zend\InputFilter\InputFilter;
use Zend\Hydrator\Strategy\StrategyInterface;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;

use geoPHP;
use Point;

use Autowp\Commons\Paginator\Adapter\Zend1DbSelect;
use Autowp\Message\MessageService;
use Autowp\User\Model\DbTable\User;
use Autowp\ZFComponents\Filter\FilenameSafe;
use Autowp\ZFComponents\Filter\SingleSpaces;

use Application\HostManager;
use Application\Hydrator\Api\RestHydrator;
use Application\Hydrator\Api\Strategy\Image;
use Application\ItemNameFormatter;
use Application\Model\Brand as BrandModel;
use Application\Model\BrandVehicle;
use Application\Model\DbTable;
use Application\Service\SpecificationsService;

use Zend_Db_Expr;


class ItemController extends AbstractRestfulController
{
    /**
     * @var DbTable\Item
     */
    private $table;

    /**
     * @var RestHydrator
     */
    private $hydrator;

    /**
     * @var StrategyInterface
     */
    private $logoHydrator;

    /**
     * @var ItemNameFormatter
     */
    private $itemNameFormatter;

    /**
     * @var InputFilter
     */
    private $listInputFilter;

    /**
     * @var InputFilter
     */
    private $itemInputFilter;

    /**
     * @var SpecificationsService
     */
    private $specificationsService;

    /**
     * @var DbTable\Item\ParentTable
     */
    private $itemParentTable;

    /**
     * @var HostManager
     */
    private $hostManager;

    /**
     * @var MessageService
     */
    private $message;

    public function __construct(
        RestHydrator $hydrator,
        Image $logoHydrator,
        ItemNameFormatter $itemNameFormatter,
        InputFilter $listInputFilter,
        InputFilter $itemInputFilter,
        SpecificationsService $specificationsService,
        BrandVehicle $brandVehicle,
        HostManager $hostManager,
        MessageService $message
    ) {
        $this->hydrator = $hydrator;
        $this->logoHydrator = $logoHydrator;
        $this->itemNameFormatter = $itemNameFormatter;
        $this->listInputFilter = $listInputFilter;
        $this->itemInputFilter = $itemInputFilter;
        $this->specificationsService = $specificationsService;
        $this->brandVehicle = $brandVehicle;
        $this->hostManager = $hostManager;
        $this->message = $message;

        $this->table = new DbTable\Item();
        $this->itemParentTable = new DbTable\Item\ParentTable();
    }

    public function indexAction()
    {
        if (! $this->user()->inheritsRole('moder')) {
            return $this->forbiddenAction();
        }

        $user = $this->user()->get();

        $this->listInputFilter->setData($this->params()->fromQuery());

        if (! $this->listInputFilter->isValid()) {
            return $this->inputFilterResponse($this->listInputFilter);
        }

        $data = $this->listInputFilter->getValues();

        $select = $this->table->getAdapter()->select()
            ->from('item');

        $group = false;

        if ($data['last_item']) {
            $namespace = new \Zend\Session\Container('Moder_Car');
            if (isset($namespace->lastCarId)) {
                $select->where('item.id = ?', (int)$namespace->lastCarId);
            } else {
                $select->where(new Zend_Db_Expr('0'));
            }
        }

        switch ($data['order']) {
            case 'id_asc':
                $select->order('item.id ASC');
                break;
            case 'id_desc':
                $select->order('item.id DESC');
                break;
            case 'childs_count':
                $group = true;
                $select
                    ->columns(['childs_count' => new Zend_Db_Expr('count(item_parent.item_id)')])
                    ->join('item_parent', 'item_parent.parent_id = item.id', null)
                    ->order('childs_count desc');
                break;
            default:
                $select->order([
                    'item.name',
                    'item.body',
                    'item.spec_id',
                    'item.begin_order_cache',
                    'item.end_order_cache'
                ]);
                break;
        }

        if ($data['name']) {
            $select
                ->join('item_language', 'item.id = item_language.item_id', null)
                ->where('item_language.name like ?', $data['name']);
        }

        if ($data['name_exclude']) {
            $select
                ->join('item_language', 'item.id = item_language.item_id', null)
                ->where('item_language.name not like ?', $data['name_exclude']);
        }

        $id = (int)$this->params()->fromQuery('id');
        if ($id) {
            $select->where('item.id = ?', $id);
        }

        if ($data['descendant']) {
            $group = true;
            $select->join('item_parent_cache', 'item.id = item_parent_cache.parent_id', null)
                ->where('item_parent_cache.item_id = ?', $data['descendant']);
        }

        if ($data['type_id']) {
            $select->where('item.item_type_id = ?', $data['type_id']);
        }

        if ($data['vehicle_type_id']) {
            if ($data['vehicle_type_id'] == 'empty') {
                $select
                    ->joinLeft('vehicle_vehicle_type', 'item.id = vehicle_vehicle_type.vehicle_id', null)
                    ->where('vehicle_vehicle_type.vehicle_id is null');
            } else {<commit_msg>group results when search by name<commit_after><?php

namespace Application\Controller\Api;

use Zend\InputFilter\InputFilter;
use Zend\Hydrator\Strategy\StrategyInterface;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;

use geoPHP;
use Point;

use Autowp\Commons\Paginator\Adapter\Zend1DbSelect;
use Autowp\Message\MessageService;
use Autowp\User\Model\DbTable\User;
use Autowp\ZFComponents\Filter\FilenameSafe;
use Autowp\ZFComponents\Filter\SingleSpaces;

use Application\HostManager;
use Application\Hydrator\Api\RestHydrator;
use Application\Hydrator\Api\Strategy\Image;
use Application\ItemNameFormatter;
use Application\Model\Brand as BrandModel;
use Application\Model\BrandVehicle;
use Application\Model\DbTable;
use Application\Service\SpecificationsService;

use Zend_Db_Expr;


class ItemController extends AbstractRestfulController
{
    /**
     * @var DbTable\Item
     */
    private $table;

    /**
     * @var RestHydrator
     */
    private $hydrator;

    /**
     * @var StrategyInterface
     */
    private $logoHydrator;

    /**
     * @var ItemNameFormatter
     */
    private $itemNameFormatter;

    /**
     * @var InputFilter
     */
    private $listInputFilter;

    /**
     * @var InputFilter
     */
    private $itemInputFilter;

    /**
     * @var SpecificationsService
     */
    private $specificationsService;

    /**
     * @var DbTable\Item\ParentTable
     */
    private $itemParentTable;

    /**
     * @var HostManager
     */
    private $hostManager;

    /**
     * @var MessageService
     */
    private $message;

    public function __construct(
        RestHydrator $hydrator,
        Image $logoHydrator,
        ItemNameFormatter $itemNameFormatter,
        InputFilter $listInputFilter,
        InputFilter $itemInputFilter,
        SpecificationsService $specificationsService,
        BrandVehicle $brandVehicle,
        HostManager $hostManager,
        MessageService $message
    ) {
        $this->hydrator = $hydrator;
        $this->logoHydrator = $logoHydrator;
        $this->itemNameFormatter = $itemNameFormatter;
        $this->listInputFilter = $listInputFilter;
        $this->itemInputFilter = $itemInputFilter;
        $this->specificationsService = $specificationsService;
        $this->brandVehicle = $brandVehicle;
        $this->hostManager = $hostManager;
        $this->message = $message;

        $this->table = new DbTable\Item();
        $this->itemParentTable = new DbTable\Item\ParentTable();
    }

    public function indexAction()
    {
        if (! $this->user()->inheritsRole('moder')) {
            return $this->forbiddenAction();
        }

        $user = $this->user()->get();

        $this->listInputFilter->setData($this->params()->fromQuery());

        if (! $this->listInputFilter->isValid()) {
            return $this->inputFilterResponse($this->listInputFilter);
        }

        $data = $this->listInputFilter->getValues();

        $select = $this->table->getAdapter()->select()
            ->from('item');

        $group = false;

        if ($data['last_item']) {
            $namespace = new \Zend\Session\Container('Moder_Car');
            if (isset($namespace->lastCarId)) {
                $select->where('item.id = ?', (int)$namespace->lastCarId);
            } else {
                $select->where(new Zend_Db_Expr('0'));
            }
        }

        switch ($data['order']) {
            case 'id_asc':
                $select->order('item.id ASC');
                break;
            case 'id_desc':
                $select->order('item.id DESC');
                break;
            case 'childs_count':
                $group = true;
                $select
                    ->columns(['childs_count' => new Zend_Db_Expr('count(item_parent.item_id)')])
                    ->join('item_parent', 'item_parent.parent_id = item.id', null)
                    ->order('childs_count desc');
                break;
            default:
                $select->order([
                    'item.name',
                    'item.body',
                    'item.spec_id',
                    'item.begin_order_cache',
                    'item.end_order_cache'
                ]);
                break;
        }

        if ($data['name']) {
            $select
                ->join('item_language', 'item.id = item_language.item_id', null)
                ->where('item_language.name like ?', $data['name']);

            $group = true;
        }

        if ($data['name_exclude']) {
            $select
                ->join('item_language', 'item.id = item_language.item_id', null)
                ->where('item_language.name not like ?', $data['name_exclude']);
        }

        $id = (int)$this->params()->fromQuery('id');
        if ($id) {
            $select->where('item.id = ?', $id);
        }

        if ($data['descendant']) {
            $group = true;
            $select->join('item_parent_cache', 'item.id = item_parent_cache.parent_id', null)
                ->where('item_parent_cache.item_id = ?', $data['descendant']);
        }

        if ($data['type_id']) {
            $select->where('item.item_type_id = ?', $data['type_id']);
        }

        if ($data['vehicle_type_id']) {
            if ($data['vehicle_type_id'] == 'empty') {
                $select
                    ->joinLeft('vehicle_vehicle_type', 'item.id = vehicle_vehicle_type.vehicle_id', null)
                    ->where('vehicle_vehicle_type.vehicle_id is null');
            } else {
{'fraction_non_alphanumeric': 0.13593359514459122, 'fraction_numerical': 0.0003570153516601214, 'mean_word_length': 2.0481501632208925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6648150', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3150, 'n_words': 529}