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

Source for file upload.php

Documentation is available at upload.php

  1. <?php
  2. /**
  3. * Example for HTML_QuickForm_Controller
  4. * Handling file uploads and dynamic form generation
  5. *
  6. @version SVN: $Id: upload.php 289084 2009-10-02 06:53:09Z avb $
  7. @author  Bertrand Mansion <bmansion@mamasam.com>
  8. @ignore
  9. */
  10.  
  11. //
  12. // For this example to work, you'll need to create an 'uploads' directory
  13. // in the directory where this script is located and give write permissions
  14. // on it to your webserver.
  15. //
  16.  
  17. require_once 'HTML/QuickForm/Controller.php';
  18. require_once 'HTML/QuickForm/Action/Display.php';
  19.  
  20. // Start the session
  21.  
  22.  
  23. // Class for first page
  24. // Will propose 2 layouts.
  25. // The selection will change the 2nd page.
  26.  
  27. {
  28.     function buildForm()
  29.     {
  30.         $this->_formBuilt = true;
  31.  
  32.         $image '<div class="image">O</div>';
  33.         $text '<div class="text">In the next page, you will write your text in this box. The other box besides will contain the picture that will have to be uploaded.</div>';
  34.  
  35.         $radios[0=$this->createElement('radio'null$image.$text'&nbsp;''A');
  36.         $radios[1=$this->createElement('radio'null$text.$image'&nbsp;''B');
  37.         $this->addGroup($radios'layout''Choose a layout');
  38.  
  39.         $buttons[0=$this->createElement('submit'$this->getButtonName('next')'Next step >>');
  40.         $this->addGroup($buttons'buttons''''&nbsp'false);
  41.  
  42.         $this->setDefaultAction('next');
  43.     }
  44. }
  45.  
  46. // Class for second page
  47. // Layout will reflect choices made in the first page.
  48.  
  49. {
  50.     function buildForm()
  51.     {
  52.         $this->_formBuilt = true;
  53.  
  54.         $this->registerRule('isImage',  'callback''_ruleIsImage'get_class($this));
  55.  
  56.         $text  =$this->createElement('textarea''content''Type text here...'array('cols'=>30'rows'=>4));
  57.         $upped =$this->createElement('file''file''Upload your image here...');
  58.  
  59.         if ($this->controller->exportValue('page1''layout'== 'A'{
  60.             $this->addGroup(array($upped$text)'contents''Enter the contents'nullfalse);
  61.         else {
  62.             $this->addGroup(array($text$upped)'contents''Enter the contents'nullfalse);
  63.         }
  64.  
  65.         $prevnext[=$this->createElement('submit',   $this->getButtonName('back')'<< Previous step');
  66.         $prevnext[=$this->createElement('submit',   $this->getButtonName('upload')'Preview >>');
  67.         $this->addGroup($prevnext'buttons''''&nbsp;'false);
  68.  
  69.         $rules['file'][0= array('Must be *.jpg, *.gif or *.png''filename''/\.(jpe?g|gif|png)$/i');
  70.         $rules['file'][1= array('The file must be an image''isImage');
  71.  
  72.         $this->addGroupRule('contents'$rules);
  73.  
  74.         $this->setDefaultAction('upload');
  75.     }
  76.  
  77.     function _ruleIsImage($data)
  78.     {
  79.         if (
  80.               ((isset($data['error']&& 0 == $data['error']||
  81.                (!empty($data['tmp_name']&& 'none' != $data['tmp_name'])) &&
  82.               is_uploaded_file($data['tmp_name'])
  83.            {
  84.             $info @getimagesize($data['tmp_name']);
  85.             return is_array($info&& (1 == $info[2|| 2 == $info[2|| 3 == $info[2]);
  86.         else {
  87.             return true;
  88.         }
  89.     }
  90. }
  91.  
  92. // Class for third page
  93. // Will show the preview for the text and the image
  94.  
  95. {
  96.     function buildForm()
  97.     {
  98.         $this->_formBuilt = true;
  99.  
  100.         $data =$this->controller->container();
  101.         if (!empty($data['_upload'])) {
  102.             $image '<img src="uploads/'.$data['_upload'].'" alt="uploaded image" />';
  103.         else {
  104.             $image '';
  105.         }
  106.         $text   =  wordwrap($this->controller->exportValue('page2''content')50'<br />');
  107.         $theTxt =HTML_QuickForm::createElement('static''thetext''Your text...'$text);
  108.         $theImg =HTML_QuickForm::createElement('static''thefile''Your file...'$image);
  109.  
  110.         if ($this->controller->exportValue('page1''layout'== 'A'{
  111.             $this->addGroup(array($theImg$theTxt)'contents''Your contents'nullfalse);
  112.         else {
  113.             $this->addGroup(array($theTxt$theImg)'contents''Your contents'nullfalse);
  114.         }
  115.  
  116.         $prevnext[=$this->createElement('submit',   $this->getButtonName('back')'<< Previous step');
  117.         $prevnext[=$this->createElement('submit',   $this->getButtonName('next')'Finish');
  118.         $this->addGroup($prevnext'buttons''''&nbsp;'false);
  119.  
  120.         $this->setDefaultAction('next');
  121.     }
  122. }
  123.  
  124. // Special action for dealing with uploads
  125.  
  126. {
  127.     function perform(&$page$actionName)
  128.     {
  129.         // like in Action_Next
  130.         $page->isFormBuilt(or $page->buildForm();
  131.  
  132.         $pageName =  $page->getAttribute('id');
  133.         $data     =$page->controller->container();
  134.         $data['values'][$pageName$page->exportValues();
  135.         if (PEAR::isError($valid $page->validate())) {
  136.             return $valid;
  137.         }
  138.         $data['valid'][$pageName$valid;
  139.  
  140.         if (!$data['valid'][$pageName]{
  141.             return $page->handle('display');
  142.         }
  143.  
  144.         // get the element containing the upload
  145.         $group    =$page->getElement('contents');
  146.         $elements =$group->getElements();
  147.         foreach (array_keys($elementsas $key{
  148.             if ('file' == $elements[$key]->getType()) {
  149.                 break;
  150.             }
  151.         }
  152.  
  153.         // move the file and store the data
  154.         if ($elements[$key]->isUploadedFile()) {
  155.             $elements[$key]->moveUploadedFile('./uploads/');
  156.             $value $elements[$key]->getValue();
  157.             if (!empty($data['_upload'])) {
  158.                 @unlink('./uploads/' $data['_upload']);
  159.             }
  160.             $data['_upload'basename($value['name']);
  161.         }
  162.  
  163.         // redirect to next page
  164.         $next =$page->controller->getPage($page->controller->getNextName($pageName));
  165.         $next->handle('jump');
  166.     }
  167. }
  168.  
  169. // Class for form rendering
  170.  
  171. {
  172.     function _renderForm(&$page)
  173.     {
  174.         require_once 'HTML/Template/Sigma.php';
  175.         require_once 'HTML/QuickForm/Renderer/ITDynamic.php';
  176.  
  177.         $tpl =new HTML_Template_Sigma('./templates');
  178.         $tpl->loadTemplateFile('upload.html');
  179.  
  180.         $renderer =new HTML_QuickForm_Renderer_ITDynamic($tpl);
  181.         $renderer->setElementBlock(array(
  182.            'layout'     => 'qf_layout',
  183.            'buttons'    => 'qf_buttons',
  184.            'contents'   => 'qf_group_table'
  185.         ));
  186.  
  187.         $page->accept($renderer);
  188.         $tpl->show();
  189.     }
  190. }
  191.  
  192. // Class for form processing
  193.  
  194. {
  195.     function perform(&$page$actionName)
  196.     {
  197.         $values $page->controller->exportValues();
  198.         echo '<pre>';
  199.         var_dump($values);
  200.         echo '</pre>';
  201.         $data =$page->controller->container();
  202.         echo '<pre>';
  203.         var_dump($data['_upload']);
  204.         echo '</pre>';
  205.     }
  206. }
  207.  
  208. $wizard =new HTML_QuickForm_Controller('uploadWizard'true);
  209. $wizard->addPage(new Page_CMS_Layout('page1'));
  210. $wizard->addPage(new Page_CMS_Fill('page2'));
  211. $wizard->addPage(new Page_CMS_Preview('page3'));
  212.  
  213. $wizard->setDefaults(array('layout' => 'A'));
  214.  
  215. $wizard->addAction('upload',  new ActionUpload());
  216. $wizard->addAction('display'new ActionDisplay());
  217. $wizard->addAction('process'new ActionProcess());
  218.  
  219. $wizard->run();
  220. ?>

Documentation generated on Mon, 11 Mar 2019 15:34:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.