/**
  * @param \Sonata\AdminBundle\Datagrid\DatagridInterface      $datagrid
  * @param null                                                $type
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
  */
 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));
     $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);
 }
 /**
  * The method defines the correct default settings for the provided FieldDescription.
  *
  * {@inheritdoc}
  *
  * @throws \RuntimeException if the $fieldDescription does not have a type.
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     $fieldDescription->setAdmin($admin);
     if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
         $metadata = $admin->getModelManager()->getMetadata($admin->getClass());
         // set the default field mapping
         if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
             $fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
         }
         // set the default association mapping
         if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
             $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
         }
     }
     if (!$fieldDescription->getType()) {
         throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
     }
     $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
     $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
     if (!$fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
         if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_ONE) {
             $fieldDescription->setTemplate('SonataDoctrinePhpcrAdminBundle:CRUD:show_phpcr_many_to_one.html.twig');
         }
         if ($fieldDescription->getMappingType() == ClassMetadata::MANY_TO_MANY) {
             $fieldDescription->setTemplate('SonataDoctrinePhpcrAdminBundle:CRUD:show_phpcr_many_to_many.html.twig');
         }
     }
     switch ($fieldDescription->getMappingType()) {
         case ClassMetadata::MANY_TO_ONE:
         case ClassMetadata::MANY_TO_MANY:
             $admin->attachAdminClass($fieldDescription);
             break;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
 {
     $options = array();
     $options['sonata_field_description'] = $fieldDescription;
     if ($type == 'sonata_type_model' || $type == 'sonata_type_model_list') {
         if ($fieldDescription->getOption('edit') == 'list') {
             throw new \LogicException('The ``sonata_type_model`` type does not accept an ``edit`` option anymore, please review the UPGRADE-2.1.md file from the SonataAdminBundle');
         }
         $options['class'] = $fieldDescription->getTargetEntity();
         $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
     } elseif ($type == 'sonata_type_admin') {
         if (!$fieldDescription->getAssociationAdmin()) {
             throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
         }
         $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
         $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
     } elseif ($type == 'sonata_type_collection') {
         if (!$fieldDescription->getAssociationAdmin()) {
             throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
         }
         $options['type'] = 'sonata_type_admin';
         $options['modifiable'] = true;
         $options['type_options'] = array('sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass());
     }
     return $options;
 }
示例#4
0
 public function __construct(FieldDescriptionInterface $fieldDescription)
 {
     $this->name               = $fieldDescription->getName();
     $this->fieldDescription   = $fieldDescription;
     $this->options            = array_replace(
         $this->getDefaultOptions(),
         $this->fieldDescription->getOption('filter_options', array())
     );
 }
 /**
  * {@inheritdoc}
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     $fieldDescription->setTemplate('YnloAdminBundle::CRUD/list_enum.html.twig');
     /** @var ModelManager $modelManager */
     $modelManager = $admin->getModelManager();
     if (null === $fieldDescription->getOption('enum_type') && $modelManager->hasMetadata($admin->getClass())) {
         $mapping = $modelManager->getMetadata($admin->getClass())->getFieldMapping($fieldDescription->getName());
         $fieldDescription->setOption('enum_type', $mapping['type']);
     }
 }
 /**
  * @param FieldDescriptionInterface $fieldDescription
  * @param string                    $defaultTemplate
  *
  * @return \Twig_TemplateInterface
  */
 protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate)
 {
     $templateName = $fieldDescription->getTemplate() ?: $defaultTemplate;
     try {
         $template = $this->environment->loadTemplate($templateName);
     } catch (\Twig_Error_Loader $e) {
         $template = $this->environment->loadTemplate($defaultTemplate);
     }
     return $template;
 }
