1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base record collector file. |
4
|
|
|
* |
5
|
|
|
* @package App |
6
|
|
|
* |
7
|
|
|
* @copyright YetiForce S.A. |
8
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
10
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\RecordCollectors; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base record collector class. |
17
|
|
|
*/ |
18
|
|
|
class Base |
19
|
|
|
{ |
20
|
|
|
/** @var string Module name. */ |
21
|
|
|
public $moduleName; |
22
|
|
|
|
23
|
|
|
/** @var string[] Allowed modules. */ |
24
|
|
|
public $allowedModules; |
25
|
|
|
|
26
|
|
|
/** @var string Icon. */ |
27
|
|
|
public $icon; |
28
|
|
|
|
29
|
|
|
/** @var string Label. */ |
30
|
|
|
public $label; |
31
|
|
|
|
32
|
|
|
/** @var string Additional description, visible in the modal window. */ |
33
|
|
|
public $description; |
34
|
|
|
|
35
|
|
|
/** @var string Search results display type. */ |
36
|
|
|
public $displayType; |
37
|
|
|
|
38
|
|
|
/** @var array Configuration field list. */ |
39
|
|
|
public $settingsFields = []; |
40
|
|
|
|
41
|
|
|
/** @var string Url to Documentation API */ |
42
|
|
|
public $docUrl; |
43
|
|
|
|
44
|
|
|
/** @var string Record collector name. */ |
45
|
|
|
protected $name; |
46
|
|
|
|
47
|
|
|
/** var array List of fields for the modal search window. */ |
48
|
|
|
protected $fields = []; |
49
|
|
|
|
50
|
|
|
/** @var array Data from record collector source. */ |
51
|
|
|
protected $data = []; |
52
|
|
|
|
53
|
|
|
/** @var array Response data. */ |
54
|
|
|
protected $response = []; |
55
|
|
|
|
56
|
|
|
/** @var \App\Request Request instance. */ |
57
|
|
|
protected $request; |
58
|
|
|
|
59
|
|
|
/** @var array Fields mapping for loading record data. */ |
60
|
|
|
protected $modulesFieldsMap = []; |
61
|
|
|
|
62
|
|
|
/** @var bool Requires subscription. */ |
63
|
|
|
protected bool $paid = true; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
*/ |
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
$name = basename(str_replace('\\', '/', static::class)); |
71
|
|
|
$this->name = $name; |
72
|
|
|
|
73
|
|
|
$class = '\\Config\\Components\\RecordCollectors\\' . $name; |
74
|
|
|
if (!class_exists($class)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
$config = (new \ReflectionClass($class))->getStaticProperties(); |
78
|
|
|
if (isset($config['allowedModules'])) { |
79
|
|
|
$this->allowedModules = $config['allowedModules']; |
80
|
|
|
unset($config['allowedModules']); |
81
|
|
|
} |
82
|
|
|
foreach ($config as $key => $value) { |
83
|
|
|
$this->{$key} = $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get record collector name. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getName(): string |
93
|
|
|
{ |
94
|
|
|
return $this->name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set request. |
99
|
|
|
* |
100
|
|
|
* @param \App\Request $request |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function setRequest(\App\Request $request): void |
105
|
|
|
{ |
106
|
|
|
$this->request = $request; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get fields for the modal search window. |
111
|
|
|
* |
112
|
|
|
* @return \Vtiger_Field_Model[] |
113
|
|
|
*/ |
114
|
|
|
public function getFields(): array |
115
|
|
|
{ |
116
|
|
|
$fieldsModel = []; |
117
|
|
|
foreach ($this->fields as $fieldName => $data) { |
118
|
|
|
if (isset($data['picklistValuesFunction'])) { |
119
|
|
|
$data['picklistValues'] = $this->{$data['picklistValuesFunction']}($data); |
120
|
|
|
} elseif (isset($data['picklistValues']) && false !== $data['picklistModule']) { |
121
|
|
|
$picklistModule = $data['picklistModule'] ?? $this->moduleName; |
122
|
|
|
foreach ($data['picklistValues'] as $picklistKey => $value) { |
123
|
|
|
$data['picklistValues'][$picklistKey] = \App\Language::translate($value, $picklistModule); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
$fieldModel = \Vtiger_Field_Model::init($this->moduleName, $data, $fieldName); |
127
|
|
|
if (isset($this->modulesFieldsMap[$this->moduleName][$fieldName]) && $this->request->has($this->modulesFieldsMap[$this->moduleName][$fieldName])) { |
128
|
|
|
try { |
129
|
|
|
$uitypeModel = $fieldModel->getUITypeModel(); |
130
|
|
|
$value = $this->request->getByType($this->modulesFieldsMap[$this->moduleName][$fieldName], 'Text'); |
131
|
|
|
$uitypeModel->validate($value, true); |
132
|
|
|
$fieldModel->set('fieldvalue', $uitypeModel->getDBValue($value)); |
133
|
|
|
} catch (\Throwable $th) { |
134
|
|
|
\App\Log::error($th->__toString(), 'RecordCollectors'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
$fieldsModel[$fieldName] = $fieldModel; |
138
|
|
|
} |
139
|
|
|
return $fieldsModel; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get fields for the module name. |
144
|
|
|
* |
145
|
|
|
* @param string $moduleName |
146
|
|
|
* |
147
|
|
|
* @return string[] |
148
|
|
|
*/ |
149
|
|
|
public function getFieldsModule(string $moduleName): array |
150
|
|
|
{ |
151
|
|
|
return $this->modulesFieldsMap[$moduleName]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check whether it is active. |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isActive(): bool |
160
|
|
|
{ |
161
|
|
|
return \in_array($this->moduleName, $this->allowedModules) && $this->isAvailable(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check if product is available. |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function isAvailable(): bool |
170
|
|
|
{ |
171
|
|
|
return !$this->paid || \App\YetiForce\Shop::check($this->getName()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Search data function. |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function search(): array |
180
|
|
|
{ |
181
|
|
|
throw new \Api\Core\Exception('no search function'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Load data. |
186
|
|
|
* |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function loadData(): void |
190
|
|
|
{ |
191
|
|
|
if (empty($this->data)) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
if ($recordId = $this->request->getInteger('record')) { |
195
|
|
|
$recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName); |
196
|
|
|
$this->response['recordModel'] = $recordModel; |
197
|
|
|
$fieldsModel = $recordModel->getModule()->getFields(); |
198
|
|
|
} else { |
199
|
|
|
$fieldsModel = \Vtiger_Module_Model::getInstance($this->moduleName)->getFields(); |
200
|
|
|
} |
201
|
|
|
$additional = $fieldsData = $skip = []; |
202
|
|
|
$rows = isset($this->data[0]) ? $this->data : [$this->data]; |
203
|
|
|
foreach ($rows as $key => &$row) { |
204
|
|
|
$dataCounter[$key] = 0; |
205
|
|
|
if (empty($row)) { |
206
|
|
|
continue; |
207
|
|
|
} |
208
|
|
|
foreach ($this->formFieldsToRecordMap[$this->moduleName] as $apiKey => $fieldName) { |
209
|
|
|
if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) { |
210
|
|
|
if (isset($row[$apiKey]) && '' !== $row[$apiKey] && null !== $row[$apiKey]) { |
211
|
|
|
$skip[$fieldName]['data'][$key] = $row[$apiKey]; |
212
|
|
|
if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) { |
213
|
|
|
$skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $this->moduleName); |
214
|
|
|
} else { |
215
|
|
|
$skip[$fieldName]['label'] = $fieldName; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
unset($row[$apiKey]); |
219
|
|
|
continue; |
220
|
|
|
} |
221
|
|
|
$value = ''; |
222
|
|
|
if (isset($row[$apiKey])) { |
223
|
|
|
$value = trim($row[$apiKey]); |
224
|
|
|
unset($row[$apiKey]); |
225
|
|
|
} |
226
|
|
|
if ('' === $value && isset($fieldsData[$fieldName]['data'][$key])) { |
227
|
|
|
continue; |
228
|
|
|
} |
229
|
|
|
if ($value) { |
230
|
|
|
++$dataCounter[$key]; |
|
|
|
|
231
|
|
|
} |
232
|
|
|
$fieldModel = $fieldsModel[$fieldName]; |
233
|
|
|
$fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $this->moduleName); |
|
|
|
|
234
|
|
|
$fieldsData[$fieldName]['data'][$key] = [ |
235
|
|
|
'raw' => $value, |
236
|
|
|
'edit' => $fieldModel->getEditViewDisplayValue($value), |
237
|
|
|
'display' => $fieldModel->getDisplayValue($value, false, false, false, 40), |
238
|
|
|
]; |
239
|
|
|
} |
240
|
|
|
foreach ($row as $name => $value) { |
241
|
|
|
if ('' !== $value && null !== $value) { |
242
|
|
|
$additional[$name][$key] = $value; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
$this->response['fields'] = $fieldsData; |
247
|
|
|
$this->response['skip'] = $skip; |
248
|
|
|
$this->response['keys'] = array_keys($rows); |
249
|
|
|
$this->response['additional'] = $additional; |
250
|
|
|
$this->response['dataCounter'] = $dataCounter; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get fields labels for the module name. |
255
|
|
|
* |
256
|
|
|
* @param string $moduleName |
257
|
|
|
* |
258
|
|
|
* @return string[] |
259
|
|
|
*/ |
260
|
|
|
public function getFieldsLabelsByModule(string $moduleName): array |
261
|
|
|
{ |
262
|
|
|
$fieldsModels = \Vtiger_Module_Model::getInstance($moduleName)->getFields(); |
263
|
|
|
$labels = []; |
264
|
|
|
foreach ($this->formFieldsToRecordMap[$moduleName] as $fieldName) { |
265
|
|
|
if (isset($fieldsModels[$fieldName]) && $fieldsModels[$fieldName]->isActiveField()) { |
266
|
|
|
$labels[$fieldName] = $fieldsModels[$fieldName]->getFullLabelTranslation(); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
return $labels; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get params of collector. |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
protected function getParams(): array |
278
|
|
|
{ |
279
|
|
|
if ($params = (new \App\Db\Query())->select(['params'])->from('vtiger_links')->where(['linktype' => 'EDIT_VIEW_RECORD_COLLECTOR', 'linkurl' => static::class])->scalar()) { |
280
|
|
|
return \App\Json::decode($params, true); |
|
|
|
|
281
|
|
|
} |
282
|
|
|
return []; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|