valuesetdef.class.inc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * Fixed set values (could be hardcoded in the business model)
  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 ValueSetEnum extends ValueSetDefinition
  112. {
  113. protected $m_values;
  114. public function __construct($Values)
  115. {
  116. $this->m_values = $Values;
  117. }
  118. protected function LoadValues($aArgs)
  119. {
  120. if (is_array($this->m_values))
  121. {
  122. $aValues = $this->m_values;
  123. }
  124. else
  125. {
  126. $aValues = array();
  127. foreach (explode(",", $this->m_values) as $sVal)
  128. {
  129. $sVal = trim($sVal);
  130. $sKey = $sVal;
  131. $aValues[$sKey] = $sVal;
  132. }
  133. }
  134. $this->m_aValues = $aValues;
  135. return true;
  136. }
  137. }
  138. /**
  139. * Data model classes
  140. *
  141. * @package iTopORM
  142. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  143. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  144. * @link www.itop.com
  145. * @since 1.0
  146. * @version $itopversion$
  147. */
  148. class ValueSetEnumClasses extends ValueSetEnum
  149. {
  150. protected $m_sCategories;
  151. public function __construct($sCategories = '', $sAdditionalValues = '')
  152. {
  153. $this->m_sCategories = $sCategories;
  154. parent::__construct($sAdditionalValues);
  155. }
  156. protected function LoadValues($aArgs)
  157. {
  158. // First, get the additional values
  159. parent::LoadValues($aArgs);
  160. // Then, add the classes from the category definition
  161. foreach (MetaModel::GetClasses($this->m_sCategories) as $sClass)
  162. {
  163. $this->m_aValues[$sClass] = MetaModel::GetName($sClass);
  164. }
  165. return true;
  166. }
  167. }
  168. ?>