Example #1
0
 /**
  * Build errormessages and add rules
  *
  * @param $modelName
  * @return $this
  */
 public function getFormFieldValidation($modelName)
 {
     $formField = $this->formField->setClassName($modelName);
     $errorMessages = [];
     $defaultLocale = $this->locale->getDefaultLocale();
     foreach ($formField->getFields() as $field => $annotation) {
         if (isset($annotation['validation'])) {
             foreach ($annotation['validation'] as $validationRule) {
                 $value = isset($this->fieldsArray[$field]) ? $this->fieldsArray[$field] : '';
                 $formFieldAnnotationTranslation = $formField->getAnnotation($annotation['annotations'], 'Gedmo\\Mapping\\Annotation\\Translatable');
                 if (false !== $formFieldAnnotationTranslation && is_array($value)) {
                     $translation = isset($value[$defaultLocale->locale]) ? $value[$defaultLocale->locale] : '';
                     $this->fieldsArray[$field] = $translation;
                     // set default translation
                     $errorMessages[$field][$validationRule] = $formField->getErrorMessage($field, $translation, $modelName, $validationRule);
                 } elseif (is_array($value)) {
                     $errorMessages[$field][$validationRule] = $formField->getErrorMessage($field, implode(',', $value), $modelName, $validationRule);
                 } elseif (is_string($value)) {
                     $errorMessages[$field][$validationRule] = $formField->getErrorMessage($field, $value, $modelName, $validationRule);
                 }
                 if ($validationRule === 'unique' && !isset($this->fieldsArray['id'])) {
                     $this->addRule($field, [$this->formField, 'uniqueEntityCheck'], [$modelName, $field]);
                 } elseif ($validationRule !== 'unique') {
                     $this->addRule($field, $validationRule);
                 }
             }
         }
     }
     $this->setErrorMessages($errorMessages);
     return $this;
 }
 /**
  * @return bool|mixed
  */
 public function getContent()
 {
     if ($this->user->isAdmin() === false) {
         return false;
     }
     $currentEntity = false;
     $errors = false;
     $data = array();
     $formFields = array();
     $entities = array();
     $modelClass = false;
     $model = false;
     $modelName = $this->request->gp('model', false);
     $groupedModels = $this->db->getRepository('\\Fraym\\EntityManager\\Entity\\Group')->findAll();
     if ($modelName) {
         $model = $this->entityManager->getEntityByStringOrId($modelName);
         if ($model) {
             $modelClass = $model->className;
         }
     }
     if ($this->request->isXmlHttpRequest()) {
         return $this->createEntityFromSingleField($modelClass);
     }
     if ($modelClass && $this->request->isPost()) {
         $data = $this->request->getGPAsArray();
         $validation = $this->validation->setData($data)->getFormFieldValidation($modelClass);
         if ($id = $this->request->post('id')) {
             $currentEntity = $this->db->getRepository($modelClass)->findOneById($id);
             if (isset($data['cmd']) && $data['cmd'] == 'update' && $currentEntity && ($errors = $validation->check()) === true) {
                 $currentEntity->updateEntity($data);
             } elseif (isset($data['cmd']) && $data['cmd'] == 'remove' && $currentEntity) {
                 $this->db->remove($currentEntity);
                 $this->db->flush();
                 $data = array();
                 $currentEntity = false;
             } elseif (isset($data['cmd']) && $data['cmd'] == 'update') {
                 $currentEntity->updateEntity($data, false);
             }
         } else {
             if (isset($data['cmd']) && $data['cmd'] == 'new' && ($errors = $validation->check()) === true) {
                 $currentEntity = new $modelClass();
                 $currentEntity->updateEntity($data);
             }
         }
     }
     if ($modelClass && $model) {
         $entities = $this->db->getRepository($modelClass)->findAll();
         $formFields = $this->formField->setClassName($modelClass)->getFields();
     }
     $this->view->assign('locales', $this->locale->getLocales());
     $this->view->assign('data', $data);
     $this->view->assign('errors', $errors);
     $this->view->assign('currentEntity', $currentEntity);
     $this->view->assign('entities', $entities);
     $this->view->assign('groupedModels', $groupedModels);
     $this->view->assign('model', $model);
     $this->view->assign('formFields', $formFields);
     return $this->siteManagerController->getIframeContent($this->template->fetch('EntityView'));
 }