示例#7
0
 /**
  * {@inheritdoc}
  */
 public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
 {
     $options = array();
     $options['sonata_field_description'] = $fieldDescription;
     if ($type == 'sonata_type_model' || $type == 'sonata_type_model_list' || $type == 'sonata_type_model_hidden') {
         if ($fieldDescription->getOption('edit') == 'list') {
             throw new \LogicException('The ``sonata_type_model`` type does not accept an ``edit`` option anymore, please review the UPGRADE-2.1.md file from the SonataAdminBundle');
         }
         $options['class'] = $fieldDescription->getTargetEntity();
         $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
     } elseif ($type == 'sonata_type_admin') {
         if (!$fieldDescription->getAssociationAdmin()) {
             throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
         }
         if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) {
             throw new \RuntimeException(sprintf('You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or  Many-To-One. Maybe you want `sonata_model_list` instead?', $fieldDescription->getName()));
         }
         $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
         $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
     } elseif ($type == 'sonata_type_collection') {
         if (!$fieldDescription->getAssociationAdmin()) {
             throw new \RuntimeException(sprintf('The current field `%s` is not linked to an admin. Please create one for the target entity : `%s`', $fieldDescription->getName(), $fieldDescription->getTargetEntity()));
         }
         $options['type'] = 'sonata_type_admin';
         $options['modifiable'] = true;
         $options['type_options'] = array('sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass());
     }
     return $options;
 }
示例#8
0
 /**
  * The method defines the correct default settings for the provided FieldDescription
  *
  * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @return void
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     $fieldDescription->setAdmin($admin);
     if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
         list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
         $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
         // set the default field mapping
         if (isset($metadata->fieldMappings[$lastPropertyName])) {
             $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
         }
         // set the default association mapping
         if (isset($metadata->associationMappings[$lastPropertyName])) {
             $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
         }
     }
     if (!$fieldDescription->getType()) {
         throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
     }
     $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
     $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
     if (!$fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
         if (!$fieldDescription->getTemplate()) {
             switch ($fieldDescription->getMappingType()) {
                 case ClassMetadataInfo::MANY_TO_ONE:
                     $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_one.html.twig');
                     break;
                 case ClassMetadataInfo::ONE_TO_ONE:
                     $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_one.html.twig');
                     break;
                 case ClassMetadataInfo::ONE_TO_MANY:
                     $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_one_to_many.html.twig');
                     break;
                 case ClassMetadataInfo::MANY_TO_MANY:
                     $fieldDescription->setTemplate('SonataDoctrineORMAdminBundle:CRUD:show_orm_many_to_many.html.twig');
                     break;
             }
         }
     }
     switch ($fieldDescription->getMappingType()) {
         case ClassMetadataInfo::MANY_TO_ONE:
         case ClassMetadataInfo::ONE_TO_ONE:
         case ClassMetadataInfo::ONE_TO_MANY:
         case ClassMetadataInfo::MANY_TO_MANY:
             $admin->attachAdminClass($fieldDescription);
             break;
     }
 }
 /**
  * {@inheritdoc}
  */
 public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
 {
     $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
     foreach ($this->newLayoutBlockParameters as $attr => $value) {
         $method = sprintf('set%s', $this->camelize($attr));
         $instance->{$method}($value);
     }
     $mapping = $fieldDescription->getAssociationMapping();
     $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
     if (!method_exists($object, $method)) {
         $method = rtrim($method, 's');
         if (!method_exists($object, $method)) {
             throw new \RuntimeException(sprintf('Please add a method %s in the %s class!', $method, get_class($object)));
         }
     }
     $object->{$method}($instance);
 }
 public function testRenderRelationElementWithPropertyPath()
 {
     $this->fieldDescription->expects($this->exactly(1))->method('getOption')->will($this->returnCallback(function ($value, $default = null) {
         if ($value == 'associated_property') {
             return 'foo';
         }
     }));
     $element = new \stdClass();
     $element->foo = 'bar';
     $this->assertEquals('bar', $this->twigExtension->renderRelationElement($element, $this->fieldDescription));
 }
 /**
  * @param $type
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @return array
  */
 public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
 {
     $options = array();
     $options['sonata_field_description'] = $fieldDescription;
     if ($type == 'doctrine_phpcr_type_tree_model') {
         $options['class'] = $fieldDescription->getTargetEntity();
         $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
     }
     if ($type == 'sonata_type_model') {
         $options['class'] = $fieldDescription->getTargetEntity();
         $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager();
         switch ($fieldDescription->getMappingType()) {
             case ClassMetadata::ONE_TO_MANY:
             case ClassMetadata::MANY_TO_MANY:
                 $options['multiple'] = true;
                 $options['parent'] = 'choice';
                 break;
             case ClassMetadata::MANY_TO_ONE:
             case ClassMetadata::ONE_TO_ONE:
                 break;
         }
         if ($fieldDescription->getOption('edit') == 'list') {
             $options['parent'] = 'text';
             if (!array_key_exists('required', $options)) {
                 $options['required'] = false;
             }
         }
     } else {
         if ($type == 'sonata_type_admin') {
             // nothing here ...
             $options['edit'] = 'inline';
         } else {
             if ($type == 'sonata_type_collection') {
                 $options['type'] = 'sonata_type_admin';
                 $options['modifiable'] = true;
                 $options['type_options'] = array('sonata_field_description' => $fieldDescription, 'data_class' => $fieldDescription->getAssociationAdmin()->getClass());
             }
         }
     }
     return $options;
 }
