valuesetdef.class.inc.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // No filtering
  57. $aRet = $this->m_aValues;
  58. }
  59. else
  60. {
  61. // Filter on results containing the needle <sContain>
  62. $aRet = array();
  63. foreach ($this->m_aValues as $sKey=>$sValue)
  64. {
  65. if (stripos($sValue, $sContains) !== false)
  66. {
  67. $aRet[$sKey] = $sValue;
  68. }
  69. }
  70. }
  71. // Sort on the display value
  72. asort($aRet);
  73. return $aRet;
  74. }
  75. abstract protected function LoadValues($aArgs);
  76. }
  77. /**
  78. * Set of existing values for an attribute, given a search filter
  79. *
  80. * @package iTopORM
  81. */
  82. class ValueSetObjects extends ValueSetDefinition
  83. {
  84. protected $m_sContains;
  85. protected $m_sFilterExpr; // in OQL
  86. protected $m_sValueAttCode;
  87. protected $m_aOrderBy;
  88. protected $m_aExtraConditions;
  89. private $m_bAllowAllData;
  90. private $m_aModifierProperties;
  91. public function __construct($sFilterExp, $sValueAttCode = '', $aOrderBy = array(), $bAllowAllData = false, $aModifierProperties = array())
  92. {
  93. $this->m_sContains = '';
  94. $this->m_sFilterExpr = $sFilterExp;
  95. $this->m_sValueAttCode = $sValueAttCode;
  96. $this->m_aOrderBy = $aOrderBy;
  97. $this->m_bAllowAllData = $bAllowAllData;
  98. $this->m_aModifierProperties = $aModifierProperties;
  99. $this->m_aExtraConditions = array();
  100. }
  101. public function SetModifierProperty($sPluginClass, $sProperty, $value)
  102. {
  103. $this->m_aModifierProperties[$sPluginClass][$sProperty] = $value;
  104. }
  105. public function AddCondition(DBObjectSearch $oFilter)
  106. {
  107. $this->m_aExtraConditions[] = $oFilter;
  108. }
  109. public function ToObjectSet($aArgs = array(), $sContains = '')
  110. {
  111. if ($this->m_bAllowAllData)
  112. {
  113. $oFilter = DBObjectSearch::FromOQL_AllData($this->m_sFilterExpr);
  114. }
  115. else
  116. {
  117. $oFilter = DBObjectSearch::FromOQL($this->m_sFilterExpr);
  118. }
  119. foreach($this->m_aExtraConditions as $oExtraFilter)
  120. {
  121. $oFilter->MergeWith($oExtraFilter);
  122. }
  123. foreach($this->m_aModifierProperties as $sPluginClass => $aProperties)
  124. {
  125. foreach ($aProperties as $sProperty => $value)
  126. {
  127. $oFilter->SetModifierProperty($sPluginClass, $sProperty, $value);
  128. }
  129. }
  130. return new DBObjectSet($oFilter, $this->m_aOrderBy, $aArgs);
  131. }
  132. public function GetValues($aArgs, $sContains = '')
  133. {
  134. if (!$this->m_bIsLoaded || ($sContains != $this->m_sContains))
  135. {
  136. $this->LoadValues($aArgs, $sContains);
  137. $this->m_bIsLoaded = true;
  138. }
  139. // The results are already filtered and sorted (on friendly name)
  140. $aRet = $this->m_aValues;
  141. return $aRet;
  142. }
  143. protected function LoadValues($aArgs, $sContains = '')
  144. {
  145. $this->m_sContains = $sContains;
  146. $this->m_aValues = array();
  147. if ($this->m_bAllowAllData)
  148. {
  149. $oFilter = DBObjectSearch::FromOQL_AllData($this->m_sFilterExpr);
  150. }
  151. else
  152. {
  153. $oFilter = DBObjectSearch::FromOQL($this->m_sFilterExpr);
  154. }
  155. if (!$oFilter) return false;
  156. foreach($this->m_aExtraConditions as $oExtraFilter)
  157. {
  158. $oFilter->MergeWith($oExtraFilter);
  159. }
  160. foreach($this->m_aModifierProperties as $sPluginClass => $aProperties)
  161. {
  162. foreach ($aProperties as $sProperty => $value)
  163. {
  164. $oFilter->SetModifierProperty($sPluginClass, $sProperty, $value);
  165. }
  166. }
  167. $oValueExpr = new ScalarExpression('%'.$sContains.'%');
  168. $oNameExpr = new FieldExpression('friendlyname', $oFilter->GetClassAlias());
  169. $oNewCondition = new BinaryExpression($oNameExpr, 'LIKE', $oValueExpr);
  170. $oFilter->AddConditionExpression($oNewCondition);
  171. $oObjects = new DBObjectSet($oFilter, $this->m_aOrderBy, $aArgs);
  172. while ($oObject = $oObjects->Fetch())
  173. {
  174. if (empty($this->m_sValueAttCode))
  175. {
  176. $this->m_aValues[$oObject->GetKey()] = $oObject->GetName();
  177. }
  178. else
  179. {
  180. $this->m_aValues[$oObject->GetKey()] = $oObject->Get($this->m_sValueAttCode);
  181. }
  182. }
  183. return true;
  184. }
  185. public function GetValuesDescription()
  186. {
  187. return 'Filter: '.$this->m_sFilterExpr;
  188. }
  189. public function GetFilterExpression()
  190. {
  191. return $this->m_sFilterExpr;
  192. }
  193. }
  194. /**
  195. * Set of existing values for a link set attribute, given a relation code
  196. *
  197. * @package iTopORM
  198. */
  199. class ValueSetRelatedObjectsFromLinkSet extends ValueSetDefinition
  200. {
  201. protected $m_sLinkSetAttCode;
  202. protected $m_sExtKeyToRemote;
  203. protected $m_sRelationCode;
  204. protected $m_iMaxDepth;
  205. protected $m_sTargetClass;
  206. protected $m_sTargetExtKey;
  207. // protected $m_aOrderBy;
  208. public function __construct($sLinkSetAttCode, $sExtKeyToRemote, $sRelationCode, $iMaxDepth, $sTargetClass, $sTargetLinkClass, $sTargetExtKey)
  209. {
  210. $this->m_sLinkSetAttCode = $sLinkSetAttCode;
  211. $this->m_sExtKeyToRemote = $sExtKeyToRemote;
  212. $this->m_sRelationCode = $sRelationCode;
  213. $this->m_iMaxDepth = $iMaxDepth;
  214. $this->m_sTargetClass = $sTargetClass;
  215. $this->m_sTargetLinkClass = $sTargetLinkClass;
  216. $this->m_sTargetExtKey = $sTargetExtKey;
  217. // $this->m_aOrderBy = $aOrderBy;
  218. }
  219. protected function LoadValues($aArgs)
  220. {
  221. $this->m_aValues = array();
  222. if (!array_key_exists('this', $aArgs))
  223. {
  224. throw new CoreException("Missing 'this' in arguments", array('args' => $aArgs));
  225. }
  226. $oTarget = $aArgs['this->object()'];
  227. // Nodes from which we will start the search for neighbourhood
  228. $oNodes = DBObjectSet::FromLinkSet($oTarget, $this->m_sLinkSetAttCode, $this->m_sExtKeyToRemote);
  229. // Neighbours, whatever their class
  230. $aRelated = $oNodes->GetRelatedObjects($this->m_sRelationCode, $this->m_iMaxDepth);
  231. $sRootClass = MetaModel::GetRootClass($this->m_sTargetClass);
  232. if (array_key_exists($sRootClass, $aRelated))
  233. {
  234. $aLinksToCreate = array();
  235. foreach($aRelated[$sRootClass] as $iKey => $oObject)
  236. {
  237. if (MetaModel::IsParentClass($this->m_sTargetClass, get_class($oObject)))
  238. {
  239. $oNewLink = MetaModel::NewObject($this->m_sTargetLinkClass);
  240. $oNewLink->Set($this->m_sTargetExtKey, $iKey);
  241. //$oNewLink->Set('role', 'concerned by an impacted CI');
  242. $aLinksToCreate[] = $oNewLink;
  243. }
  244. }
  245. // #@# or AddObjectArray($aObjects) ?
  246. $oSetToCreate = DBObjectSet::FromArray($this->m_sTargetLinkClass, $aLinksToCreate);
  247. $this->m_aValues[$oObject->GetKey()] = $oObject->GetName();
  248. }
  249. return true;
  250. }
  251. public function GetValuesDescription()
  252. {
  253. return 'Filter: '.$this->m_sFilterExpr;
  254. }
  255. }
  256. /**
  257. * Fixed set values (could be hardcoded in the business model)
  258. *
  259. * @package iTopORM
  260. */
  261. class ValueSetEnum extends ValueSetDefinition
  262. {
  263. protected $m_values;
  264. public function __construct($Values)
  265. {
  266. $this->m_values = $Values;
  267. }
  268. // Helper to export the datat model
  269. public function GetValueList()
  270. {
  271. $this->LoadValues($aArgs = array());
  272. return $this->m_aValues;
  273. }
  274. protected function LoadValues($aArgs)
  275. {
  276. if (is_array($this->m_values))
  277. {
  278. $aValues = $this->m_values;
  279. }
  280. elseif (is_string($this->m_values) && strlen($this->m_values) > 0)
  281. {
  282. $aValues = array();
  283. foreach (explode(",", $this->m_values) as $sVal)
  284. {
  285. $sVal = trim($sVal);
  286. $sKey = $sVal;
  287. $aValues[$sKey] = $sVal;
  288. }
  289. }
  290. else
  291. {
  292. $aValues = array();
  293. }
  294. $this->m_aValues = $aValues;
  295. return true;
  296. }
  297. }
  298. /**
  299. * Fixed set values, defined as a range: 0..59 (with an optional increment)
  300. *
  301. * @package iTopORM
  302. */
  303. class ValueSetRange extends ValueSetDefinition
  304. {
  305. protected $m_iStart;
  306. protected $m_iEnd;
  307. public function __construct($iStart, $iEnd, $iStep = 1)
  308. {
  309. $this->m_iStart = $iStart;
  310. $this->m_iEnd = $iEnd;
  311. $this->m_iStep = $iStep;
  312. }
  313. protected function LoadValues($aArgs)
  314. {
  315. $iValue = $this->m_iStart;
  316. for($iValue = $this->m_iStart; $iValue <= $this->m_iEnd; $iValue += $this->m_iStep)
  317. {
  318. $this->m_aValues[$iValue] = $iValue;
  319. }
  320. return true;
  321. }
  322. }
  323. /**
  324. * Data model classes
  325. *
  326. * @package iTopORM
  327. */
  328. class ValueSetEnumClasses extends ValueSetEnum
  329. {
  330. protected $m_sCategories;
  331. public function __construct($sCategories = '', $sAdditionalValues = '')
  332. {
  333. $this->m_sCategories = $sCategories;
  334. parent::__construct($sAdditionalValues);
  335. }
  336. protected function LoadValues($aArgs)
  337. {
  338. // First, get the additional values
  339. parent::LoadValues($aArgs);
  340. // Then, add the classes from the category definition
  341. foreach (MetaModel::GetClasses($this->m_sCategories) as $sClass)
  342. {
  343. $this->m_aValues[$sClass] = MetaModel::GetName($sClass);
  344. }
  345. return true;
  346. }
  347. }
  348. ?>