|
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
|
1 |
|
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
|
1 |
|
if (!$models = CustomFieldsModules::findFirstByName($this->getSource())) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
$bind = [$this->getId(), $this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company]; |
|
29
|
|
|
|
|
30
|
|
|
// $customFieldsValueTable = $this->getSource() . '_custom_fields'; |
|
31
|
1 |
|
$customFieldsValueTable = $this->getSource() . '_custom_fields'; |
|
32
|
|
|
|
|
33
|
|
|
//We are to make a new query to replace old gewaer implementation. |
|
34
|
1 |
|
$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
|
1 |
|
FROM {$customFieldsValueTable} l, |
|
42
|
|
|
custom_fields c |
|
43
|
|
|
WHERE c.id = l.custom_fields_id |
|
44
|
1 |
|
AND l.{$this->getSource()}_id = ? |
|
45
|
|
|
AND c.apps_id = ? |
|
46
|
|
|
AND c.custom_fields_modules_id = ? |
|
47
|
|
|
AND c.companies_id = ? "); |
|
48
|
|
|
|
|
49
|
1 |
|
$result->execute($bind); |
|
50
|
|
|
|
|
51
|
|
|
// $listOfCustomFields = $result->fetchAll(); |
|
52
|
1 |
|
$listOfCustomFields = []; |
|
53
|
|
|
|
|
54
|
1 |
|
while ($row = $result->fetch(\PDO::FETCH_OBJ)) { |
|
55
|
|
|
$listOfCustomFields[$row->name] = $row->value; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
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 bool |
|
87
|
|
|
*/ |
|
88
|
1 |
|
protected function saveCustomFields(): bool |
|
89
|
|
|
{ |
|
90
|
|
|
//find the custom field module |
|
91
|
1 |
|
if (!$module = CustomFieldsModules::findFirstByName($this->getSource())) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
//we need a new instane to avoid overwrite |
|
95
|
1 |
|
$reflector = new \ReflectionClass($this); |
|
96
|
1 |
|
$classNameWithNameSpace = $reflector->getNamespaceName() . '\\' . $reflector->getShortName() . 'CustomFields'; |
|
97
|
|
|
|
|
98
|
|
|
//if all is good now lets get the custom fields and save them |
|
99
|
1 |
|
foreach ($this->customFields as $key => $value) { |
|
100
|
|
|
//create a new obj per itration to se can save new info |
|
101
|
|
|
$customModel = new $classNameWithNameSpace(); |
|
102
|
|
|
|
|
103
|
|
|
//validate the custome field by it model |
|
104
|
|
|
$customField = CustomFields::findFirst([ |
|
105
|
|
|
'conditions' => 'name = ?0 AND custom_fields_modules_id = ?1 AND companies_id = ?2 AND apps_id = ?3', |
|
106
|
|
|
'bind' => [$key, $module->id, $this->di->getUserData()->default_company, $this->di->getApp()->getId()] |
|
107
|
|
|
]); |
|
108
|
|
|
|
|
109
|
|
|
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
|
|
|
throw new Exception('Custome ' . $key . ' - ' . current($customModel->getMessages())); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//clean |
|
122
|
1 |
|
unset($this->customFields); |
|
123
|
|
|
|
|
124
|
1 |
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Before create |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function beforeCreate() |
|
133
|
|
|
{ |
|
134
|
1 |
|
$this->created_at = date('Y-m-d H:i:s'); |
|
135
|
1 |
|
$this->updated_at = null; |
|
136
|
1 |
|
$this->is_deleted = 0; |
|
137
|
1 |
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|