Esempio n. 1
0
 /**
  * Add relations to the form.
  *
  * @param FormBuilderInterface $builder
  */
 public function addRelations(FormBuilderInterface $builder)
 {
     $relations = $this->entityHelper->getRelations($this->object);
     foreach ($relations as $key => $relation) {
         if (!$this->isAllowed($relation['fieldName'])) {
             continue;
         }
         if ($relation['isOwningSide'] === false) {
             $builder->add($relation['fieldName'], 'collapsible_collection', ['allow_add' => true, 'allow_delete' => true, 'type' => new CrudRelationType($this->entityHelper, $relation['targetEntity'], $this->annotationReader), 'by_reference' => false]);
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function buildRules()
 {
     $rules = [];
     foreach ($this->entityHelper->getProperties($this->context) as $property) {
         $condition = new AttributeCondition();
         $condition->setName(ucfirst($property['fieldName']))->setEntity($this->context)->setAttribute($property['fieldName'])->setType($property['type']);
         $rules[] = $condition;
     }
     foreach ($this->entityHelper->getRelations($this->context) as $key => $relation) {
         foreach ($this->entityHelper->getProperties($relation['targetEntity']) as $relProperty) {
             $condition = new RelationCondition();
             $condition->setName(ucfirst($key) . ' ' . ucfirst($relProperty['fieldName']))->setRelation($key)->setEntity($relation['targetEntity'])->setAttribute($relProperty['fieldName'])->setType($relProperty['type']);
             $rules[] = $condition;
         }
     }
     return $rules;
 }
Esempio n. 3
0
 /**
  * Handle a ListView form
  *
  * @param  Request  $request
  * @param  Datagrid $datagrid
  *
  * @return \Symfony\Component\Form\FormView
  */
 public function handleForm(Request $request, Datagrid $datagrid)
 {
     // Clone the view, so the current view won't get changed by the empty view form
     $view = clone $datagrid->getView();
     $columns = $this->entityHelper->getAllProperties($view->getEntity());
     $type = new ListViewType($datagrid->getSource(), $columns);
     $viewForm = $this->formFactory->create($type, $view);
     $viewForm->handleRequest($request);
     if ($viewForm->get('save')->isClicked()) {
         if ($viewForm->isValid()) {
             $this->save($view);
             $request->request->set('view', $view->getSlug());
         } else {
             throw new \Exception('The view could not be saved because the form was invalid.');
         }
     }
     return $viewForm->createView();
 }
Esempio n. 4
0
 /**
  * Builds the relations properly so all relations can get saved properly
  *
  * @param $relations
  * @param $originalRelations
  * @param $entity
  */
 public function setRelations($relations, $originalRelations, $entity)
 {
     foreach ($relations as $key => $relation) {
         if ($relation['isOwningSide'] === false) {
             // Set getters and setters
             $getRelations = 'get' . ucfirst($relation['fieldName']);
             $setRelation = 'set' . ucfirst($relation['mappedBy']);
             $getBackReference = 'get' . ucfirst($relation['mappedBy']);
             // Connect the added relations
             foreach ($entity->{$getRelations}() as $relationEntity) {
                 if ($relationEntity->{$getBackReference}() instanceof Collection) {
                     if (!$relationEntity->{$getBackReference}()->contains($entity)) {
                         $relationEntity->{$getBackReference}()->add($entity);
                     }
                 } else {
                     $relationEntity->{$setRelation}($entity);
                 }
                 $relationRelations = $this->entityHelper->getRelations($relationEntity);
                 $originalRelationsForEntity = count($originalRelations) > 0 && array_key_exists($relationEntity->getId(), $originalRelations[$key]) ? $originalRelations[$key][$relationEntity->getId()]['relations'] : [];
                 $this->setRelations($relationRelations, $originalRelationsForEntity, $relationEntity);
             }
             // Disconnect the removed relations
             if (array_key_exists($key, $originalRelations)) {
                 foreach ($originalRelations[$key] as $relationEntityArray) {
                     if (false === $entity->{$getRelations}()->contains($relationEntityArray['entity'])) {
                         if ($relationEntityArray['entity']->{$getBackReference}() instanceof Collection) {
                             $relationEntityArray['entity']->{$getBackReference}()->removeElement($relationEntityArray['entity']);
                         } else {
                             $relationEntityArray['entity']->{$setRelation}(null);
                         }
                         $this->em->persist($relationEntityArray['entity']);
                     }
                 }
             }
         }
     }
 }
Esempio n. 5
0
 /**
  * {@inheritDoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $allowedProperties = $this->annotationReader->getEditableProperties($options['data']);
     $transformer = new DoctrineTypeTransformer();
     // Get the standard property form fields
     foreach ($this->entityHelper->getProperties($options['data']) as $property) {
         if (count($allowedProperties) && !in_array($property['fieldName'], $allowedProperties)) {
             continue;
         }
         if ($propertyType = $this->annotationReader->getPropertyType($options['data'], $property['fieldName'])) {
             $builder->add($property['fieldName'], $propertyType);
         } elseif ($property['type'] == 'boolean') {
             $builder->add($property['fieldName'], $transformer->transform($property['type']), ['attr' => ['align_with_widget' => true]]);
         } elseif ($property['type'] == 'json_array') {
             $builder->add($builder->create($property['fieldName'], 'textarea')->addModelTransformer(new ArrayToJsonTransformer()));
         } elseif ($property['type'] == 'simple_array') {
             $builder->add($builder->create($property['fieldName'], 'bootstrap_collection', ['allow_add' => true, 'allow_delete' => true, 'add_button_text' => 'Add', 'delete_button_text' => 'Delete', 'sub_widget_col' => 8, 'button_col' => 4]));
         } else {
             $builder->add($property['fieldName'], $transformer->transform($property['type']));
         }
     }
     // Get the relations' form fields
     foreach ($this->entityHelper->getRelations($options['data']) as $key => $relation) {
         if (count($allowedProperties) && !in_array($relation['fieldName'], $allowedProperties)) {
             continue;
         }
         if ($relation['isOwningSide'] === false) {
             $builder->add($relation['fieldName'], 'bootstrap_collection', ['allow_add' => true, 'allow_delete' => true, 'type' => new CrudRelationType($this->entityHelper, $relation['targetEntity'], $this->annotationReader), 'by_reference' => false]);
         } elseif ($relation['targetEntity'] == $this->valuesetClass) {
             $builder->add($relation['fieldName'], 'opifer_valueset');
         } else {
             $builder->add($relation['fieldName'], new Select2Type('entity'), ['class' => $relation['targetEntity'], 'property' => 'name', 'empty_value' => '(empty)', 'empty_data' => null]);
         }
     }
     $builder->add('save', 'submit', ['label' => 'Save']);
 }