123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\BaseApplicationBundle\Admin;
- use Symfony\Component\DependencyInjection\ContainerAware;
- use Symfony\Component\Form\Form;
- use Doctrine\ORM\Mapping\ClassMetadataInfo;
- use Sonata\BaseApplicationBundle\Tool\Datagrid;
- abstract class EntityAdmin extends Admin
- {
- /**
- * make sure the base fields are set in the correct format
- *
- * @param $selected_fields
- * @return array
- */
- static public function getBaseFields(ClassMetadataInfo $metadata, $selectedFields)
- {
- // if nothing is defined we display all fields
- if (!$selectedFields) {
- $selectedFields = array_keys($metadata->reflFields) + array_keys($metadata->associationMappings);
- }
- $fields = array();
- // make sure we works with array
- foreach ($selectedFields as $name => $options) {
- $description = new FieldDescription;
- if (is_array($options)) {
- // remove property value
- if (isset($options['type'])) {
- $description->setType($options['type']);
- unset($options['type']);
- }
- // remove property value
- if (isset($options['template'])) {
- $description->setTemplate($options['template']);
- unset($options['template']);
- }
- $description->setOptions($options);
- } else {
- $name = $options;
- }
- $description->setName($name);
- if (isset($metadata->fieldMappings[$name])) {
- $description->setFieldMapping($metadata->fieldMappings[$name]);
- }
- if (isset($metadata->associationMappings[$name])) {
- $description->setAssociationMapping($metadata->associationMappings[$name]);
- }
- $fields[$name] = $description;
- }
- return $fields;
- }
- /**
- * return the entity manager
- *
- * @return EntityManager
- */
- public function getEntityManager()
- {
- return $this->container->get('doctrine.orm.default_entity_manager');
- }
- /**
- * build the field to use in the list view
- *
- * @return void
- */
- protected function buildListFields()
- {
- if ($this->loaded['list_fields']) {
- return;
- }
- $this->loaded['list_fields'] = true;
- $this->listFields = self::getBaseFields($this->getClassMetaData(), $this->listFields);
- // normalize field
- foreach ($this->listFields as $name => $fieldDescription) {
- $fieldDescription->setOption('code', $fieldDescription->getOption('code', $name));
- $fieldDescription->setOption('label', $fieldDescription->getOption('label', $name));
- // set the default type if none is set
- if (!$fieldDescription->getType()) {
- $fieldDescription->setType('string');
- }
- $fieldDescription->setAdmin($this);
- if (!$fieldDescription->getTemplate()) {
- $fieldDescription->setTemplate(sprintf('SonataBaseApplicationBundle:CRUD:list_%s.twig.html', $fieldDescription->getType()));
- if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_ONE) {
- $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:list_many_to_one.twig.html');
- $this->attachAdminClass($fieldDescription);
- }
- if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_ONE) {
- $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:list_one_to_one.twig.html');
- $this->attachAdminClass($fieldDescription);
- }
- if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_MANY) {
- $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:list_one_to_many.twig.html');
- $this->attachAdminClass($fieldDescription);
- }
- if ($fieldDescription->getType() == ClassMetadataInfo::MANY_TO_MANY) {
- $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:list_many_to_many.twig.html');
- $this->attachAdminClass($fieldDescription);
- }
- }
- }
- $this->configureListFields();
- if (!isset($this->listFields['_batch'])) {
- $fieldDescription = new FieldDescription();
- $fieldDescription->setOptions(array(
- 'label' => 'batch',
- 'code' => '_batch'
- ));
- $fieldDescription->setTemplate('SonataBaseApplicationBundle:CRUD:list__batch.twig.html');
- $this->listFields = array( '_batch' => $fieldDescription ) + $this->listFields;
- }
- return $this->listFields;
- }
- /**
- * return the list of choices for one entity
- *
- * @param FieldDescription $description
- * @return array
- */
- protected function getChoices(FieldDescription $description, $prependChoices = array())
- {
- /*
- UNUSED RIGHT NOW
- if (!isset($this->choicesCache[$description->getTargetEntity()])) {
- $targets = $this->getEntityManager()
- ->createQueryBuilder()
- ->select('t')
- ->from($description->getTargetEntity(), 't')
- ->getQuery()
- ->execute();
- $choices = array();
- foreach ($targets as $target) {
- // todo : puts this into a configuration option and use reflection
- foreach (array('getTitle', 'getName', '__toString') as $getter) {
- if (method_exists($target, $getter)) {
- $choices[$target->getId()] = $target->$getter();
- break;
- }
- }
- }
- $this->choicesCache[$description->getTargetEntity()] = $choices;
- }
- return $prependChoices + $this->choicesCache[$description->getTargetEntity()];
- */
- }
- /**
- * return the field associated to a FieldDescription
- * ie : build the embedded form from the related Admin instance
- *
- * @throws RuntimeException
- * @param $object
- * @param FieldDescription $fieldDescription
- * @param null $fieldName
- * @return FieldGroup
- */
- protected function getRelatedAssociatedField($object, FieldDescription $fieldDescription, $fieldName = null)
- {
- /*
- UNUSED RIGHT NOW
- $fieldName = $fieldName ?: $fieldDescription->getFieldName();
- $associatedAdmin = $fieldDescription->getAssociationAdmin();
- if (!$associatedAdmin) {
- throw new \RuntimeException(sprintf('inline mode for field `%s` required an Admin definition', $fieldName));
- }
- // retrieve the related object
- $targetObject = $associatedAdmin->getNewInstance();
- // retrieve the related form
- $targetFields = $associatedAdmin->getFormFields();
- $targetForm = $associatedAdmin->getForm($targetObject, $targetFields);
- // create the transformer
- $transformer = new \Sonata\BaseApplicationBundle\Form\ValueTransformer\ArrayToObjectTransformer(array(
- 'em' => $this->getEntityManager(),
- 'className' => $fieldDescription->getTargetEntity()
- ));
- // create the "embedded" field
- if ($fieldDescription->getType() == ClassMetadataInfo::ONE_TO_MANY) {
- $field = new \Sonata\BaseApplicationBundle\Form\EditableFieldGroup($fieldName, array(
- 'value_transformer' => $transformer,
- ));
- } else {
- $field = new \Symfony\Component\Form\FieldGroup($fieldName, array(
- 'value_transformer' => $transformer,
- ));
- }
- foreach ($targetForm->getFields() as $name => $formField) {
- if ($name == '_token') {
- continue;
- }
- $field->add($formField);
- }
- return $field;
- */
- }
- /**
- * return the class associated to a FieldDescription
- *
- * @throws RuntimeException
- * @param FieldDescription $fieldDescription
- * @return bool
- */
- public function getFormFieldClass(FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- $class = isset($this->formFieldClasses[$fieldDescription->getType()]) ? $this->formFieldClasses[$fieldDescription->getType()] : false;
- $class = $fieldDescription->getOption('form_field_widget', $class);
- if(!$class) {
- throw new \RuntimeException(sprintf('unknow type `%s`', $fieldDescription->getType()));
- }
- if(!class_exists($class)) {
- throw new \RuntimeException(sprintf('The class `%s` does not exist for field `%s`', $class, $fieldDescription->getType()));
- }
- return $class;
- */
- }
- /**
- * Add a new instance to the related FieldDescription value
- *
- * @param $object
- * @param FieldDescription $fieldDescription
- * @return void
- */
- public function addNewInstance($object, FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
- $mapping = $fieldDescription->getAssociationMapping();
- $method = sprintf('add%s', FieldDescription::camelize($mapping['fieldName']));
- $object->$method($instance);
- */
- }
- /**
- * return an OneToOne associated field
- *
- * @param $object
- * @param FieldDescription $fieldDescription
- * @return ChoiceField
- */
- protected function getOneToOneField($object, FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- // tweak the widget depend on the edit mode
- if ($fieldDescription->getOption('edit') == 'inline') {
- return $this->getRelatedAssociatedField($object, $fieldDescription);
- }
- $options = array(
- 'value_transformer' => new \Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer(array(
- 'em' => $this->getEntityManager(),
- 'className' => $fieldDescription->getTargetEntity()
- ))
- );
- $options = array_merge($options, $fieldDescription->getOption('form_field_options', array()));
- if ($fieldDescription->getOption('edit') == 'list') {
- return new \Symfony\Component\Form\TextField($fieldDescription->getFieldName(), $options);
- }
- $class = $fieldDescription->getOption('form_field_widget', 'Symfony\\Component\\Form\\ChoiceField');
- // set valid default value
- if ($class == 'Symfony\\Component\\Form\\ChoiceField') {
- $choices = array();
- if($fieldDescription->getOption('add_empty', false)) {
- $choices = array(
- $fieldDescription->getOption('add_empty_value', '') => $fieldDescription->getOption('add_empty_value', '')
- );
- }
- $options = array_merge(array(
- 'expanded' => false,
- 'choices' => $this->getChoices($fieldDescription, $choices),
- ), $options);
- }
- return new $class($fieldDescription->getFieldName(), $options);
- */
- }
- /**
- * return the OneToMany associated field
- *
- * @param $object
- * @param FieldDescription $fieldDescription
- * @return ChoiceField|CollectionField
- */
- protected function getOneToManyField($object, FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- if ($fieldDescription->getOption('edit') == 'inline') {
- $prototype = $this->getRelatedAssociatedField($object, $fieldDescription);
- $value = $fieldDescription->getValue($object);
- // add new instances if the min number is not matched
- if ($fieldDescription->getOption('min', 0) > count($value)) {
- $diff = $fieldDescription->getOption('min', 0) - count($value);
- foreach (range(1, $diff) as $i) {
- $this->addNewInstance($object, $fieldDescription);
- }
- }
- // use custom one to expose the newfield method
- return new \Sonata\BaseApplicationBundle\Form\EditableCollectionField($prototype);
- }
- return $this->getManyToManyField($object, $fieldDescription);
- */
- }
- protected function getManyToManyField($object, FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- $options = array(
- 'value_transformer' => new \Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\CollectionToChoiceTransformer(array(
- 'em' => $this->getEntityManager(),
- 'className' => $fieldDescription->getTargetEntity()
- ))
- );
- $options = array_merge($options, $fieldDescription->getOption('form_field_options', array()));
- $class = $fieldDescription->getOption('form_field_widget', 'Symfony\\Component\\Form\\ChoiceField');
- // set valid default value
- if ($class == 'Symfony\\Component\\Form\\ChoiceField') {
- $choices = array();
- if($fieldDescription->getOption('add_empty', false)) {
- $choices = array(
- $fieldDescription->getOption('add_empty_value', '') => $fieldDescription->getOption('add_empty_value', '')
- );
- }
- $options = array_merge(array(
- 'expanded' => true,
- 'multiple' => true,
- 'choices' => $this->getChoices($fieldDescription, $choices),
- ), $options);
- }
- return new $class($fieldDescription->getFieldName(), $options);
- */
- }
- protected function getManyToOneField($object, FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- // tweak the widget depend on the edit mode
- if ($fieldDescription->getOption('edit') == 'inline') {
- return $this->getRelatedAssociatedField($object, $fieldDescription);
- }
- $options = array(
- 'value_transformer' => new \Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer(array(
- 'em' => $this->getEntityManager(),
- 'className' => $fieldDescription->getTargetEntity()
- ))
- );
- $options = array_merge($options, $fieldDescription->getOption('form_field_options', array()));
- if ($fieldDescription->getOption('edit') == 'list') {
- return new \Symfony\Component\Form\TextField($fieldDescription->getFieldName(), $options);
- }
- $class = $fieldDescription->getOption('form_field_widget', 'Symfony\\Component\\Form\\ChoiceField');
- // set valid default value
- if ($class == 'Symfony\\Component\\Form\\ChoiceField') {
- $choices = array();
- if($fieldDescription->getOption('add_empty', false)) {
- $choices = array(
- $fieldDescription->getOption('add_empty_value', '') => $fieldDescription->getOption('add_empty_value', '')
- );
- }
- $options = array_merge(array(
- 'expanded' => false,
- 'choices' => $this->getChoices($fieldDescription, $choices),
- ), $options);
- }
- return new $class($fieldDescription->getFieldName(), $options);
- */
- }
- protected function getFormFieldInstance($object, FieldDescription $fieldDescription)
- {
- /*
- UNUSED RIGHT NOW
- switch ($fieldDescription->getType()) {
- case ClassMetadataInfo::ONE_TO_MANY:
- return $this->getOneToManyField($object, $fieldDescription);
- case ClassMetadataInfo::MANY_TO_MANY:
- return $this->getManyToManyField($object, $fieldDescription);
- case ClassMetadataInfo::MANY_TO_ONE:
- return $this->getManyToOneField($object, $fieldDescription);
- case ClassMetadataInfo::ONE_TO_ONE:
- return $this->getOneToOneField($object, $fieldDescription);
- default:
- $class = $this->getFormFieldClass($fieldDescription);
- $options = $fieldDescription->getOption('form_field_options', array());
- return new $class($fieldDescription->getFieldName(), $options);
- }
- */
- }
- }
|