1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base record collector file. |
4
|
|
|
* |
5
|
|
|
* @package App |
6
|
|
|
* |
7
|
|
|
* @copyright YetiForce S.A. |
8
|
|
|
* @license YetiForce Public License 5.0 (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
|
|
|
protected static $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 array List of fields for the modal search window. */ |
45
|
|
|
protected $fields = []; |
46
|
|
|
|
47
|
|
|
/** @var array Data from record collector source. */ |
48
|
|
|
protected $data = []; |
49
|
|
|
|
50
|
|
|
/** @var array Response data. */ |
51
|
|
|
protected $response = []; |
52
|
|
|
|
53
|
|
|
/** @var \App\Request Request instance. */ |
54
|
|
|
protected $request; |
55
|
|
|
|
56
|
|
|
/** @var array Fields mapping for loading record data. */ |
57
|
|
|
protected $modulesFieldsMap = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor. |
61
|
|
|
*/ |
62
|
|
|
public function __construct() |
63
|
|
|
{ |
64
|
|
|
$name = last(explode('\\', static::class)); |
65
|
|
|
$class = '\\Config\\Components\\RecordCollectors\\' . $name; |
66
|
|
|
if (!\class_exists($class)) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
$config = (new \ReflectionClass($class))->getStaticProperties(); |
70
|
|
|
if (isset($config['allowedModules'])) { |
71
|
|
|
static::$allowedModules = $config['allowedModules']; |
72
|
|
|
unset($config['allowedModules']); |
73
|
|
|
} |
74
|
|
|
foreach ($config as $key => $value) { |
75
|
|
|
$this->{$key} = $value; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set request. |
81
|
|
|
* |
82
|
|
|
* @param \App\Request $request |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function setRequest(\App\Request $request): void |
87
|
|
|
{ |
88
|
|
|
$this->request = $request; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get fields for the modal search window. |
93
|
|
|
* |
94
|
|
|
* @return \Vtiger_Field_Model[] |
95
|
|
|
*/ |
96
|
|
|
public function getFields(): array |
97
|
|
|
{ |
98
|
|
|
$fieldsModel = []; |
99
|
|
|
foreach ($this->fields as $fieldName => $data) { |
100
|
|
|
if (isset($data['picklistValues']) && false !== $data['picklistModule']) { |
101
|
|
|
$picklistModule = $data['picklistModule'] ?? $this->moduleName; |
102
|
|
|
foreach ($data['picklistValues'] as $picklistKey => $value) { |
103
|
|
|
$data['picklistValues'][$picklistKey] = \App\Language::translate($value, $picklistModule); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$fieldModel = \Vtiger_Field_Model::init($this->moduleName, $data, $fieldName); |
107
|
|
|
if (isset($this->modulesFieldsMap[$this->moduleName][$fieldName]) && $this->request->has($this->modulesFieldsMap[$this->moduleName][$fieldName])) { |
108
|
|
|
try { |
109
|
|
|
$uitypeModel = $fieldModel->getUITypeModel(); |
110
|
|
|
$value = $this->request->getByType($this->modulesFieldsMap[$this->moduleName][$fieldName], 'Text'); |
111
|
|
|
$uitypeModel->validate($value, true); |
112
|
|
|
$fieldModel->set('fieldvalue', $uitypeModel->getDBValue($value)); |
113
|
|
|
} catch (\Throwable $th) { |
114
|
|
|
\App\Log::error($th->__toString(), 'RecordCollectors'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
$fieldsModel[$fieldName] = $fieldModel; |
118
|
|
|
} |
119
|
|
|
return $fieldsModel; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get fields for the module name. |
124
|
|
|
* |
125
|
|
|
* @param string $moduleName |
126
|
|
|
* |
127
|
|
|
* @return string[] |
128
|
|
|
*/ |
129
|
|
|
public function getFieldsModule(string $moduleName): array |
130
|
|
|
{ |
131
|
|
|
return $this->modulesFieldsMap[$moduleName]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Check whether it is active. |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isActive(): bool |
140
|
|
|
{ |
141
|
|
|
return \in_array($this->moduleName, static::$allowedModules); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Search data function. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function search(): array |
150
|
|
|
{ |
151
|
|
|
throw new \Api\Core\Exception('no search function'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get params of collector. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
protected function getParams(): array |
160
|
|
|
{ |
161
|
|
|
if ($params = (new \App\Db\Query())->select(['params'])->from('vtiger_links')->where(['linktype' => 'EDIT_VIEW_RECORD_COLLECTOR', 'linkurl' => static::class])->scalar()) { |
162
|
|
|
return \App\Json::decode($params, true); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
return []; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Load data. |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function loadData(): void |
173
|
|
|
{ |
174
|
|
|
if ($recordId = $this->request->getInteger('record')) { |
175
|
|
|
$recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName); |
176
|
|
|
$this->response['recordModel'] = $recordModel; |
177
|
|
|
$fieldsModel = $recordModel->getModule()->getFields(); |
178
|
|
|
} else { |
179
|
|
|
$fieldsModel = \Vtiger_Module_Model::getInstance($this->moduleName)->getFields(); |
180
|
|
|
} |
181
|
|
|
$fieldsData = $skip = []; |
182
|
|
|
$rows = isset($this->data[0]) ? $this->data : [$this->data]; |
183
|
|
|
foreach ($rows as $key => &$row) { |
184
|
|
|
$dataCounter[$key] = 0; |
185
|
|
|
if (empty($row)) { |
186
|
|
|
continue; |
187
|
|
|
} |
188
|
|
|
foreach ($this->formFieldsToRecordMap[$this->moduleName] as $apiKey => $fieldName) { |
189
|
|
|
if (empty($fieldsModel[$fieldName]) || !$fieldsModel[$fieldName]->isActiveField()) { |
190
|
|
|
if (isset($row[$apiKey]) && '' !== $row[$apiKey]) { |
191
|
|
|
$skip[$fieldName]['data'][$key] = $row[$apiKey]; |
192
|
|
|
if (isset($fieldsModel[$fieldName]) && empty($skip[$fieldName]['label'])) { |
193
|
|
|
$skip[$fieldName]['label'] = \App\Language::translate($fieldsModel[$fieldName]->getFieldLabel(), $this->moduleName); |
194
|
|
|
} else { |
195
|
|
|
$skip[$fieldName]['label'] = $fieldName; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
unset($row[$apiKey]); |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
|
|
$value = ''; |
202
|
|
|
if (isset($row[$apiKey])) { |
203
|
|
|
$value = trim($row[$apiKey]); |
204
|
|
|
unset($row[$apiKey]); |
205
|
|
|
} |
206
|
|
|
if ($value) { |
207
|
|
|
++$dataCounter[$key]; |
|
|
|
|
208
|
|
|
} |
209
|
|
|
$fieldModel = $fieldsModel[$fieldName]; |
210
|
|
|
$fieldsData[$fieldName]['label'] = \App\Language::translate($fieldModel->getFieldLabel(), $this->moduleName); |
211
|
|
|
$fieldsData[$fieldName]['data'][$key] = [ |
212
|
|
|
'raw' => $value, |
213
|
|
|
'edit' => $fieldModel->getEditViewDisplayValue($value), |
214
|
|
|
'display' => $fieldModel->getDisplayValue($value), |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
foreach ($row as $name => $value) { |
218
|
|
|
$additional[$name][$key] = $value; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
$this->response['fields'] = $fieldsData; |
222
|
|
|
$this->response['skip'] = $skip; |
223
|
|
|
$this->response['keys'] = array_keys($rows); |
224
|
|
|
$this->response['additional'] = $additional; |
|
|
|
|
225
|
|
|
$this->response['dataCounter'] = $dataCounter; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|