advmultiselect.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. <?php
  2. /**
  3. * Copyright (c) 2005-2009, Laurent Laville <pear@laurent-laville.org>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of the authors nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * PHP versions 4 and 5
  33. *
  34. * @category HTML
  35. * @package HTML_QuickForm_advmultiselect
  36. * @author Laurent Laville <pear@laurent-laville.org>
  37. * @copyright 2005-2009 Laurent Laville
  38. * @license http://www.opensource.org/licenses/bsd-license.php BSD
  39. * @version CVS: $Id: advmultiselect.php,v 1.36 2009/04/05 07:03:39 farell Exp $
  40. * @link http://pear.php.net/package/HTML_QuickForm_advmultiselect
  41. * @since File available since Release 0.4.0
  42. */
  43. /**
  44. * Basic error codes
  45. *
  46. * @var integer
  47. * @since 1.5.0
  48. */
  49. define('HTML_QUICKFORM_ADVMULTISELECT_ERROR_INVALID_INPUT', 1);
  50. /**
  51. * Element for HTML_QuickForm that emulate a multi-select.
  52. *
  53. * The HTML_QuickForm_advmultiselect package adds an element to the
  54. * HTML_QuickForm package that is two select boxes next to each other
  55. * emulating a multi-select.
  56. *
  57. * @category HTML
  58. * @package HTML_QuickForm_advmultiselect
  59. * @author Laurent Laville <pear@laurent-laville.org>
  60. * @copyright 2005-2009 Laurent Laville
  61. * @license http://www.opensource.org/licenses/bsd-license.php BSD
  62. * @version Release: @package_version@
  63. * @link http://pear.php.net/package/HTML_QuickForm_advmultiselect
  64. * @since Class available since Release 0.4.0
  65. */
  66. class HTML_QuickForm_advmultiselect extends HTML_QuickForm_select
  67. {
  68. /**
  69. * Prefix function name in javascript move selections
  70. *
  71. * @var string
  72. * @access private
  73. * @since 0.4.0
  74. */
  75. var $_jsPrefix;
  76. /**
  77. * Postfix function name in javascript move selections
  78. *
  79. * @var string
  80. * @access private
  81. * @since 0.4.0
  82. */
  83. var $_jsPostfix;
  84. /**
  85. * Associative array of the multi select container attributes
  86. *
  87. * @var array
  88. * @access private
  89. * @since 0.4.0
  90. */
  91. var $_tableAttributes;
  92. /**
  93. * Associative array of the add button attributes
  94. *
  95. * @var array
  96. * @access private
  97. * @since 0.4.0
  98. */
  99. var $_addButtonAttributes;
  100. /**
  101. * Associative array of the remove button attributes
  102. *
  103. * @var array
  104. * @access private
  105. * @since 0.4.0
  106. */
  107. var $_removeButtonAttributes;
  108. /**
  109. * Associative array of the select all button attributes
  110. *
  111. * @var array
  112. * @access private
  113. * @since 1.1.0
  114. */
  115. var $_allButtonAttributes;
  116. /**
  117. * Associative array of the select none button attributes
  118. *
  119. * @var array
  120. * @access private
  121. * @since 1.1.0
  122. */
  123. var $_noneButtonAttributes;
  124. /**
  125. * Associative array of the toggle selection button attributes
  126. *
  127. * @var array
  128. * @access private
  129. * @since 1.1.0
  130. */
  131. var $_toggleButtonAttributes;
  132. /**
  133. * Associative array of the move up button attributes
  134. *
  135. * @var array
  136. * @access private
  137. * @since 0.5.0
  138. */
  139. var $_upButtonAttributes;
  140. /**
  141. * Associative array of the move up button attributes
  142. *
  143. * @var array
  144. * @access private
  145. * @since 0.5.0
  146. */
  147. var $_downButtonAttributes;
  148. /**
  149. * Associative array of the move top button attributes
  150. *
  151. * @var array
  152. * @access private
  153. * @since 1.5.0
  154. */
  155. var $_topButtonAttributes;
  156. /**
  157. * Associative array of the move bottom button attributes
  158. *
  159. * @var array
  160. * @access private
  161. * @since 1.5.0
  162. */
  163. var $_bottomButtonAttributes;
  164. /**
  165. * Defines if both list (unselected, selected) will have their elements be
  166. * arranged from lowest to highest (or reverse)
  167. * depending on comparaison function.
  168. *
  169. * SORT_ASC is used to sort in ascending order
  170. * SORT_DESC is used to sort in descending order
  171. *
  172. * @var string ('none' == false, 'asc' == SORT_ASC, 'desc' == SORT_DESC)
  173. * @access private
  174. * @since 0.5.0
  175. */
  176. var $_sort;
  177. /**
  178. * Associative array of the unselected item box attributes
  179. *
  180. * @var array
  181. * @access private
  182. * @since 0.4.0
  183. */
  184. var $_attributesUnselected;
  185. /**
  186. * Associative array of the selected item box attributes
  187. *
  188. * @var array
  189. * @access private
  190. * @since 0.4.0
  191. */
  192. var $_attributesSelected;
  193. /**
  194. * Associative array of the internal hidden box attributes
  195. *
  196. * @var array
  197. * @access private
  198. * @since 0.4.0
  199. */
  200. var $_attributesHidden;
  201. public $selectAllCheckBox = false;
  202. /**
  203. * Default Element template string
  204. *
  205. * @var string
  206. * @access private
  207. * @since 0.4.0
  208. */
  209. var $_elementTemplate;
  210. /**
  211. * Default Element stylesheet string
  212. *
  213. * @var string
  214. * @access private
  215. * @since 0.4.0
  216. */
  217. var $_elementCSS = '
  218. #qfams_{id} {
  219. font: 13.3px sans-serif;
  220. background-color: #fff;
  221. overflow: auto;
  222. height: 14.3em;
  223. width: 12em;
  224. border-left: 1px solid #404040;
  225. border-top: 1px solid #404040;
  226. border-bottom: 1px solid #d4d0c8;
  227. border-right: 1px solid #d4d0c8;
  228. }
  229. #qfams_{id} label {
  230. padding-right: 3px;
  231. display: block;
  232. }
  233. ';
  234. /**
  235. * Class constructor
  236. *
  237. * Class constructors :
  238. * Zend Engine 1 uses HTML_QuickForm_advmultiselect, while
  239. * Zend Engine 2 uses __construct
  240. *
  241. * @param string $elementName Dual Select name attribute
  242. * @param mixed $elementLabel Label(s) for the select boxes
  243. * @param mixed $options Data to be used to populate options
  244. * @param mixed $attributes Either a typical HTML attribute string or
  245. * an associative array
  246. * @param integer $sort Either SORT_ASC for auto ascending arrange,
  247. * SORT_DESC for auto descending arrange, or
  248. * NULL for no sort (append at end: default)
  249. *
  250. * @access public
  251. * @return void
  252. * @since version 0.4.0 (2005-06-25)
  253. */
  254. public function __construct(
  255. $elementName = null,
  256. $elementLabel = null,
  257. $options = null,
  258. $attributes = null,
  259. $sort = null
  260. ) {
  261. $opts = $options;
  262. $options = null; // prevent to use the default select element load options
  263. parent::__construct($elementName, $elementLabel, $options, $attributes);
  264. $this->selectAllCheckBox = isset($attributes['select_all_checkbox']) ? $attributes['select_all_checkbox'] : false;
  265. // allow to load options at once and take care of fancy attributes
  266. $this->load($opts);
  267. // add multiple selection attribute by default if missing
  268. $this->updateAttributes(array('multiple' => 'multiple'));
  269. if (is_null($this->getAttribute('size'))) {
  270. // default size is ten item on each select box (left and right)
  271. $this->updateAttributes(array('size' => 10));
  272. }
  273. if (is_null($this->getAttribute('class'))) {
  274. // default width of each select box
  275. $this->updateAttributes(array('class' => 'form-control'));
  276. }
  277. /* $this->_tableAttributes = $this->getAttribute('class');
  278. $attr = null;
  279. if (is_null($this->_tableAttributes)) {
  280. $this->updateAttributes(array('class' => 'col-md-4'));
  281. } else {
  282. $attr = array('class' => $this->_tableAttributes);
  283. $this->_removeAttr('class', $this->_attributes);
  284. }*/
  285. //$this->_tableAttributes = $this->_getAttrString($attr);
  286. $this->removeAttribute('class');
  287. $this->setAttribute('class','form-control');
  288. // set default add button attributes
  289. $this->setButtonAttributes('add');
  290. // set default remove button attributes
  291. $this->setButtonAttributes('remove');
  292. // set default selectall button attributes
  293. $this->setButtonAttributes('all');
  294. // set default selectnone button attributes
  295. $this->setButtonAttributes('none');
  296. // set default toggle selection button attributes
  297. $this->setButtonAttributes('toggle');
  298. // set default move up button attributes
  299. $this->setButtonAttributes('moveup');
  300. // set default move up button attributes
  301. $this->setButtonAttributes('movedown');
  302. // set default move top button attributes
  303. $this->setButtonAttributes('movetop');
  304. // set default move bottom button attributes
  305. $this->setButtonAttributes('movebottom');
  306. // defines javascript functions names
  307. $this->_jsPrefix = 'QFAMS.';
  308. $this->_jsPostfix = 'moveSelection';
  309. // set select boxes sort order (none by default)
  310. if (!isset($sort)) {
  311. $sort = false;
  312. }
  313. if ($sort === SORT_ASC) {
  314. $this->_sort = 'asc';
  315. } elseif ($sort === SORT_DESC) {
  316. $this->_sort = 'desc';
  317. } else {
  318. $this->_sort = 'none';
  319. }
  320. // set the default advmultiselect element template (with javascript embedded)
  321. $this->setElementTemplate();
  322. }
  323. /**
  324. * Sets the button attributes
  325. *
  326. * In <b>custom example 1</b>, the <i>add</i> and <i>remove</i> buttons
  327. * have look set by the css class <i>inputCommand</i>.
  328. *
  329. * In <b>custom example 2</b>, the basic text <i>add</i> and <i>remove</i>
  330. * buttons are now replaced by images.
  331. *
  332. * In <b>custom example 5</b>, we have ability to sort the selection list
  333. * (on right side) by :
  334. * <pre>
  335. * - <b>user-end</b>: with <i>Up</i> and <i>Down</i> buttons
  336. * - <b>programming</b>: with the QF element constructor $sort option
  337. * </pre>
  338. *
  339. * @param string $button Button identifier, either 'add', 'remove',
  340. * 'all', 'none', 'toggle',
  341. * 'movetop', 'movebottom'
  342. * 'moveup' or 'movedown'
  343. * @param mixed $attributes (optional) Either a typical HTML attribute string
  344. * or an associative array
  345. *
  346. * @return void
  347. * @throws PEAR_Error $button argument
  348. * is not a string, or not in range
  349. * (add, remove, all, none, toggle,
  350. * movetop, movebottom, moveup, movedown)
  351. * @access public
  352. * @since version 0.4.0 (2005-06-25)
  353. *
  354. * @example examples/qfams_custom_5.php
  355. * Custom example 5: source code
  356. * @link http://www.laurent-laville.org/img/qfams/screenshot/custom5.png
  357. * Custom example 5: screenshot
  358. *
  359. * @example examples/qfams_custom_2.php
  360. * Custom example 2: source code
  361. * @link http://www.laurent-laville.org/img/qfams/screenshot/custom2.png
  362. * Custom example 2: screenshot
  363. *
  364. * @example examples/qfams_custom_1.php
  365. * Custom example 1: source code
  366. * @link http://www.laurent-laville.org/img/qfams/screenshot/custom1.png
  367. * Custom example 1: screenshot
  368. */
  369. function setButtonAttributes($button, $attributes = null)
  370. {
  371. if (!is_string($button)) {
  372. return PEAR::throwError('Argument 1 of HTML_QuickForm_advmultiselect::' .
  373. 'setButtonAttributes is not a string',
  374. HTML_QUICKFORM_ADVMULTISELECT_ERROR_INVALID_INPUT,
  375. array('level' => 'exception'));
  376. }
  377. switch ($button) {
  378. case 'add':
  379. if (is_null($attributes)) {
  380. $this->_addButtonAttributes = array(
  381. 'name' => 'add',
  382. 'value' => ' ',
  383. 'type' => 'button',
  384. 'class'=> 'btn btn-primary'
  385. );
  386. } else {
  387. $this->_updateAttrArray(
  388. $this->_addButtonAttributes,
  389. $this->_parseAttributes($attributes)
  390. );
  391. }
  392. break;
  393. case 'remove':
  394. if (is_null($attributes)) {
  395. $this->_removeButtonAttributes = array(
  396. 'name' => 'remove',
  397. 'value' => ' ',
  398. 'type' => 'button',
  399. 'class'=> 'btn btn-primary'
  400. );
  401. } else {
  402. $this->_updateAttrArray($this->_removeButtonAttributes,
  403. $this->_parseAttributes($attributes));
  404. }
  405. break;
  406. case 'all':
  407. if (is_null($attributes)) {
  408. $this->_allButtonAttributes = array(
  409. 'name' => 'all',
  410. 'value' => ' Select All ',
  411. 'type' => 'button'
  412. );
  413. } else {
  414. $this->_updateAttrArray($this->_allButtonAttributes,
  415. $this->_parseAttributes($attributes));
  416. }
  417. break;
  418. case 'none':
  419. if (is_null($attributes)) {
  420. $this->_noneButtonAttributes
  421. = array('name' => 'none',
  422. 'value' => ' Select None ',
  423. 'type' => 'button');
  424. } else {
  425. $this->_updateAttrArray($this->_noneButtonAttributes,
  426. $this->_parseAttributes($attributes));
  427. }
  428. break;
  429. case 'toggle':
  430. if (is_null($attributes)) {
  431. $this->_toggleButtonAttributes
  432. = array('name' => 'toggle',
  433. 'value' => ' Toggle Selection ',
  434. 'type' => 'button');
  435. } else {
  436. $this->_updateAttrArray($this->_toggleButtonAttributes,
  437. $this->_parseAttributes($attributes));
  438. }
  439. break;
  440. case 'moveup':
  441. if (is_null($attributes)) {
  442. $this->_upButtonAttributes
  443. = array('name' => 'up',
  444. 'value' => ' Up ',
  445. 'type' => 'button');
  446. } else {
  447. $this->_updateAttrArray($this->_upButtonAttributes,
  448. $this->_parseAttributes($attributes));
  449. }
  450. break;
  451. case 'movedown':
  452. if (is_null($attributes)) {
  453. $this->_downButtonAttributes
  454. = array('name' => 'down',
  455. 'value' => ' Down ',
  456. 'type' => 'button');
  457. } else {
  458. $this->_updateAttrArray($this->_downButtonAttributes,
  459. $this->_parseAttributes($attributes));
  460. }
  461. break;
  462. case 'movetop':
  463. if (is_null($attributes)) {
  464. $this->_topButtonAttributes
  465. = array('name' => 'top',
  466. 'value' => ' Top ',
  467. 'type' => 'button');
  468. } else {
  469. $this->_updateAttrArray($this->_topButtonAttributes,
  470. $this->_parseAttributes($attributes));
  471. }
  472. break;
  473. case 'movebottom':
  474. if (is_null($attributes)) {
  475. $this->_bottomButtonAttributes
  476. = array('name' => 'bottom',
  477. 'value' => ' Bottom ',
  478. 'type' => 'button');
  479. } else {
  480. $this->_updateAttrArray($this->_bottomButtonAttributes,
  481. $this->_parseAttributes($attributes));
  482. }
  483. break;
  484. default;
  485. return PEAR::throwError('Argument 1 of HTML_QuickForm_advmultiselect::' .
  486. 'setButtonAttributes has unexpected value',
  487. HTML_QUICKFORM_ADVMULTISELECT_ERROR_INVALID_INPUT,
  488. array('level' => 'error'));
  489. }
  490. }
  491. /**
  492. * Sets element template
  493. *
  494. * @param string $html (optional) The HTML surrounding select boxes and buttons
  495. * @param string $js (optional) if we need to include qfams javascript handler
  496. *
  497. * @access public
  498. * @return string
  499. * @since version 0.4.0 (2005-06-25)
  500. */
  501. function setElementTemplate($html = null, $js = true)
  502. {
  503. $oldTemplate = $this->_elementTemplate;
  504. if (isset($html) && is_string($html)) {
  505. $this->_elementTemplate = $html;
  506. } else {
  507. $this->_elementTemplate = '
  508. {javascript}
  509. <div class="row">
  510. <div class="col-sm-5"><!-- BEGIN label_2 -->{label_2}<!-- END label_2 --> {unselected}</div>
  511. <div class="col-sm-2"><div class="text-center">{add}{remove}</div></div>
  512. <div class="col-sm-5"><!-- BEGIN label_3 -->{label_3}<!-- END label_3 -->{selected}</div>
  513. </div>
  514. ';
  515. }
  516. if ($js == false) {
  517. $this->_elementTemplate = str_replace('{javascript}', '',
  518. $this->_elementTemplate);
  519. }
  520. return $oldTemplate;
  521. }
  522. /**
  523. * Gets default element stylesheet for a single multi-select shape render
  524. *
  525. * In <b>custom example 4</b>, the template defined allows
  526. * a single multi-select checkboxes shape. Useful when javascript is disabled
  527. * (or when browser is not js compliant). In our example, no need to add
  528. * javascript code, but css is mandatory.
  529. *
  530. * @param boolean $raw (optional) html output with style tags or just raw data
  531. *
  532. * @access public
  533. * @return string
  534. * @since version 0.4.0 (2005-06-25)
  535. *
  536. * @example qfams_custom_4.php
  537. * Custom example 4: source code
  538. * @link http://www.laurent-laville.org/img/qfams/screenshot/custom4.png
  539. * Custom example 4: screenshot
  540. */
  541. function getElementCss($raw = true)
  542. {
  543. $id = $this->getAttribute('name');
  544. $css = str_replace('{id}', $id, $this->_elementCSS);
  545. if ($raw !== true) {
  546. $css = '<style type="text/css">' . PHP_EOL
  547. // Modified by Chamilo team, 16-MAR-2010.
  548. //. '<!--' . $css . '// -->' . PHP_EOL
  549. . '/*<![CDATA[*/' . PHP_EOL
  550. . $css . PHP_EOL
  551. . '/*]]>*/' . PHP_EOL
  552. //
  553. . '</style>';
  554. }
  555. return $css;
  556. }
  557. /**
  558. * Returns the HTML generated for the advanced mutliple select component
  559. *
  560. * @access public
  561. * @return string
  562. * @since version 0.4.0 (2005-06-25)
  563. */
  564. public function toHtml()
  565. {
  566. if ($this->_flagFrozen) {
  567. return $this->getFrozenHtml();
  568. }
  569. $tabs = $this->_getTabs();
  570. $tab = $this->_getTab();
  571. $selectId = $this->getName();
  572. $selectName = $this->getName() . '[]';
  573. $selectNameFrom = $this->getName() . '-f[]';
  574. $selectNameTo = $this->getName() . '-t[]';
  575. $selected_count = 0;
  576. // placeholder {unselected} existence determines if we will render
  577. if (strpos($this->_elementTemplate, '{unselected}') === false) {
  578. // ... a single multi-select with checkboxes
  579. $this->_jsPostfix = 'editSelection';
  580. $id = $this->getAttribute('name');
  581. $strHtmlSelected = $tab . '<div id="qfams_'.$id.'">' . PHP_EOL;
  582. $unselected_count = count($this->_options);
  583. $checkbox_id_suffix = 0;
  584. foreach ($this->_options as $option) {
  585. $_labelAttributes
  586. = array('style', 'class', 'onmouseover', 'onmouseout');
  587. $labelAttributes = array();
  588. foreach ($_labelAttributes as $attr) {
  589. if (isset($option['attr'][$attr])) {
  590. $labelAttributes[$attr] = $option['attr'][$attr];
  591. unset($option['attr'][$attr]);
  592. }
  593. }
  594. if (is_array($this->_values)
  595. && in_array((string)$option['attr']['value'], $this->_values)) {
  596. // The items is *selected*
  597. $checked = ' checked="checked"';
  598. $selected_count++;
  599. } else {
  600. // The item is *unselected* so we want to put it
  601. $checked = '';
  602. }
  603. $checkbox_id_suffix++;
  604. $strHtmlSelected .= $tab
  605. . '<label'
  606. . $this->_getAttrString($labelAttributes) .'>'
  607. . '<input type="checkbox"'
  608. . ' id="'.$selectId . $checkbox_id_suffix.'"'
  609. . ' name="'.$selectName.'"'
  610. . $checked
  611. . $this->_getAttrString($option['attr'])
  612. . ' />' . $option['text'] . '</label>'
  613. . PHP_EOL;
  614. }
  615. $strHtmlSelected .= $tab . '</div>'. PHP_EOL;
  616. $strHtmlHidden = '';
  617. $strHtmlUnselected = '';
  618. $strHtmlAdd = '';
  619. $strHtmlRemove = '';
  620. // build the select all button with all its attributes
  621. $jsName = $this->_jsPrefix . $this->_jsPostfix;
  622. $attributes = array('onclick' => $jsName ."('". $selectId ."', 1);");
  623. $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes);
  624. $attrStrAll = $this->_getAttrString($this->_allButtonAttributes);
  625. $strHtmlAll = "<input$attrStrAll />". PHP_EOL;
  626. // build the select none button with all its attributes
  627. $attributes = array('onclick' => $jsName ."('". $selectId ."', 0);");
  628. $this->_noneButtonAttributes
  629. = array_merge($this->_noneButtonAttributes, $attributes);
  630. $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes);
  631. $strHtmlNone = "<input$attrStrNone />". PHP_EOL;
  632. // build the toggle selection button with all its attributes
  633. $attributes = array('onclick' => $jsName .
  634. "('". $selectId ."', 2);");
  635. $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes);
  636. $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes);
  637. $strHtmlToggle = "<input$attrStrToggle />". PHP_EOL;
  638. $strHtmlMoveUp = '';
  639. $strHtmlMoveDown = '';
  640. $strHtmlMoveTop = '';
  641. $strHtmlMoveBottom = '';
  642. // default selection counters
  643. $strHtmlSelectedCount = $selected_count . '/' . $unselected_count;
  644. } else {
  645. // ... or a dual multi-select
  646. $this->_jsPostfix = 'moveSelection';
  647. $jsName = $this->_jsPrefix . $this->_jsPostfix;
  648. // set name of Select From Box
  649. $this->_attributesUnselected
  650. = array('id' => $selectId . '-f',
  651. 'name' => $selectNameFrom,
  652. 'ondblclick' => $jsName .
  653. "('{$selectId}', ".
  654. "this.form.elements['" . $selectNameFrom . "'], " .
  655. "this.form.elements['" . $selectNameTo . "'], " .
  656. "this.form.elements['" . $selectName . "'], " .
  657. "'add', '{$this->_sort}')");
  658. $this->_attributesUnselected
  659. = array_merge($this->_attributes, $this->_attributesUnselected);
  660. $attrUnselected = $this->_getAttrString($this->_attributesUnselected);
  661. // set name of Select To Box
  662. $this->_attributesSelected
  663. = array('id' => $selectId . '-t',
  664. 'name' => $selectNameTo,
  665. 'ondblclick' => $jsName .
  666. "('{$selectId}', " .
  667. "this.form.elements['" . $selectNameFrom . "'], " .
  668. "this.form.elements['" . $selectNameTo . "'], ".
  669. "this.form.elements['" . $selectName . "'], " .
  670. "'remove', '{$this->_sort}')");
  671. $this->_attributesSelected
  672. = array_merge($this->_attributes, $this->_attributesSelected);
  673. $attrSelected = $this->_getAttrString($this->_attributesSelected);
  674. // set name of Select hidden Box
  675. $this->_attributesHidden
  676. = array('name' => $selectName,
  677. 'style' => 'overflow: hidden; visibility: hidden; ' .
  678. 'width: 1px; height: 0;');
  679. $this->_attributesHidden
  680. = array_merge($this->_attributes, $this->_attributesHidden);
  681. $attrHidden = $this->_getAttrString($this->_attributesHidden);
  682. // prepare option tables to be displayed as in POST order
  683. $append = count($this->_values);
  684. if ($append > 0) {
  685. $arrHtmlSelected = array_fill(0, $append, ' ');
  686. } else {
  687. $arrHtmlSelected = array();
  688. }
  689. $options = count($this->_options);
  690. $arrHtmlUnselected = array();
  691. if ($options > 0) {
  692. $arrHtmlHidden = array_fill(0, $options, ' ');
  693. foreach ($this->_options as $option) {
  694. if (is_array($this->_values)
  695. && in_array((string)$option['attr']['value'],
  696. $this->_values)) {
  697. // Get the post order
  698. $key = array_search($option['attr']['value'],
  699. $this->_values);
  700. /** The items is *selected* so we want to put it
  701. in the 'selected' multi-select */
  702. $arrHtmlSelected[$key] = $option;
  703. /** Add it to the 'hidden' multi-select
  704. and set it as 'selected' */
  705. if (isset($option['attr']['disabled'])) {
  706. unset($option['attr']['disabled']);
  707. }
  708. $option['attr']['selected'] = 'selected';
  709. $arrHtmlHidden[$key] = $option;
  710. } else {
  711. /** The item is *unselected* so we want to put it
  712. in the 'unselected' multi-select */
  713. $arrHtmlUnselected[] = $option;
  714. // Add it to the hidden multi-select as 'unselected'
  715. $arrHtmlHidden[$append] = $option;
  716. $append++;
  717. }
  718. }
  719. } else {
  720. $arrHtmlHidden = array();
  721. }
  722. // The 'unselected' multi-select which appears on the left
  723. $unselected_count = count($arrHtmlUnselected);
  724. if ($unselected_count == 0) {
  725. $this->_attributesUnselected['disabled'] = 'disabled';
  726. $this->_attributesUnselected
  727. = array_merge($this->_attributes, $this->_attributesUnselected);
  728. $attrUnselected = $this->_getAttrString($this->_attributesUnselected);
  729. }
  730. $strHtmlUnselected = "<select$attrUnselected>". PHP_EOL;
  731. if ($unselected_count > 0) {
  732. foreach ($arrHtmlUnselected as $data) {
  733. $strHtmlUnselected
  734. .= $tabs . $tab
  735. . '<option' . $this->_getAttrString($data['attr']) . '>'
  736. . $data['text'] . '</option>' . PHP_EOL;
  737. }
  738. } else {
  739. $strHtmlUnselected .= '<option value="">&nbsp;</option>';
  740. }
  741. $strHtmlUnselected .= '</select>';
  742. $strHtmlUnselected = '<input placeholder="'.get_lang('Search').'" id="'.$selectId.'-f-filter" type="text" class="form-control search-query select_class_filter"><br />'.$strHtmlUnselected;
  743. // The 'selected' multi-select which appears on the right
  744. $selected_count = count($arrHtmlSelected);
  745. if ($selected_count == 0) {
  746. $this->_attributesSelected['disabled'] = 'disabled';
  747. $this->_attributesSelected
  748. = array_merge($this->_attributes, $this->_attributesSelected);
  749. $attrSelected = $this->_getAttrString($this->_attributesSelected);
  750. }
  751. $strHtmlSelected = "<select$attrSelected>";
  752. if ($selected_count > 0) {
  753. foreach ($arrHtmlSelected as $data) {
  754. $attribute = null;
  755. if (isset($data['attr'])) {
  756. $attribute = $this->_getAttrString($data['attr']);
  757. }
  758. $text = null;
  759. if (isset($data['text'])) {
  760. $text = $data['text'];
  761. }
  762. $strHtmlSelected
  763. .= $tabs . $tab
  764. . '<option' . $attribute. '>'
  765. . $text . '</option>';
  766. }
  767. } else {
  768. $strHtmlSelected .= '<option value="">&nbsp;</option>';
  769. }
  770. $strHtmlSelected .= '</select>';
  771. $strHtmlSelected = '<input placeholder="'.get_lang('Search').'" id="'.$selectId.'-t-filter" type="text" class="form-control search-query select_class_filter"><br />'.$strHtmlSelected;
  772. // The 'hidden' multi-select
  773. $strHtmlHidden = "<select$attrHidden>". PHP_EOL;
  774. if (count($arrHtmlHidden) > 0) {
  775. foreach ($arrHtmlHidden as $data) {
  776. $attribute = null;
  777. if (isset($data['attr'])) {
  778. $attribute = $this->_getAttrString($data['attr']);
  779. }
  780. $text = null;
  781. if (isset($data['text'])) {
  782. $text = $data['text'];
  783. }
  784. $strHtmlHidden
  785. .= $tabs . $tab
  786. . '<option' . $attribute . '>'
  787. . $text . '</option>' . PHP_EOL;
  788. }
  789. }
  790. $strHtmlHidden .= '</select>';
  791. // build the remove button with all its attributes
  792. $attributes
  793. = array('onclick' => $jsName .
  794. "('{$selectId}', " .
  795. "this.form.elements['" . $selectNameFrom . "'], " .
  796. "this.form.elements['" . $selectNameTo . "'], " .
  797. "this.form.elements['" . $selectName . "'], " .
  798. "'remove', '{$this->_sort}'); return false;");
  799. $this->_removeButtonAttributes
  800. = array_merge($this->_removeButtonAttributes, $attributes);
  801. $attrStrRemove = $this->_getAttrString($this->_removeButtonAttributes);
  802. $strHtmlRemove = "<button $attrStrRemove /> <em class='fa fa-arrow-left'></em></button>";
  803. // build the add button with all its attributes
  804. $attributes
  805. = array('onclick' => $jsName .
  806. "('{$selectId}', " .
  807. "this.form.elements['" . $selectNameFrom . "'], " .
  808. "this.form.elements['" . $selectNameTo . "'], " .
  809. "this.form.elements['" . $selectName . "'], " .
  810. "'add', '{$this->_sort}'); return false;");
  811. $this->_addButtonAttributes = array_merge($this->_addButtonAttributes, $attributes);
  812. $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes);
  813. $strHtmlAdd = "<button $attrStrAdd /> <em class='fa fa-arrow-right'></em></button><br /><br />";
  814. // build the select all button with all its attributes
  815. $attributes
  816. = array('onclick' => $jsName .
  817. "('{$selectId}', " .
  818. "this.form.elements['" . $selectNameFrom . "'], " .
  819. "this.form.elements['" . $selectNameTo . "'], " .
  820. "this.form.elements['" . $selectName . "'], " .
  821. "'all', '{$this->_sort}'); return false;");
  822. $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes);
  823. $attrStrAll = $this->_getAttrString($this->_allButtonAttributes);
  824. $strHtmlAll = "<input$attrStrAll />". PHP_EOL;
  825. // build the select none button with all its attributes
  826. $attributes
  827. = array('onclick' => $jsName .
  828. "('{$selectId}', " .
  829. "this.form.elements['" . $selectNameFrom . "'], " .
  830. "this.form.elements['" . $selectNameTo . "'], " .
  831. "this.form.elements['" . $selectName . "'], " .
  832. "'none', '{$this->_sort}'); return false;");
  833. $this->_noneButtonAttributes
  834. = array_merge($this->_noneButtonAttributes, $attributes);
  835. $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes);
  836. $strHtmlNone = "<input$attrStrNone />". PHP_EOL;
  837. // build the toggle button with all its attributes
  838. $attributes
  839. = array('onclick' => $jsName .
  840. "('{$selectId}', " .
  841. "this.form.elements['" . $selectNameFrom . "'], " .
  842. "this.form.elements['" . $selectNameTo . "'], " .
  843. "this.form.elements['" . $selectName . "'], " .
  844. "'toggle', '{$this->_sort}'); return false;");
  845. $this->_toggleButtonAttributes
  846. = array_merge($this->_toggleButtonAttributes, $attributes);
  847. $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes);
  848. $strHtmlToggle = "<input$attrStrToggle />". PHP_EOL;
  849. // build the move up button with all its attributes
  850. $attributes
  851. = array('onclick' => "{$this->_jsPrefix}moveUp" .
  852. "(this.form.elements['" . $selectNameTo . "'], " .
  853. "this.form.elements['" . $selectName . "']); " .
  854. "return false;");
  855. $this->_upButtonAttributes
  856. = array_merge($this->_upButtonAttributes, $attributes);
  857. $attrStrUp = $this->_getAttrString($this->_upButtonAttributes);
  858. $strHtmlMoveUp = "<input$attrStrUp />". PHP_EOL;
  859. // build the move down button with all its attributes
  860. $attributes
  861. = array('onclick' => "{$this->_jsPrefix}moveDown" .
  862. "(this.form.elements['" . $selectNameTo . "'], " .
  863. "this.form.elements['" . $selectName . "']); " .
  864. "return false;");
  865. $this->_downButtonAttributes
  866. = array_merge($this->_downButtonAttributes, $attributes);
  867. $attrStrDown = $this->_getAttrString($this->_downButtonAttributes);
  868. $strHtmlMoveDown = "<input$attrStrDown />". PHP_EOL;
  869. // build the move top button with all its attributes
  870. $attributes
  871. = array('onclick' => "{$this->_jsPrefix}moveTop" .
  872. "(this.form.elements['" . $selectNameTo . "'], " .
  873. "this.form.elements['" . $selectName . "']); " .
  874. "return false;");
  875. $this->_topButtonAttributes
  876. = array_merge($this->_topButtonAttributes, $attributes);
  877. $attrStrTop = $this->_getAttrString($this->_topButtonAttributes);
  878. $strHtmlMoveTop = "<input$attrStrTop />". PHP_EOL;
  879. // build the move bottom button with all its attributes
  880. $attributes
  881. = array('onclick' => "{$this->_jsPrefix}moveBottom" .
  882. "(this.form.elements['" . $selectNameTo . "'], " .
  883. "this.form.elements['" . $selectName . "']); " .
  884. "return false;");
  885. $this->_bottomButtonAttributes
  886. = array_merge($this->_bottomButtonAttributes, $attributes);
  887. $attrStrBottom = $this->_getAttrString($this->_bottomButtonAttributes);
  888. $strHtmlMoveBottom = "<input$attrStrBottom />". PHP_EOL;
  889. // default selection counters
  890. $strHtmlSelectedCount = $selected_count;
  891. }
  892. $strHtmlUnselectedCount = $unselected_count;
  893. $strHtmlSelectedCountId = $selectId .'_selected';
  894. $strHtmlUnselectedCountId = $selectId .'_unselected';
  895. // render all part of the multi select component with the template
  896. $strHtml = $this->_elementTemplate;
  897. // Prepare multiple labels
  898. $labels = $this->getLabel();
  899. if (is_array($labels)) {
  900. array_shift($labels);
  901. }
  902. // render extra labels, if any
  903. if (is_array($labels)) {
  904. foreach ($labels as $key => $text) {
  905. $key = is_int($key)? $key + 2: $key;
  906. $strHtml = str_replace("{label_{$key}}", $text, $strHtml);
  907. $strHtml = str_replace("<!-- BEGIN label_{$key} -->", '', $strHtml);
  908. $strHtml = str_replace("<!-- END label_{$key} -->", '', $strHtml);
  909. }
  910. }
  911. // clean up useless label tags
  912. if (strpos($strHtml, '{label_')) {
  913. $strHtml = preg_replace('/\s*<!-- BEGIN label_(\S+) -->'.
  914. '.*<!-- END label_\1 -->\s*/i', '', $strHtml);
  915. }
  916. $placeHolders = array(
  917. '{stylesheet}',
  918. '{javascript}',
  919. '{class}',
  920. '{unselected_count_id}',
  921. '{selected_count_id}',
  922. '{unselected_count}',
  923. '{selected_count}',
  924. '{unselected}',
  925. '{selected}',
  926. '{add}',
  927. '{remove}',
  928. '{all}',
  929. '{none}',
  930. '{toggle}',
  931. '{moveup}',
  932. '{movedown}',
  933. '{movetop}',
  934. '{movebottom}',
  935. );
  936. $htmlElements = array(
  937. $this->getElementCss(false),
  938. $this->getElementJs(false),
  939. $this->_tableAttributes,
  940. $strHtmlUnselectedCountId,
  941. $strHtmlSelectedCountId,
  942. $strHtmlUnselectedCount,
  943. $strHtmlSelectedCount,
  944. $strHtmlUnselected,
  945. $strHtmlSelected.$strHtmlHidden,
  946. $strHtmlAdd,
  947. $strHtmlRemove,
  948. $strHtmlAll,
  949. $strHtmlNone,
  950. $strHtmlToggle,
  951. $strHtmlMoveUp,
  952. $strHtmlMoveDown,
  953. $strHtmlMoveTop,
  954. $strHtmlMoveBottom,
  955. );
  956. if ($this->selectAllCheckBox) {
  957. $strHtml .= '
  958. <div class="col-sm-8">
  959. <label >'.get_lang('SelectAll').'
  960. <input type="checkbox" class="advmultiselect_checkbox" id="'.$selectId.'_select_all'.'">
  961. </label>
  962. </div>
  963. ';
  964. }
  965. $strHtml = str_replace($placeHolders, $htmlElements, $strHtml);
  966. $comment = $this->getComment();
  967. if (!empty($comment)) {
  968. $strHtml = $tabs . '<!-- ' . $comment . " //-->" . PHP_EOL . $strHtml;
  969. }
  970. return $strHtml;
  971. }
  972. /**
  973. * Returns the javascript code generated to handle this element
  974. *
  975. * @param boolean $raw (optional) html output with script tags or just raw data
  976. * @param boolean $min (optional) uses javascript compressed version
  977. *
  978. * @access public
  979. * @return string
  980. * @since version 0.4.0 (2005-06-25)
  981. */
  982. function getElementJs($raw = true, $min = true)
  983. {
  984. $js = api_get_path(LIBRARY_PATH).'javascript'.DIRECTORY_SEPARATOR.'pear'.DIRECTORY_SEPARATOR;
  985. /*if ($min) {
  986. $js .= 'qfamsHandler-min.js';
  987. } else {
  988. $js .= 'qfamsHandler.js';
  989. }*/
  990. $js .= 'qfamsHandler.js';
  991. if (file_exists($js)) {
  992. $js = file_get_contents($js);
  993. } else {
  994. $js = '';
  995. }
  996. if ($raw !== true) {
  997. $js = '<script type="text/javascript">'
  998. . PHP_EOL . '//<![CDATA['
  999. . PHP_EOL . $js
  1000. . PHP_EOL . '//]]>'
  1001. . PHP_EOL . '</script>'
  1002. . PHP_EOL;
  1003. }
  1004. return $js;
  1005. }
  1006. /**
  1007. * Loads options from different types of data sources
  1008. *
  1009. * This method overloaded parent method of select element, to allow
  1010. * loading options with fancy attributes.
  1011. *
  1012. * @param mixed &$options Options source currently supports assoc array or DB_result
  1013. * @param mixed $param1 (optional) See function detail
  1014. * @param mixed $param2 (optional) See function detail
  1015. * @param mixed $param3 (optional) See function detail
  1016. * @param mixed $param4 (optional) See function detail
  1017. *
  1018. * @access public
  1019. * @since version 1.5.0 (2009-02-15)
  1020. * @return PEAR_Error|NULL on error and TRUE on success
  1021. * @throws PEAR_Error
  1022. * @see loadArray()
  1023. */
  1024. function load(&$options,
  1025. $param1 = null, $param2 = null, $param3 = null, $param4 = null)
  1026. {
  1027. if (is_array($options)) {
  1028. $ret = $this->loadArray($options, $param1);
  1029. } else {
  1030. $ret = parent::load($options, $param1, $param2, $param3, $param4);
  1031. }
  1032. return $ret;
  1033. }
  1034. /**
  1035. * Loads the options from an associative array
  1036. *
  1037. * This method overloaded parent method of select element, to allow to load
  1038. * array of options with fancy attributes.
  1039. *
  1040. * @param array $arr Associative array of options
  1041. * @param mixed $values (optional) Array or comma delimited string of selected values
  1042. *
  1043. * @since version 1.5.0 (2009-02-15)
  1044. * @access public
  1045. * @return PEAR_Error on error and TRUE on success
  1046. * @throws PEAR_Error
  1047. * @see load()
  1048. */
  1049. function loadArray($arr, $values = null)
  1050. {
  1051. if (!is_array($arr)) {
  1052. return PEAR::throwError('Argument 1 of HTML_QuickForm_advmultiselect::' .
  1053. 'loadArray is not a valid array',
  1054. HTML_QUICKFORM_ADVMULTISELECT_ERROR_INVALID_INPUT,
  1055. array('level' => 'exception'));
  1056. }
  1057. if (isset($values)) {
  1058. $this->setSelected($values);
  1059. }
  1060. if (is_array($arr)) {
  1061. foreach ($arr as $key => $val) {
  1062. if (is_array($val)) {
  1063. $this->addOption($val[0], $key, $val[1]);
  1064. } else {
  1065. $this->addOption($val, $key);
  1066. }
  1067. }
  1068. }
  1069. return true;
  1070. }
  1071. /**
  1072. * Sets which items should be persistant
  1073. *
  1074. * Sets which items should have the disabled attribute
  1075. * to keep it persistant
  1076. *
  1077. * @param mixed $optionValues Options (key-values) that should be persistant
  1078. * @param bool $persistant (optional) TRUE if persistant, FALSE otherwise
  1079. *
  1080. * @since version 1.5.0 (2009-02-15)
  1081. * @access public
  1082. * @return PEAR_Error on error and TRUE on success
  1083. * @throws PEAR_Error
  1084. */
  1085. function setPersistantOptions($optionValues, $persistant = true)
  1086. {
  1087. if (!is_bool($persistant)) {
  1088. return PEAR::throwError('Argument 2 of HTML_QuickForm_advmultiselect::' .
  1089. 'setPersistantOptions is not a boolean',
  1090. HTML_QUICKFORM_ADVMULTISELECT_ERROR_INVALID_INPUT,
  1091. array('level' => 'exception'));
  1092. }
  1093. if (is_string($optionValues)) {
  1094. $optionValues = array($optionValues);
  1095. }
  1096. if (!is_array($optionValues)) {
  1097. return PEAR::throwError('Argument 1 of HTML_QuickForm_advmultiselect::' .
  1098. 'setPersistantOptions is not a valid array',
  1099. HTML_QUICKFORM_ADVMULTISELECT_ERROR_INVALID_INPUT,
  1100. array('level' => 'exception'));
  1101. }
  1102. foreach ($this->_options as $k => $v) {
  1103. if (in_array($v['attr']['value'], $optionValues)) {
  1104. if ($persistant) {
  1105. $this->_options[$k]['attr']['disabled'] = 'disabled';
  1106. } else {
  1107. unset($this->_options[$k]['attr']['disabled']);
  1108. }
  1109. }
  1110. }
  1111. return true;
  1112. }
  1113. /**
  1114. * Returns list of persistant options
  1115. *
  1116. * Returns list of persistant options (key-values) that could not
  1117. * be selected or unselected.
  1118. *
  1119. * @since version 1.5.0 (2009-02-15)
  1120. * @access public
  1121. * @return array
  1122. */
  1123. function getPersistantOptions()
  1124. {
  1125. $options = array();
  1126. foreach ($this->_options as $k => $v) {
  1127. if (isset($v['attr']['disabled'])) {
  1128. $options[] = $this->_options[$k]['attr']['value'];
  1129. }
  1130. }
  1131. return $options;
  1132. }
  1133. }