<?php
/**
 * MetaTagSet AdminList
 *
 * @author Visual Media
 */

namespace VisualM\TagManagerBundle\AdminList;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use VisualM\ManagementBundle\AdminList\AdminList;
use VisualM\ManagementBundle\AdminList\Column\Action\PrimaryAction;
use VisualM\ManagementBundle\AdminList\Column\ActionsColumn;
use VisualM\ManagementBundle\AdminList\Column\BooleanColumn;
use VisualM\ManagementBundle\AdminList\Column\Decorator\WidthDecorator;
use VisualM\ManagementBundle\AdminList\Column\LinkColumn;
use VisualM\ManagementBundle\AdminList\Column\PlainValueColumn;
use VisualM\ManagementBundle\AdminList\PaginationConfiguration;
use VisualM\ManagementBundle\AdminList\Param\QueryParamProvider;
use VisualM\ManagementBundle\AdminList\ParamConfiguration;
use VisualM\ManagementBundle\AdminList\TableConfiguration;
use VisualM\TagManagerBundle\AdminList\Column\ContentTypesColumn;
use VisualM\TagManagerBundle\Model\MetaTagSetManager;

/**
 * MetaTagSet AdminList
 *
 * @author Visual Media
 */
class MetaTagSetAdminList extends AdminList
{

    /**
     * Constructor
     *
     * @param RequestStack                  $requestStack Request Stack
     * @param MetaTagSetManager             $manager      Model Manager
     * @param TranslatorInterface           $translator   Translator
     * @param AuthorizationCheckerInterface $authChecker  Authorization Checker
     */
    public function __construct(RequestStack $requestStack, MetaTagSetManager $manager, TranslatorInterface $translator, AuthorizationCheckerInterface $authChecker)
    {
        $actions = array();
        $request = $requestStack->getMasterRequest();
        
        if ($authChecker->isGranted('ROLE_PERM_DELETE_META_TAG_SET')) {
            $actions[] = new PrimaryAction('manage_meta_tag_set_delete', 'id', $translator->trans('meta_tag_set.action.delete.label', [], 'backend'), 'text-natural', 'trash', array(
                'data-confirm' => $translator->trans('meta_tag_set.action.delete.confirm', [], 'backend'),
                'data-method' => 'POST'
            ));
        }

        if ($authChecker->isGranted('ROLE_PERM_EDIT_META_TAG_SET')) {
            $actions[] = new PrimaryAction('manage_meta_tag_set_edit', 'id', $translator->trans('meta_tag_set.action.edit', array(), 'backend'));
        }

        parent::__construct(
            $request, 
            $manager, 
            new PaginationConfiguration(15, 7),
            new TableConfiguration(
                array(
                    $authChecker->isGranted('ROLE_PERM_EDIT_META_TAG_SET')
                        ? new WidthDecorator('50%', new LinkColumn('naam', $translator->trans('meta_tag_set.header.naam', [], 'backend'), 'manage_meta_tag_set_edit', 'id', true))
                        : new WidthDecorator('50%', new PlainValueColumn('naam', $translator->trans('meta_tag_set.header.naam', [], 'backend'), true)),
                    new BooleanColumn('kiesbaar', $translator->trans('meta_tag_set.header.kiesbaar', [], 'backend'), true, 'label label-success', 'label label-danger'),
                    new ContentTypesColumn('contentTypes', $translator->trans('meta_tag_set.header.content_types', [], 'backend')),
                    new ActionsColumn('id', $translator->trans('meta_tag_set.header.actions', [], 'backend'), $actions)
                ), 
                'naam',
                'ASC'
            ), 
            new ParamConfiguration(array(
                new QueryParamProvider('page'),
                new QueryParamProvider('orderBy', $request->getSession(), 'metaTagSetOrderBy'),
                new QueryParamProvider('orderDir', $request->getSession(), 'metaTagSetOrderDir'),
                new QueryParamProvider('search')
            ))
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getRowClass($item)
    {
        if ($item->getId() == $this->Request->getSession()->get('meta_tag_set.lastEdit')) {
            return 'last-edit';
        } else {
            return '';
        }
    }

}
