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

namespace VisualM\FormBundle\AdminList;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Translation\TranslatorInterface;
use VisualM\ManagementBundle\AdminList\AdminList;
use VisualM\ManagementBundle\AdminList\Column\Action\PrimaryAction;
use VisualM\ManagementBundle\AdminList\Column\Action\Action;
use VisualM\ManagementBundle\AdminList\Column\ActionsColumn;
use VisualM\ManagementBundle\AdminList\Column\Decorator\WidthDecorator;
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;

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

    /**
     * Constructor
     *
     * @param Request                  $request         Current Request
     * @param ModelManagerInterface    $manager         Model Manager
     * @param TranslatorInterface      $translator      Translator
     * @param SecurityContextInterface $securityContext Security Context
     */
    public function __construct(Request $request, ModelManagerInterface $manager, TranslatorInterface $translator, SecurityContextInterface $securityContext)
    {
        $actions = [];

        if ($securityContext->isGranted('ROLE_PERM_VIEW_FORM_ENTRY')) {
            $actions[] = new PrimaryAction('manage_form_entry_view', 'id', $translator->trans('form_entry.action.view', [], 'backend'));
        }

        if ($securityContext->isGranted('ROLE_PERM_EDIT_FORM')) {
            $actions[] = new Action('manage_form_edit', function($item) { return [ 'id' => $item->getForm()->getId() ]; }, $translator->trans('form_entry.action.form', [], 'backend'));
        }

        parent::__construct(
            $request, 
            $manager, 
            new PaginationConfiguration(15, 7),
            new TableConfiguration(
                array(
                    new WidthDecorator('50%', new Column\FormEntryDateColumn('toegevoegd', $translator->trans('form_entry.header.toegevoegd', [], 'backend'), true)),
                    new PlainValueColumn('form', $translator->trans('form_entry.header.form', [], 'backend'), false),
                    new PlainValueColumn('url', $translator->trans('form_entry.header.url', [], 'backend'), true),
                    new ActionsColumn('id', $translator->trans('form_entry.header.actions', [], 'backend'), $actions)
                ), 
                'toegevoegd',
                'DESC'
            ), 
            new ParamConfiguration(array(
                new QueryParamProvider('page', $request->getSession(), 'formEntryPage'),
                new QueryParamProvider('orderBy', $request->getSession(), 'formEntryOrderBy'),
                new QueryParamProvider('orderDir', $request->getSession(), 'formEntryOrderDir'),
                new QueryParamProvider('search', $request->getSession(), 'formEntrySearch'),
                new QueryParamProvider('form_id', $request->getSession(), 'formEntryFormId'),
            ))
        );
    }

}
