advmultiselect.php 46 KB

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