-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathObjectRepository.php
More file actions
349 lines (290 loc) · 9.35 KB
/
ObjectRepository.php
File metadata and controls
349 lines (290 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Service\Base;
use cmdbAbstractObject;
use Combodo\iTop\Core\MetaModel\FriendlyNameType;
use DBObject;
use DBObjectSearch;
use DBObjectSet;
use DBSearch;
use Exception;
use ExceptionLog;
use iDBObjectSetIterator;
use MetaModel;
use utils;
use WizardHelper;
/**
* Class ObjectRepository
*
*
* @internal
* @since 3.1.0
* @package Combodo\iTop\Service\Base
*/
class ObjectRepository
{
/**
* Search.
*
* @param string $sObjectClass Object class to search
* @param array $aFieldsToLoad Additional fields to load
* @param string $sSearch Friendly name search string
*
* @return array|null
*/
public static function Search(string $sObjectClass, array $aFieldsToLoad, string $sSearch): ?array
{
try {
// Create db search
$oDbObjectSearch = new DBObjectSearch($sObjectClass);
$oDbObjectSearch->SetShowObsoleteData(utils::ShowObsoleteData());
// Add a friendly name search condition
$oDbObjectSearch->AddCondition('friendlyname', $sSearch, 'Contains');
// Create db object set
$oSet = new DBObjectSet($oDbObjectSearch);
// Transform set to array
$aResult = ObjectRepository::DBSetToObjectArray($oSet, $sObjectClass, $aFieldsToLoad);
// Handle max results for autocomplete
if (Utils::IsNullOrEmptyString($sSearch)
&& count($aResult) > MetaModel::GetConfig()->Get('max_autocomplete_results')) {
return [];
}
return $aResult;
} catch (Exception $e) {
ExceptionLog::LogException($e);
return null;
}
}
/**
* SearchFromOql.
*
* @param string $sObjectClass Object class to search
* @param array $aFieldsToLoad Additional fields to load
* @param string $sOql Oql expression
* @param string $sSearch Friendly name search string
* @param DBObject|null $oThisObject This object reference for oql
* @param int $iLimit Limit results to the $iLimit first elements
*
* @return array|null
* @since 3.2.0 Add $iLimit parameter
*/
public static function SearchFromOql(string $sObjectClass, array $aFieldsToLoad, string $sOql, string $sSearch, DBObject $oThisObject = null, int $iLimit = 0): ?array
{
try {
// Create db search
$oDbObjectSearch = DBSearch::FromOQL($sOql);
$oDbObjectSearch->SetShowObsoleteData(utils::ShowObsoleteData());
$oDbObjectSearch->AddCondition('friendlyname', $sSearch, 'Contains');
// Create db set from db search
$oDbObjectSet = new DBObjectSet($oDbObjectSearch, [], ['this' => $oThisObject]);
// Limit results
if ($iLimit > 0) {
$oDbObjectSet->SetLimit($iLimit);
}
// return object array
return ObjectRepository::DBSetToObjectArray($oDbObjectSet, $sObjectClass, $aFieldsToLoad);
} catch (Exception $e) {
ExceptionLog::LogException($e);
return null;
}
}
/**
* DBSetToObjectArray.
*
* @param iDBObjectSetIterator $oDbObjectSet Db object set
* @param string $sObjectClass Object class
* @param array $aFieldsToLoad Additional fields to load
*
* @return array
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
private static function DBSetToObjectArray(iDBObjectSetIterator $oDbObjectSet, string $sObjectClass, array $aFieldsToLoad): array
{
// Retrieve friendly name complementary specification
$aComplementAttributeSpec = MetaModel::GetNameSpec($sObjectClass, FriendlyNameType::COMPLEMENTARY);
// Retrieve image attribute code
$sObjectImageAttCode = MetaModel::GetImageAttributeCode($sObjectClass);
// Prepare fields to load
$aDefaultFieldsToLoad = ObjectRepository::GetDefaultFieldsToLoad($aComplementAttributeSpec, $sObjectImageAttCode);
$aFieldsToLoad = array_merge($aDefaultFieldsToLoad, $aFieldsToLoad);
// Optimize columns load
$oDbObjectSet->OptimizeColumnLoad([
$sObjectClass => $aFieldsToLoad,
]);
// Prepare result
$aResult = [];
// Iterate throw objects...
$oDbObjectSet->Rewind();
while ($oObject = $oDbObjectSet->Fetch()) {
// Compute others data
$aResult[] = self::ConvertObjectToArray($oObject, $sObjectClass, $aFieldsToLoad, $aComplementAttributeSpec, $sObjectImageAttCode);
}
return $aResult;
}
/**
* GetDefaultFieldsToLoad.
*
* Return attributes to load for any objects.
*
* @param array $aComplementAttributeSpec Friendly name complementary spec
* @param string $sObjectImageAttCode Image attribute code
*
* @return mixed
*/
public static function GetDefaultFieldsToLoad(array $aComplementAttributeSpec, string $sObjectImageAttCode)
{
// Friendly name complementary fields
$aFieldsToLoad = $aComplementAttributeSpec[1];
// Image attribute
if (!empty($sObjectImageAttCode)) {
$aFieldsToLoad[] = $sObjectImageAttCode;
}
// Add friendly name
$aFieldsToLoad[] = 'friendlyname';
return $aFieldsToLoad;
}
/**
* ComputeOthersData.
*
* @param DBObject $oDbObject Db object
* @param string $sClass Object class
* @param array $aData Object data to fill
* @param array $aComplementAttributeSpec Friendly name complementary spec
* @param string $sObjectImageAttCode Image attribute code
*
* @return array
*/
public static function ComputeOthersData(DBObject $oDbObject, string $sClass, array $aData, array $aComplementAttributeSpec, string $sObjectImageAttCode): array
{
try {
// Object key
$aData['id'] = $oDbObject->GetKey();
// Object class
$aData['class_name'] = get_class($oDbObject);
// Obsolescence flag
$aData['obsolescence_flag'] = $oDbObject->IsObsolete();
// Additional fields
$sFriendlyNameForHtml = utils::EscapeHtml($aData['friendlyname']);
if (count($aComplementAttributeSpec[1]) > 0) {
$aData['has_additional_field'] = true;
$aArguments = [];
foreach ($aComplementAttributeSpec[1] as $sAdditionalField) {
//getAsCSV to have user friendly value in text format
$aArguments[] = $oDbObject->GetAsCSV($sAdditionalField,' ','');
}
$aData['additional_field'] = utils::VSprintf($aComplementAttributeSpec[0], $aArguments);
$sAdditionalFieldForHtml = utils::EscapeHtml($aData['additional_field']);
$aData['full_description'] = "{$sFriendlyNameForHtml}<br><i><small>{$sAdditionalFieldForHtml}</small></i>";
} else {
$aData['full_description'] = $sFriendlyNameForHtml;
}
// Image
if (!empty($sObjectImageAttCode)) {
$aData['has_image'] = true;
/** @var \ormDocument $oImage */
$oImage = $oDbObject->Get($sObjectImageAttCode);
if (!$oImage->IsEmpty()) {
$aData['picture_url'] = "url('{$oImage->GetDisplayURL($sClass, $oDbObject->GetKey(), $sObjectImageAttCode)}')";
$aData['initials'] = '';
} else {
$aData['initials'] = utils::FormatInitialsForMedallion(utils::ToAcronym($oDbObject->Get('friendlyname')));
}
}
// Link
$aData['link'] = utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=details&class=$sClass&id={$oDbObject->GetKey()}";
return $aData;
} catch (Exception $e) {
ExceptionLog::LogException($e);
return $aData;
}
}
/**
* GetObjectFromWizardHelperData
*
* @param string $sData
*
* @return DBObject|null
*/
public static function GetObjectFromWizardHelperData(string $sData): ?DBObject
{
try {
$oThisObj = null;
if ($sData != null) {
$oWizardHelper = WizardHelper::FromJSON($sData);
$oThisObj = $oWizardHelper->GetTargetObject();
}
return $oThisObj;
} catch (Exception $e) {
return null;
}
}
/**
* ConvertObjectToArray.
*
* @param DBObject $oObject
* @param string $sObjectClass
* @param array|null $aFieldsToLoad
* @param array|null $aComplementAttributeSpec
* @param string|null $sObjectImageAttCode
*
* @return array
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
public static function ConvertObjectToArray(DBObject $oObject, string $sObjectClass, array $aFieldsToLoad = null, array $aComplementAttributeSpec = null, string $sObjectImageAttCode = null): array
{
// Retrieve friendly name complementary specification
if ($aComplementAttributeSpec === null) {
$aComplementAttributeSpec = MetaModel::GetNameSpec($sObjectClass, FriendlyNameType::COMPLEMENTARY);
}
// Retrieve image attribute code
if ($sObjectImageAttCode === null) {
$sObjectImageAttCode = MetaModel::GetImageAttributeCode($sObjectClass);
}
// Fields to load
if ($aFieldsToLoad === null) {
$aFieldsToLoad = self::GetDefaultFieldsToLoad($aComplementAttributeSpec, $sObjectImageAttCode);
}
// Prepare objet data
$aObjectData = [];
// Object key
$aObjectData['key'] = $oObject->GetKey();
// Fill loaded columns...
foreach ($aFieldsToLoad as $sField) {
$aObjectData[$sField] = $oObject->Get($sField);
}
// Compute others data
return ObjectRepository::ComputeOthersData($oObject, $sObjectClass, $aObjectData, $aComplementAttributeSpec, $sObjectImageAttCode);
}
/**
* DeleteFromOql.
*
* @param string $sOql OQL expression
*
* @return bool
*/
public static function DeleteFromOql(string $sOql): bool
{
try {
// Create db search
$oDbObjectSearch = DBSearch::FromOQL($sOql);
// Create db set from db search
$oDbObjectSet = new DBObjectSet($oDbObjectSearch);
// Delete objects
while ($oObject = $oDbObjectSet->Fetch()) {
$oObject->DBDelete();
}
// return operation success
return true;
} catch (Exception $e) {
ExceptionLog::LogException($e);
return false;
}
}
}