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