modelreflection.class.inc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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);
  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 GetFiltersList($sClass);
  38. public function IsValidFilterCode($sClass, $sFilterCode);
  39. }
  40. class ModelReflectionRuntime implements ModelReflection
  41. {
  42. public function __construct()
  43. {
  44. }
  45. public function GetClassIcon($sClass, $bImgTag = true)
  46. {
  47. return MetaModel::GetClassIcon($sClass, $bImgTag);
  48. }
  49. public function IsValidAttCode($sClass, $sAttCode)
  50. {
  51. return MetaModel::IsValidAttCode($sClass, $sAttCode);
  52. }
  53. public function GetName($sClass)
  54. {
  55. return MetaModel::GetName($sClass);
  56. }
  57. public function GetLabel($sClass, $sAttCodeEx)
  58. {
  59. return MetaModel::GetLabel($sClass, $sAttCodeEx);
  60. }
  61. public function ListAttributes($sClass, $sScope = null)
  62. {
  63. $aScope = null;
  64. if ($sScope != null)
  65. {
  66. $aScope = array();
  67. foreach (explode(',', $sScope) as $sScopeClass)
  68. {
  69. $aScope[] = trim($sScopeClass);
  70. }
  71. }
  72. $aAttributes = array();
  73. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  74. {
  75. $sAttributeClass = get_class($oAttDef);
  76. if ($aScope != null)
  77. {
  78. foreach ($aScope as $sScopeClass)
  79. {
  80. if (($sAttributeClass == $sScopeClass) || is_subclass_of($sAttributeClass, $sScopeClass))
  81. {
  82. $aAttributes[$sAttCode] = $sAttributeClass;
  83. break;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. $aAttributes[$sAttCode] = $sAttributeClass;
  90. }
  91. }
  92. return $aAttributes;
  93. }
  94. public function GetAttributeProperty($sClass, $sAttCode, $sPropName, $default = null)
  95. {
  96. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  97. $aParams = $oAttDef->GetParams();
  98. if (array_key_exists($sPropName, $aParams))
  99. {
  100. $ret = $aParams[$sPropName];
  101. }
  102. else
  103. {
  104. $ret = $default;
  105. }
  106. return $ret;
  107. }
  108. public function GetAllowedValues_att($sClass, $sAttCode)
  109. {
  110. return MetaModel::GetAllowedValues_att($sClass, $sAttCode);
  111. }
  112. public function HasChildrenClasses($sClass)
  113. {
  114. return MetaModel::HasChildrenClasses($sClass);
  115. }
  116. public function GetClasses($sCategories = '')
  117. {
  118. return MetaModel::GetClasses($sCategories);
  119. }
  120. public function IsValidClass($sClass)
  121. {
  122. return MetaModel::IsValidClass($sClass);
  123. }
  124. public function IsSameFamilyBranch($sClassA, $sClassB)
  125. {
  126. return MetaModel::IsSameFamilyBranch($sClassA, $sClassB);
  127. }
  128. public function GetFiltersList($sClass)
  129. {
  130. return MetaModel::GetFiltersList($sClass);
  131. }
  132. public function IsValidFilterCode($sClass, $sFilterCode)
  133. {
  134. return MetaModel::IsValidFilterCode($sClass, $sFilterCode);
  135. }
  136. }