valuesetdef.class.inc.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. protected $m_aArgsObj = array();
  20. protected $m_aArgsApp = array();
  21. // Displayable description that could be computed out of the std usage context
  22. public function GetValuesDescription()
  23. {
  24. $aValues = $this->GetValues(array(), '');
  25. $aDisplayedValues = array();
  26. foreach($aValues as $key => $value)
  27. {
  28. $aDisplayedValues[] = "$key => $value";
  29. }
  30. $sAllowedValues = implode(', ', $aDisplayedValues);
  31. return $sAllowedValues;
  32. }
  33. public function GetValues($aArgs, $sBeginsWith)
  34. {
  35. if (!$this->m_bIsLoaded)
  36. {
  37. $this->LoadValues($aArgs);
  38. $this->m_bIsLoaded = true;
  39. }
  40. if (strlen($sBeginsWith) == 0)
  41. {
  42. $aRet = $this->m_aValues;
  43. }
  44. else
  45. {
  46. $iCheckedLen = strlen($sBeginsWith);
  47. $sBeginsWith = strtolower($sBeginsWith);
  48. $aRet = array();
  49. foreach ($this->m_aValues as $sKey=>$sValue)
  50. {
  51. if (strtolower(substr($sValue, 0, $iCheckedLen)) == $sBeginsWith)
  52. {
  53. $aRet[$sKey] = $sValue;
  54. }
  55. }
  56. }
  57. return $aRet;
  58. }
  59. public function ListArgsFromContextApp()
  60. {
  61. return $this->m_aArgsObj;
  62. }
  63. public function ListArgsFromContextObj()
  64. {
  65. return $this->m_aArgsApp;
  66. }
  67. abstract protected function LoadValues($aArgs);
  68. }
  69. /**
  70. * Set of existing values for an attribute, given a search filter
  71. *
  72. * @package iTopORM
  73. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  74. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  75. * @link www.itop.com
  76. * @since 1.0
  77. * @version $itopversion$
  78. */
  79. class ValueSetObjects extends ValueSetDefinition
  80. {
  81. protected $m_sFilterExpr; // in SibuSQL
  82. protected $m_sValueAttCode;
  83. protected $m_aOrderBy;
  84. public function __construct($sFilterExp, $sValueAttCode = '', $aOrderBy = array())
  85. {
  86. $this->m_sFilterExpr = $sFilterExp;
  87. $this->m_sValueAttCode = $sValueAttCode;
  88. $this->m_aOrderBy = $aOrderBy;
  89. }
  90. protected function LoadValues($aArgs)
  91. {
  92. $this->m_aValues = array();
  93. $oFilter = DBObjectSearch::FromSibuSQL($this->m_sFilterExpr, $aArgs);
  94. if (!$oFilter) return false;
  95. if (empty($this->m_sValueAttCode))
  96. {
  97. $this->m_sValueAttCode = MetaModel::GetNameAttributeCode($oFilter->GetClass());
  98. }
  99. $oObjects = new DBObjectSet($oFilter, $this->m_aOrderBy);
  100. while ($oObject = $oObjects->Fetch())
  101. {
  102. $this->m_aValues[$oObject->GetKey()] = $oObject->GetAsHTML($this->m_sValueAttCode);
  103. }
  104. return true;
  105. }
  106. public function GetValuesDescription()
  107. {
  108. return 'Filter: '.$this->m_sFilterExpr;
  109. }
  110. }
  111. /**
  112. * Set of existing values for an attribute, given a search filter and a relation id
  113. *
  114. * @package iTopORM
  115. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  116. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  117. * @link www.itop.com
  118. * @since 1.0
  119. * @version $itopversion$
  120. */
  121. class ValueSetRelatedObjects extends ValueSetObjects
  122. {
  123. public function __construct($sFilterExp, $sRelCode, $sClass, $sValueAttCode = '', $aOrderBy = array())
  124. {
  125. $sFullFilterExp = "$sClass: RELATED ($sRelCode, 1) TO ($sFilterExp)";
  126. parent::__construct($sFullFilterExp, $sValueAttCode, $aOrderBy);
  127. }
  128. }
  129. /**
  130. * Set oof existing values for an attribute, given a set of objects (AttributeLinkedSet)
  131. *
  132. * @package iTopORM
  133. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  134. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  135. * @link www.itop.com
  136. * @since 1.0
  137. * @version $itopversion$
  138. */
  139. class ValueSetRelatedObjectsFromLinkedSet extends ValueSetDefinition
  140. {
  141. protected $m_sLinkedSetAttCode;
  142. protected $m_sRelCode;
  143. protected $m_sValueAttCode;
  144. protected $m_aOrderBy;
  145. public function __construct($sLinkedSetAttCode, $sRelCode, $sValueAttCode = '', $aOrderBy = array())
  146. {
  147. $this->m_sLinkedSetAttCode = $sLinkedSetAttCode;
  148. $this->m_sRelCode = $sRelCode;
  149. $this->m_sValueAttCode = $sValueAttCode;
  150. $this->m_aOrderBy = $aOrderBy;
  151. }
  152. protected function LoadValues($aArgs)
  153. {
  154. $this->m_aValues = array();
  155. if (empty($this->m_sValueAttCode))
  156. {
  157. $this->m_sValueAttCode = MetaModel::GetNameAttributeCode($oFilter->GetClass());
  158. }
  159. $oCurrentObject = @$aArgs['*this*'];
  160. if (!is_object($oCurrentObject)) return false;
  161. $oObjects = $oCurrentObject->Get($this->m_sLinkedSetAttCode);
  162. while ($oObject = $oObjects->Fetch())
  163. {
  164. $this->m_aValues[$oObject->GetKey()] = $oObject->Get($this->m_sValueAttCode);
  165. }
  166. return true;
  167. }
  168. public function GetValuesDescription()
  169. {
  170. return 'Objects related ('.$this->m_sRelCode.') to objects linked through '.$this->m_sLinkedSetAttCode;
  171. }
  172. }
  173. /**
  174. * Fixed set values (could be hardcoded in the business model)
  175. *
  176. * @package iTopORM
  177. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  178. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  179. * @link www.itop.com
  180. * @since 1.0
  181. * @version $itopversion$
  182. */
  183. class ValueSetEnum extends ValueSetDefinition
  184. {
  185. public function __construct($Values)
  186. {
  187. if (is_array($Values))
  188. {
  189. $aValues = $Values;
  190. }
  191. else
  192. {
  193. $aValues = array();
  194. foreach (explode(",", $Values) as $sVal)
  195. {
  196. $sVal = trim($sVal);
  197. $sKey = $sVal;
  198. $aValues[$sKey] = $sVal;
  199. }
  200. }
  201. $this->m_aValues = $aValues;
  202. }
  203. protected function LoadValues($aArgs)
  204. {
  205. return true;
  206. }
  207. }
  208. ?>