valuesetdef.class.inc.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Value set definitions (from a fixed list or from a query, etc.)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('MyHelpers.class.inc.php');
  25. /**
  26. * ValueSetDefinition
  27. * value sets API and implementations
  28. *
  29. * @package iTopORM
  30. */
  31. abstract class ValueSetDefinition
  32. {
  33. protected $m_bIsLoaded = false;
  34. protected $m_aValues = array();
  35. // Displayable description that could be computed out of the std usage context
  36. public function GetValuesDescription()
  37. {
  38. $aValues = $this->GetValues(array(), '');
  39. $aDisplayedValues = array();
  40. foreach($aValues as $key => $value)
  41. {
  42. $aDisplayedValues[] = "$key => $value";
  43. }
  44. $sAllowedValues = implode(', ', $aDisplayedValues);
  45. return $sAllowedValues;
  46. }
  47. public function GetValues($aArgs, $sContains = '')
  48. {
  49. if (!$this->m_bIsLoaded)
  50. {
  51. $this->LoadValues($aArgs);
  52. $this->m_bIsLoaded = true;
  53. }
  54. if (strlen($sContains) == 0)
  55. {
  56. $aRet = $this->m_aValues;
  57. }
  58. else
  59. {
  60. $aRet = array();
  61. foreach ($this->m_aValues as $sKey=>$sValue)
  62. {
  63. if (stripos($sValue, $sContains) !== false)
  64. {
  65. $aRet[$sKey] = $sValue;
  66. }
  67. }
  68. }
  69. asort($aRet);
  70. return $aRet;
  71. }
  72. abstract protected function LoadValues($aArgs);
  73. }
  74. /**
  75. * Set of existing values for an attribute, given a search filter
  76. *
  77. * @package iTopORM
  78. */
  79. class ValueSetObjects extends ValueSetDefinition
  80. {
  81. protected $m_sFilterExpr; // in OQL
  82. protected $m_sValueAttCode;
  83. protected $m_aOrderBy;
  84. private $m_bAllowAllData;
  85. public function __construct($sFilterExp, $sValueAttCode = '', $aOrderBy = array(), $bAllowAllData = false)
  86. {
  87. $this->m_sFilterExpr = $sFilterExp;
  88. $this->m_sValueAttCode = $sValueAttCode;
  89. $this->m_aOrderBy = $aOrderBy;
  90. $this->m_bAllowAllData = $bAllowAllData;
  91. }
  92. public function ToObjectSet($aArgs = array(), $sContains = '')
  93. {
  94. if ($this->m_bAllowAllData)
  95. {
  96. $oFilter = DBObjectSearch::FromOQL_AllData($this->m_sFilterExpr);
  97. }
  98. else
  99. {
  100. $oFilter = DBObjectSearch::FromOQL($this->m_sFilterExpr);
  101. }
  102. return new DBObjectSet($oFilter, $this->m_aOrderBy, $aArgs);
  103. }
  104. protected function LoadValues($aArgs)
  105. {
  106. $this->m_aValues = array();
  107. if ($this->m_bAllowAllData)
  108. {
  109. $oFilter = DBObjectSearch::FromOQL_AllData($this->m_sFilterExpr);
  110. }
  111. else
  112. {
  113. $oFilter = DBObjectSearch::FromOQL($this->m_sFilterExpr);
  114. }
  115. if (!$oFilter) return false;
  116. $oObjects = new DBObjectSet($oFilter, $this->m_aOrderBy, $aArgs);
  117. while ($oObject = $oObjects->Fetch())
  118. {
  119. if (empty($this->m_sValueAttCode))
  120. {
  121. $this->m_aValues[$oObject->GetKey()] = $oObject->GetName();
  122. }
  123. else
  124. {
  125. $this->m_aValues[$oObject->GetKey()] = $oObject->Get($this->m_sValueAttCode);
  126. }
  127. }
  128. return true;
  129. }
  130. public function GetValuesDescription()
  131. {
  132. return 'Filter: '.$this->m_sFilterExpr;
  133. }
  134. }
  135. /**
  136. * Set of existing values for a link set attribute, given a relation code
  137. *
  138. * @package iTopORM
  139. */
  140. class ValueSetRelatedObjectsFromLinkSet extends ValueSetDefinition
  141. {
  142. protected $m_sLinkSetAttCode;
  143. protected $m_sExtKeyToRemote;
  144. protected $m_sRelationCode;
  145. protected $m_iMaxDepth;
  146. protected $m_sTargetClass;
  147. protected $m_sTargetExtKey;
  148. // protected $m_aOrderBy;
  149. public function __construct($sLinkSetAttCode, $sExtKeyToRemote, $sRelationCode, $iMaxDepth, $sTargetClass, $sTargetLinkClass, $sTargetExtKey)
  150. {
  151. $this->m_sLinkSetAttCode = $sLinkSetAttCode;
  152. $this->m_sExtKeyToRemote = $sExtKeyToRemote;
  153. $this->m_sRelationCode = $sRelationCode;
  154. $this->m_iMaxDepth = $iMaxDepth;
  155. $this->m_sTargetClass = $sTargetClass;
  156. $this->m_sTargetLinkClass = $sTargetLinkClass;
  157. $this->m_sTargetExtKey = $sTargetExtKey;
  158. // $this->m_aOrderBy = $aOrderBy;
  159. }
  160. protected function LoadValues($aArgs)
  161. {
  162. $this->m_aValues = array();
  163. if (!array_key_exists('this', $aArgs))
  164. {
  165. throw new CoreException("Missing 'this' in arguments", array('args' => $aArgs));
  166. }
  167. $oTarget = $aArgs['this->object()'];
  168. // Nodes from which we will start the search for neighbourhood
  169. $oNodes = DBObjectSet::FromLinkSet($oTarget, $this->m_sLinkSetAttCode, $this->m_sExtKeyToRemote);
  170. // Neighbours, whatever their class
  171. $aRelated = $oNodes->GetRelatedObjects($this->m_sRelationCode, $this->m_iMaxDepth);
  172. $sRootClass = MetaModel::GetRootClass($this->m_sTargetClass);
  173. if (array_key_exists($sRootClass, $aRelated))
  174. {
  175. $aLinksToCreate = array();
  176. foreach($aRelated[$sRootClass] as $iKey => $oObject)
  177. {
  178. if (MetaModel::IsParentClass($this->m_sTargetClass, get_class($oObject)))
  179. {
  180. $oNewLink = MetaModel::NewObject($this->m_sTargetLinkClass);
  181. $oNewLink->Set($this->m_sTargetExtKey, $iKey);
  182. //$oNewLink->Set('role', 'concerned by an impacted CI');
  183. $aLinksToCreate[] = $oNewLink;
  184. }
  185. }
  186. // #@# or AddObjectArray($aObjects) ?
  187. $oSetToCreate = DBObjectSet::FromArray($this->m_sTargetLinkClass, $aLinksToCreate);
  188. $this->m_aValues[$oObject->GetKey()] = $oObject->GetAsHTML($oObject->GetName());
  189. }
  190. return true;
  191. }
  192. public function GetValuesDescription()
  193. {
  194. return 'Filter: '.$this->m_sFilterExpr;
  195. }
  196. }
  197. /**
  198. * Fixed set values (could be hardcoded in the business model)
  199. *
  200. * @package iTopORM
  201. */
  202. class ValueSetEnum extends ValueSetDefinition
  203. {
  204. protected $m_values;
  205. public function __construct($Values)
  206. {
  207. $this->m_values = $Values;
  208. }
  209. protected function LoadValues($aArgs)
  210. {
  211. if (is_array($this->m_values))
  212. {
  213. $aValues = $this->m_values;
  214. }
  215. elseif (is_string($this->m_values) && strlen($this->m_values) > 0)
  216. {
  217. $aValues = array();
  218. foreach (explode(",", $this->m_values) as $sVal)
  219. {
  220. $sVal = trim($sVal);
  221. $sKey = $sVal;
  222. $aValues[$sKey] = $sVal;
  223. }
  224. }
  225. else
  226. {
  227. $aValues = array();
  228. }
  229. $this->m_aValues = $aValues;
  230. return true;
  231. }
  232. }
  233. /**
  234. * Data model classes
  235. *
  236. * @package iTopORM
  237. */
  238. class ValueSetEnumClasses extends ValueSetEnum
  239. {
  240. protected $m_sCategories;
  241. public function __construct($sCategories = '', $sAdditionalValues = '')
  242. {
  243. $this->m_sCategories = $sCategories;
  244. parent::__construct($sAdditionalValues);
  245. }
  246. protected function LoadValues($aArgs)
  247. {
  248. // First, get the additional values
  249. parent::LoadValues($aArgs);
  250. // Then, add the classes from the category definition
  251. foreach (MetaModel::GetClasses($this->m_sCategories) as $sClass)
  252. {
  253. $this->m_aValues[$sClass] = MetaModel::GetName($sClass);
  254. }
  255. return true;
  256. }
  257. }
  258. ?>