|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Record basic 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; |
|
14
|
|
|
|
|
15
|
|
|
use vtlib\Functions; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Record basic class. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Record |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var int Record state - Deleted permanently */ |
|
23
|
|
|
public const STATE_DELETED = -1; |
|
24
|
4 |
|
/** @var int Record state - Active */ |
|
25
|
|
|
public const STATE_ACTIVE = 0; |
|
26
|
4 |
|
/** @var int Record state - Deleted (Recycle bin) */ |
|
27
|
4 |
|
public const STATE_TRASH = 1; |
|
28
|
4 |
|
/** @var int Record state - Archived */ |
|
29
|
4 |
|
public const STATE_ARCHIVED = 2; |
|
30
|
4 |
|
|
|
31
|
4 |
|
/** @var string[] Possible record states */ |
|
32
|
|
|
public const STATES = [ |
|
33
|
|
|
self::STATE_ACTIVE => 'Active', |
|
34
|
4 |
|
self::STATE_TRASH => 'Trash', |
|
35
|
4 |
|
self::STATE_ARCHIVED => 'Archived' |
|
36
|
4 |
|
]; |
|
37
|
4 |
|
|
|
38
|
4 |
|
/** |
|
39
|
|
|
* Get label. |
|
40
|
4 |
|
* |
|
41
|
4 |
|
* @param mixed $mixedId |
|
42
|
1 |
|
* @param bool $raw |
|
43
|
1 |
|
* |
|
44
|
1 |
|
* @return mixed |
|
45
|
1 |
|
*/ |
|
46
|
1 |
|
public static function getLabel($mixedId, bool $raw = false) |
|
47
|
|
|
{ |
|
48
|
|
|
$multiMode = \is_array($mixedId); |
|
49
|
|
|
$ids = array_filter($multiMode ? array_unique($mixedId) : [$mixedId]); |
|
50
|
|
|
$result = $missing = []; |
|
51
|
4 |
|
foreach ($ids as $id) { |
|
52
|
4 |
|
if (!Cache::has('recordLabel', $id)) { |
|
53
|
4 |
|
$missing[$id] = $id; |
|
54
|
4 |
|
} else { |
|
55
|
|
|
$result[$id] = Cache::get('recordLabel', $id); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
if (!empty($missing)) { |
|
59
|
4 |
|
$query = (new \App\Db\Query())->select(['crmid', 'label'])->from('u_#__crmentity_label')->where(['crmid' => $missing]); |
|
60
|
|
|
$dataReader = $query->createCommand()->query(); |
|
61
|
|
|
while ($row = $dataReader->read()) { |
|
62
|
|
|
$label = \App\Utils\Completions::decodeEmoji($row['label']); |
|
63
|
|
|
Cache::save('recordLabel', $row['crmid'], $label); |
|
64
|
|
|
$result[$row['crmid']] = $label; |
|
65
|
|
|
} |
|
66
|
|
|
foreach (array_diff_key($missing, $result) as $id) { |
|
67
|
|
|
$metaInfo = Functions::getCRMRecordMetadata($id); |
|
68
|
|
|
$computeLabel = static::computeLabels($metaInfo['setype'] ?? '', $id)[$id] ?? ''; |
|
69
|
|
|
Cache::save('recordLabel', $id, $computeLabel); |
|
70
|
|
|
$result[$id] = $computeLabel; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
if (!$raw && $result) { |
|
74
|
|
|
$result = array_map('\App\Purifier::encodeHtml', $result); |
|
75
|
|
|
} |
|
76
|
|
|
return $multiMode ? $result : reset($result); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Function searches for record ID with given label. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $moduleName |
|
83
|
|
|
* @param string $label |
|
84
|
|
|
* @param int $userId |
|
85
|
|
|
* |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function getCrmIdByLabel($moduleName, $label, $userId = false) |
|
89
|
|
|
{ |
|
90
|
|
|
$key = $moduleName . $label . '_' . $userId; |
|
|
|
|
|
|
91
|
|
|
if (\App\Cache::staticHas(__METHOD__, $key)) { |
|
92
|
|
|
return \App\Cache::staticGet(__METHOD__, $key); |
|
93
|
|
|
} |
|
94
|
|
|
$query = (new \App\Db\Query()) |
|
95
|
|
|
->select(['cl.crmid']) |
|
96
|
|
|
->from('u_#__crmentity_label cl') |
|
97
|
|
|
->innerJoin('vtiger_crmentity', 'cl.crmid = vtiger_crmentity.crmid') |
|
98
|
|
|
->where(['vtiger_crmentity.setype' => $moduleName]) |
|
99
|
|
|
->andWhere(['cl.label' => $label]); |
|
100
|
|
|
if ($userId) { |
|
|
|
|
|
|
101
|
1 |
|
$query->andWhere(['like', 'vtiger_crmentity.users', ",$userId,"]); |
|
102
|
|
|
} |
|
103
|
1 |
|
$crmId = $query->scalar(); |
|
104
|
|
|
\App\Cache::staticSave(__METHOD__, $key, $crmId); |
|
105
|
|
|
|
|
106
|
1 |
|
return $crmId; |
|
|
|
|
|
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
/** |
|
110
|
1 |
|
* Function gets labels for record data. |
|
111
|
1 |
|
* |
|
112
|
1 |
|
* @param string $moduleName |
|
113
|
1 |
|
* @param array|int $ids |
|
114
|
|
|
* @param bool $search |
|
115
|
|
|
* |
|
116
|
1 |
|
* @return string[]|null |
|
117
|
1 |
|
*/ |
|
118
|
1 |
|
public static function computeLabels($moduleName, $ids, $search = false): ?array |
|
119
|
1 |
|
{ |
|
120
|
1 |
|
if (empty($moduleName) || empty($ids)) { |
|
121
|
|
|
return []; |
|
122
|
1 |
|
} |
|
123
|
1 |
|
$metaInfo = \App\Module::getEntityInfo($moduleName); |
|
124
|
1 |
|
if (!$metaInfo || (empty($metaInfo['fieldnameArr']) && empty($metaInfo['searchcolumnArr']))) { |
|
125
|
1 |
|
return null; |
|
126
|
1 |
|
} |
|
127
|
1 |
|
if (!\is_array($ids)) { |
|
128
|
1 |
|
$ids = [$ids]; |
|
129
|
1 |
|
} |
|
130
|
|
|
$moduleModel = \Vtiger_Module_Model::getInstance($moduleName); |
|
131
|
1 |
|
$entityDisplay = []; |
|
132
|
1 |
|
$cacheName = 'computeLabelsQuery'; |
|
133
|
|
|
if (!\App\Cache::staticHas($cacheName, $moduleName)) { |
|
134
|
|
|
$table = $metaInfo['tablename']; |
|
135
|
|
|
$idColumn = $table . '.' . $metaInfo['entityidfield']; |
|
136
|
|
|
$columnsName = $metaInfo['fieldnameArr']; |
|
137
|
|
|
$columnsSearch = $metaInfo['searchcolumnArr']; |
|
138
|
|
|
$columns = array_unique(array_merge($columnsName, $columnsSearch)); |
|
139
|
1 |
|
$leftJoinTables = $paramsCol = []; |
|
140
|
1 |
|
$query = new \App\Db\Query(); |
|
141
|
1 |
|
$focus = $moduleModel->getEntityInstance(); |
|
142
|
|
|
$moduleInfoExtend = \App\Field::getModuleFieldInfos($moduleName, true); |
|
143
|
1 |
|
foreach (array_filter($columns) as $column) { |
|
144
|
1 |
|
if (\array_key_exists($column, $moduleInfoExtend)) { |
|
145
|
1 |
|
$otherTable = $moduleInfoExtend[$column]['tablename']; |
|
146
|
1 |
|
$paramsCol[] = $otherTable . '.' . $column; |
|
147
|
|
|
if ($otherTable !== $table && !\in_array($otherTable, $leftJoinTables)) { |
|
148
|
|
|
$leftJoinTables[] = $otherTable; |
|
149
|
1 |
|
$focusTables = $focus->tab_name_index; |
|
150
|
1 |
|
$query->leftJoin($otherTable, "$table.$focusTables[$table] = $otherTable.$focusTables[$otherTable]"); |
|
151
|
1 |
|
} |
|
152
|
|
|
} |
|
153
|
1 |
|
} |
|
154
|
1 |
|
$paramsCol['id'] = $idColumn; |
|
155
|
1 |
|
$query->select($paramsCol)->from($table); |
|
156
|
1 |
|
\App\Cache::staticSave($cacheName, $moduleName, clone $query); |
|
157
|
1 |
|
} else { |
|
158
|
1 |
|
$query = \App\Cache::staticGet($cacheName, $moduleName); |
|
159
|
1 |
|
$columnsName = $metaInfo['fieldnameArr']; |
|
160
|
|
|
$columnsSearch = $metaInfo['searchcolumnArr']; |
|
161
|
1 |
|
$idColumn = $metaInfo['entityidfield']; |
|
162
|
|
|
} |
|
163
|
|
|
$separator = $metaInfo['separator'] ?? ' '; |
|
164
|
1 |
|
$ids = array_unique($ids); |
|
165
|
|
|
$query->where([$idColumn => $ids]); |
|
166
|
|
|
$dataReader = $query->createCommand()->query(); |
|
167
|
1 |
|
while ($row = $dataReader->read()) { |
|
168
|
1 |
|
$recordId = $row['id']; |
|
169
|
1 |
|
$labelName = []; |
|
170
|
1 |
|
foreach ($columnsName as $columnName) { |
|
171
|
|
|
$fieldModel = $moduleModel->getFieldByColumn($columnName); |
|
172
|
1 |
|
$labelName[] = $fieldModel ? $fieldModel->getDisplayValue($row[$columnName], $recordId, false, true) : ''; |
|
173
|
|
|
} |
|
174
|
|
|
$label = TextUtils::textTruncate(trim(implode($separator, $labelName)), 250, false); |
|
175
|
1 |
|
if ($search) { |
|
176
|
|
|
$labelName = []; |
|
177
|
|
|
foreach ($columnsSearch as $columnName) { |
|
178
|
1 |
|
$fieldModel = $moduleModel->getFieldByColumn($columnName); |
|
179
|
|
|
$labelName[] = $fieldModel ? $fieldModel->getDisplayValue($row[$columnName], $recordId, false, true) : ''; |
|
180
|
1 |
|
} |
|
181
|
|
|
$searchLabel = TextUtils::textTruncate(trim(implode($separator, $labelName)), 250, false); |
|
182
|
|
|
$entityDisplay[$recordId] = ['name' => $label, 'search' => $searchLabel]; |
|
183
|
1 |
|
} else { |
|
184
|
|
|
$entityDisplay[$recordId] = $label; |
|
185
|
|
|
} |
|
186
|
1 |
|
} |
|
187
|
|
|
return $entityDisplay; |
|
188
|
1 |
|
} |
|
189
|
1 |
|
|
|
190
|
1 |
|
/** |
|
191
|
|
|
* Update record label. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $moduleName |
|
194
|
1 |
|
* @param int $id |
|
195
|
1 |
|
* |
|
196
|
1 |
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function updateLabel(string $moduleName, int $id): void |
|
199
|
1 |
|
{ |
|
200
|
|
|
$dbCommand = \App\Db::getInstance()->createCommand(); |
|
201
|
|
|
$labelTableName = \App\RecordSearch::LABEL_TABLE_NAME; |
|
202
|
1 |
|
$searchTableName = \App\RecordSearch::SEARCH_TABLE_NAME; |
|
203
|
|
|
|
|
204
|
|
|
$metaInfo = static::computeLabels($moduleName, $id, true); |
|
205
|
|
|
if (!$metaInfo) { |
|
206
|
|
|
$dbCommand->delete($labelTableName, ['crmid' => $id])->execute(); |
|
207
|
|
|
$dbCommand->delete($searchTableName, ['crmid' => $id])->execute(); |
|
208
|
|
|
} else { |
|
209
|
|
|
$label = $metaInfo[$id]['name']; |
|
210
|
|
|
if ((new \App\Db\Query())->from($labelTableName)->where(['crmid' => $id])->exists()) { |
|
211
|
|
|
$dbCommand->update($labelTableName, ['label' => $label], ['crmid' => $id])->execute(); |
|
212
|
1 |
|
} else { |
|
213
|
1 |
|
$dbCommand->insert($labelTableName, ['crmid' => $id, 'label' => $label])->execute(); |
|
214
|
|
|
} |
|
215
|
1 |
|
if (isset(\App\RecordSearch::getSearchableModules()[$moduleName])) { |
|
216
|
1 |
|
$label = $metaInfo[$id]['search']; |
|
217
|
|
|
if ((new \App\Db\Query())->from($searchTableName)->where(['crmid' => $id])->exists()) { |
|
218
|
1 |
|
$dbCommand->update($searchTableName, ['searchlabel' => $label], ['crmid' => $id])->execute(); |
|
219
|
|
|
} else { |
|
220
|
1 |
|
$dbCommand->insert($searchTableName, ['crmid' => $id, 'searchlabel' => $label, 'tabid' => \App\Module::getModuleId($moduleName)])->execute(); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
Cache::save('recordLabel', $id, $metaInfo[$id]['name']); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
25 |
|
/** |
|
228
|
|
|
* Update record label on save. |
|
229
|
25 |
|
* |
|
230
|
25 |
|
* @param \Vtiger_Record_Model $recordModel |
|
231
|
25 |
|
* |
|
232
|
25 |
|
* @return void |
|
233
|
25 |
|
*/ |
|
234
|
|
|
public static function updateLabelOnSave(\Vtiger_Record_Model $recordModel): void |
|
235
|
25 |
|
{ |
|
236
|
25 |
|
$label = ''; |
|
237
|
25 |
|
$metaInfo = \App\Module::getEntityInfo($recordModel->getModuleName()); |
|
238
|
25 |
|
$dbCommand = Db::getInstance()->createCommand(); |
|
239
|
|
|
if (!$metaInfo || (empty($metaInfo['fieldnameArr']) && empty($metaInfo['searchcolumnArr']))) { |
|
240
|
25 |
|
$dbCommand->delete('u_#__crmentity_label', ['crmid' => $recordModel->getId()])->execute(); |
|
241
|
25 |
|
$dbCommand->delete('u_#__crmentity_search_label', ['crmid' => $recordModel->getId()])->execute(); |
|
242
|
1 |
|
} else { |
|
243
|
|
|
$separator = $metaInfo['separator'] ?? ' '; |
|
244
|
25 |
|
$labelName = []; |
|
245
|
25 |
|
foreach ($metaInfo['fieldnameArr'] as $columnName) { |
|
246
|
1 |
|
$fieldModel = $recordModel->getModule()->getFieldByColumn($columnName); |
|
247
|
|
|
$labelName[] = $fieldModel->getDisplayValue($recordModel->get($fieldModel->getName()), $recordModel->getId(), $recordModel, true); |
|
248
|
25 |
|
} |
|
249
|
25 |
|
$label = TextUtils::textTruncate(trim(implode($separator, $labelName)), 250, false) ?: ''; |
|
250
|
13 |
|
if ($recordModel->isNew()) { |
|
251
|
13 |
|
$dbCommand->insert('u_#__crmentity_label', ['crmid' => $recordModel->getId(), 'label' => $label])->execute(); |
|
252
|
|
|
} else { |
|
253
|
19 |
|
$dbCommand->update('u_#__crmentity_label', ['label' => $label], ['crmid' => $recordModel->getId()])->execute(); |
|
254
|
19 |
|
} |
|
255
|
19 |
|
|
|
256
|
19 |
|
if (isset(\App\RecordSearch::getSearchableModules()[$recordModel->getModuleName()])) { |
|
257
|
19 |
|
$labelSearch = []; |
|
258
|
19 |
|
foreach ($metaInfo['searchcolumnArr'] as $columnName) { |
|
259
|
|
|
$fieldModel = $recordModel->getModule()->getFieldByColumn($columnName); |
|
260
|
25 |
|
$labelSearch[] = $fieldModel->getDisplayValue($recordModel->get($fieldModel->getName()), $recordModel->getId(), $recordModel, true); |
|
261
|
25 |
|
} |
|
262
|
|
|
$search = TextUtils::textTruncate(trim(implode($separator, $labelSearch)), 250, false) ?: ''; |
|
263
|
|
|
if ($recordModel->isNew()) { |
|
264
|
|
|
$dbCommand->insert('u_#__crmentity_search_label', ['crmid' => $recordModel->getId(), 'searchlabel' => $search, 'tabid' => $recordModel->getModule()->getId()])->execute(); |
|
265
|
|
|
} else { |
|
266
|
|
|
$dbCommand->update('u_#__crmentity_search_label', ['searchlabel' => $search], ['crmid' => $recordModel->getId()])->execute(); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
$recordModel->label = \App\Purifier::encodeHtml($label); |
|
271
|
5 |
|
Cache::save('recordLabel', $recordModel->getId(), $label); |
|
272
|
|
|
} |
|
273
|
5 |
|
|
|
274
|
5 |
|
/** |
|
275
|
|
|
* Function checks if record exists. |
|
276
|
|
|
* |
|
277
|
|
|
* @param int $recordId Record ID |
|
278
|
|
|
* @param string $moduleName |
|
279
|
|
|
* @param int|null $state null = [self::STATE_ACTIVE, self::STATE_ARCHIVED] |
|
280
|
|
|
* |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
|
|
public static function isExists(int $recordId, string $moduleName = '', ?int $state = null) |
|
284
|
19 |
|
{ |
|
285
|
|
|
$recordMetaData = Functions::getCRMRecordMetadata($recordId); |
|
286
|
19 |
|
return isset($recordMetaData) |
|
287
|
19 |
|
&& (null === $state ? self::STATE_TRASH !== $recordMetaData['deleted'] : $recordMetaData['deleted'] === $state) |
|
288
|
|
|
&& ($moduleName ? $recordMetaData['setype'] === $moduleName : true); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Get record module name. |
|
293
|
|
|
* |
|
294
|
|
|
* @param int $recordId |
|
295
|
|
|
* |
|
296
|
|
|
* @return string|null |
|
297
|
|
|
*/ |
|
298
|
|
|
public static function getType($recordId) |
|
299
|
|
|
{ |
|
300
|
|
|
$metadata = Functions::getCRMRecordMetadata($recordId); |
|
301
|
|
|
return $metadata ? $metadata['setype'] : null; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Get the currency ID for the inventory record. |
|
306
|
|
|
* |
|
307
|
|
|
* @param int $recordId |
|
308
|
|
|
* @param string $moduleName |
|
309
|
|
|
* |
|
310
|
|
|
* @return int|null |
|
311
|
1 |
|
*/ |
|
312
|
|
|
public static function getCurrencyIdFromInventory(int $recordId, string $moduleName): ?int |
|
313
|
1 |
|
{ |
|
314
|
1 |
|
$invData = \Vtiger_Inventory_Model::getInventoryDataById($recordId, $moduleName); |
|
315
|
|
|
return current($invData)['currency'] ?? Fields\Currency::getDefault()['id'] ?? null; |
|
316
|
1 |
|
} |
|
317
|
1 |
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Get record state. |
|
320
|
|
|
* |
|
321
|
|
|
* @param int $recordId |
|
322
|
|
|
* |
|
323
|
|
|
* @return int |
|
324
|
|
|
*/ |
|
325
|
|
|
public static function getState(int $recordId): int |
|
326
|
|
|
{ |
|
327
|
|
|
$metadata = Functions::getCRMRecordMetadata($recordId); |
|
328
|
|
|
return $metadata['deleted'] ?? self::STATE_DELETED; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Get record state label. |
|
333
|
|
|
* |
|
334
|
|
|
* @param int $recordId |
|
335
|
|
|
* |
|
336
|
|
|
* @return string {@see \App\Record::STATES} |
|
337
|
|
|
*/ |
|
338
|
|
|
public static function getStateLabel(int $recordId): string |
|
339
|
|
|
{ |
|
340
|
|
|
return self::STATES[self::getState($recordId)] ?? ''; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Get parent record. |
|
345
|
|
|
* |
|
346
|
|
|
* @param int $recordId |
|
347
|
|
|
* @param bool|string $moduleName |
|
348
|
|
|
* |
|
349
|
|
|
* @return int|null |
|
350
|
|
|
*/ |
|
351
|
|
|
public static function getParentRecord($recordId, $moduleName = false): ?int |
|
352
|
|
|
{ |
|
353
|
|
|
if (Cache::has(__METHOD__, $recordId)) { |
|
354
|
|
|
return Cache::get(__METHOD__, $recordId); |
|
355
|
|
|
} |
|
356
|
|
|
if (!$moduleName) { |
|
357
|
|
|
$moduleName = static::getType($recordId); |
|
358
|
|
|
} |
|
359
|
|
|
$parentId = null; |
|
360
|
|
|
if ($parentModules = ModuleHierarchy::getModulesMap1M($moduleName)) { |
|
361
|
|
|
foreach ($parentModules as $parentModule) { |
|
362
|
|
|
if ($field = Field::getRelatedFieldForModule($moduleName, $parentModule)) { |
|
363
|
|
|
$entity = \CRMEntity::getInstance($moduleName); |
|
364
|
|
|
$index = $entity->tab_name_index[$field['tablename']]; |
|
365
|
|
|
$parentId = (new \App\Db\Query())->select(["{$field['tablename']}.{$field['columnname']}"]) |
|
366
|
|
|
->from($field['tablename']) |
|
367
|
|
|
->innerJoin('vtiger_crmentity', "{$field['tablename']}.{$index} = vtiger_crmentity.crmid") |
|
368
|
|
|
->where(["{$field['tablename']}.{$index}" => $recordId, 'vtiger_crmentity.deleted' => 0]) |
|
369
|
|
|
->scalar(); |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
} |
|
373
|
|
|
Cache::save(__METHOD__, $recordId, $parentId); |
|
374
|
|
|
return $parentId; |
|
|
|
|
|
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Get record id by record number . |
|
379
|
|
|
* |
|
380
|
|
|
* @param string $recordNumber |
|
381
|
|
|
* @param string $moduleName |
|
382
|
|
|
* |
|
383
|
|
|
* @return int|bool |
|
384
|
|
|
*/ |
|
385
|
|
|
public static function getIdByRecordNumber(string $recordNumber, string $moduleName) |
|
386
|
|
|
{ |
|
387
|
|
|
if (Cache::staticHas(__METHOD__, $recordNumber)) { |
|
388
|
|
|
return Cache::staticGet(__METHOD__, $recordNumber); |
|
389
|
|
|
} |
|
390
|
|
|
$field = Fields\RecordNumber::getSequenceNumberField(Module::getModuleId($moduleName)); |
|
391
|
|
|
$entity = \CRMEntity::getInstance($moduleName); |
|
392
|
|
|
$index = $entity->tab_name_index[$field['tablename']]; |
|
393
|
|
|
$id = (new \App\Db\Query())->select(['vtiger_crmentity.crmid']) |
|
394
|
|
|
->from($field['tablename']) |
|
395
|
|
|
->innerJoin('vtiger_crmentity', "{$field['tablename']}.{$index} = vtiger_crmentity.crmid") |
|
396
|
|
|
->where(["{$field['tablename']}.{$field['columnname']}" => $recordNumber, 'vtiger_crmentity.deleted' => 0]) |
|
397
|
|
|
->scalar(); |
|
398
|
|
|
Cache::staticSave(__METHOD__, $recordNumber, $id); |
|
399
|
|
|
return $id; |
|
|
|
|
|
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* Get record link and label. |
|
404
|
|
|
* |
|
405
|
|
|
* @param int $id |
|
406
|
|
|
* @param string|null $moduleName |
|
407
|
|
|
* @param int|null $length |
|
408
|
|
|
* @param bool $fullUrl |
|
409
|
|
|
* |
|
410
|
|
|
* @return string |
|
411
|
|
|
*/ |
|
412
|
|
|
public static function getHtmlLink(int $id, ?string $moduleName = null, ?int $length = null, bool $fullUrl = false): string |
|
413
|
|
|
{ |
|
414
|
|
|
$state = self::getState($id); |
|
415
|
|
|
if (null === $state) { |
|
|
|
|
|
|
416
|
|
|
return '<i class="color-red-500" title="' . $id . '">' . Language::translate('LBL_RECORD_DOES_NOT_EXIST') . '</i>'; |
|
417
|
|
|
} |
|
418
|
|
|
$label = self::getLabel($id, true); |
|
419
|
|
|
if (empty($label) && 0 != $label) { |
|
420
|
|
|
return '<i class="color-red-500" title="' . $id . '">' . Language::translate('LBL_RECORD_DOES_NOT_EXIST') . '</i>'; |
|
421
|
|
|
} |
|
422
|
|
|
if (null !== $length) { |
|
423
|
|
|
$label = TextUtils::textTruncate($label, $length); |
|
424
|
|
|
} |
|
425
|
|
|
if (empty($moduleName)) { |
|
426
|
|
|
$moduleName = self::getType($id); |
|
427
|
|
|
} |
|
428
|
|
|
$deleted = 'Trash' === $state; |
|
429
|
|
|
if ($id && !Privilege::isPermitted($moduleName, 'DetailView', $id)) { |
|
430
|
|
|
return $deleted ? "<s>{$label}</s>" : $label; |
|
|
|
|
|
|
431
|
|
|
} |
|
432
|
|
|
$moduleModel = \Vtiger_Module_Model::getInstance($moduleName); |
|
433
|
|
|
$url = $moduleModel->getDetailViewUrl($id); |
|
434
|
|
|
if ($fullUrl) { |
|
435
|
|
|
$url = \Config\Main::$site_URL . $url; |
|
|
|
|
|
|
436
|
|
|
} |
|
437
|
|
|
$label = $moduleModel->getCustomLinkLabel($id, $label); |
|
438
|
|
|
$label = "<a class=\"modCT_{$moduleName} showReferenceTooltip js-popover-tooltip--record\" href=\"{$url}\">{$label}</a>"; |
|
439
|
|
|
|
|
440
|
|
|
return $deleted ? "<s>{$label}</s>" : $label; |
|
|
|
|
|
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
/** |
|
444
|
|
|
* Check if record is related in relations or inventory. |
|
445
|
|
|
* |
|
446
|
|
|
* @param \Vtiger_Record_Model $parentRecordModel |
|
447
|
|
|
* @param bool $checkRelations |
|
448
|
|
|
* @param bool $checkInventory |
|
449
|
|
|
* |
|
450
|
|
|
* @return string|false|null |
|
451
|
|
|
*/ |
|
452
|
|
|
public static function isRelated(\Vtiger_Record_Model $parentRecordModel, bool $checkRelations = true, bool $checkInventory = false) |
|
453
|
|
|
{ |
|
454
|
|
|
$parentModuleModel = $parentRecordModel->getModule(); |
|
455
|
|
|
$relatedRecord = false; |
|
456
|
|
|
if ($checkRelations) { |
|
457
|
|
|
$relations = \Vtiger_Relation_Model::getAllRelations($parentModuleModel, false, true, true); |
|
458
|
|
|
foreach ($relations as $relationModel) { |
|
459
|
|
|
$relationModel->set('parentRecord', $parentRecordModel); |
|
460
|
|
|
$queryGenerator = $relationModel->getQuery(); |
|
461
|
|
|
$queryGenerator->permissions = false; |
|
462
|
|
|
$queryGenerator->clearFields()->setFields(['id']); |
|
463
|
|
|
if ($result = $queryGenerator->createQuery()->scalar()) { |
|
464
|
|
|
$relatedRecord = $result; |
|
465
|
|
|
break; |
|
466
|
|
|
} |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
if ($checkInventory && !$relatedRecord) { |
|
470
|
|
|
$recordId = $parentRecordModel->getId(); |
|
471
|
|
|
$parentModuleName = $parentModuleModel->getName(); |
|
472
|
|
|
$allModules = \vtlib\Functions::getAllModules(false, true); |
|
473
|
|
|
foreach ($allModules as $moduleData) { |
|
474
|
|
|
$moduleModel = \Vtiger_Module_Model::getInstance($moduleData['name']); |
|
475
|
|
|
$inventoryModel = \Vtiger_Inventory_Model::getInstance($moduleData['name']); |
|
|
|
|
|
|
476
|
|
|
if ($moduleModel->isInventory() |
|
477
|
|
|
&& ($inventoryModel = \Vtiger_Inventory_Model::getInstance($moduleData['name'])) |
|
478
|
|
|
&& ($inventoryNameField = $inventoryModel->getField('name')) |
|
479
|
|
|
&& ($modules = $inventoryNameField->getModules()) |
|
|
|
|
|
|
480
|
|
|
&& \in_array($parentModuleName, $modules) |
|
481
|
|
|
) { |
|
482
|
|
|
$inventoryTable = $inventoryModel->getDataTableName(); |
|
483
|
|
|
$result = (new \App\Db\Query())->select(['crmid'])->from($inventoryTable)->where(['name' => $recordId])->scalar(); |
|
484
|
|
|
if ($result) { |
|
485
|
|
|
$relatedRecord = $result; |
|
486
|
|
|
break; |
|
487
|
|
|
} |
|
488
|
|
|
} |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
return $relatedRecord; |
|
492
|
|
|
} |
|
493
|
|
|
} |
|
494
|
|
|
|