home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / upload.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  7.3 KB  |  225 lines

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