valuesetdef.class.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * ValueSetDefinition
  4. * value sets API and implementations
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. require_once('MyHelpers.class.inc.php');
  15. abstract class ValueSetDefinition
  16. {
  17. protected $m_bIsLoaded = false;
  18. protected $m_aValues = array();
  19. // Displayable description that could be computed out of the std usage context
  20. public function GetValuesDescription()
  21. {
  22. $aValues = $this->GetValues(array(), '');
  23. $aDisplayedValues = array();
  24. foreach($aValues as $key => $value)
  25. {
  26. $aDisplayedValues[] = "$key => $value";
  27. }
  28. $sAllowedValues = implode(', ', $aDisplayedValues);
  29. return $sAllowedValues;
  30. }
  31. public function GetValues($aArgs, $sBeginsWith = '')
  32. {
  33. if (!$this->m_bIsLoaded)
  34. {
  35. $this->LoadValues($aArgs);
  36. $this->m_bIsLoaded = true;
  37. }
  38. if (strlen($sBeginsWith) == 0)
  39. {
  40. $aRet = $this->m_aValues;
  41. }
  42. else
  43. {
  44. $iCheckedLen = strlen($sBeginsWith);
  45. $sBeginsWith = strtolower($sBeginsWith);
  46. $aRet = array();
  47. foreach ($this->m_aValues as $sKey=>$sValue)
  48. {
  49. if (strtolower(substr($sValue, 0, $iCheckedLen)) == $sBeginsWith)
  50. {
  51. $aRet[$sKey] = $sValue;
  52. }
  53. }
  54. }
  55. return $aRet;
  56. }
  57. abstract protected function LoadValues($aArgs);
  58. }
  59. /**
  60. * Set of existing values for an attribute, given a search filter
  61. *
  62. * @package iTopORM
  63. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  64. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  65. * @link www.itop.com
  66. * @since 1.0
  67. * @version $itopversion$
  68. */
  69. class ValueSetObjects extends ValueSetDefinition
  70. {
  71. protected $m_sFilterExpr; // in OQL
  72. protected $m_sValueAttCode;
  73. protected $m_aOrderBy;
  74. public function __construct($sFilterExp, $sValueAttCode = '', $aOrderBy = array())
  75. {
  76. $this->m_sFilterExpr = $sFilterExp;
  77. $this->m_sValueAttCode = $sValueAttCode;
  78. $this->m_aOrderBy = $aOrderBy;
  79. }
  80. protected function LoadValues($aArgs)
  81. {
  82. $this->m_aValues = array();
  83. $oFilter = DBObjectSearch::FromOQL($this->m_sFilterExpr, $aArgs);
  84. if (!$oFilter) return false;
  85. $oObjects = new DBObjectSet($oFilter, $this->m_aOrderBy, $aArgs);
  86. while ($oObject = $oObjects->Fetch())
  87. {
  88. if (empty($this->m_sValueAttCode))
  89. {
  90. $this->m_aValues[$oObject->GetKey()] = $oObject->GetName();
  91. }
  92. else
  93. {
  94. $this->m_aValues[$oObject->GetKey()] = $oObject->GetAsHTML($this->m_sValueAttCode);
  95. }
  96. }
  97. return true;
  98. }
  99. public function GetValuesDescription()
  100. {
  101. return 'Filter: '.$this->m_sFilterExpr;
  102. }
  103. }
  104. /**
  105. * Set of existing values for a link set attribute, given a relation code
  106. *
  107. * @package iTopORM
  108. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  109. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  110. * @link www.itop.com
  111. * @since 1.0
  112. * @version $itopversion$
  113. */
  114. class ValueSetRelatedObjectsFromLinkSet extends ValueSetDefinition
  115. {
  116. protected $m_sLinkSetAttCode;
  117. protected $m_sExtKeyToRemote;
  118. protected $m_sRelationCode;
  119. protected $m_iMaxDepth;
  120. protected $m_sTargetClass;
  121. protected $m_sTargetExtKey;
  122. // protected $m_aOrderBy;
  123. public function __construct($sLinkSetAttCode, $sExtKeyToRemote, $sRelationCode, $iMaxDepth, $sTargetClass, $sTargetLinkClass, $sTargetExtKey)
  124. {
  125. $this->m_sLinkSetAttCode = $sLinkSetAttCode;
  126. $this->m_sExtKeyToRemote = $sExtKeyToRemote;
  127. $this->m_sRelationCode = $sRelationCode;
  128. $this->m_iMaxDepth = $iMaxDepth;
  129. $this->m_sTargetClass = $sTargetClass;
  130. $this->m_sTargetLinkClass = $sTargetLinkClass;
  131. $this->m_sTargetExtKey = $sTargetExtKey;
  132. // $this->m_aOrderBy = $aOrderBy;
  133. }
  134. protected function LoadValues($aArgs)
  135. {
  136. $this->m_aValues = array();
  137. if (!array_key_exists('this', $aArgs))
  138. {
  139. throw new CoreException("Missing 'this' in arguments", array('args' => $aArgs));
  140. }
  141. $oTarget = $aArgs['this->object()'];
  142. // Nodes from which we will start the search for neighbourhood
  143. $oNodes = DBObjectSet::FromLinkSet($oTarget, $this->m_sLinkSetAttCode, $this->m_sExtKeyToRemote);
  144. // Neighbours, whatever their class
  145. $aRelated = $oNodes->GetRelatedObjects($this->m_sRelationCode, $this->m_iMaxDepth);
  146. $sRootClass = MetaModel::GetRootClass($this->m_sTargetClass);
  147. if (array_key_exists($sRootClass, $aRelated))
  148. {
  149. $aLinksToCreate = array();
  150. foreach($aRelated[$sRootClass] as $iKey => $oObject)
  151. {
  152. if (MetaModel::IsParentClass($this->m_sTargetClass, get_class($oObject)))
  153. {
  154. $oNewLink = MetaModel::NewObject($this->m_sTargetLinkClass);
  155. $oNewLink->Set($this->m_sTargetExtKey, $iKey);
  156. //$oNewLink->Set('role', 'concerned by an impacted CI');
  157. $aLinksToCreate[] = $oNewLink;
  158. }
  159. }
  160. // #@# or AddObjectArray($aObjects) ?
  161. $oSetToCreate = DBObjectSet::FromArray($this->m_sTargetLinkClass, $aLinksToCreate);
  162. $this->m_aValues[$oObject->GetKey()] = $oObject->GetAsHTML($oObject->GetName());
  163. }
  164. return true;
  165. }
  166. public function GetValuesDescription()
  167. {
  168. return 'Filter: '.$this->m_sFilterExpr;
  169. }
  170. }
  171. /**
  172. * Fixed set values (could be hardcoded in the business model)
  173. *
  174. * @package iTopORM
  175. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  176. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  177. * @link www.itop.com
  178. * @since 1.0
  179. * @version $itopversion$
  180. */
  181. class ValueSetEnum extends ValueSetDefinition
  182. {
  183. protected $m_values;
  184. public function __construct($Values)
  185. {
  186. $this->m_values = $Values;
  187. }
  188. protected function LoadValues($aArgs)
  189. {
  190. if (is_array($this->m_values))
  191. {
  192. $aValues = $this->m_values;
  193. }
  194. else
  195. {
  196. $aValues = array();
  197. foreach (explode(",", $this->m_values) as $sVal)
  198. {
  199. $sVal = trim($sVal);
  200. $sKey = $sVal;
  201. $aValues[$sKey] = $sVal;
  202. }
  203. }
  204. $this->m_aValues = $aValues;
  205. return true;
  206. }
  207. }
  208. /**
  209. * Data model classes
  210. *
  211. * @package iTopORM
  212. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  213. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  214. * @link www.itop.com
  215. * @since 1.0
  216. * @version $itopversion$
  217. */
  218. class ValueSetEnumClasses extends ValueSetEnum
  219. {
  220. protected $m_sCategories;
  221. public function __construct($sCategories = '', $sAdditionalValues = '')
  222. {
  223. $this->m_sCategories = $sCategories;
  224. parent::__construct($sAdditionalValues);
  225. }
  226. protected function LoadValues($aArgs)
  227. {
  228. // First, get the additional values
  229. parent::LoadValues($aArgs);
  230. // Then, add the classes from the category definition
  231. foreach (MetaModel::GetClasses($this->m_sCategories) as $sClass)
  232. {
  233. $this->m_aValues[$sClass] = MetaModel::GetName($sClass);
  234. }
  235. return true;
  236. }
  237. }
  238. ?>