示例#12
0
 /**
  * The method defines the correct default settings for the provided FieldDescription
  *
  * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @return void
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     $fieldDescription->setAdmin($admin);
     if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
         list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName());
         $fieldDescription->setParentAssociationMappings($parentAssociationMappings);
         // set the default field mapping
         if (isset($metadata->fieldMappings[$lastPropertyName])) {
             $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]);
         }
         // set the default association mapping
         if (isset($metadata->associationMappings[$lastPropertyName])) {
             $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]);
         }
     }
     if (!$fieldDescription->getType()) {
         throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
     }
     $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
     $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
     if (!$fieldDescription->getTemplate()) {
         if ($fieldDescription->getType() == 'id') {
             $fieldDescription->setType('string');
         }
         if ($fieldDescription->getType() == 'int') {
             $fieldDescription->setType('integer');
         }
         $template = $this->getTemplate($fieldDescription->getType());
         if ($template === null) {
             if ($fieldDescription->getMappingType() == ClassMetadataInfo::ONE) {
                 $template = 'SonataDoctrineMongoDBAdminBundle:CRUD:show_mongo_one.html.twig';
             } elseif ($fieldDescription->getMappingType() == ClassMetadataInfo::MANY) {
                 $template = 'SonataDoctrineMongoDBAdminBundle:CRUD:show_mongo_many.html.twig';
             }
         }
         $fieldDescription->setTemplate($template);
     }
     if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE, ClassMetadataInfo::MANY))) {
         $admin->attachAdminClass($fieldDescription);
     }
 }
 /**
  * @param FieldDescriptionInterface $fieldDescription
  *
  * @return FieldDescriptionInterface
  */
 public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription)
 {
     if (null === $fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
     }
     if (null === $fieldDescription->getType()) {
         $fieldDescription->setType('action');
     }
     if (null === $fieldDescription->getOption('name')) {
         $fieldDescription->setOption('name', 'Action');
     }
     if (null === $fieldDescription->getOption('code')) {
         $fieldDescription->setOption('code', 'Action');
     }
     if (null !== $fieldDescription->getOption('actions')) {
         $actions = $fieldDescription->getOption('actions');
         foreach ($actions as $k => $action) {
             if (!isset($action['template'])) {
                 $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
             }
         }
         $fieldDescription->setOption('actions', $actions);
     }
     return $fieldDescription;
 }
 /**
  * @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);
 }
 /**
  * {@inheritdoc}
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     if (null === $fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig');
     }
     if (null === $fieldDescription->getType()) {
         $fieldDescription->setType('action');
     }
     if (null === $fieldDescription->getOption('name')) {
         $fieldDescription->setOption('name', 'Action');
     }
     if (null === $fieldDescription->getOption('code')) {
         $fieldDescription->setOption('code', 'Action');
     }
     if (null === $fieldDescription->getOption('header_style') && $fieldDescription->getOption('dropdown')) {
         $fieldDescription->setOption('header_style', 'width:40px');
     }
     if (null !== $fieldDescription->getOption('actions')) {
         $actions = $fieldDescription->getOption('actions');
         foreach ($actions as $k => &$action) {
             //only set the template if really exists
             //set to default any template that not exists
             if (!isset($action['template'])) {
                 if ($fieldDescription->getOption('dropdown')) {
                     $template = sprintf('SonataAdminBundle:CRUD:list__action_dropdown_%s.html.twig', $k);
                 } else {
                     $template = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k);
                 }
                 try {
                     $this->twig->loadTemplate($template);
                 } catch (\Twig_Error_Loader $e) {
                     if ($fieldDescription->getOption('dropdown')) {
                         $template = 'YnloAdminBundle::CRUD/list__action_dropdown_default.html.twig';
                     } else {
                         $template = 'YnloAdminBundle::CRUD/list__action_default.html.twig';
                     }
                 }
                 $action['template'] = $template;
             }
             //set default role
             if (!isset($action['role'])) {
                 $role = strtoupper($k);
                 $action['role'] = $role === 'SHOW' ? 'VIEW' : $role;
             }
             //set default visibility
             if (!isset($action['visible'])) {
                 $action['visible'] = true;
             }
         }
         $fieldDescription->setOption('actions', $actions);
     }
     //hide default label
     if (in_array($fieldDescription->getOption('label'), ['_action', 'Action'])) {
         $fieldDescription->setOption('label', ' ');
     }
     return $fieldDescription;
 }
示例#16
0
 /**
  * {@inheritdoc}
  */
 public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid)
 {
     $values = $datagrid->getValues();
     if ($fieldDescription->getName() == $values['_sort_by']->getName() || $values['_sort_by']->getName() === $fieldDescription->getOption('sortable')) {
         if ($values['_sort_order'] == 'ASC') {
             $values['_sort_order'] = 'DESC';
         } else {
             $values['_sort_order'] = 'ASC';
         }
     } else {
         $values['_sort_order'] = 'ASC';
     }
     $values['_sort_by'] = is_string($fieldDescription->getOption('sortable')) ? $fieldDescription->getOption('sortable') : $fieldDescription->getName();
     return array('filter' => $values);
 }
