modelreflection.class.inc.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. abstract class ModelReflection
  25. {
  26. abstract public function GetClassIcon($sClass, $bImgTag = true);
  27. abstract public function IsValidAttCode($sClass, $sAttCode);
  28. abstract public function GetName($sClass);
  29. abstract public function GetLabel($sClass, $sAttCodeEx);
  30. abstract public function GetValueLabel($sClass, $sAttCode, $sValue);
  31. abstract public function ListAttributes($sClass, $sScope = null);
  32. abstract public function GetAttributeProperty($sClass, $sAttCode, $sPropName, $default = null);
  33. abstract public function GetAllowedValues_att($sClass, $sAttCode);
  34. abstract public function HasChildrenClasses($sClass);
  35. abstract public function GetClasses($sCategories = '', $bExcludeLinks = false);
  36. abstract public function IsValidClass($sClass);
  37. abstract public function IsSameFamilyBranch($sClassA, $sClassB);
  38. abstract public function GetParentClass($sClass);
  39. abstract public function GetFiltersList($sClass);
  40. abstract public function IsValidFilterCode($sClass, $sFilterCode);
  41. abstract public function GetQuery($sOQL);
  42. abstract public function DictString($sStringCode, $sDefault = null, $bUserLanguageOnly = false);
  43. public function DictFormat($sFormatCode /*, ... arguments ....*/)
  44. {
  45. $sLocalizedFormat = $this->DictString($sFormatCode);
  46. $aArguments = func_get_args();
  47. array_shift($aArguments);
  48. if ($sLocalizedFormat == $sFormatCode)
  49. {
  50. // Make sure the information will be displayed (ex: an error occuring before the dictionary gets loaded)
  51. return $sFormatCode.' - '.implode(', ', $aArguments);
  52. }
  53. return vsprintf($sLocalizedFormat, $aArguments);
  54. }
  55. abstract public function GetIconSelectionField($sCode, $sLabel = '', $defaultValue = '');
  56. }
  57. abstract class QueryReflection
  58. {
  59. /**
  60. * Throws an exception in case of an invalid syntax
  61. */
  62. abstract public function __construct($sOQL, ModelReflection $oModelReflection);
  63. abstract public function GetClass();
  64. abstract public function GetClassAlias();
  65. }
  66. class ModelReflectionRuntime extends ModelReflection
  67. {
  68. public function __construct()
  69. {
  70. }
  71. public function GetClassIcon($sClass, $bImgTag = true)
  72. {
  73. return MetaModel::GetClassIcon($sClass, $bImgTag);
  74. }
  75. public function IsValidAttCode($sClass, $sAttCode)
  76. {
  77. return MetaModel::IsValidAttCode($sClass, $sAttCode);
  78. }
  79. public function GetName($sClass)
  80. {
  81. return MetaModel::GetName($sClass);
  82. }
  83. public function GetLabel($sClass, $sAttCodeEx)
  84. {
  85. return MetaModel::GetLabel($sClass, $sAttCodeEx);
  86. }
  87. public function GetValueLabel($sClass, $sAttCode, $sValue)
  88. {
  89. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  90. return $oAttDef->GetValueLabel($sValue);
  91. }
  92. public function ListAttributes($sClass, $sScope = null)
  93. {
  94. $aScope = null;
  95. if ($sScope != null)
  96. {
  97. $aScope = array();
  98. foreach (explode(',', $sScope) as $sScopeClass)
  99. {
  100. $aScope[] = trim($sScopeClass);
  101. }
  102. }
  103. $aAttributes = array();
  104. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  105. {
  106. $sAttributeClass = get_class($oAttDef);
  107. if ($aScope != null)
  108. {
  109. foreach ($aScope as $sScopeClass)
  110. {
  111. if (($sAttributeClass == $sScopeClass) || is_subclass_of($sAttributeClass, $sScopeClass))
  112. {
  113. $aAttributes[$sAttCode] = $sAttributeClass;
  114. break;
  115. }
  116. }
  117. }
  118. else
  119. {
  120. $aAttributes[$sAttCode] = $sAttributeClass;
  121. }
  122. }
  123. return $aAttributes;
  124. }
  125. public function GetAttributeProperty($sClass, $sAttCode, $sPropName, $default = null)
  126. {
  127. $ret = $default;
  128. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  129. $aParams = $oAttDef->GetParams();
  130. if (array_key_exists($sPropName, $aParams))
  131. {
  132. $ret = $aParams[$sPropName];
  133. }
  134. if ($oAttDef instanceof AttributeHierarchicalKey)
  135. {
  136. if ($sPropName == 'targetclass')
  137. {
  138. $ret = $sClass;
  139. }
  140. }
  141. return $ret;
  142. }
  143. public function GetAllowedValues_att($sClass, $sAttCode)
  144. {
  145. return MetaModel::GetAllowedValues_att($sClass, $sAttCode);
  146. }
  147. public function HasChildrenClasses($sClass)
  148. {
  149. return MetaModel::HasChildrenClasses($sClass);
  150. }
  151. public function GetClasses($sCategories = '', $bExcludeLinks = false)
  152. {
  153. $aClasses = MetaModel::GetClasses($sCategories);
  154. if ($bExcludeLinks)
  155. {
  156. $aExcluded = MetaModel::GetLinkClasses();
  157. $aRes = array();
  158. foreach ($aClasses as $sClass)
  159. {
  160. if (!array_key_exists($sClass, $aExcluded))
  161. {
  162. $aRes[] = $sClass;
  163. }
  164. }
  165. }
  166. else
  167. {
  168. $aRes = $aClasses;
  169. }
  170. return $aRes;
  171. }
  172. public function IsValidClass($sClass)
  173. {
  174. return MetaModel::IsValidClass($sClass);
  175. }
  176. public function IsSameFamilyBranch($sClassA, $sClassB)
  177. {
  178. return MetaModel::IsSameFamilyBranch($sClassA, $sClassB);
  179. }
  180. public function GetParentClass($sClass)
  181. {
  182. return MetaModel::GetParentClass($sClass);
  183. }
  184. public function GetFiltersList($sClass)
  185. {
  186. return MetaModel::GetFiltersList($sClass);
  187. }
  188. public function IsValidFilterCode($sClass, $sFilterCode)
  189. {
  190. return MetaModel::IsValidFilterCode($sClass, $sFilterCode);
  191. }
  192. public function GetQuery($sOQL)
  193. {
  194. return new QueryReflectionRuntime($sOQL, $this);
  195. }
  196. public function DictString($sStringCode, $sDefault = null, $bUserLanguageOnly = false)
  197. {
  198. return Dict::S($sStringCode, $sDefault, $bUserLanguageOnly);
  199. }
  200. public function GetIconSelectionField($sCode, $sLabel = '', $defaultValue = '')
  201. {
  202. return new RunTimeIconSelectionField($sCode, $sLabel, $defaultValue);
  203. }
  204. }
  205. class QueryReflectionRuntime extends QueryReflection
  206. {
  207. protected $oFilter;
  208. /**
  209. * throws an exception in case of a wrong syntax
  210. */
  211. public function __construct($sOQL, ModelReflection $oModelReflection)
  212. {
  213. $this->oFilter = DBObjectSearch::FromOQL($sOQL);
  214. }
  215. public function GetClass()
  216. {
  217. return $this->oFilter->GetClass();
  218. }
  219. public function GetClassAlias()
  220. {
  221. return $this->oFilter->GetClassAlias();
  222. }
  223. }