bakaphp /
phalcon-api
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Gewaer\CustomFields; |
||
| 5 | |||
| 6 | use Gewaer\Models\CustomFieldsModules; |
||
| 7 | use Exception; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Custom Fields Abstract Class |
||
| 11 | * @property \Phalcon\Di $di |
||
| 12 | */ |
||
| 13 | abstract class AbstractCustomFieldsModel extends \Baka\Database\ModelCustomFields |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Get all custom fields of the given object |
||
| 17 | * |
||
| 18 | * @param array $fields |
||
| 19 | * @return array |
||
| 20 | */ |
||
| 21 | 8 | public function getAllCustomFields(array $fields = []) |
|
| 22 | { |
||
| 23 | //We does it only find names in plural? We need to fix this or make a workaroun |
||
| 24 | 8 | if (!$models = CustomFieldsModules::findFirstByName($this->getSource())) { |
|
| 25 | return; |
||
| 26 | } |
||
| 27 | |||
| 28 | 8 | $bind = [$this->getId(), $this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company]; |
|
| 29 | |||
| 30 | // $customFieldsValueTable = $this->getSource() . '_custom_fields'; |
||
| 31 | 8 | $customFieldsValueTable = $this->getSource() . '_custom_fields'; |
|
| 32 | |||
| 33 | //We are to make a new query to replace old gewaer implementation. |
||
| 34 | 8 | $result = $this->getReadConnection()->prepare("SELECT l.{$this->getSource()}_id, |
|
| 35 | c.id as field_id, |
||
| 36 | c.name, |
||
| 37 | l.value , |
||
| 38 | c.users_id, |
||
| 39 | l.created_at, |
||
| 40 | l.updated_at |
||
| 41 | 8 | FROM {$customFieldsValueTable} l, |
|
| 42 | custom_fields c |
||
| 43 | WHERE c.id = l.custom_fields_id |
||
| 44 | 8 | AND l.{$this->getSource()}_id = ? |
|
| 45 | AND c.apps_id = ? |
||
| 46 | AND c.custom_fields_modules_id = ? |
||
| 47 | AND c.companies_id = ? "); |
||
| 48 | |||
| 49 | 8 | $result->execute($bind); |
|
| 50 | |||
| 51 | // $listOfCustomFields = $result->fetchAll(); |
||
| 52 | 8 | $listOfCustomFields = []; |
|
| 53 | |||
| 54 | 8 | while ($row = $result->fetch(\PDO::FETCH_OBJ)) { |
|
| 55 | $listOfCustomFields[$row->name] = $row->value; |
||
| 56 | } |
||
| 57 | |||
| 58 | 8 | return $listOfCustomFields; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get all custom fields of the given model |
||
| 63 | * |
||
| 64 | * @param array $fields |
||
| 65 | * @return \Phalcon\Mvc\Model |
||
| 66 | */ |
||
| 67 | public function getCustomFieldsByModel($modelName) |
||
| 68 | { |
||
| 69 | if (!$module = CustomFieldsModules::findFirstByName($modelName)) { |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | $allFields = []; |
||
| 73 | if ($fields = CustomFields::findByModulesId($module->id)->toArray()) { |
||
| 74 | foreach ($fields as $field) { |
||
| 75 | array_push($allFields, $field['name']); |
||
| 76 | } |
||
| 77 | return $allFields; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Create new custom fields |
||
| 83 | * |
||
| 84 | * We never update any custom fields, we delete them and create them again, thats why we call cleanCustomFields before updates |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | 4 | protected function saveCustomFields(): bool |
|
| 89 | { |
||
| 90 | //find the custom field module |
||
| 91 | 4 | if (!$module = CustomFieldsModules::findFirstByName($this->getSource())) { |
|
| 92 | return false; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 93 | } |
||
| 94 | //we need a new instane to avoid overwrite |
||
| 95 | 4 | $reflector = new \ReflectionClass($this); |
|
| 96 | 4 | $classNameWithNameSpace = $reflector->getNamespaceName() . '\\' . $reflector->getShortName() . 'CustomFields'; |
|
| 97 | |||
| 98 | //if all is good now lets get the custom fields and save them |
||
| 99 | 4 | foreach ($this->customFields as $key => $value) { |
|
| 100 | //create a new obj per itration to se can save new info |
||
| 101 | 3 | $customModel = new $classNameWithNameSpace(); |
|
| 102 | |||
| 103 | //validate the custome field by it model |
||
| 104 | 3 | $customField = CustomFields::findFirst([ |
|
| 105 | 3 | 'conditions' => 'name = ?0 AND custom_fields_modules_id = ?1 AND companies_id = ?2 AND apps_id = ?3', |
|
| 106 | 3 | 'bind' => [$key, $module->id, $this->di->getUserData()->default_company, $this->di->getApp()->getId()] |
|
| 107 | ]); |
||
| 108 | |||
| 109 | 3 | if ($customField) { |
|
| 110 | $customModel->setCustomId($this->getId()); |
||
| 111 | $customModel->custom_fields_id = $customField->id; |
||
| 112 | $customModel->value = $value; |
||
| 113 | $customModel->created_at = date('Y-m-d H:i:s'); |
||
| 114 | |||
| 115 | if (!$customModel->save()) { |
||
| 116 | 3 | throw new Exception('Custome ' . $key . ' - ' . current($customModel->getMessages())); |
|
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | //clean |
||
| 122 | 4 | unset($this->customFields); |
|
| 123 | |||
| 124 | 4 | return true; |
|
|
0 ignored issues
–
show
|
|||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Before create |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 3 | public function beforeCreate() |
|
| 133 | { |
||
| 134 | 3 | $this->created_at = date('Y-m-d H:i:s'); |
|
| 135 | 3 | $this->updated_at = null; |
|
| 136 | 3 | $this->is_deleted = 0; |
|
| 137 | 3 | } |
|
| 138 | } |
||
| 139 |