示例#17
0
 /**
  * {@inheritdoc}
  */
 public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
 {
     $pool = $this->getConfigurationPool();
     $adminCode = $fieldDescription->getOption('admin_code');
     if ($adminCode !== null) {
         $admin = $pool->getAdminByAdminCode($adminCode);
     } else {
         $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
     }
     if (!$admin) {
         return;
     }
     if ($this->hasRequest()) {
         $admin->setRequest($this->getRequest());
     }
     $fieldDescription->setAssociationAdmin($admin);
 }
示例#18
0
 /**
  * return the value related to FieldDescription, if the associated object does no
  * exists => a temporary one is created.
  *
  * @param object                                              $object
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @return mixed
  */
 public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription)
 {
     $value = null;
     if (!$object) {
         return $value;
     }
     try {
         $value = $fieldDescription->getValue($object);
     } catch (NoValueException $e) {
         if ($fieldDescription->getAssociationAdmin()) {
             $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
         }
     }
     return $value;
 }
示例#19
0
 /**
  * Add a new instance to the related FieldDescriptionInterface value.
  *
  * @param object                                              $object
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @throws \RuntimeException
  */
 public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
 {
     $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
     $mapping = $fieldDescription->getAssociationMapping();
     $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
     if (!method_exists($object, $method)) {
         $method = rtrim($method, 's');
         if (!method_exists($object, $method)) {
             $method = sprintf('add%s', $this->camelize(Inflector::singularize($mapping['fieldName'])));
             if (!method_exists($object, $method)) {
                 throw new \RuntimeException(sprintf('Please add a method %s in the %s class!', $method, ClassUtils::getClass($object)));
             }
         }
     }
     $object->{$method}($instance);
 }
示例#20
0
    /**
     * attach an admin instance to the given FieldDescription
     *
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
     */
    public function attachAdminClass(FieldDescriptionInterface $fieldDescription)
    {
        $pool = $this->getConfigurationPool();

        $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
        if (!$admin) {
            return;
        }

        $fieldDescription->setAssociationAdmin($admin);
    }
 /**
  * @param \Sonata\AdminBundle\Admin\AdminInterface            $admin
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     $fieldDescription->setAdmin($admin);
     if (!$fieldDescription->getType()) {
         throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
     }
     // define the template to use
     if (!$fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType()));
     }
     // define code and label
     $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
     $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName()));
 }
    /**
     * @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);
    }
 /**
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @return void
  */
 public function add(FieldDescriptionInterface $fieldDescription)
 {
     $this->elements[$fieldDescription->getName()] = $fieldDescription;
 }
