em = $em; $this->rdp = $rdp; $this->class = $class; $this->fieldValueManager = $fieldValueManager; } public function createCustomFieldFromArray(array $_customField){ $formForgeField = new FormForgeField(); if(!$this->rdp->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->em->persist($tt); $this->em->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->em->persist($tt); $this->em->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->em->contains($element)){ try { $this->em->remove($element); $this->em->flush(); } catch (ORMException $e) { return false; } } return $element; } /** * @param FormForgeFieldValueInterface $tt * @return bool */ public function deleteValue(FormForgeFieldValueInterface $tt){ if($this->em->contains($tt)){ try { $this->em->remove($tt); $this->em->flush(); } catch (ORMException $e) { return false; } } return true; } /** * @param FormForgeReasonInterface $_reason * @param string $_label * @param string $_local * @param int $_type * @param string $_required * @param int $_order * @return FormForgeField|null */ public function create(FormForgeReasonInterface $_reason,string $_label, string $_local,int $_type, string $_required, int $_order){ $new = new FormForgeField(); $required = ($_required == 'true') ? true : false; $name = new LinguisticaTradItem(); $name->addStrValues($_label, $_local); $new->setDefault($_reason, $name, $_type, $required, $_order); if(!$this->quickUpdate($new)){ return null; } return $new; } /** * @param string $publicKey * @param string $label * @param string $local * @param int $type * @return FormForgeField|null */ public function updateFormForgeField(string $publicKey,string $label, string $local,int $type){ $formForgeField = $this->getByPublicKey($publicKey); $formForgeField->getLabel()->addStrValues($label, $local); $formForgeField->setType($type); if(!$this->quickUpdate($formForgeField)){ return null; } return $formForgeField; } /** * @param string $publicKey * @return FormForgeField|null */ public function getByPublicKey(string $publicKey): ?FormForgeField{ $field = $this->em->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(); } }