<?php
/**
 * FormEntry 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\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 RequestStack                  $requestStack    Request Stack
     * @param ModelManagerInterface         $manager         Model Manager
     * @param TranslatorInterface           $translator      Translator
     * @param AuthorizationCheckerInterface $authChecker AuthorizationChecker
	 * @param array                         $status_waarden  Status waarden
     */
    public function __construct(RequestStack $requestStack, ModelManagerInterface $manager, TranslatorInterface $translator, AuthorizationCheckerInterface $authChecker, array $status_waarden)
    {
        $actions = [];
        $statusActions = [];
		$showStatusColums = false;
        $request = $requestStack->getMasterRequest();

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

        if ($authChecker->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'));
        }

        if ($authChecker->isGranted('ROLE_PERM_WORKFLOW_FORM') AND count($status_waarden) > 1) {
            $showStatusColums = true;
            foreach($status_waarden as $status_waarde){
                $statusActions[] = new Action(
                    'manage_form_entry_wijzig_status',
                    function($item) use ($status_waarde) {
                        return [ 'id' => $item->getId(), 'status' => $status_waarde];
                    },
                    $status_waarde,
                    null,
                    null,
                    [
                        'data-method' => 'POST'
                    ]
                );
            }
        }

        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),
                    $showStatusColums === true 
                        ? new PlainValueColumn('status', $translator->trans('form_entry.header.status', [], 'backend'), true)
                        : null,
                    $showStatusColums === true 
                        ? new ActionsColumn('status', $translator->trans('form_entry.header.statusaction', [], 'backend'), $statusActions)
                        : null,
                    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('status', $request->getSession(), 'formEntryStatus'),
                new QueryParamProvider('toegevoegd_gte', $request->getSession(), 'formEntryToegevoegdGte'),
                new QueryParamProvider('toegevoegd_lte', $request->getSession(), 'formEntryToegevoegdLte'),
                new QueryParamProvider('form_id', $request->getSession(), 'formEntryFormId'),
            ))
        );
    }

}
