-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathmetamodel.class.php
More file actions
7758 lines (7089 loc) · 234 KB
/
metamodel.class.php
File metadata and controls
7758 lines (7089 loc) · 234 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Copyright (c) 2010-2024 Combodo SAS
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// iTop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
//
use Combodo\iTop\Application\EventRegister\ApplicationEvents;
use Combodo\iTop\Core\MetaModel\FriendlyNameType;
use Combodo\iTop\Service\Events\EventData;
use Combodo\iTop\Service\Events\EventService;
require_once APPROOT.'core/modulehandler.class.inc.php';
require_once APPROOT.'core/querymodifier.class.inc.php';
require_once APPROOT.'core/metamodelmodifier.inc.php';
require_once APPROOT.'core/computing.inc.php';
require_once APPROOT.'core/relationgraph.class.inc.php';
require_once APPROOT.'core/apc-compat.php';
require_once APPROOT.'core/expressioncache.class.inc.php';
/**
* We need to have all iLoginFSMExtension/iLoginUIExtension impl loaded ! Cannot use autoloader...
*/
require_once APPROOT.'application/loginform.class.inc.php';
require_once APPROOT.'application/loginbasic.class.inc.php';
require_once APPROOT.'application/logindefault.class.inc.php';
require_once APPROOT.'application/loginexternal.class.inc.php';
require_once APPROOT.'application/loginurl.class.inc.php';
/**
* Metamodel
*
* @copyright Copyright (C) 2010-2024 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
/**
* @package iTopORM
*/
define('ENUM_PARENT_CLASSES_EXCLUDELEAF', 1);
/**
* @package iTopORM
*/
define('ENUM_PARENT_CLASSES_ALL', 2);
/**
* Specifies that this attribute is visible/editable.... normal (default config)
*
* @package iTopORM
*/
define('OPT_ATT_NORMAL', 0);
/**
* Specifies that this attribute is hidden in that state
*
* @package iTopORM
*/
define('OPT_ATT_HIDDEN', 1);
/**
* Specifies that this attribute is not editable in that state
*
* @package iTopORM
*/
define('OPT_ATT_READONLY', 2);
/**
* Specifieds that the attribute must be set (different than default value?) when arriving into that state
*
* @package iTopORM
*/
define('OPT_ATT_MANDATORY', 4);
/**
* Specifies that the attribute must change when arriving into that state
*
* @package iTopORM
*/
define('OPT_ATT_MUSTCHANGE', 8);
/**
* Specifies that the attribute must be proposed when arriving into that state
*
* @package iTopORM
*/
define('OPT_ATT_MUSTPROMPT', 16);
/**
* Specifies that the attribute is in 'slave' mode compared to one data exchange task:
* it should not be edited inside iTop anymore
*
* @package iTopORM
*/
define('OPT_ATT_SLAVE', 32);
/**
* DB Engine -should be moved into CMDBSource
*
* Used to be myisam, the switch was made with r798
*
* @package iTopORM
*/
define('MYSQL_ENGINE', 'innodb');
/**
* The objects definitions as well as their mapping to the database
*
* @api
* @package iTopORM
*/
abstract class MetaModel
{
///////////////////////////////////////////////////////////////////////////
//
// STATIC Members
//
///////////////////////////////////////////////////////////////////////////
/** @var bool */
private static $m_bTraceSourceFiles = false;
/** @var array */
private static $m_aClassToFile = array();
/** @var string */
protected static $m_sEnvironment = 'production';
/**
* Objects currently created/updated.
*
* if an object is already being updated, then this method will return this object instead of recreating a new one.
* At this point the method DBUpdate of a new object with the same class and id won't do anything due to reentrance protection,
* so to ensure that the potential modifications are correctly saved, the object currently being updated is returned.
* DBUpdate() method then will take care that all the modifications will be saved.
*
* [class][key] -> object
*/
protected static array $m_aReentranceProtection = [];
/**
* MetaModel constructor.
*/
public function __construct()
{
}
/**
* @return array
*/
public static function GetClassFiles()
{
return self::$m_aClassToFile;
}
//
/**
* Purpose: workaround the following limitation = PHP5 does not allow to know the class (derived
* from the current one) from which a static function is called (__CLASS__ and self are
* interpreted during parsing)
*
* @param string $sExpectedFunctionName
* @param bool $bRecordSourceFile
*
* @return string
*/
private static function GetCallersPHPClass($sExpectedFunctionName = null, $bRecordSourceFile = false)
{
$aBacktrace = debug_backtrace();
// $aBacktrace[0] is where we are
// $aBacktrace[1] is the caller of GetCallersPHPClass
// $aBacktrace[1] is the info we want
if (!empty($sExpectedFunctionName))
{
assert($aBacktrace[2]['function'] == $sExpectedFunctionName);
}
if ($bRecordSourceFile)
{
self::$m_aClassToFile[$aBacktrace[2]["class"]] = $aBacktrace[1]["file"];
}
return $aBacktrace[2]["class"];
}
// Static init -why and how it works
//
// We found the following limitations:
//- it is not possible to define non scalar constants
//- it is not possible to declare a static variable as '= new myclass()'
// Then we had do propose this model, in which a derived (non abstract)
// class should implement Init(), to call InheritAttributes or AddAttribute.
/**
* @param string $sClass
*
* @throws \CoreException
*/
private static function _check_subclass($sClass)
{
// See also IsValidClass()... ???? #@#
// class is mandatory
// (it is not possible to guess it when called as myderived::...)
if (!array_key_exists($sClass, self::$m_aClassParams))
{
throw new CoreException("Unknown class '$sClass'");
}
}
public static function static_var_dump()
{
var_dump(get_class_vars(__CLASS__));
}
/** @var Config m_oConfig */
private static $m_oConfig = null;
/** @var array */
protected static $m_aModulesParameters = array();
/** @var bool */
private static $m_bSkipCheckToWrite = false;
/** @var bool */
private static $m_bSkipCheckExtKeys = false;
/** @var bool */
private static $m_bUseAPCCache = false;
/** @var bool */
private static $m_bLogIssue = false;
/** @var bool */
private static $m_bLogNotification = false;
/** @var bool */
private static $m_bLogWebService = false;
/**
* @return bool the current flag value
*/
public static function SkipCheckToWrite()
{
return self::$m_bSkipCheckToWrite;
}
/**
* @return bool the current flag value
*/
public static function SkipCheckExtKeys()
{
return self::$m_bSkipCheckExtKeys;
}
/**
* @return bool the current flag value
*/
public static function IsLogEnabledIssue()
{
return self::$m_bLogIssue;
}
/**
* @return bool the current flag value
*/
public static function IsLogEnabledNotification()
{
return self::$m_bLogNotification;
}
/**
* @return bool the current flag value
*/
public static function IsLogEnabledWebService()
{
return self::$m_bLogWebService;
}
/** @var string */
private static $m_sDBName = "";
/**
* table prefix for the current application instance (allow several applications on the same DB)
*
* @var string
*/
private static $m_sTablePrefix = "";
/** @var array */
private static $m_Category2Class = array();
/**
* array of "classname" => "rootclass"
*
* @var array
*/
private static $m_aRootClasses = array();
/**
* array of ("classname" => array of "parentclass")
*
* @var array
*/
private static $m_aParentClasses = array();
/**
* array of ("classname" => array of "childclass")
*
* @var array
*/
private static $m_aChildClasses = array();
/**
* array of ("classname" => array of class information)
*
* @var array
*/
private static $m_aClassParams = array();
/**
* array of ("classname" => array of highlightscale information)
*
* @var array
*/
private static $m_aHighlightScales = array();
/**
* @param string $sRefClass
*
* @return string
*/
public static function GetParentPersistentClass($sRefClass)
{
$sClass = get_parent_class($sRefClass);
if (!$sClass) {
return '';
}
if ($sClass == 'DBObject') {
return '';
} // Warning: __CLASS__ is lower case in my version of PHP
// Note: the UI/business model may implement pure PHP classes (intermediate layers)
if (array_key_exists($sClass, self::$m_aClassParams)) {
return $sClass;
}
return self::GetParentPersistentClass($sClass);
}
/**
* @param string $sClass
*
* @return string
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
final public static function GetName($sClass)
{
self::_check_subclass($sClass);
return call_user_func([$sClass, 'GetClassName'], $sClass);
}
/**
* @param string $sClass
*
* @return string
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
final public static function GetName_Obsolete($sClass)
{
// Written for compatibility with a data model written prior to version 0.9.1
self::_check_subclass($sClass);
if (array_key_exists('name', self::$m_aClassParams[$sClass])) {
return self::$m_aClassParams[$sClass]['name'];
} else {
return self::GetName($sClass);
}
}
/**
* @param string $sClassLabel
* @param bool $bCaseSensitive
*
* @return null
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
final public static function GetClassFromLabel($sClassLabel, $bCaseSensitive = true)
{
foreach (self::GetClasses() as $sClass) {
if ($bCaseSensitive) {
if (self::GetName($sClass) == $sClassLabel) {
return $sClass;
}
} else {
if (strcasecmp(self::GetName($sClass), $sClassLabel) == 0) {
return $sClass;
}
}
}
return null;
}
/**
* @param string $sClass
*
* @return string
* @throws \CoreException
*/
final public static function GetCategory($sClass)
{
self::_check_subclass($sClass);
return self::$m_aClassParams[$sClass]["category"];
}
/**
* @param string $sClass
* @param string $sCategory
*
* @return bool
* @throws \CoreException
*/
final public static function HasCategory($sClass, $sCategory)
{
self::_check_subclass($sClass);
return (strpos(self::$m_aClassParams[$sClass]["category"], $sCategory) !== false);
}
/**
* @param string $sClass
*
* @return string
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
final public static function GetClassDescription($sClass)
{
self::_check_subclass($sClass);
return call_user_func([$sClass, 'GetClassDescription'], $sClass);
}
/**
* @param string $sClass
*
* @return string
* @throws \CoreException
* @throws \DictExceptionMissingString
*/
final public static function GetClassDescription_Obsolete($sClass)
{
// Written for compatibility with a data model written prior to version 0.9.1
self::_check_subclass($sClass);
if (array_key_exists('description', self::$m_aClassParams[$sClass])) {
return self::$m_aClassParams[$sClass]['description'];
} else {
return self::GetClassDescription($sClass);
}
}
/**
* @param string $sClass
* @param bool $bImgTag Whether to surround the icon URL with an HTML IMG tag or not
* @param string $sMoreStyles Additional inline CSS style to add to the IMG tag. Only used if $bImgTag is set to true
*
* @return string Absolute URL the class icon
* @throws \CoreException
*/
final public static function GetClassIcon($sClass, $bImgTag = true, $sMoreStyles = '')
{
self::_check_subclass($sClass);
$sIcon = '';
if (array_key_exists('style', self::$m_aClassParams[$sClass])) {
/** @var ormStyle $oStyle */
$oStyle = self::$m_aClassParams[$sClass]['style'];
$sIcon = $oStyle->GetIconAsAbsUrl();
}
if (utils::IsNullOrEmptyString($sIcon)) {
$sParentClass = self::GetParentPersistentClass($sClass);
if (strlen($sParentClass) > 0) {
return self::GetClassIcon($sParentClass, $bImgTag, $sMoreStyles);
}
}
$sIcon = str_replace('/modules/', '/env-'.self::$m_sEnvironment.'/', $sIcon ?? ''); // Support of pre-2.0 modules
if ($bImgTag && ($sIcon != '')) {
$sIcon = "<img src=\"$sIcon\" style=\"vertical-align:middle;$sMoreStyles\"/>";
}
return $sIcon;
}
/**
* @param string $sClass
*
* @return ormStyle|null
* @throws \CoreException
*
* @since 3.0
*/
final public static function GetClassStyle($sClass)
{
self::_check_subclass($sClass);
if (array_key_exists('style', self::$m_aClassParams[$sClass])) {
$oStyle = self::$m_aClassParams[$sClass]['style'];
} else {
// Create empty style
$oStyle = new ormStyle("ibo-class-style--$sClass", "ibo-class-style-alt--$sClass");
}
if (utils::IsNotNullOrEmptyString($oStyle->GetMainColor()) && utils::IsNotNullOrEmptyString($oStyle->GetComplementaryColor()) && utils::IsNotNullOrEmptyString($oStyle->GetIconAsRelPath())) {
// all the parameters are set, no need to search in the parent classes
return $oStyle;
}
// Search missing parameters in the parent classes
$sParentClass = self::GetParentPersistentClass($sClass);
while (strlen($sParentClass) > 0) {
$oParentStyle = self::GetClassStyle($sParentClass);
if (!is_null($oParentStyle)) {
if (utils::IsNullOrEmptyString($oStyle->GetMainColor())) {
$oStyle->SetMainColor($oParentStyle->GetMainColor());
$oStyle->SetStyleClass($oParentStyle->GetStyleClass());
}
if (utils::IsNullOrEmptyString($oStyle->GetComplementaryColor())) {
$oStyle->SetComplementaryColor($oParentStyle->GetComplementaryColor());
$oStyle->SetAltStyleClass($oParentStyle->GetAltStyleClass());
}
if (utils::IsNullOrEmptyString($oStyle->GetIconAsRelPath())) {
$oStyle->SetIcon($oParentStyle->GetIconAsRelPath());
}
if (utils::IsNotNullOrEmptyString($oStyle->GetMainColor()) && utils::IsNotNullOrEmptyString($oStyle->GetComplementaryColor()) && utils::IsNotNullOrEmptyString($oStyle->GetIconAsRelPath())) {
// all the parameters are set, no need to search in the parent classes
return $oStyle;
}
}
$sParentClass = self::GetParentPersistentClass($sParentClass);
}
if (utils::IsNullOrEmptyString($oStyle->GetMainColor()) && utils::IsNullOrEmptyString($oStyle->GetComplementaryColor()) && utils::IsNullOrEmptyString($oStyle->GetIconAsRelPath())) {
return null;
}
return $oStyle;
}
/**
* @param string $sClass
*
* @return bool
* @throws \CoreException
*/
final public static function IsAutoIncrementKey($sClass)
{
self::_check_subclass($sClass);
return (self::$m_aClassParams[$sClass]["key_type"] == "autoincrement");
}
/**
* @param string $sClass
*
* @return bool
* @throws \CoreException
*/
final public static function IsArchivable($sClass)
{
self::_check_subclass($sClass);
return self::$m_aClassParams[$sClass]["archive"];
}
/**
* @param string $sClass
*
* @return bool
* @throws \CoreException
*/
final public static function IsObsoletable($sClass)
{
self::_check_subclass($sClass);
return (!is_null(self::$m_aClassParams[$sClass]['obsolescence_expression']));
}
/**
* @param string $sClass
*
* @return \Expression
* @throws \CoreException
*/
final public static function GetObsolescenceExpression($sClass)
{
if (self::IsObsoletable($sClass)) {
self::_check_subclass($sClass);
$sOql = self::$m_aClassParams[$sClass]['obsolescence_expression'];
$oRet = Expression::FromOQL("COALESCE($sOql, 0)");
} else {
$oRet = Expression::FromOQL("0");
}
return $oRet;
}
/**
* @param string $sClass
* @param bool $bClassDefinitionOnly if true then will only return properties defined in the specified class on not the properties
* from its parent classes
*
* @return array rule id as key, rule properties as value
* @throws \CoreException
* @since 2.6.0 N°659 uniqueness constraint
* @see #SetUniquenessRuleRootClass that fixes a specific 'root_class' property to know which class is root per rule
*/
final public static function GetUniquenessRules($sClass, $bClassDefinitionOnly = false)
{
if (!isset(self::$m_aClassParams[$sClass]))
{
return array();
}
$aCurrentUniquenessRules = array();
if (array_key_exists('uniqueness_rules', self::$m_aClassParams[$sClass]))
{
$aCurrentUniquenessRules = self::$m_aClassParams[$sClass]['uniqueness_rules'];
}
if ($bClassDefinitionOnly)
{
return $aCurrentUniquenessRules;
}
$sParentClass = self::GetParentClass($sClass);
if ($sParentClass)
{
$aParentUniquenessRules = self::GetUniquenessRules($sParentClass);
foreach ($aParentUniquenessRules as $sUniquenessRuleId => $aParentUniquenessRuleProperties)
{
$bCopyDisabledKey = true;
$bCurrentDisabledValue = null;
if (array_key_exists($sUniquenessRuleId, $aCurrentUniquenessRules))
{
if (self::IsUniquenessRuleContainingOnlyDisabledKey($aCurrentUniquenessRules[$sUniquenessRuleId]))
{
$bCopyDisabledKey = false;
}
else
{
continue;
}
}
$aMergedUniquenessProperties = $aParentUniquenessRuleProperties;
if (!$bCopyDisabledKey)
{
$aMergedUniquenessProperties['disabled'] = $aCurrentUniquenessRules[$sUniquenessRuleId]['disabled'];
}
$aCurrentUniquenessRules[$sUniquenessRuleId] = $aMergedUniquenessProperties;
}
}
return $aCurrentUniquenessRules;
}
/**
* @param string $sRootClass
* @param string $sRuleId
*
* @throws \CoreException
* @since 2.6.1 N°1968 (sous les pavés, la plage) initialize in 'root_class' property the class that has the first
* definition of the rule in the hierarchy
*/
private static function SetUniquenessRuleRootClass($sRootClass, $sRuleId)
{
foreach (self::EnumChildClasses($sRootClass, ENUM_CHILD_CLASSES_ALL) as $sClass)
{
self::$m_aClassParams[$sClass]['uniqueness_rules'][$sRuleId]['root_class'] = $sClass;
}
}
/**
* @param string $sRuleId
* @param string $sLeafClassName
*
* @return string name of the class, null if not present
*/
final public static function GetRootClassForUniquenessRule($sRuleId, $sLeafClassName)
{
$sFirstClassWithRuleId = null;
if (isset(self::$m_aClassParams[$sLeafClassName]['uniqueness_rules'][$sRuleId]))
{
$sFirstClassWithRuleId = $sLeafClassName;
}
$sParentClass = self::GetParentClass($sLeafClassName);
if ($sParentClass)
{
$sParentClassWithRuleId = self::GetRootClassForUniquenessRule($sRuleId, $sParentClass);
if (!is_null($sParentClassWithRuleId))
{
$sFirstClassWithRuleId = $sParentClassWithRuleId;
}
}
return $sFirstClassWithRuleId;
}
/**
* @param string $sRootClass
* @param string $sRuleId
*
* @return string[] child classes with the rule disabled, and that are concrete classes
*
* @throws \CoreException
* @since 2.6.1 N°1968 (soyez réalistes, demandez l'impossible)
*/
final public static function GetChildClassesWithDisabledUniquenessRule($sRootClass, $sRuleId)
{
$aClassesWithDisabledRule = array();
foreach (self::EnumChildClasses($sRootClass, ENUM_CHILD_CLASSES_EXCLUDETOP) as $sChildClass)
{
if (array_key_exists($sChildClass, $aClassesWithDisabledRule))
{
continue;
}
if (!array_key_exists('uniqueness_rules', self::$m_aClassParams[$sChildClass]))
{
continue;
}
if (!array_key_exists($sRuleId, self::$m_aClassParams[$sChildClass]['uniqueness_rules']))
{
continue;
}
if (self::$m_aClassParams[$sChildClass]['uniqueness_rules'][$sRuleId]['disabled'] === true)
{
$aDisabledClassChildren = self::EnumChildClasses($sChildClass, ENUM_CHILD_CLASSES_ALL);
foreach ($aDisabledClassChildren as $sDisabledClassChild)
{
if (!self::IsAbstract($sDisabledClassChild))
{
$aClassesWithDisabledRule[] = $sDisabledClassChild;
}
}
}
}
return $aClassesWithDisabledRule;
}
/**
* @param array $aRuleProperties
*
* @return bool
* @since 2.6.0 N°659 uniqueness constraint
*/
private static function IsUniquenessRuleContainingOnlyDisabledKey($aRuleProperties)
{
$aNonNullRuleProperties = array_filter($aRuleProperties, function ($v) {
return (!is_null($v));
});
return ((count($aNonNullRuleProperties) == 1) && (array_key_exists('disabled', $aNonNullRuleProperties)));
}
/**
* @param string $sClass
* @param string $sType {@see \Combodo\iTop\Core\MetaModel\FriendlyNameType}
*
* @return array
* @throws \CoreException
* @throws \DictExceptionMissingString
*
* @since 3.0.0 N°580 New $sType parameter
*/
final public static function GetNameSpec($sClass, $sType = FriendlyNameType::SHORT)
{
self::_check_subclass($sClass);
switch ($sType) {
case FriendlyNameType::COMPLEMENTARY:
if (!isset(self::$m_aClassParams[$sClass]["complementary_name_attcode"])) {
return [$sClass, []];
}
$nameRawSpec = self::$m_aClassParams[$sClass]["complementary_name_attcode"];
$sDictName = 'ComplementaryName';
break;
case FriendlyNameType::LONG:
$nameRawSpec = self::$m_aClassParams[$sClass]["name_attcode"];
if (!isset(self::$m_aClassParams[$sClass]["complementary_name_attcode"])) {
return self::GetNameSpec($sClass, FriendlyNameType::SHORT);
}
$complementaryNameRawSpec = self::$m_aClassParams[$sClass]["complementary_name_attcode"];
if (is_array($nameRawSpec)) {
if (is_array($complementaryNameRawSpec)) {
$nameRawSpec = merge($nameRawSpec, $complementaryNameRawSpec);
} elseif (!empty($nameRawSpec)) {
$nameRawSpec = merge($nameRawSpec, [$complementaryNameRawSpec]);
}
} elseif (empty($nameRawSpec)) {
$nameRawSpec = $complementaryNameRawSpec;
} else {
if (is_array($complementaryNameRawSpec)) {
$nameRawSpec = merge([$nameRawSpec], $complementaryNameRawSpec);
} elseif (!empty($nameRawSpec)) {
$nameRawSpec = [$nameRawSpec, $complementaryNameRawSpec];
}
}
$sDictName = 'LongName';
break;
default:
$nameRawSpec = self::$m_aClassParams[$sClass]["name_attcode"];
$sDictName = 'Name';
}
if (is_array($nameRawSpec)) {
$sFormat = Dict::S("Class:$sClass/$sDictName", '');
if (strlen($sFormat) == 0) {
// Default to "%1$s %2$s..."
for ($i = 1; $i <= count($nameRawSpec); $i++) {
if (empty($sFormat)) {
$sFormat .= '%'.$i.'$s';
} else {
$sFormat .= ' %'.$i.'$s';
}
}
}
return [$sFormat, $nameRawSpec];
} elseif (empty($nameRawSpec)) {
return [$sClass, []];
} else {
// string -> attcode
return ['%1$s', [$nameRawSpec]];
}
}
/**
*
* @param string $sClass
* @param bool $bWithAttributeDefinition
* @param string $sType {@see \Combodo\iTop\Core\MetaModel\FriendlyNameType}
*
* @return array of attribute codes used by friendlyname
* @throws \CoreException
* @since 3.0.0
*/
final public static function GetNameAttributes(string $sClass, $bWithAttributeDefinition = false, $sType = FriendlyNameType::SHORT): array
{
self::_check_subclass($sClass);
$aNameAttCodes = [];
if ($sType == FriendlyNameType::SHORT || FriendlyNameType::LONG) {
$rawNameAttCodes = self::$m_aClassParams[$sClass]["name_attcode"];
if (!is_array($rawNameAttCodes)) {
if (self::IsValidAttCode($sClass, $rawNameAttCodes)) {
$aNameAttCodes[] = $rawNameAttCodes;
}
} else {
$aNameAttCodes = $rawNameAttCodes;
}
}
if ($sType == FriendlyNameType::COMPLEMENTARY || FriendlyNameType::LONG) {
$rawNameAttCodes = self::$m_aClassParams[$sClass]["complementary_name_attcode"];
if (!isEmpty($rawNameAttCodes)) {
if (!is_array($rawNameAttCodes)) {
if (self::IsValidAttCode($sClass, $rawNameAttCodes)) {
$aNameAttCodes[] = array_merge($aNameAttCodes, [$rawNameAttCodes]);
}
} else {
$aNameAttCodes = array_merge($rawNameAttCodes, $rawNameAttCodes);
}
}
}
if ($bWithAttributeDefinition) {
$aResults = [];
foreach ($aNameAttCodes as $sAttCode) {
$aResults[$sAttCode] = self::GetAttributeDef($sClass, $sAttCode);
}
return $aResults;
}
return $aNameAttCodes;
}
/**
* @param string $sClass
* @param false $bWithAttributeDefinition
*
* @return array of attributes to always reload in tables
* @throws \CoreException
* @since 3.0.0
*/
final public static function GetAttributesToAlwaysLoadInTables(string $sClass, $bWithAttributeDefinition = false): array
{
$aResults = [];
foreach (self::GetAttributesList($sClass) as $sAttCode) {
$oAttDef = self::GetAttributeDef($sClass, $sAttCode);
if ($oAttDef->AlwaysLoadInTables()) {
if ($bWithAttributeDefinition) {
$aResults[$sAttCode] = $oAttDef;
} else {
$aResults[] = $sAttCode;
}
}
}
return $aResults;
}
/**
* Get the friendly name expression for a given class
*
* @param string $sClass
* @param string $sType {@see \Combodo\iTop\Core\MetaModel\FriendlyNameType}
*
* @return Expression
* @throws \CoreException
* @throws \DictExceptionMissingString
*
* @since 3.0.0 N°580 New $sType parameter
*/
final public static function GetNameExpression($sClass, $sType = FriendlyNameType::SHORT)
{
$aNameSpec = self::GetNameSpec($sClass, $sType);
$sFormat = $aNameSpec[0];
$aAttributes = $aNameSpec[1];
$aPieces = preg_split('/%([0-9])\\$s/', $sFormat, -1, PREG_SPLIT_DELIM_CAPTURE);
$aExpressions = array();
foreach ($aPieces as $i => $sPiece) {
if ($i & 1) {
// $i is ODD - sPiece is a delimiter
//
$iReplacement = (int)$sPiece - 1;
if (isset($aAttributes[$iReplacement])) {
$sAttCode = $aAttributes[$iReplacement];
$aExpressions[] = new FieldExpression($sAttCode);
}
} else
{
// $i is EVEN - sPiece is a literal
//
if (strlen($sPiece) > 0)
{
$aExpressions[] = new ScalarExpression($sPiece);
}
}
}
return new CharConcatExpression($aExpressions);
}
/**
* @param string $sClass
* @param string $sType {@see \Combodo\iTop\Core\MetaModel\FriendlyNameType}
*
* @return string The friendly name IIF it is equivalent to a single attribute
* @throws \CoreException
* @throws \DictExceptionMissingString
*
* @since 3.0.0 N°580 New $sType parameter
*/
final public static function GetFriendlyNameAttributeCode($sClass, $sType = FriendlyNameType::SHORT)
{
$aNameSpec = self::GetNameSpec($sClass, $sType);
$sFormat = trim($aNameSpec[0]);
$aAttributes = $aNameSpec[1];
if (($sFormat != '') && ($sFormat != '%1$s')) {
return null;
}
if (count($aAttributes) > 1) {
return null;
}
return reset($aAttributes);
}
/**
* Returns the list of attributes composing the friendlyname
*
* @param string $sClass
* @param string $sType {@see \Combodo\iTop\Core\MetaModel\FriendlyNameType}
*
* @return array
*
* @since 3.0.0 N°580 New $sType parameter
*/
final public static function GetFriendlyNameAttributeCodeList($sClass, $sType = FriendlyNameType::SHORT)
{
$aNameSpec = self::GetNameSpec($sClass, $sType);
$aAttributes = $aNameSpec[1];
return $aAttributes;
}