示例#24
0
    /**
     * Add a new instance to the related FieldDescriptionInterface value
     *
     * @param object $object
     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
     * @return void
     */
    public function addNewInstance($object, FieldDescriptionInterface $fieldDescription)
    {
        $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
        $mapping  = $fieldDescription->getAssociationMapping();

        $method = sprintf('add%s', $this->camelize($mapping['fieldName']));

        $object->$method($instance);
    }
 /**
  * {@inheritDoc}
  */
 public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid)
 {
     $values = $datagrid->getValues();
     if ($fieldDescription->getName() == $values['_sort_by']->getName()) {
         if ($values['_sort_order'] == 'ASC') {
             $values['_sort_order'] = 'DESC';
         } else {
             $values['_sort_order'] = 'ASC';
         }
         $values['_sort_by'] = $fieldDescription->getName();
     } else {
         $values['_sort_order'] = 'ASC';
         $values['_sort_by'] = $fieldDescription->getName();
     }
     return array('filter' => $values);
 }
 /**
  * Return xEditable choices based on the field description choices options & catalogue options.
  * With the following choice options:
  *     ['Status1' => 'Alias1', 'Status2' => 'Alias2']
  * The method will return:
  *     [['value' => 'Status1', 'text' => 'Alias1'], ['value' => 'Status2', 'text' => 'Alias2']].
  *
  * @param FieldDescriptionInterface $fieldDescription
  *
  * @return array
  */
 public function getXEditableChoices(FieldDescriptionInterface $fieldDescription)
 {
     $choices = $fieldDescription->getOption('choices', array());
     $catalogue = $fieldDescription->getOption('catalogue');
     $xEditableChoices = array();
     if (!empty($choices)) {
         reset($choices);
         $first = current($choices);
         // the choices are already in the right format
         if (is_array($first) && array_key_exists('value', $first) && array_key_exists('text', $first)) {
             $xEditableChoices = $choices;
         } else {
             foreach ($choices as $value => $text) {
                 $text = $catalogue ? $fieldDescription->getAdmin()->trans($text, array(), $catalogue) : $text;
                 $xEditableChoices[] = array('value' => $value, 'text' => $text);
             }
         }
     }
     return $xEditableChoices;
 }
示例#27
0
    /**
     * 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;
    }
 /**
  * @throws \RunTimeException
  *
  * @param mixed                     $element
  * @param FieldDescriptionInterface $fieldDescription
  *
  * @return mixed
  */
 public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
 {
     if (!is_object($element)) {
         return $element;
     }
     $propertyPath = $fieldDescription->getOption('associated_property');
     if (null === $propertyPath) {
         // For BC kept associated_tostring option behavior
         $method = $fieldDescription->getOption('associated_tostring', '__toString');
         if (!method_exists($element, $method)) {
             throw new \RuntimeException(sprintf('You must define an `associated_property` option or create a `%s::__toString` method to the field option %s from service %s is ', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode()));
         }
         return call_user_func(array($element, $method));
     }
     return PropertyAccess::createPropertyAccessor()->getValue($element, $propertyPath);
 }
 /**
  * @throws \RunTimeException
  *
  * @param mixed                                               $element
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @return mixed
  */
 public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription)
 {
     $method = $fieldDescription->getOption('associated_tostring', '__toString');
     if (!is_object($element)) {
         return $element;
     }
     if (!method_exists($element, $method)) {
         throw new \RunTimeException(sprintf('You must define an `associated_tostring` option or create a `%s::__toString` method to the field option %s from service %s is ', get_class($element), $fieldDescription->getName(), $fieldDescription->getAdmin()->getCode()));
     }
     return call_user_func(array($element, $method));
 }
 /**
  * {@inheritdoc}
  */
 public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
 {
     if (null === $fieldDescription->getTemplate()) {
         $fieldDescription->setTemplate('YnloAdminBundle::CRUD/list__details.html.twig');
     }
     if (null === $fieldDescription->getType()) {
         $fieldDescription->setType('_details');
     }
     if (null === $fieldDescription->getOption('name')) {
         $fieldDescription->setOption('name', 'Details');
     }
     if (null === $fieldDescription->getOption('header_style')) {
         $fieldDescription->setOption('header_style', 'width:20px');
     }
     if (null === $fieldDescription->getOption('details_template')) {
         throw new \LogicException('The option "details_template" is required');
     }
     //encode the template
     $template = base64_encode($fieldDescription->getOption('details_template'));
     $fieldDescription->setOption('details_template_encoded', $template);
     if (null === $fieldDescription->getOption('ajax')) {
         $fieldDescription->setOption('ajax', false);
     }
     if (null === $fieldDescription->getOption('code')) {
         $fieldDescription->setOption('code', '_details');
     }
     //hide default label
     if ($fieldDescription->getOption('label') == '_details' || $fieldDescription->getOption('label') == 'Details') {
         $fieldDescription->setOption('label', ' ');
     }
 }