public function create($class_name, $property_name, \Symforce\AdminBundle\Compiler\Annotation\Form $annot, \Symforce\AdminBundle\Compiler\MetaType\PropertyContainer $parent, \Symforce\AdminBundle\Compiler\MetaType\Admin\Entity $entity = null)
 {
     $om = $this->doctrine->getManagerForClass($class_name);
     if (!$om) {
         throw new \Exception(sprintf("%s->%s has no orm", $class_name, $property_name));
     }
     $meta = $om->getClassMetadata($class_name);
     if (!$meta) {
         throw new \Exception(sprintf("%s->%s has no orm", $class_name, $property_name));
     }
     $map = null;
     if ($meta->hasAssociation($property_name)) {
         $map = $meta->getAssociationMapping($property_name);
     }
     $orm_type = $meta->getTypeOfField($property_name);
     $form_type = $annot->type;
     if ($entity) {
         if ($entity->class_name !== $class_name) {
             throw new \Exception(sprintf("%s->%s not match admin(%s) ", $class_name, $property_name, $entity->admin_name));
         }
     } else {
         if (!$this->gen->hasAdminClass($class_name)) {
             throw new \Exception(sprintf("%s->%s not find admin", $class_name, $property_name));
         }
         $entity = $this->gen->getAdminByClass($class_name);
     }
     if ($form_type) {
         if ('workflow' === $form_type) {
             if (!$entity->workflow) {
                 throw new \Exception(sprintf("%s->%s use form type:%s without define workflow", $class_name, $property_name, $form_type));
             } else {
                 if ($entity->workflow->property !== $property_name) {
                     throw new \Exception(sprintf("%s->%s use form type:%s, but workflow property is %s", $class_name, $property_name, $form_type, $entity->workflow->property));
                 }
             }
         }
         if ('owner' === $form_type) {
             if (!$entity->owner) {
                 throw new \Exception(sprintf("%s->%s use form type:%s without define owner", $class_name, $property_name, $form_type));
             } else {
                 if ($entity->owner->owner_property !== $property_name) {
                     throw new \Exception(sprintf("%s->%s use form type:%s, but owner property is %s", $class_name, $property_name, $form_type, $entity->owner->owner_property));
                 }
             }
         }
         if ($map) {
             if ($map['targetEntity'] !== $this->types[$form_type]['map'] && '*' !== $this->types[$form_type]['map']) {
                 if ($this->types[$form_type]['map']) {
                     throw new \Exception(sprintf("%s->%s use form type:%s with orm map type:%s, form type not accept orm map", $class_name, $property_name, $form_type, $map['targetEntity']));
                 } else {
                     throw new \Exception(sprintf("%s->%s use form type:%s with orm map type:%s, only accept orm map:(%s)", $class_name, $property_name, $form_type, $map['targetEntity'], $this->types[$form_type]['map']));
                 }
             }
         } else {
             if ($orm_type && !in_array($orm_type, $this->types[$form_type]['orm'])) {
                 throw new \Exception(sprintf("%s->%s use form type:%s with orm type:%s, only accept orm:(%s)", $class_name, $property_name, $form_type, $orm_type, join(',', $this->types[$form_type]['orm'])));
             }
         }
     } else {
         if ($entity->workflow && $entity->workflow->property === $property_name) {
             $form_type = 'workflow';
         } else {
             if ($entity->owner && $entity->owner->owner_property === $property_name) {
                 $form_type = 'owner';
             }
         }
         if (!$form_type) {
             if ($map) {
                 // $map['targetEntity']
                 foreach ($this->types as $_type_name => $_type) {
                     if ($map['targetEntity'] === $_type['map']) {
                         $form_type = $_type_name;
                         break;
                     }
                 }
                 if (!$form_type) {
                     // check if it is embed type
                     $cache = $this->gen->getAnnotationCache($class_name);
                     if (isset($cache->propertie_annotations[$property_name]['properties'])) {
                         $form_type = 'embed';
                     } else {
                         $form_type = 'entity';
                     }
                 }
             } else {
                 if ('string' === $orm_type) {
                     foreach ($this->guess as $keyword => $_type) {
                         if (false !== strpos($property_name, $keyword)) {
                             $form_type = $_type;
                             break;
                         }
                     }
                 } else {
                     if (isset($this->default_type[$orm_type])) {
                         $form_type = $this->default_type[$orm_type];
                     }
                     foreach ($this->types as $_type_name => $_type) {
                         if (!$_type['map'] && in_array($orm_type, $_type['orm'])) {
                             $form_type = $_type_name;
                             break;
                         }
                     }
                 }
                 if (!$form_type) {
                     $form_type = 'text';
                 }
             }
         }
     }
     if (!isset($this->types[$form_type]['class'])) {
         throw new \Exception(sprintf("%s->%s with form(%s) no class ", $class_name, $property_name, $form_type));
     }
     $form_class = $this->types[$form_type]['class'];
     /*
             if( !($form_class instanceof \Symforce\AdminBundle\Compiler\MetaType\Form\Element) ) {
                 throw new \Exception(sprintf("`%s` is invalid ", $form_class));
             }*/
     $form_element = new $form_class($parent, $entity, $property_name, $annot);
     $form_element->compile_meta_type = $form_type;
     $form_element->compile_form_type = $this->types[$form_type]['type'];
     $form_element->compile_orm_type = $orm_type;
     if ($meta->hasField($property_name)) {
         if ($meta->isUniqueField($property_name)) {
             if (null === $annot->unique) {
                 $form_element->unique = true;
             }
         }
     }
     if ($form_element->unique) {
         $cache = $this->gen->getAnnotationCache($class_name);
         if (isset($cache->propertie_annotations[$property_name])) {
             // $cache->propertie_annotations[$property_name]['Gedmo\Mapping\Annotation\Translatable']
             if (isset($cache->propertie_annotations[$property_name]['Gedmo\\Mapping\\Annotation\\Slug'])) {
                 if (null === $annot->unique) {
                     $form_element->unique = false;
                 }
                 if (null === $annot->required) {
                     $form_element->required = false;
                 }
             }
         }
     }
     return $form_element;
 }
Beispiel #2
0
 public function getPropertyLabel($property)
 {
     if (!isset($this->_property_label[$property])) {
         if (!$this->form_initialized) {
             throw new \Exception("bigger error!");
         }
         if ($this->form->children->hasProperty($property)) {
             $label = $this->form->children->getProperty($property)->label;
             // $this->debug("%s , %s, %s", $property , $label->getDomain(), $label->getPath() ) ;
         } else {
             $map = $this->getPropertyDoctrineAssociationMapping($property);
             if ($map && $this->generator->hasAdminClass($map['targetEntity'])) {
                 $admin = $this->generator->getAdminByClass($map['targetEntity']);
                 $label = $admin->getLabel();
             } else {
                 $label = $this->generator->getTransValue('property', $property . '.label');
             }
             // $this->debug("%s , %s, %s", $property , $label->getDomain(), $label->getPath() ) ;
         }
         $this->_property_label[$property] = $label;
     }
     return $this->_property_label[$property];
 }