<?php

use KupShop\AdminBundle\AdminList\BaseList;

/**
 * Created by PhpStorm.
 * User: hanz
 * Date: 12.6.14
 * Time: 14:46.
 */
class TemplatesCategoriesList extends BaseList
{
    use AdminListSortable;

    protected $template = 'listSortable.tpl';

    protected $showMassEdit = true;
    protected $tableName = 'templates_categories';
    protected ?string $tableAlias = 'tc';

    protected $tableDef = [
        'id' => 'tc.id',
        'fields' => [
            'position' => ['translate' => true, 'field' => 'tc.position', 'render' => 'renderPosition', 'size' => '70px'],
            'name' => ['translate' => true, 'field' => 'tc.name', 'spec' => 'tc.name', 'fieldType' => BaseList::TYPE_STRING],
        ],
    ];

    public function getSQLOrdering(&$var, &$orderParam)
    {
        parent::getSQLOrdering($var, $orderParam);
        $var['orderField'] = 'position';
    }

    public function handle()
    {
        parent::handle();

        $item = getVal('moved_item');

        if (!empty($item)) {
            $this->saveList($item, 'templates_categories');
            exit('{}');
        }
    }

    public function getQuery()
    {
        $qb = sqlQueryBuilder();

        $qb->select('id', 'position')
            ->from(getTableName('templates_categories'), 'tc')
            ->where(1);

        return $qb;
    }
}
