HTML_QuickForm_advmultiselect
[ class tree: HTML_QuickForm_advmultiselect ] [ index: HTML_QuickForm_advmultiselect ] [ all elements ]

Custom example 4: source code

  1. <?php
  2. /**
  3.  * Custom advMultiSelect HTML_QuickForm element
  4.  * that present alternatively a dual multsi-select
  5.  * or a single checkboxes with fancy attributes.
  6.  *
  7.  * @version    $Id: qfams_custom_4.php,v 1.6 2008/04/26 12:45:44 farell Exp $
  8.  * @author     Laurent Laville <pear@laurent-laville.org>
  9.  * @package    HTML_QuickForm_advmultiselect
  10.  * @subpackage Examples
  11.  * @access     public
  12.  * @example    examples/qfams_custom_4.php
  13.  *              qfams_custom_4 source code
  14.  * @link       http://www.laurent-laville.org/img/qfams/screenshot/custom4.png
  15.  *              screenshot (Image PNG, 419x317 pixels) 6.02 Kb
  16.  */
  17.  
  18. require_once 'HTML/QuickForm.php';
  19. require_once 'HTML/QuickForm/advmultiselect.php';
  20.  
  21. $form = new HTML_QuickForm('amsCustom4');
  22. $form->removeAttribute('name');        // XHTML compliance
  23.  
  24. $fruit_styles = array(
  25.     'class' => 'icons',
  26.     'onmouseover' => "this.className='over'",
  27.     'onmouseout'  => "this.className='icons'"
  28. );
  29. $pear  array_merge($fruit_stylesarray('disabled' => 'disabled'));
  30. $lemon array_merge($fruit_styles,
  31.                      array('class' => 'goldstar',
  32.                            'onmouseout'  => "this.className='goldstar'"
  33. ));
  34. $tangerine array_merge($fruit_styles,
  35.                          array('class' => 'bluestar',
  36.                                'onmouseout'  => "this.className='bluestar'",
  37.                                'style' => 'color:red;')
  38. );
  39.  
  40. $fruit_array = array(
  41.     'apple'     =>  'Apple',
  42.     'orange'    =>  'Orange',
  43.     'pear'      =>  array('Pear'$pear),
  44.     'banana'    =>  'Banana',
  45.     'cherry'    =>  'Cherry',
  46.     'kiwi'      =>  'Kiwi',
  47.     'lemon'     =>  array('Lemon'$lemon),
  48.     'lime'      =>  'Lime',
  49.     'tangerine' =>  array('Tangerine'$tangerine),
  50. );
  51.  
  52. // rendering with QF renderer engine and template system
  53. $form->addElement('header'null'Advanced Multiple Select: custom layout ');
  54.  
  55. $form->addElement('text''name''Name:'array('size' => 40'maxlength' => 80));
  56.  
  57. $ams =$form->addElement('advmultiselect''fruit'nullnull,
  58.                            array('class' => 'pool')
  59. );
  60. foreach ($fruit_array as $key => $data{
  61.     if (!is_array($data)) {
  62.         $data = array($data$fruit_styles);
  63.     }
  64.     $attr = isset($data[1]$data[1: null;
  65.     $ams->addOption($data[0]$key$attr);
  66. }
  67.  
  68. $ams->setLabel(array('Fruit:''Available''Selected'));
  69.  
  70. // template for a dual multi-select element shape
  71. $template2 '
  72. <table{class}>
  73. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  74. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  75. <tr>
  76.   <td>{unselected}</td>
  77.   <td align="center">{add}{remove}</td>
  78.   <td>{selected}</td>
  79. </tr>
  80. </table>
  81. ';
  82.  
  83. // template for a single checkboxes multi-select element shape
  84. $template1 '
  85. <table{class}>
  86. <!-- BEGIN label_3 --><tr><th>{label_3}</th></tr><!-- END label_3 -->
  87. <tr>
  88.   <td>{selected}</td>
  89. </tr>
  90. </table>
  91. ';
  92.  
  93. if (isset($_POST['multiselect'])) {
  94.     $ams->setElementTemplate($template2);
  95. else {
  96.     $ams->setElementTemplate($template1);
  97. }
  98.  
  99. if ($_SERVER['REQUEST_METHOD'== 'GET'{
  100.     // fruit default values already selected without any end-user actions
  101.     $form->setDefaults(array('fruit' => array('kiwi','lime')));
  102.  
  103. elseif (isset($_POST['fruit'])) {
  104.     // fruit end-user selection
  105.     $form->setDefaults(array('fruit' => $_POST['fruit']));
  106. }
  107.  
  108. $buttons[=$form->createElement('submit'null'Submit');
  109. $buttons[=$form->createElement('reset'null'Reset');
  110. $buttons[=$form->createElement('checkbox''multiselect'null,
  111.                                    'use dual select boxes layout');
  112. $form->addGroup($buttonsnull'&nbsp;');
  113.  
  114. $form->addRule('name''Your name is required''required');
  115. $form->addGroupRule('fruit''At least one fruit is required''required'null1);
  116.  
  117. $form->applyFilter('__ALL__''trim');
  118. $form->applyFilter('__ALL__''strip_tags');
  119. ?>
  120. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  121.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  122. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  123. <head>
  124. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  125. <title>HTML_QuickForm::advMultiSelect custom example 4</title>
  126. <style type="text/css">
  127. <!--
  128. body {
  129.   background-color: #FFF;
  130.   font-family: Verdana, Arial, helvetica;
  131.   font-size: 10pt;
  132. }
  133.  
  134. table.pool {
  135.   border: 0;
  136.   background-color: lightyellow;
  137. }
  138. table.pool th {
  139.   font-size: 80%;
  140.   font-style: italic;
  141.   text-align: center;
  142. }
  143.  
  144. <?php
  145. if (!isset($_POST['multiselect'])) {
  146.     echo $ams->getElementCss();
  147.  
  148.     echo '
  149. label input {
  150.   margin-right: 16px;
  151. }
  152. label.bluestar, label.goldstar {
  153.   background-repeat: no-repeat;
  154.   background-position: 20px center;
  155. }
  156. label.bluestar{
  157.   background-image: url(bluestar-12.gif) ;
  158. }
  159. label.goldstar {
  160.   background-image: url(goldstar-12.gif) ;
  161. }
  162.  
  163. label.over {
  164.   background-color: #0a246a;
  165.   color: #fff;
  166. }
  167. ';
  168. }
  169. ?>
  170.  
  171.  -->
  172. </style>
  173. <?php
  174. if (isset($_POST['multiselect'])) {
  175.     echo $ams->getElementJs(false);
  176. }
  177. ?>
  178. </head>
  179. <body>
  180. <?php
  181. if ($form->validate()) {
  182.     $clean $form->getSubmitValues();
  183.  
  184.     echo '<pre>';
  185.     print_r($clean);
  186.     echo '</pre>';
  187.  
  188.     printf("<p>Welcome <b>%s</b> you've selected these fruits:</p>",
  189.            $clean['name']);
  190.     echo '<p>' implode(', '$clean['fruit']'</p>';
  191. }
  192. $form->display();
  193. ?>
  194. </body>
  195. </html>

Documentation generated on Sat, 26 Apr 2008 14:30:07 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.