Action
2 years ago
Action.php
2 years ago
DefaultAction.php
2 years ago
Page.php
2 years ago
SessionContainer.php
2 years ago
Page.php
228 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Class representing a page of a multipage form |
| 6 | * |
| 7 | * PHP version 5 |
| 8 | * |
| 9 | * LICENSE: |
| 10 | * |
| 11 | * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>, |
| 12 | * Bertrand Mansion <golgote@mamasam.com> |
| 13 | * All rights reserved. |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * |
| 19 | * * Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * * Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * * The names of the authors may not be used to endorse or promote products |
| 25 | * derived from this software without specific prior written permission. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 28 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 29 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 30 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 31 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 32 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 33 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 34 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 35 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 36 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 37 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | * @category HTML |
| 40 | * @package HTML_QuickForm2 |
| 41 | * @author Alexey Borzov <avb@php.net> |
| 42 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 43 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 44 | * @version SVN: $Id: Page.php 295963 2010-03-08 14:33:43Z avb $ |
| 45 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 46 | */ |
| 47 | /** |
| 48 | * Class representing a page of a multipage form |
| 49 | * |
| 50 | * Unlike old HTML_QuickForm_Controller, this does not extend HTML_QuickForm2 |
| 51 | * but accepts an instance of that in the constructor. You need to create a |
| 52 | * subclass of this class and implement its populateForm() method. |
| 53 | * |
| 54 | * @category HTML |
| 55 | * @package HTML_QuickForm2 |
| 56 | * @author Alexey Borzov <avb@php.net> |
| 57 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 58 | * @version Release: @package_version@ |
| 59 | */ |
| 60 | abstract class HTML_QuickForm2_Controller_Page |
| 61 | { |
| 62 | /** |
| 63 | * Button name template (needs form ID and action name substituted by sprintf()) |
| 64 | */ |
| 65 | const KEY_NAME = '_qf_%s_%s'; |
| 66 | /** |
| 67 | * Whether populateForm() was already called |
| 68 | * @var boolean |
| 69 | */ |
| 70 | private $_formPopulated = \false; |
| 71 | /** |
| 72 | * The form wrapped by this page |
| 73 | * @var HTML_QuickForm2 |
| 74 | */ |
| 75 | protected $form = null; |
| 76 | /** |
| 77 | * Controller this page belongs to |
| 78 | * @var HTML_QuickForm2_Controller |
| 79 | */ |
| 80 | protected $controller = null; |
| 81 | /** |
| 82 | * Contains the mapping of action names to handlers (objects implementing HTML_QuickForm2_Controller_Action) |
| 83 | * @var array |
| 84 | */ |
| 85 | protected $handlers = array(); |
| 86 | /** |
| 87 | * Class constructor, accepts the form to wrap around |
| 88 | * |
| 89 | * @param HTML_QuickForm2 |
| 90 | */ |
| 91 | public function __construct(\HTML_QuickForm2 $form) |
| 92 | { |
| 93 | $this->form = $form; |
| 94 | } |
| 95 | /** |
| 96 | * Returns the form this page wraps around |
| 97 | * |
| 98 | * @return HTML_QuickForm2 |
| 99 | */ |
| 100 | public function getForm() |
| 101 | { |
| 102 | return $this->form; |
| 103 | } |
| 104 | /** |
| 105 | * Sets the controller owning the page |
| 106 | * |
| 107 | * @param HTML_QuickForm2_Controller controller the page belongs to |
| 108 | */ |
| 109 | public function setController(\HTML_QuickForm2_Controller $controller) |
| 110 | { |
| 111 | $this->controller = $controller; |
| 112 | } |
| 113 | /** |
| 114 | * Returns the controller owning this page |
| 115 | * |
| 116 | * @return HTML_QuickForm2_Controller |
| 117 | */ |
| 118 | public function getController() |
| 119 | { |
| 120 | return $this->controller; |
| 121 | } |
| 122 | /** |
| 123 | * Adds a handler for a specific action |
| 124 | * |
| 125 | * @param string action name |
| 126 | * @param HTML_QuickForm2_Controller_Action the handler for the action |
| 127 | */ |
| 128 | public function addHandler($actionName, \HTML_QuickForm2_Controller_Action $action) |
| 129 | { |
| 130 | $this->handlers[$actionName] = $action; |
| 131 | } |
| 132 | /** |
| 133 | * Handles an action |
| 134 | * |
| 135 | * If the page does not contain a handler for this action, controller's |
| 136 | * handle() method will be called. |
| 137 | * |
| 138 | * @param string Name of the action |
| 139 | * @throws HTML_QuickForm2_NotFoundException if handler for an action is missing |
| 140 | */ |
| 141 | public function handle($actionName) |
| 142 | { |
| 143 | if (isset($this->handlers[$actionName])) { |
| 144 | return $this->handlers[$actionName]->perform($this, $actionName); |
| 145 | } else { |
| 146 | return $this->getController()->handle($this, $actionName); |
| 147 | } |
| 148 | } |
| 149 | /** |
| 150 | * Returns a name for a submit button that will invoke a specific action |
| 151 | * |
| 152 | * @param string Name of the action |
| 153 | * @return string "name" attribute for a submit button |
| 154 | */ |
| 155 | public function getButtonName($actionName) |
| 156 | { |
| 157 | return \sprintf(self::KEY_NAME, $this->getForm()->getId(), $actionName); |
| 158 | } |
| 159 | /** |
| 160 | * Sets the default action invoked on page-form submit |
| 161 | * |
| 162 | * This is necessary as the user may just press Enter instead of |
| 163 | * clicking one of the named submit buttons and then no action name will |
| 164 | * be passed to the script. |
| 165 | * |
| 166 | * @param string Default action name |
| 167 | * @param string Path to a 1x1 transparent GIF image |
| 168 | * @return object Returns the image input used for default action |
| 169 | */ |
| 170 | public function setDefaultAction($actionName, $imageSrc = '') |
| 171 | { |
| 172 | // require_once 'HTML/QuickForm2/Controller/DefaultAction.php'; |
| 173 | if (0 == \count($this->form)) { |
| 174 | $image = $this->form->appendChild(new \HTML_QuickForm2_Controller_DefaultAction($this->getButtonName($actionName), array('src' => $imageSrc))); |
| 175 | // replace the existing DefaultAction |
| 176 | } elseif ($image = $this->form->getElementById('_qf_default')) { |
| 177 | $image->setName($this->getButtonName($actionName))->setAttribute('src', $imageSrc); |
| 178 | // Inject the element to the first position to improve chances that |
| 179 | // it ends up on top in the output |
| 180 | } else { |
| 181 | $it = $this->form->getIterator(); |
| 182 | $it->rewind(); |
| 183 | $image = $this->form->insertBefore(new \HTML_QuickForm2_Controller_DefaultAction($this->getButtonName($actionName), array('src' => $imageSrc)), $it->current()); |
| 184 | } |
| 185 | return $image; |
| 186 | } |
| 187 | /** |
| 188 | * Wrapper around populateForm() ensuring that it is only called once |
| 189 | */ |
| 190 | public final function populateFormOnce() |
| 191 | { |
| 192 | if (!$this->_formPopulated) { |
| 193 | if (!empty($this->controller) && $this->controller->propagateId()) { |
| 194 | $this->form->addElement('hidden', \HTML_QuickForm2_Controller::KEY_ID, array('id' => \HTML_QuickForm2_Controller::KEY_ID))->setValue($this->controller->getId()); |
| 195 | } |
| 196 | $this->populateForm(); |
| 197 | $this->_formPopulated = \true; |
| 198 | } |
| 199 | } |
| 200 | /** |
| 201 | * Populates the form with the elements |
| 202 | * |
| 203 | * The implementation of this method in your subclass of |
| 204 | * HTML_QuickForm2_Controller_Page should contain all the necessary |
| 205 | * addElement(), addRule() etc. calls. The method will only be called if |
| 206 | * needed to prevent wasting resources on the forms that aren't going to |
| 207 | * be seen by the user. |
| 208 | */ |
| 209 | protected abstract function populateForm(); |
| 210 | /** |
| 211 | * Stores the form values (and validation status) is session container |
| 212 | * |
| 213 | * @param bool Whether to store validation status |
| 214 | */ |
| 215 | public function storeValues($validate = \true) |
| 216 | { |
| 217 | $this->populateFormOnce(); |
| 218 | $container = $this->getController()->getSessionContainer(); |
| 219 | $id = $this->form->getId(); |
| 220 | $container->storeValues($id, (array) $this->form->getValue()); |
| 221 | if ($validate) { |
| 222 | $container->storeValidationStatus($id, $this->form->validate()); |
| 223 | } |
| 224 | return $container->getValidationStatus($id); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 |