root/trunk/lib/SGL/WizardController.php

Revision 2381, 8.5 kB (checked in by demian, 10 months ago)

comment correction

  • Property svn:eol-style set to native
Line 
1 <?php
2 /* Reminder: always indent with 4 spaces (no tabs). */
3 // +---------------------------------------------------------------------------+
4 // | Copyright (c) 2006, Demian Turner                                         |
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 // | o Redistributions of source code must retain the above copyright          |
12 // |   notice, this list of conditions and the following disclaimer.           |
13 // | o 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 // | o The names of the authors may not be used to endorse or promote          |
17 // |   products derived from this software without specific prior written      |
18 // |   permission.                                                             |
19 // |                                                                           |
20 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       |
21 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         |
22 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     |
23 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      |
24 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     |
25 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          |
26 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     |
27 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
28 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
29 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE     |
30 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.      |
31 // |                                                                           |
32 // +---------------------------------------------------------------------------+
33 // | Seagull 0.6                                                               |
34 // +---------------------------------------------------------------------------+
35 // | ClientWizard.php                                                          |
36 // +---------------------------------------------------------------------------+
37 // | Author: Malaney J. Hill <malaney@gmail.com>                               |
38 // +---------------------------------------------------------------------------+
39 // $Id: ClientWizard.php,v 1.5 2006/07/31 15:57:01 mhill Exp $
40
41 require_once 'HTML/QuickForm/Action.php';
42 require_once 'HTML/QuickForm/Action/Display.php';
43 require_once 'HTML/QuickForm/Action/Next.php';
44 require_once 'HTML/QuickForm/Action/Back.php';
45 require_once 'HTML/QuickForm/Action/Jump.php';
46 require_once 'HTML/QuickForm/Controller.php';
47 require_once 'HTML/QuickForm/Renderer/Default.php';
48
49 /**
50  * Inherit from HTML_QuickForm_Controller to build
51  * multi-page forms (wizards).
52  *
53  * @package SGL
54  * @author  Malaney J. Hill <malaney@gmail.com>
55  * @version $$
56  */
57 class SGL_WizardController extends HTML_QuickForm_Controller
58 {
59    /**
60     * Extracts the names of the current page and the current action from
61     * HTTP request data.
62     *
63     * @access public
64     * @return array     first element is page name, second is action name
65     */
66     function getActionName()
67     {
68         if (is_array($this->_actionName)) {
69             return $this->_actionName;
70         }
71         $names = array_map('preg_quote', array_keys($this->_pages));
72         $regex = '/^_qf_(' . implode('|', $names) . ')_(.+?)(_x)?$/';
73
74     $count = 0;
75     // This section of the code could most likely be improved ...
76     // Basically we are collecting the action vars from $_REQUEST
77     // and if it is a "back" or "next" action we process immediately,
78     // if it is a "display" action, we delay processing to see if there
79     // is still a "back" or "next" action.  In other words, we give priority
80     // to "next/back" over "display" actions.
81     $possibles = array();
82         foreach (array_keys($_REQUEST) as $key) {
83             if (preg_match($regex, $key, $matches)) {
84             $possibles[$key] =  $matches;
85             $count++;
86             }
87         }
88     if (count($possibles))
89     {
90         foreach ($possibles as $p => $arr)
91         {
92             if ( (preg_match("/next/", $p)) || (preg_match("/back/", $p)) )
93             {
94                 $matches = $arr;
95                 break;
96             }
97             else if (preg_match("/display/", $p))
98             {
99                 $matches = $arr;
100             }
101         }
102                 return array($matches[1], $matches[2]);
103     }
104         if (isset($_REQUEST['_qf_default'])) {
105             $matches = explode(':', $_REQUEST['_qf_default'], 2);
106             if (isset($this->_pages[$matches[0]])) {
107                 return $matches;
108             }
109         }
110         reset($this->_pages);
111         return array(key($this->_pages), 'display');
112     }
113 }
114
115 /**
116  * The action handles the HTTP redirect to a specific page.
117  *
118  * @package SGL
119  * @version $Revision: 1.4 $
120  */
121 class SGL_WizardControllerJump extends HTML_QuickForm_Action_Jump
122 {
123    /**
124     * Extracts the names of the current page and the current action from
125     * HTTP request data, strips off all query string vars.
126     *
127     * @access public
128     * @return array     first element is page name, second is action name
129     */
130     function perform(&$page, $actionName)
131     {
132         // check whether the page is valid before trying to go to it
133         if ($page->controller->isModal()) {
134             // we check whether *all* pages up to current are valid
135             // if there is an invalid page we go to it, instead of the
136             // requested one
137             $pageName = $page->getAttribute('id');
138             if (!$page->controller->isValid($pageName)) {
139                 $pageName = $page->controller->findInvalid();
140             }
141             $current =& $page->controller->getPage($pageName);
142
143         } else {
144             $current =& $page;
145         }
146         // generate the URL for the page 'display' event and redirect to it
147         $action = $current->getAttribute('action');
148
149     // simply remove all query string vars ...
150     // NOTE:  this may only work with Seagull SEF Url Handler, may need to
151     // be expanded to deal with other URL Handlers
152     $action = preg_replace("/\?.*/", "", $action);
153
154         $url    = $action . (false === strpos($action, '?')? '?': '&') .
155                   $current->getButtonName('display') . '=true' .
156                   ((!defined('SID') || '' == SID || ini_get('session.use_only_cookies'))? '': '&' . SID);
157         header('Location: ' . $url);
158         exit;
159     }
160 }
161
162 /**
163  * Class representing an action to perform on HTTP request. The Controller
164  * will select the appropriate Action to call on the request and call its
165  * perform() method. The subclasses of this class should implement all the
166  * necessary business logic.
167  *
168  * @package SGL
169  * @version $Revision: 1.1 $
170  */
171 class SGL_WizardControllerProcess extends HTML_QuickForm_Action
172 {
173    /**
174     * Processes the request, assigning controller output to var for
175     * rendering.
176     *
177     * @access public
178     * @param  object HTML_QuickForm_Page    the current form-page
179     * @param  string    Current action name, as one Action object can serve multiple actions
180     */
181     function perform(&$page, $actionName)
182     {
183         // Assign form data for rendering purposes
184         $page->wizardData = $page->controller->exportValues();
185         // reset controller
186         $page->controller->container(true);
187     }
188 }
189
190 /**
191  * This action handles the output of the form.
192  *
193  * @author  Alexey Borzov <avb@php.net>
194  * @package SGL
195  * @version $Revision: 1.5 $
196  */
197 class SGL_WizardControllerDisplay extends HTML_Quickform_Action_Display
198 {
199    /**
200     * Actually outputs the form.
201     *
202     * This assigns the wizard output to a variable for rendering
203     *
204     * @access public
205     * @param  object HTML_QuickForm_Page  the page being processed
206     */
207     function _renderForm(&$page)
208     {
209         $renderer = & new HTML_QuickForm_Renderer_Default();
210         $page->accept($renderer);
211         $page->wizardOutput = $renderer->toHtml();
212     }
213 }
214 ?>
215
Note: See TracBrowser for help on using the browser.