valuesetdef.class.inc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 SibuSQL
  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::FromSibusQL($this->m_sFilterExpr, $aArgs);
  84. if (!$oFilter) return false;
  85. if (empty($this->m_sValueAttCode))
  86. {
  87. $this->m_sValueAttCode = MetaModel::GetNameAttributeCode($oFilter->GetClass());
  88. }
  89. $oObjects = new DBObjectSet($oFilter, $this->m_aOrderBy, $aArgs);
  90. while ($oObject = $oObjects->Fetch())
  91. {
  92. $this->m_aValues[$oObject->GetKey()] = $oObject->GetAsHTML($this->m_sValueAttCode);
  93. }
  94. return true;
  95. }
  96. public function GetValuesDescription()
  97. {
  98. return 'Filter: '.$this->m_sFilterExpr;
  99. }
  100. }
  101. /**
  102. * Set of existing values for a link set attribute, given a relation code
  103. *
  104. * @package iTopORM
  105. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  106. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  107. * @link www.itop.com
  108. * @since 1.0
  109. * @version $itopversion$
  110. */
  111. class ValueSetRelatedObjects extends ValueSetDefinition
  112. {
  113. protected $m_sRelationCode;
  114. protected $m_iMaxDepth;
  115. // protected $m_aOrderBy;
  116. public function __construct($sRelationCode, $iMaxDepth = 99)
  117. {
  118. $this->m_sRelationCode = $sRelationCode;
  119. $this->m_iMaxDepth = $iMaxDepth;
  120. // $this->m_aOrderBy = $aOrderBy;
  121. }
  122. protected function LoadValues($aArgs)
  123. {
  124. $this->m_aValues = array();
  125. if (!array_key_exists('this', $aArgs))
  126. {
  127. throw new CoreException("Missing 'this' in arguments", array('args' => $aArgs));
  128. }
  129. $oTarget = $aArgs['this->object()'];
  130. $oTargetNeighbors = $oTarget->GetRelatedObjects($this->m_sRelationCode, $this->m_iMaxDepth);
  131. while ($oObject = $oTargetNeighbors->Fetch())
  132. {
  133. $this->m_aValues[$oObject->GetKey()] = $oObject->GetAsHTML($oObject->GetName());
  134. }
  135. return true;
  136. }
  137. public function GetValuesDescription()
  138. {
  139. return 'Filter: '.$this->m_sFilterExpr;
  140. }
  141. }
  142. /**
  143. * Fixed set values (could be hardcoded in the business model)
  144. *
  145. * @package iTopORM
  146. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  147. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  148. * @link www.itop.com
  149. * @since 1.0
  150. * @version $itopversion$
  151. */
  152. class ValueSetEnum extends ValueSetDefinition
  153. {
  154. protected $m_values;
  155. public function __construct($Values)
  156. {
  157. $this->m_values = $Values;
  158. }
  159. protected function LoadValues($aArgs)
  160. {
  161. if (is_array($this->m_values))
  162. {
  163. $aValues = $this->m_values;
  164. }
  165. else
  166. {
  167. $aValues = array();
  168. foreach (explode(",", $this->m_values) as $sVal)
  169. {
  170. $sVal = trim($sVal);
  171. $sKey = $sVal;
  172. $aValues[$sKey] = $sVal;
  173. }
  174. }
  175. $this->m_aValues = $aValues;
  176. return true;
  177. }
  178. }
  179. /**
  180. * Data model classes
  181. *
  182. * @package iTopORM
  183. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  184. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  185. * @link www.itop.com
  186. * @since 1.0
  187. * @version $itopversion$
  188. */
  189. class ValueSetEnumClasses extends ValueSetEnum
  190. {
  191. protected $m_sCategories;
  192. public function __construct($sCategories = '', $sAdditionalValues = '')
  193. {
  194. $this->m_sCategories = $sCategories;
  195. parent::__construct($sAdditionalValues);
  196. }
  197. protected function LoadValues($aArgs)
  198. {
  199. // First, get the additional values
  200. parent::LoadValues($aArgs);
  201. // Then, add the classes from the category definition
  202. foreach (MetaModel::GetClasses($this->m_sCategories) as $sClass)
  203. {
  204. $this->m_aValues[$sClass] = MetaModel::GetName($sClass);
  205. }
  206. return true;
  207. }
  208. }
  209. ?>