/**
  * {@inheritdoc}
  */
 public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
 {
     // Try to wrap all types to search types
     if ($type == null) {
         $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
         $type = $guessType->getType();
         $fieldDescription->setType($type);
         $options = $guessType->getOptions();
         foreach ($options as $name => $value) {
             if (is_array($value)) {
                 $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
             } else {
                 $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
             }
         }
     } else {
         $fieldDescription->setType($type);
     }
     $this->fixFieldDescription($admin, $fieldDescription);
     $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
     $fieldDescription->mergeOption('field_options', array('required' => false));
     $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
     if (false !== $filter->getLabel() && !$filter->getLabel()) {
         $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
     }
     $datagrid->addFilter($filter);
 }
 /**
  * @param \Sonata\AdminBundle\Datagrid\DatagridInterface      $datagrid
  * @param null                                                $type
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
  *
  * @return void
  */
 public function addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
 {
     if ($type == null) {
         $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
         $type = $guessType->getType();
         $fieldDescription->setType($type);
         $options = $guessType->getOptions();
         foreach ($options as $name => $value) {
             if (is_array($value)) {
                 $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
             } else {
                 $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
             }
         }
     } else {
         $fieldDescription->setType($type);
     }
     $this->fixFieldDescription($admin, $fieldDescription);
     $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
     $fieldDescription->mergeOption('field_options', array('required' => false));
     if ($type === 'doctrine_orm_model_autocomplete') {
         $fieldDescription->mergeOption('field_options', array('class' => $fieldDescription->getTargetEntity(), 'model_manager' => $fieldDescription->getAdmin()->getModelManager(), 'admin_code' => $admin->getCode(), 'context' => 'filter'));
     }
     $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
     if (false !== $filter->getLabel() && !$filter->getLabel()) {
         $filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
     }
     $datagrid->addFilter($filter);
 }
 /**
  * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
  * @param null $type
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  * @return \Sonata\AdminBundle\Filter\FilterInterface
  */
 public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
 {
     if ($type == null) {
         $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName());
         $type = $guessType->getType();
         $fieldDescription->setType($type);
         $options = $guessType->getOptions();
         foreach ($options as $name => $value) {
             if (is_array($value)) {
                 $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
             } else {
                 $fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
             }
         }
     } else {
         $fieldDescription->setType($type);
     }
     $this->fixFieldDescription($admin, $fieldDescription);
     $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
     $fieldDescription->mergeOption('field_options', array('required' => false));
     $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
     $datagrid->addFilter($filter);
     return $datagrid->addFilter($filter);
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function buildDatagrid()
 {
     if ($this->datagrid) {
         return;
     }
     $filterParameters = $this->getFilterParameters();
     // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid.
     if (isset($filterParameters['_sort_by']) && is_string($filterParameters['_sort_by'])) {
         if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
             $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
         } else {
             $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance($this->getClass(), $filterParameters['_sort_by'], array());
             $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this);
         }
     }
     // initialize the datagrid
     $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters);
     $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
     $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
     // build the datagrid filter
     $this->configureDatagridFilters($mapper);
     // ok, try to limit to add parent filter
     if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) {
         $mapper->add($this->getParentAssociationMapping(), null, array('show_filter' => false, 'label' => false, 'field_type' => 'sonata_type_model_hidden', 'field_options' => array('model_manager' => $this->getModelManager()), 'operator_type' => 'hidden'), null, null, array('admin_code' => $this->getParent()->getCode()));
     }
     foreach ($this->getExtensions() as $extension) {
         $extension->configureDatagridFilters($mapper);
     }
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function getPaginationParameters(DatagridInterface $datagrid, $page)
 {
     $values = $datagrid->getValues();
     $values['_sort_by'] = $values['_sort_by']->getName();
     $values['_page'] = $page;
     return array('filter' => $values);
 }
