Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propel / date picker problem #79

Open
lionelbzv opened this issue Sep 4, 2017 · 7 comments
Open

Propel / date picker problem #79

lionelbzv opened this issue Sep 4, 2017 · 7 comments

Comments

@lionelbzv
Copy link
Contributor

lionelbzv commented Sep 4, 2017

Hello,

After a long time, I've just updated my project's libs (sf2.8 with propel 1.6) and I'me facing new troubles with admingenerator bundles: the date range filter is systematically filled (I can't leave this field empty) and throws an error.

Stack trace:


in vendor/symfony2admingenerator/generator-bundle/QueryFilter/BaseQueryFilter.php at line 62   -
        }
        if (!($date instanceof \DateTime)) {
            $date = new \DateTime($date);
        }
        if (false !== $date) {
at DateTime ->__construct ('26/08/2017') 
in vendor/symfony2admingenerator/generator-bundle/QueryFilter/BaseQueryFilter.php at line 62   + 
at BaseQueryFilter ->formatDate ('26/08/2017', 'Y-m-d') 
in vendor/symfony2admingenerator/generator-bundle/QueryFilter/PropelQueryFilter.php at line 62   + 
at PropelQueryFilter ->addDateFilter ('published_at', '26/08/2017 - 26/08/2017') 
in vendor/symfony2admingenerator/generator-bundle/QueryFilter/PropelQueryFilter.php at line 90   + 
at PropelQueryFilter ->addTimestampFilter ('published_at', '26/08/2017 - 26/08/2017') 
in app/cache/dev/Admingenerated/ProjectAdminBundle/BasePublicationController/ListController.php at line 413   + 
at ListController ->filterPublishedAt (object(PropelQueryFilter), '26/08/2017 - 26/08/2017') 
in app/cache/dev/Admingenerated/ProjectAdminBundle/BasePublicationController/ListController.php at line 288   + 
at ListController ->processFilters (object(PropelQueryFilter)) 
in app/cache/dev/Admingenerated/ProjectAdminBundle/BasePublicationController/ListController.php at line 674   + 
at ListController ->getQuery () 
in app/cache/dev/Admingenerated/ProjectAdminBundle/BasePublicationController/ListController.php at line 113   + 
at ListController ->getPager () 
in app/cache/dev/Admingenerated/ProjectAdminBundle/BasePublicationController/ListController.php at line 32   + 

I was using the commit 46e0fc9 and the next one 6ce16e0 generates this problem.

Any tips welcome to solve this problem!

@sescandell
Copy link
Member

Hi @Lionel09

Could you please copy/paste the generated controller?

Thanks,

@lionelbzv
Copy link
Contributor Author

lionelbzv commented Sep 4, 2017

Generated list controller:

<?php

namespace Admingenerated\ProjectAdminBundle\BaseDocumentController;

use Project\AdminBundle\Form\Type\Document\FiltersType;
    use Symfony\Component\Security\Core\Exception\AccessDeniedException;
    use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;


use Admingenerator\GeneratorBundle\Controller\Propel\BaseController as BaseController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\PropelAdapter as PagerAdapter;

class ListController extends BaseController
{
    public function indexAction(Request $request)
    {
        $this->request = $request;
            

        $this->parseRequestForPager();

        // Scopes have to be processed before the filter form is initialized
        // so default scopes are synchronized with filters.
        $scopes = $this->getScopes();
        $form = $this->getFilterForm();

        return $this->render('ProjectAdminBundle:DocumentList:index.html.twig', $this->getAdditionalRenderParameters() + array(
            'Documents' => $this->getPager(),
            'listRoute'                 => $this->getListRoute(),
            'filtersUrl'                => $this->getFiltersUrl(),
            'form'                      => $form->createView(),
            'sortColumn'                => $this->getSortColumn(),
            'sortOrder'                 => $this->getSortOrder(),
            'scopes'                    => $scopes,
            'perPageChoices'            => $this->getPerPageChoices(),
        ));
    }
    /**
     * Check if request contains pager parameters and
     * persist them in session if any.
     */
    protected function parseRequestForPager()
    {
        if ($this->request->query->get('page')) {
            $this->setPage($this->request->query->get('page'));
        }

        if ($this->request->query->get('perPage')) {
            $this->setPerPage($this->request->query->get('perPage'));
        }

        if ($this->request->query->get('sort')) {
            $this->setSort($this->request->query->get('sort'), $this->request->query->get('order_by','ASC'));
        }
    }

    /**
     * Store in the session service the current page
     *
     * @param integer $page The page number
     */
    protected function setPage($page)
    {
        $this->get('session')->set($this->getSessionPrefix().'List\Page', $page);
    }

    /**
     * Return the stored page
     *
     * @return integer $page The page number
     */
    protected function getPage()
    {
        return $this->get('session')->get($this->getSessionPrefix().'List\Page', 1);
    }

    /**
     * Return array of possible perPageChoices
     *
     * @return array
     */
    protected function getPerPageChoices()
    {
        return array(  0 => 10,  1 => 25,  2 => 50,  3 => 75,  4 => 100,);
    }

    /**
     * Store in the session service the perPage
     *
     * @param integer $perPage The perPage number
     */
    protected function setPerPage($perPage)
    {
        $this->get('session')->set($this->getSessionPrefix().'List\PerPage', $perPage);
    }

    /**
     * Return the stored perPage
     *
     * @return integer $perPage The perPage number
     */
    protected function getPerPage()
    {
        return $this->get('session')->get($this->getSessionPrefix().'List\PerPage', 10);
    }

    protected function getPager()
    {
        $paginator = new Pagerfanta(new PagerAdapter($this->getQuery()));
        $paginator->setMaxPerPage($this->getPerPage());
        $paginator->setCurrentPage($this->getPage(), false, true);

        return $paginator;
    }
    /**
     * Store in the session service the current sort
     *
     * @param string $column The column
     * @param string $order_by The order sorting (ASC,DESC)
     */
    protected function setSort($column, $order_by)
    {
        $this->get('session')->set($this->getSessionPrefix().'List\Sort', $column);

        if ($order_by == 'desc') {
            $this->get('session')->set($this->getSessionPrefix().'List\OrderBy', 'DESC');
        } else {
             $this->get('session')->set($this->getSessionPrefix().'List\OrderBy', 'ASC');
        }
    }

    /**
     * Return the stored sort
     *
     * @return string The column to sort
     */
    protected function getSortColumn()
    {
        return $this->get('session')->get($this->getSessionPrefix().'List\Sort', 'published_at');
        }

    /**
     * Return the stored sort order
     *
     * @return string the order mode ASC|DESC
     */
    protected function getSortOrder()
    {
        return $this->get('session')->get($this->getSessionPrefix().'List\OrderBy', 'DESC');
        }


   /**
    * Action called on filter request. After the call, the user is redirected
    * to the list page.
    *
    * @param Request $request The request
    * @return RedirectResponse
    */
    public function filtersAction(Request $request)
    {
        $this->request = $request;
        if ($this->request->get('reset')) {
            $this->setFilters(array());
            // Remove scopes and re-apply default ones
            $this->setScopes(null);

            return $this->redirect($this->getListUrl());
        }

        if ('POST' === $this->request->getMethod()) {
            $form = $this->getFilterForm();
            $form->handleRequest($this->request);

            if ($form->isValid()) {
                $filters = $form->getViewData();
            }
        }

        if ('GET' === $this->request->getMethod()) {
            $filters = $this->request->query->all();

                                                if (array_key_exists('id', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['id']);
                    }
                                                                if (array_key_exists('uuid', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['uuid']);
                    }
                                                                if (array_key_exists('p_user_id', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['p_user_id']);
                    }
                                                                if (array_key_exists('p_c_topic', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['p_c_topic']);
                    }
                                                                if (array_key_exists('p_e_operation', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['p_e_operation']);
                    }
                                                                if (array_key_exists('p_l_department', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['p_l_department']);
                    }
                                                                if (array_key_exists('p_l_region', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['p_l_region']);
                    }
                                                                if (array_key_exists('p_l_country', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['p_l_country']);
                    }
                                                                if (array_key_exists('title', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['title']);
                    }
                                                                if (array_key_exists('online', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['online']);
                    }
                                                                if (array_key_exists('homepage', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['homepage']);
                    }
                                                                if (array_key_exists('published', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['published']);
                    }
                                                                if (array_key_exists('published_at', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['published_at']);
                    }
                                                                if (array_key_exists('created_at', $filters) && !$this->validateCredentials('AdmingenAllowed')) {
                        unset($filters['created_at']);
                    }
                                    }

        if (isset($filters)) {
            // Set no active scope
            $this->setScopes(array());
            $this->setFilters($filters);
        }

        return $this->redirect($this->getListUrl());
    }

    protected function setFilters(array $filters)
    {
        $this->get('session')->set($this->getSessionPrefix().'List\Filters', $filters);
    }
    
    /**
    * @return array
    */
    protected function getFilters()
    {
        return $this->get('session')->get($this->getSessionPrefix().'List\Filters', array());
    }
    
    protected function processFilters($queryFilter)
    {
        $filterObject = $this->getFilters();

            if (isset($filterObject['id']) && null !== $filterObject['id']) {
            $this->filterId($queryFilter, $filterObject['id']);
        }
            if (isset($filterObject['uuid']) && null !== $filterObject['uuid']) {
            $this->filterUuid($queryFilter, $filterObject['uuid']);
        }
            if (isset($filterObject['p_user_id']) && null !== $filterObject['p_user_id']) {
            $this->filterPUserId($queryFilter, $filterObject['p_user_id']);
        }
            if (isset($filterObject['p_c_topic']) && null !== $filterObject['p_c_topic']) {
            $this->filterPCTopic($queryFilter, $filterObject['p_c_topic']);
        }
            if (isset($filterObject['p_e_operation']) && null !== $filterObject['p_e_operation']) {
            $this->filterPEOperation($queryFilter, $filterObject['p_e_operation']);
        }
            if (isset($filterObject['p_l_department']) && null !== $filterObject['p_l_department']) {
            $this->filterPLDepartment($queryFilter, $filterObject['p_l_department']);
        }
            if (isset($filterObject['p_l_region']) && null !== $filterObject['p_l_region']) {
            $this->filterPLRegion($queryFilter, $filterObject['p_l_region']);
        }
            if (isset($filterObject['p_l_country']) && null !== $filterObject['p_l_country']) {
            $this->filterPLCountry($queryFilter, $filterObject['p_l_country']);
        }
            if (isset($filterObject['title']) && null !== $filterObject['title']) {
            $this->filterTitle($queryFilter, $filterObject['title']);
        }
            if (isset($filterObject['online']) && null !== $filterObject['online']) {
            $this->filterOnline($queryFilter, $filterObject['online']);
        }
            if (isset($filterObject['homepage']) && null !== $filterObject['homepage']) {
            $this->filterHomepage($queryFilter, $filterObject['homepage']);
        }
            if (isset($filterObject['published']) && null !== $filterObject['published']) {
            $this->filterPublished($queryFilter, $filterObject['published']);
        }
            if (isset($filterObject['published_at']) && null !== $filterObject['published_at']) {
            $this->filterPublishedAt($queryFilter, $filterObject['published_at']);
        }
            if (isset($filterObject['created_at']) && null !== $filterObject['created_at']) {
            $this->filterCreatedAt($queryFilter, $filterObject['created_at']);
        }
        }

       /**
    * Add filters to the query for id
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterId($queryFilter, $value)
    {
        $queryFilter->addINTEGERFilter('id', $value);
    }
       /**
    * Add filters to the query for uuid
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterUuid($queryFilter, $value)
    {
        $queryFilter->addVARCHARFilter('uuid', $value);
    }
       /**
    * Add filters to the query for p_user_id
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPUserId($queryFilter, $value)
    {
        $queryFilter->addINTEGERFilter('p_user_id', $value);
    }
       /**
    * Add filters to the query for p_c_topic
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPCTopic($queryFilter, $value)
    {
        $queryFilter->addModelFilter('p_c_topic', $value);
    }
       /**
    * Add filters to the query for p_e_operation
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPEOperation($queryFilter, $value)
    {
        $queryFilter->addModelFilter('p_e_operation', $value);
    }
       /**
    * Add filters to the query for p_l_department
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPLDepartment($queryFilter, $value)
    {
        $queryFilter->addModelFilter('p_l_department', $value);
    }
       /**
    * Add filters to the query for p_l_region
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPLRegion($queryFilter, $value)
    {
        $queryFilter->addModelFilter('p_l_region', $value);
    }
       /**
    * Add filters to the query for p_l_country
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPLCountry($queryFilter, $value)
    {
        $queryFilter->addModelFilter('p_l_country', $value);
    }
       /**
    * Add filters to the query for title
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterTitle($queryFilter, $value)
    {
        $queryFilter->addVARCHARFilter('title', $value);
    }
       /**
    * Add filters to the query for online
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterOnline($queryFilter, $value)
    {
        $queryFilter->addBOOLEANFilter('online', $value);
    }
       /**
    * Add filters to the query for homepage
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterHomepage($queryFilter, $value)
    {
        $queryFilter->addBOOLEANFilter('homepage', $value);
    }
       /**
    * Add filters to the query for published
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPublished($queryFilter, $value)
    {
        $queryFilter->addBOOLEANFilter('published', $value);
    }
       /**
    * Add filters to the query for published_at
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterPublishedAt($queryFilter, $value)
    {
        $queryFilter->addTIMESTAMPFilter('published_at', $value);
    }
       /**
    * Add filters to the query for created_at
    *
    * @param \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface The queryFilter
    * @param mixed The value
    */
    protected function filterCreatedAt($queryFilter, $value)
    {
        $queryFilter->addTIMESTAMPFilter('created_at', $value);
    }
    
   /**
    * Returns the FilterType form FQCN
    *
    * @return string
    */
    protected function getFiltersType()
    {
        return 'Project\AdminBundle\Form\Type\Document\FiltersType';
    }

   /**
    * Get optional form filter options.
    *
    * @return array
    */
    protected function getFiltersOptions()
    {
        return array();
    }

   /**
    * Get list filters URL
    *
    * @return string
    */
    protected function getFiltersUrl()
    {
        return $this->generateUrl($this->getFiltersRoute());
    }

   /**
    * Get list filters route
    *
    * @return string
    */
    protected function getFiltersRoute()
    {
        return "Project_AdminBundle_Document_filters";
    }
    public function scopesAction(Request $request)
    {
        $this->request = $request;
        $this->setScope($this->request->get('group'), $this->request->get('scope'));

        return $this->redirect($this->getListUrl());
    }

    /**
     * Store in the session service the current scopes
     * If $scopes is null, simply remove any informations about scopes
     * (so in next request, defaults one will be used).
     *
     * @param null|array the scopes
     */
    protected function setScopes($scopes)
    {
        if (null === $scopes) {
            $this->get('session')->remove($this->getSessionPrefix().'List\Scopes');
        } else {
            $this->get('session')->set($this->getSessionPrefix().'List\Scopes', $scopes);
        }
    }

    /**
    * Change the value of one Scope
    *
    * @param string the group name
    * @param string the scope name
    */
    protected function setScope($groupName, $scopeName)
    {
        $scopes = $this->getScopes();
        $scopes[strtolower($groupName)] = strtolower($scopeName);
        $this->setScopes($scopes);

        $filters = array();
        foreach ($scopes as $scopeGroupName => $scopeName) {
            $filters += $this->getScopeFilters($scopeGroupName);
        }
        $this->setFilters($filters);

    }

    protected function getScopes()
    {
        if (null === $scopes = $this->get('session')->get($this->getSessionPrefix().'List\Scopes')) {
            $scopes = $this->getDefaultScopes();
            $this->setScopes($scopes);
            foreach ($scopes as $groupName => $scopeName) {
                $this->setScope($groupName, $scopeName);
            }
        }

        return $scopes;
    }

    protected function getDefaultScopes()
    {
        $scopes = array();

                $scopes['group1'] = '';

                    $scopes['group1'] = 'publiés';
            
        return $scopes;
    }

    /*
    * @return string|null the scope setted for the current group
    */
    protected function getScope($groupName)
    {
        $groupName = strtolower($groupName);
        $scopes = $this->getScopes();

        return isset($scopes[$groupName]) ? $scopes[$groupName] : null ;
    }

    /**
     * @return array the filters for the $groupName scope
     */
    protected function getScopeFilters($groupName)
    {
        if (!$scope = $this->getScope($groupName)) {
            return array();
        }

        $groupName = strtolower($groupName);
        $filters = array();

        if ('group1' === $groupName) {                if ('publiés' === $scope) {
                $filters['published'] = 1;
                }
                            if ('brouillons' === $scope) {
                $filters['published'] = 0;
                }
            }
        return $filters;
    }

    protected function processScopes($queryFilter)
    {
    $scopes = $this->getScopes();

                        if (isset($scopes['group1']) && $scopes['group1'] == 'publiés') {
                                }
                            if (isset($scopes['group1']) && $scopes['group1'] == 'brouillons') {
                                }
                }

                                        /**
    * Check user credentials
    *
    * @param string $credentials
    * @param \Project\Model\Document $Document
    * @return boolean
    */
    protected function validateCredentials($credentials, \Project\Model\Document $Document = null)
    {
    
    return $this->isGranted($credentials, $Document);
    }

    /**
    * Throws exception if credentials are not validated
    *
    * @param string $credentials
    * @param \Project\Model\Document $Document
    * @throws AccessDeniedException if is not allowed
    */
    protected function denyAccessUnlessValidateCredentials($credentials, \Project\Model\Document $Document = null)
    {
    if (!$this->validateCredentials($credentials, $Document)) {
    throw $this->createAccessDeniedException('Credentials unsatisfied');
    }
    }

    /**
     * Check crsf token provided for action
     *
     * @throw InvalidCsrfTokenException if token is invalid
     */
    protected function isGeneratedCsrfTokenValid($intention)
    {
        if (!$this->isCsrfTokenValid($intention, $this->request->request->get('_csrf_token'))) {
            throw new InvalidCsrfTokenException();
        }
    }



    /**
     * Get asession prefix
     *
     * @return string
     */
    protected function getSessionPrefix()
    {
        return 'Project\AdminBundle\Document';
    }

    /**
     * Get list index URL
     *
     * @return string
     */
    protected function getListUrl()
    {
        return $this->generateUrl($this->getListRoute());
    }

    /**
     * Get list index route
     *
     * @return string
     */
    protected function getListRoute()
    {
        return "Project_AdminBundle_Document_list";
    }

    /**
     * Get additional parameters for rendering.
     *
     * @return array
     */
    protected function getAdditionalRenderParameters()
    {
        return array();
    }

    /**
     * This function is for your convinience. Overwrite it if you need to
     * process the query.
     */
    protected function processQuery($query)
    {
        return $query;
    }

    protected function getQuery()
    {
        $query = $this->buildQuery();

        $this->processQuery($query);
        $this->processSort($query);

        $queryFilter = $this->getQueryFilter($query);
        $this->processFilters($queryFilter);
        $this->processScopes($queryFilter);

        return $query;
    }

    protected function buildQuery()
    {
        return \Project\Model\DocumentQuery::create();
    }

    protected function processSort($query)
    {
        if ($this->getSortColumn()) {
            if (!strstr($this->getSortColumn(), '.')) { //direct column
                $query->orderBy($this->getSortColumn(), $this->getSortOrder());
            } else {
                list($table, $column) = explode('.', $this->getSortColumn(), 2);
                $this->addJoinFor($table, $query);
                $query->orderBy($this->getSortColumn(), $this->getSortOrder());
            }
        }
    }

    protected function getFilterForm()
    {
        $filters = $this->getFilters();
                return $this->createForm(
            $this->getFiltersType(),
            $filters,
            $this->getFiltersOptions()
        );
    }

    protected function addJoinFor($table, $query)
    {
                $query->leftJoin($table);
    }

        /**
     * @return \Admingenerator\GeneratorBundle\QueryFilter\QueryFilterInterface
     */
    protected function getQueryFilter($query)
    {
        $queryFilterClass = $this->container->getParameter('admingenerator.queryfilter.propel.class');

        return new $queryFilterClass($query);
    }

}

@sescandell
Copy link
Member

I don't see the filterPublishedAt call... is this the good Controller?

@lionelbzv
Copy link
Contributor Author

code updated, thanks.

@lionelbzv
Copy link
Contributor Author

any news for this one?

@bobvandevijver
Copy link
Member

It seems like a format issue, did you try specifying the format?

@lionelbzv
Copy link
Contributor Author

I can't set a format option, cf #81

BTW, the generated PHP code seems ok because I can't see any difference in generated controllers between a version that works, and the ones who don't.

This is the last commit working, after I've got the date issues describing here (the date range filter is systematically filled - can't leave this field empty - and throws an error when filtering).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants