modelreflection.class.inc.php 7.4 KB

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