<?php
/**
 * Form AdminList
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\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\Action;
use VisualM\ManagementBundle\AdminList\Column\Action\PrimaryAction;
use VisualM\ManagementBundle\AdminList\Column\ActionsColumn;
use VisualM\ManagementBundle\AdminList\Column\DateTimeColumn;
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\ModelManagementBundle\Definition\ModelManagerInterface;

/**
 * Form AdminList
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class FormAdminList extends AdminList
{

    /**
     * Constructor
     *
     * @param RequestStack                  $requestStack Current Request
     * @param ModelManagerInterface         $manager      Model Manager
     * @param TranslatorInterface           $translator   Translator
     * @param AuthorizationCheckerInterface $authChecker  Authorization Checker
     */
    public function __construct(RequestStack $requestStack, ModelManagerInterface $manager, TranslatorInterface $translator, AuthorizationCheckerInterface $authChecker)
    {
        $actions = [];

        $request = $requestStack->getMasterRequest();

        if ($authChecker->isGranted('ROLE_PERM_DELETE_FORM')) {
            $actions[] = new PrimaryAction('manage_form_delete', 'id', $translator->trans('form.action.delete.title', [], 'backend'), 'text-natural', 'trash', array(
                'data-confirm' => $translator->trans('form.action.delete.confirm', [], 'backend'),
                'data-method' => 'POST'
            ));
        }

        if ($authChecker->isGranted('ROLE_PERM_EDIT_FORM')) {
            $actions[] = new PrimaryAction('manage_form_edit', 'id', $translator->trans('form.action.edit', [], 'backend'));
        }

        if ($authChecker->isGranted('ROLE_PERM_ADD_FORM')) {
            $actions[] = new Action('manage_form_copy', 'id', $translator->trans('form.action.copy', [], 'backend'));
        }

        parent::__construct(
            $request, 
            $manager, 
            new PaginationConfiguration(15, 7),
            new TableConfiguration(
                array(
                    $authChecker->isGranted('ROLE_PERM_EDIT_FORM')
                        ? new WidthDecorator('50%', new LinkColumn('titel', $translator->trans('form.header.titel', [], 'backend'), 'manage_form_edit', 'id', true))
                        : new WidthDecorator('50%', new PlainValueColumn('titel', $translator->trans('form.header.titel', [], 'backend'), true)),
                    new PlainValueColumn('referentie', $translator->trans('form.header.referentie', [], 'backend'), true),
                    new DateTimeColumn('gewijzigd', $translator->trans('form.header.gewijzigd', [], 'backend'), true, 'd-m-Y H:i'),
                    new Column\FormEntryCountColumn('id', $translator->trans('form.header.form_entries', [], 'backend')),
                    new ActionsColumn('id', $translator->trans('form.header.actions', [], 'backend'), $actions)
                ), 
                'titel',
                'ASC'
            ), 
            new ParamConfiguration(array(
                new QueryParamProvider('page', $request->getSession(), 'formPage'),
                new QueryParamProvider('orderBy', $request->getSession(), 'formOrderBy'),
                new QueryParamProvider('orderDir', $request->getSession(), 'formOrderDir'),
                new QueryParamProvider('search', $request->getSession(), 'formSearch'),
            ))
        );
    }

}
