PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/php/html/formvalidator/Element/element_finder.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 261 lines | 185 code | 52 blank | 24 comment | 10 complexity | 8a122cef93d7d382935aa9cf4f67671c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. use common\libraries\Translation;
  3. use common\libraries\ResourceManager;
  4. use common\libraries\Path;
  5. use common\libraries\Utilities;
  6. /**
  7. * $Id: element_finder.php 128 2009-11-09 13:13:20Z vanpouckesven $
  8. * @package common.html.formvalidator.Element
  9. */
  10. /**
  11. * AJAX-based tree search and multiselect element. Use at your own risk.
  12. * @author Tim De Pauw
  13. */
  14. class HTML_QuickForm_element_finder extends HTML_QuickForm_group
  15. {
  16. const DEFAULT_HEIGHT = 300;
  17. const DEFAULT_WIDTH = 292;
  18. private static $initialized;
  19. private $search_url;
  20. private $locale;
  21. private $default_collapsed;
  22. private $height;
  23. private $width;
  24. private $exclude;
  25. private $defaults;
  26. function __construct($elementName, $elementLabel, $search_url, $locale = array ('Display' => 'Display'), $default_values = array (), $options = array())
  27. {
  28. parent :: __construct($elementName, $elementLabel);
  29. $this->_type = 'element_finder';
  30. $this->_persistantFreeze = true;
  31. $this->_appendName = false;
  32. $this->locale = $locale;
  33. $this->exclude = array();
  34. $this->height = self :: DEFAULT_HEIGHT;
  35. $this->width = self :: DEFAULT_WIDTH;
  36. $this->search_url = $search_url;
  37. $this->options = $options;
  38. $this->build_elements();
  39. $this->setValue($default_values, 0);
  40. }
  41. function isCollapsed()
  42. {
  43. return $this->isDefaultCollapsed() && ! count($this->getValue());
  44. }
  45. function isDefaultCollapsed()
  46. {
  47. return $this->default_collapsed;
  48. }
  49. function getHeight()
  50. {
  51. return $this->height;
  52. }
  53. function getWidth()
  54. {
  55. return $this->width;
  56. }
  57. function excludeElements($excluded_ids)
  58. {
  59. $this->exclude = array_merge($this->exclude, $excluded_ids);
  60. }
  61. function setDefaultCollapsed($default_collapsed)
  62. {
  63. $this->default_collapsed = $default_collapsed;
  64. }
  65. function setHeight($height)
  66. {
  67. $this->height = $height;
  68. }
  69. function setWidth($width)
  70. {
  71. $this->height = $width;
  72. }
  73. private function build_elements()
  74. {
  75. $active_id = 'elf_' . $this->getName() . '_active';
  76. $inactive_id = 'elf_' . $this->getName() . '_inactive';
  77. $active_hidden_id = 'elf_' . $this->getName() . '_active_hidden';
  78. $activate_button_id = $inactive_id . '_button';
  79. $deactivate_button_id = $active_id . '_button';
  80. $this->_elements = array();
  81. $this->_elements[] = new HTML_QuickForm_hidden($this->getName() . '_active_hidden', null, array(
  82. 'id' => $active_hidden_id));
  83. $this->_elements[] = new HTML_QuickForm_text($this->getName() . '_search', null, array(
  84. 'class' => 'element_query', 'id' => $this->getName() . '_search_field'));
  85. $this->_elements[] = new HTML_QuickForm_button($this->getName() . '_activate', '', array(
  86. 'id' => $activate_button_id, 'disabled' => 'disabled', 'class' => 'activate_elements'));
  87. $this->_elements[] = new HTML_QuickForm_button($this->getName() . '_deactivate', '', array(
  88. 'id' => $deactivate_button_id, 'disabled' => 'disabled', 'class' => 'deactivate_elements'));
  89. }
  90. function getValue()
  91. {
  92. $results = array();
  93. $values = $this->get_active_elements();
  94. /**
  95. * Process the array values so we end up with a 2-dimensional array
  96. * Keys are the selection type, values are the selected objects
  97. */
  98. foreach ($values as $value)
  99. {
  100. $value = explode('_', $value['id'], 2);
  101. if (! isset($results[$value[0]]) || ! is_array($results[$value[0]]))
  102. {
  103. $results[$value[0]] = array();
  104. }
  105. $results[$value[0]][] = $value[1];
  106. }
  107. return $results;
  108. }
  109. function exportValue($submitValues, $assoc = false)
  110. {
  111. if ($assoc)
  112. {
  113. return array($this->getName() => $this->getValue());
  114. }
  115. return $this->getValue();
  116. }
  117. function setValue($value, $element_id = 0)
  118. {
  119. $serialized = serialize($value);
  120. $this->_elements[$element_id]->setValue($serialized);
  121. }
  122. function get_active_elements()
  123. {
  124. return unserialize($this->_elements[0]->getValue());
  125. }
  126. function toHTML()
  127. {
  128. /*
  129. * 0 active hidden
  130. * 1 search
  131. * 2 deactivate
  132. * 3 activate
  133. */
  134. $html = array();
  135. if ($this->isCollapsed())
  136. {
  137. $html[] = '<button id="' . $this->getName() . '_expand_button" class="normal select">' . htmlentities($this->locale['Display']) . '</button>';
  138. }
  139. else
  140. {
  141. $html[] = '<button id="' . $this->getName() . '_expand_button" style="display: none" class="normal select">' . htmlentities($this->locale['Display']) . '</button>';
  142. }
  143. $id = 'tbl_' . $this->getName();
  144. $html[] = '<div class="element_finder" id="' . $id . '" style="margin-top: 5px;' . ($this->isCollapsed() ? ' display: none;' : '') . '">';
  145. $html[] = $this->_elements[0]->toHTML();
  146. // Search
  147. $html[] = '<div class="element_finder_search">';
  148. $this->_elements[1]->setValue('');
  149. $html[] = $this->_elements[1]->toHTML();
  150. if ($this->isCollapsed())
  151. {
  152. $html[] = '<button id="' . $this->getName() . '_collapse_button" style="display: none" class="normal hide">' . htmlentities(Translation :: get('Hide', null, Utilities :: COMMON_LIBRARIES)) . '</button>';
  153. }
  154. else
  155. {
  156. $html[] = '<button id="' . $this->getName() . '_collapse_button" class="normal hide mini">' . htmlentities(Translation :: get('Hide', null, Utilities :: COMMON_LIBRARIES)) . '</button>';
  157. }
  158. $html[] = '</div>';
  159. $html[] = '<div class="clear"></div>';
  160. // The elements
  161. $html[] = '<div class="element_finder_elements">';
  162. // Inactive
  163. $html[] = '<div class="element_finder_inactive">';
  164. $html[] = '<div id="elf_' . $this->getName() . '_inactive" class="inactive_elements" style="height: ' . $this->getHeight() . 'px; width: ' . $this->getWidth() . 'px; overflow: auto;">';
  165. $html[] = '</div>';
  166. $html[] = '<div class="clear"></div>';
  167. $html[] = '</div>';
  168. // Active
  169. $html[] = '<div class="element_finder_active">';
  170. $html[] = '<div id="elf_' . $this->getName() . '_active" class="active_elements" style="height: ' . $this->getHeight() . 'px; width: ' . $this->getWidth() . 'px; overflow: auto;"></div>';
  171. $html[] = '<div class="clear"></div>';
  172. $html[] = '</div>';
  173. // Make sure the elements are all within the div.
  174. $html[] = '<div class="clear"></div>';
  175. $html[] = '</div>';
  176. // Make sure everything is within the general div.
  177. $html[] = '<div class="clear"></div>';
  178. $html[] = '</div>';
  179. $html[] = ResourceManager :: get_instance()->get_resource_html(Path :: get(WEB_PLUGIN_PATH) . 'jquery/serializer.pack.js');
  180. $html[] = ResourceManager :: get_instance()->get_resource_html(Path :: get(WEB_PLUGIN_PATH) . 'jquery/jquery.elementfinder.js');
  181. $html[] = '<script type="text/javascript">';
  182. $exclude_ids = array();
  183. if (count($this->exclude))
  184. {
  185. $exclude_ids = array();
  186. foreach ($this->exclude as $exclude_id)
  187. {
  188. $exclude_ids[] = "'$exclude_id'";
  189. }
  190. }
  191. $html[] = 'var ' . $this->getName() . '_excluded = new Array(' . implode(',', $exclude_ids) . ');';
  192. $load_elements = $this->options['load_elements'];
  193. $load_elements = (isset($load_elements) && $load_elements == true ? ', loadElements: true' : ', loadElements: false');
  194. $default_query = $this->options['default_query'];
  195. $default_query = (isset($default_query) && ! empty($default_query) ? ', defaultQuery: "' . $default_query . '"' : '');
  196. $html[] = '$("#' . $id . '").elementfinder({ name: "' . $this->getName() . '", search: "' . $this->search_url . '"' . $load_elements . $default_query . ' });';
  197. $html[] = '</script>';
  198. return implode("\n", $html);
  199. }
  200. function setDefaults($defaults)
  201. {
  202. $this->defaults = $defaults;
  203. }
  204. function accept($renderer, $required = false, $error = null)
  205. {
  206. $renderer->renderElement($this, $required, $error);
  207. }
  208. }
  209. ?>