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
|
|
|
/** @var string The name of Add-on. */ |
66
|
|
|
protected string $addOnName = ''; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Constructor. |
70
|
|
|
*/ |
71
|
|
|
public function __construct() |
72
|
|
|
{ |
73
|
|
|
$name = basename(str_replace('\\', '/', static::class)); |
74
|
|
|
$this->name = $name; |
75
|
|
|
|
76
|
|
|
$class = '\\Config\\Components\\RecordCollectors\\' . $name; |
77
|
|
|
if (!class_exists($class)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
$config = (new \ReflectionClass($class))->getStaticProperties(); |
81
|
|
|
if (isset($config['allowedModules'])) { |
82
|
|
|
$this->allowedModules = $config['allowedModules']; |
83
|
|
|
unset($config['allowedModules']); |
84
|
|
|
} |
85
|
|
|
foreach ($config as $key => $value) { |
86
|
|
|
$this->{$key} = $value; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get record collector name. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getName(): string |
96
|
|
|
{ |
97
|
|
|
return $this->name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set request. |
102
|
|
|
* |
103
|
|
|
* @param \App\Request $request |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setRequest(\App\Request $request): void |
108
|
|
|
{ |
109
|
|
|
$this->request = $request; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get fields for the modal search window. |
114
|
|
|
* |
115
|
|
|
* @return \Vtiger_Field_Model[] |
116
|
|
|
*/ |
117
|
|
|
public function getFields(): array |
118
|
|
|
{ |
119
|
|
|
$fieldsModel = []; |
120
|
|
|
foreach ($this->fields as $fieldName => $data) { |
121
|
|
|
if (isset($data['picklistValuesFunction'])) { |
122
|
|
|
$data['picklistValues'] = $this->{$data['picklistValuesFunction']}($data); |
123
|
|
|
} elseif (isset($data['picklistValues']) && false !== $data['picklistModule']) { |
124
|
|
|
$picklistModule = $data['picklistModule'] ?? $this->moduleName; |
125
|
|
|
foreach ($data['picklistValues'] as $picklistKey => $value) { |
126
|
|
|
$data['picklistValues'][$picklistKey] = \App\Language::translate($value, $picklistModule); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
$fieldModel = \Vtiger_Field_Model::init($this->moduleName, $data, $fieldName); |
130
|
|
|
if (isset($this->modulesFieldsMap[$this->moduleName][$fieldName]) && $this->request->has($this->modulesFieldsMap[$this->moduleName][$fieldName])) { |
131
|
|
|
try { |
132
|
|
|
$uitypeModel = $fieldModel->getUITypeModel(); |
133
|
|
|
$value = $this->request->getByType($this->modulesFieldsMap[$this->moduleName][$fieldName], 'Text'); |
134
|
|
|
$uitypeModel->validate($value, true); |
135
|
|
|
$fieldModel->set('fieldvalue', $uitypeModel->getDBValue($value)); |
136
|
|
|
} catch (\Throwable $th) { |
137
|
|
|
\App\Log::error($th->__toString(), 'RecordCollectors'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
$fieldsModel[$fieldName] = $fieldModel; |
141
|
|
|
} |
142
|
|
|
return $fieldsModel; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get fields for the module name. |
147
|
|
|
* |
148
|
|
|
* @param string $moduleName |
149
|
|
|
* |
150
|
|
|
* @return string[] |
151
|
|
|
*/ |
152
|
|
|
public function getFieldsModule(string $moduleName): array |
153
|
|
|
{ |
154
|
|
|
return $this->modulesFieldsMap[$moduleName]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Check whether it is active. |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isActive(): bool |
163
|
|
|
{ |
164
|
|
|
return \in_array($this->moduleName, $this->allowedModules) && $this->isAvailable(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Check if product is available. |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function isAvailable(): bool |
173
|
|
|
{ |
174
|
|
|
return !$this->paid || \App\YetiForce\Shop::check($this->addOnName); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Search data function. |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
public function search(): array |
183
|
|
|
{ |
184
|
|
|
throw new \Api\Core\Exception('no search function'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Load data. |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function loadData(): void |
193
|
|
|
{ |
194
|
|
|
if (empty($this->data)) { |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
if ($recordId = $this->request->getInteger('record')) { |
198
|
|
|
$recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName); |
199
|
|
|
$this->response['recordModel'] = $recordModel; |
200
|
|
|
$fieldsModel = $recordModel->getModule()->getFields(); |
201
|
|
|
} else { |
202
|
|
|
$fieldsModel = \Vtiger_Module_Model::getInstance($this->moduleName)->getFields(); |
203
|
|
|
} |
204
|
|
|
$additional = $fieldsData = $skip = []; |
205
|
|
|
$rows = isset($this->data[0]) ? $this->data : [$this->data]; |
206
|
|
|
foreach ($rows as $key => &$row) { |
207
|
|
|
$dataCounter[$key] = 0; |
208
|
|
|
if (empty($row)) { |
209
|
|
|
continue; |
210
|
|
|
} |
211
|
|
|
foreach ($this->formFieldsToRecordMap[$this->moduleName] as $apiKey => $fieldName) { |
212
|
|
|
if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) { |
213
|
|
|
if (isset($row[$apiKey]) && '' !== $row[$apiKey] && null !== $row[$apiKey]) { |
214
|
|
|
$skip[$fieldName]['data'][$key] = $row[$apiKey]; |
215
|
|
|
if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) { |
216
|
|
|
$skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $this->moduleName); |
217
|
|
|
} else { |
218
|
|
|
$skip[$fieldName]['label'] = $fieldName; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
unset($row[$apiKey]); |
222
|
|
|
continue; |
223
|
|
|
} |
224
|
|
|
$value = ''; |
225
|
|
|
if (isset($row[$apiKey])) { |
226
|
|
|
$value = trim($row[$apiKey]); |
227
|
|
|
unset($row[$apiKey]); |
228
|
|
|
} |
229
|
|
|
if ('' === $value && isset($fieldsData[$fieldName]['data'][$key])) { |
230
|
|
|
continue; |
231
|
|
|
} |
232
|
|
|
if ($value) { |
233
|
|
|
++$dataCounter[$key]; |
|
|
|
|
234
|
|
|
} |
235
|
|
|
$fieldModel = $fieldsModel[$fieldName]; |
236
|
|
|
$fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $this->moduleName); |
|
|
|
|
237
|
|
|
$fieldsData[$fieldName]['data'][$key] = [ |
238
|
|
|
'raw' => $value, |
239
|
|
|
'edit' => $fieldModel->getEditViewDisplayValue($value), |
240
|
|
|
'display' => $fieldModel->getDisplayValue($value, false, false, false, 40), |
241
|
|
|
]; |
242
|
|
|
} |
243
|
|
|
foreach ($row as $name => $value) { |
244
|
|
|
if ('' !== $value && null !== $value) { |
245
|
|
|
$additional[$name][$key] = $value; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
$this->response['fields'] = $fieldsData; |
250
|
|
|
$this->response['skip'] = $skip; |
251
|
|
|
$this->response['keys'] = array_keys($rows); |
252
|
|
|
$this->response['additional'] = $additional; |
253
|
|
|
$this->response['dataCounter'] = $dataCounter; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get fields labels for the module name. |
258
|
|
|
* |
259
|
|
|
* @param string $moduleName |
260
|
|
|
* |
261
|
|
|
* @return string[] |
262
|
|
|
*/ |
263
|
|
|
public function getFieldsLabelsByModule(string $moduleName): array |
264
|
|
|
{ |
265
|
|
|
$fieldsModels = \Vtiger_Module_Model::getInstance($moduleName)->getFields(); |
266
|
|
|
$labels = []; |
267
|
|
|
foreach ($this->formFieldsToRecordMap[$moduleName] as $fieldName) { |
268
|
|
|
if (isset($fieldsModels[$fieldName]) && $fieldsModels[$fieldName]->isActiveField()) { |
269
|
|
|
$labels[$fieldName] = $fieldsModels[$fieldName]->getFullLabelTranslation(); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
return $labels; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get params of collector. |
277
|
|
|
* |
278
|
|
|
* @return array |
279
|
|
|
*/ |
280
|
|
|
protected function getParams(): array |
281
|
|
|
{ |
282
|
|
|
if ($params = (new \App\Db\Query())->select(['params'])->from('vtiger_links')->where(['linktype' => 'EDIT_VIEW_RECORD_COLLECTOR', 'linkurl' => static::class])->scalar()) { |
283
|
|
|
return \App\Json::decode($params, true); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
return []; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|