Example #6
0
    /**
     * build the filter FieldDescription array
     *
     * @return void
     */
    public function buildDatagrid()
    {
        if ($this->datagrid) {
            return;
        }

        // ok, try to limit to add parent filter
        if ($this->getParentAssociationMapping()) {
            $fieldDescription = $this->getModelManager()->getParentFieldDescription($this->getParentAssociationMapping(), $this->getClass());
            $this->filterFieldDescriptions[$this->getParentAssociationMapping()] = $fieldDescription;
        }

        foreach ($this->filterFieldDescriptions as $fieldDescription) {
            $this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
        }

        $parameters = $this->getFilterParameters();

        // initialize the datagrid
        $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
        $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);

        $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);

        // build the datagrid filter
        $this->configureDatagridFilters($mapper);
    }
    /**
     * Returns the parameters used in the columns header
     *
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
     * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
     * @return string
     */
    public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid)
    {
        $values = $datagrid->getValues();

        if ($fieldDescription->getOption('sortable') == $values['_sort_by']) {
            if ($values['_sort_order'] == 'ASC') {
                $values['_sort_order'] = 'DESC';
            } else {
                $values['_sort_order'] = 'ASC';
            }
        } else {
            $values['_sort_order']  = 'ASC';
            $values['_sort_by']     = $fieldDescription->getOption('sortable');
        }

        return $values;
    }
 /**
  * {@inheritdoc}
  */
 public function reorder(array $keys)
 {
     $this->datagrid->reorderFilters($keys);
     return $this;
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function buildDatagrid()
 {
     if ($this->datagrid) {
         return;
     }
     // initialize the datagrid
     $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $this->getFilterParameters());
     $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
     $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks);
     $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
     // build the datagrid filter
     $this->configureDatagridFilters($mapper);
     // ok, try to limit to add parent filter
     if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) {
         $mapper->add($this->getParentAssociationMapping(), null, array('field_type' => 'sonata_type_model_reference', 'field_options' => array('model_manager' => $this->getModelManager()), 'operator_type' => 'hidden'));
     }
     foreach ($this->getExtensions() as $extension) {
         $extension->configureDatagridFilters($mapper);
     }
 }
    /**
     * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
     * @return bool
     */
    public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
    {
        if ($type == null) {
            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName());
            $fieldDescription->setType($guessType->getType());
        } else {
            $fieldDescription->setType($type);
        }

        $this->fixFieldDescription($admin, $fieldDescription);
        $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);

        switch($fieldDescription->getMappingType()) {
            case ClassMetadataInfo::MANY_TO_ONE:
                $options = $fieldDescription->getOption('filter_field_options');
                $filter = new \Sonata\AdminBundle\Filter\ORM\IntegerFilter($fieldDescription);

                break;

            case ClassMetadataInfo::MANY_TO_MANY:
                $options = $fieldDescription->getOption('filter_field_options');
                $options['choices'] = $this->getChoices($fieldDescription);


                $fieldDescription->setOption('filter_field_options', $options);

                $filter = new \Sonata\AdminBundle\Filter\ORM\ChoiceFilter($fieldDescription);

                break;

            default:
                $class = $this->getFilterFieldClass($fieldDescription);
                $filter = new $class($fieldDescription);
        }

        return $datagrid->addFilter($filter);
    }
Example #11
0
 /**
  * build the filter FieldDescription array
  *
  * @return void
  */
 public function buildFilterFieldDescriptions()
 {
     if ($this->loaded['filter_fields']) {
         return;
     }
     $this->loaded['filter_fields'] = true;
     $this->filterFieldDescriptions = $this->getBaseFields($this->filter);
     // ok, try to limit to add parent filter
     if ($this->getParentAssociationMapping()) {
         $fieldDescription = $this->getModelManager()->getParentFieldDescription($this->getParentAssociationMapping(), $this->getClass());
         $this->filterFieldDescriptions[$this->getParentAssociationMapping()] = $fieldDescription;
     }
     foreach ($this->filterFieldDescriptions as $fieldDescription) {
         $this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
     }
     $parameters = array();
     // build the values array
     if ($this->hasRequest()) {
         $parameters = array_merge($this->getModelManager()->getDefaultSortValues($this->getClass()), $this->datagridValues, $this->request->query->all());
         // always force the parent value
         if ($this->isChild() && $this->getParentAssociationMapping()) {
             $parameters[$this->getParentAssociationMapping()] = $this->request->get($this->getParent()->getIdParameter());
         }
     }
     // initialize the datagrid
     $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $parameters);
     $this->datagrid->getPager()->setMaxPerPage($this->maxPerPage);
     $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this);
     // build the datagrid filter
     $this->buildFilterFieldDescriptions();
     $this->configureDatagridFilters($mapper);
     foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
         $mapper->add($fieldDescription);
     }
 }
Example #12
0
 /**
  * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @return bool
  */
 public function addFilter(DatagridInterface $datagrid, FieldDescriptionInterface $fieldDescription)
 {
     if (!$fieldDescription->getType()) {
         return false;
     }
     switch ($fieldDescription->getType()) {
         case ClassMetadataInfo::MANY_TO_ONE:
             $options = $fieldDescription->getOption('filter_field_options');
             $filter = new \Sonata\AdminBundle\Filter\ORM\IntegerFilter($fieldDescription);
             break;
         case ClassMetadataInfo::MANY_TO_MANY:
             $options = $fieldDescription->getOption('filter_field_options');
             $options['choices'] = $this->getChoices($fieldDescription);
             $fieldDescription->setOption('filter_field_options', $options);
             $filter = new \Sonata\AdminBundle\Filter\ORM\ChoiceFilter($fieldDescription);
             break;
         default:
             $class = $this->getFilterFieldClass($fieldDescription);
             $filter = new $class($fieldDescription);
     }
     return $datagrid->addFilter($filter);
 }