modelreflection.class.inc.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // Copyright (C) 2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Reflection API for the MetaModel (partial)
  20. *
  21. * @copyright Copyright (C) 2013 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. interface ModelReflection
  25. {
  26. public function GetClassIcon($sClass, $bImgTag = true);
  27. public function IsValidAttCode($sClass, $sAttCode);
  28. public function GetName($sClass);
  29. public function GetLabel($sClass, $sAttCodeEx);
  30. public function ListAttributes($sClass, $sScope = null);
  31. public function GetAttributeProperty($sClass, $sAttCode, $sPropName, $default = null);
  32. public function GetAllowedValues_att($sClass, $sAttCode);
  33. public function HasChildrenClasses($sClass);
  34. public function GetClasses($sCategories = '');
  35. public function IsValidClass($sClass);
  36. public function IsSameFamilyBranch($sClassA, $sClassB);
  37. public function GetParentClass($sClass);
  38. public function GetFiltersList($sClass);
  39. public function IsValidFilterCode($sClass, $sFilterCode);
  40. }
  41. class ModelReflectionRuntime implements ModelReflection
  42. {
  43. public function __construct()
  44. {
  45. }
  46. public function GetClassIcon($sClass, $bImgTag = true)
  47. {
  48. return MetaModel::GetClassIcon($sClass, $bImgTag);
  49. }
  50. public function IsValidAttCode($sClass, $sAttCode)
  51. {
  52. return MetaModel::IsValidAttCode($sClass, $sAttCode);
  53. }
  54. public function GetName($sClass)
  55. {
  56. return MetaModel::GetName($sClass);
  57. }
  58. public function GetLabel($sClass, $sAttCodeEx)
  59. {
  60. return MetaModel::GetLabel($sClass, $sAttCodeEx);
  61. }
  62. public function ListAttributes($sClass, $sScope = null)
  63. {
  64. $aScope = null;
  65. if ($sScope != null)
  66. {
  67. $aScope = array();
  68. foreach (explode(',', $sScope) as $sScopeClass)
  69. {
  70. $aScope[] = trim($sScopeClass);
  71. }
  72. }
  73. $aAttributes = array();
  74. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  75. {
  76. $sAttributeClass = get_class($oAttDef);
  77. if ($aScope != null)
  78. {
  79. foreach ($aScope as $sScopeClass)
  80. {
  81. if (($sAttributeClass == $sScopeClass) || is_subclass_of($sAttributeClass, $sScopeClass))
  82. {
  83. $aAttributes[$sAttCode] = $sAttributeClass;
  84. break;
  85. }
  86. }
  87. }
  88. else
  89. {
  90. $aAttributes[$sAttCode] = $sAttributeClass;
  91. }
  92. }
  93. return $aAttributes;
  94. }
  95. public function GetAttributeProperty($sClass, $sAttCode, $sPropName, $default = null)
  96. {
  97. $ret = $default;
  98. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  99. $aParams = $oAttDef->GetParams();
  100. if (array_key_exists($sPropName, $aParams))
  101. {
  102. $ret = $aParams[$sPropName];
  103. }
  104. if ($oAttDef instanceof AttributeHierarchicalKey)
  105. {
  106. if ($sPropName == 'targetclass')
  107. {
  108. $ret = $sClass;
  109. }
  110. }
  111. return $ret;
  112. }
  113. public function GetAllowedValues_att($sClass, $sAttCode)
  114. {
  115. return MetaModel::GetAllowedValues_att($sClass, $sAttCode);
  116. }
  117. public function HasChildrenClasses($sClass)
  118. {
  119. return MetaModel::HasChildrenClasses($sClass);
  120. }
  121. public function GetClasses($sCategories = '')
  122. {
  123. return MetaModel::GetClasses($sCategories);
  124. }
  125. public function IsValidClass($sClass)
  126. {
  127. return MetaModel::IsValidClass($sClass);
  128. }
  129. public function IsSameFamilyBranch($sClassA, $sClassB)
  130. {
  131. return MetaModel::IsSameFamilyBranch($sClassA, $sClassB);
  132. }
  133. public function GetParentClass($sClass)
  134. {
  135. return MetaModel::GetParentClass($sClass);
  136. }
  137. public function GetFiltersList($sClass)
  138. {
  139. return MetaModel::GetFiltersList($sClass);
  140. }
  141. public function IsValidFilterCode($sClass, $sFilterCode)
  142. {
  143. return MetaModel::IsValidFilterCode($sClass, $sFilterCode);
  144. }
  145. }