entityManager = $_container->get('doctrine.orm.entity_manager'); $this->restifier = $_container->get('net15.restifier.process'); $this->class = $_class; $this->fieldValueManager = $_container->get('net15.form.forge.field.value.manager'); } public function createCustomFieldFromArray(array $_customField){ $formForgeField = new FormForgeField(); if(!$this->restifier->hydrateFromArray($formForgeField,$_customField,['type','attributes','elId','label','required','order'])) return null; return $formForgeField; } /** * @param FormForgeFieldInterface $tt * @return bool */ public function quickUpdate(FormForgeFieldInterface $tt){ if(empty($tt)) return false; try { $this->entityManager->persist($tt); $this->entityManager->flush(); } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeFieldValueInterface $tt * @return bool */ public function quickUpdateValue(FormForgeFieldValueInterface $tt){ if(empty($tt)) return false; try { $this->entityManager->persist($tt); $this->entityManager->flush(); } catch (ORMException $e) { return false; } return true; } /** * @param FormForgeFieldInterface $element * @return bool */ public function update(FormForgeFieldInterface $element){ if(empty($element)) return false; /* do some shit in here */ return $this->quickUpdate($element); } public function deleteField(string $publicKey){ $element = $this->getByPublicKey($publicKey); if($this->entityManager->contains($element)){ try { $this->entityManager->remove($element); $this->entityManager->flush(); } catch (ORMException $e) { return false; } } return $element; } /** * @param FormForgeFieldValueInterface $tt * @return bool */ public function deleteValue(FormForgeFieldValueInterface $tt){ if($this->entityManager->contains($tt)){ try { $this->entityManager->remove($tt); $this->entityManager->flush(); } catch (ORMException $e) { return false; } } return true; } /** * @param FormForgeReasonInterface $_reason * @param string $_label * @param string $_local * @param FormForgeType $_type * @param string $_required * @param int $_order * @param string $_tag * @param string $_smallClass * @param string $_mediumClass * @param string $_largeClass * @param string $_xLargeClass * @param string $parameter * @return FormForgeField|null */ public function create( FormForgeReasonInterface $_reason, string $_label, string $_local, FormForgeType $_type, string $_required, int $_order, string $_tag, string $_smallClass, string $_mediumClass, string $_largeClass, string $_xLargeClass, string $parameter=null ){ $newField = new FormForgeField(); $required = ($_required == 'true') ? true : false; $name = new LinguisticaTradItem(); $name->addStrValues($_label, $_local); $newField->setDefault($_reason, $name, $_type, $required, $_order, $_tag,$_smallClass, $_mediumClass, $_largeClass, $_xLargeClass); $newField->setParameter($parameter); if(!$this->quickUpdate($newField)){ return null; } return $newField; } /** * @param string $publicKey * @param string $label * @param string $local * @param FormForgeType $type * @param string $_required * @param string $_tag * @param string $_smallClass * @param string $_mediumClass * @param string $_largeClass * @param string $get_parameter * @return FormForgeField|null */ public function updateFormForgeField(string $publicKey, string $label, string $local, FormForgeType $type, string $_required, string $_tag, string $_smallClass, string $_mediumClass, string $_largeClass, $_xLargeClass, string $get_parameter=null){ $required = ($_required == 'true') ? true : false; $formForgeField = $this->getByPublicKey($publicKey); $formForgeField->getLabel()->addStrValues($label, $local); $formForgeField->setFormForgeType($type); $formForgeField->setRequired($required); $formForgeField->setFieldTag($_tag); $formForgeField->setSmall($_smallClass); $formForgeField->setMedium($_mediumClass); $formForgeField->setLarge($_largeClass); $formForgeField->setXLarge($_xLargeClass); $formForgeField->setParameter($get_parameter); if(!$this->quickUpdate($formForgeField)){ return null; } return $formForgeField; } /** * @param string $publicKey * @return FormForgeField|null */ public function getByPublicKey(string $publicKey): ?FormForgeField{ $field = $this->entityManager->getRepository($this->class)->findOneBy(['publicKey'=>$publicKey]); return $field; } /** * @param string $publicKey * @return FormForgeFieldValueInterface|null */ public function getCustomFieldValueByPublicKey(string $publicKey): ?FormForgeFieldValueInterface{ return $this->fieldValueManager->getCustomFieldValueByPublicKey($publicKey); } /** * @param string $publicKey * @param $label * @param $local * @return FormForgeField */ public function addCustomFiedValue(string $publicKey, $label, $local): FormForgeField{ $fieldValue = new FormForgeFieldValue(); $formField = $this->getByPublicKey($publicKey); $order = count($formField->getValues()); $fieldValue->setFormForgeField($formField); $fieldValue->addStrValues($label, $local); $fieldValue->setOrder($order); $fieldValue->setDeleted(0); if(!$this->quickUpdateValue($fieldValue)){ return null; } $formField->addValue($fieldValue); if(!$this->quickUpdate($formField)){ return null; } return $formField; } /** * @param string $publicKey * @param $label * @param $local * @return null */ public function updateCustomFieldValue(string $publicKey, $label, $local){ $fieldValue = $this->getCustomFieldValueByPublicKey($publicKey); $fieldValue->addStrValues($label, $local); if(!$this->quickUpdateValue($fieldValue)){ return null; } return $fieldValue->getFormForgeField(); } /** * @param string $publicKey * @return FormForgeFieldInterface|null */ public function removeCustomFieldValue(string $publicKey): ?FormForgeFieldInterface{ $customFieldValue = $this->getCustomFieldValueByPublicKey($publicKey); $this->deleteValue($customFieldValue); return $customFieldValue->getFormForgeField(); } public function updateOrderFields(array $dataFieldOrder){ foreach ($dataFieldOrder as $data){ $fieldOrder = explode('=', $data); $field = $this->getByPublicKey($fieldOrder[0]); if (!$field instanceof FormForgeField) return null; $field->setOrder($fieldOrder[1]); if (!$this->quickUpdate($field)) return null; } return true; } }