valuesetdef.class.inc.php 4.3 KB

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