QuickForm2.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Class representing a HTML 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: QuickForm2.php 299706 2010-05-24 18:32:37Z avb $ |
| 45 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 46 | */ |
| 47 | /** |
| 48 | * Abstract base class for QuickForm2 containers |
| 49 | */ |
| 50 | // require_once 'HTML/QuickForm2/Container.php'; |
| 51 | /** |
| 52 | * Data source for HTML_QuickForm2 objects based on superglobal arrays |
| 53 | */ |
| 54 | // require_once 'HTML/QuickForm2/DataSource/SuperGlobal.php'; |
| 55 | /** |
| 56 | * Class representing a HTML form |
| 57 | * |
| 58 | * @category HTML |
| 59 | * @package HTML_QuickForm2 |
| 60 | * @author Alexey Borzov <avb@php.net> |
| 61 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 62 | * @version Release: @package_version@ |
| 63 | */ |
| 64 | class HTML_QuickForm2 extends \HTML_QuickForm2_Container |
| 65 | { |
| 66 | /** |
| 67 | * Data sources providing values for form elements |
| 68 | * @var array |
| 69 | */ |
| 70 | protected $datasources = array(); |
| 71 | /** |
| 72 | * We do not allow setting "method" and "id" other than through constructor |
| 73 | * @var array |
| 74 | */ |
| 75 | protected $watchedAttributes = array('id', 'method'); |
| 76 | /** |
| 77 | * Class constructor, form's "id" and "method" attributes can only be set here |
| 78 | * |
| 79 | * @param string "id" attribute of <form> tag |
| 80 | * @param string HTTP method used to submit the form |
| 81 | * @param mixed Additional attributes (either a string or an array) |
| 82 | * @param bool Whether to track if the form was submitted by adding |
| 83 | * a special hidden field |
| 84 | */ |
| 85 | public function __construct($id, $method = 'post', $attributes = null, $trackSubmit = \true) |
| 86 | { |
| 87 | $method = 'GET' == \strtoupper($method) ? 'get' : 'post'; |
| 88 | if (empty($id)) { |
| 89 | $id = self::generateId(''); |
| 90 | $trackSubmit = \false; |
| 91 | } else { |
| 92 | self::storeId($id); |
| 93 | } |
| 94 | $this->attributes = \array_merge(self::prepareAttributes($attributes), array('id' => (string) $id, 'method' => $method)); |
| 95 | if (!isset($this->attributes['action'])) { |
| 96 | $this->attributes['action'] = $_SERVER['PHP_SELF']; |
| 97 | } |
| 98 | if ($trackSubmit && isset($_REQUEST['_qf__' . $id]) || !$trackSubmit && ('get' == $method && !empty($_GET) || 'post' == $method && (!empty($_POST) || !empty($_FILES)))) { |
| 99 | $this->addDataSource(new \HTML_QuickForm2_DataSource_SuperGlobal($method, \False)); |
| 100 | } |
| 101 | if ($trackSubmit) { |
| 102 | $this->appendChild(\HTML_QuickForm2_Factory::createElement('hidden', '_qf__' . $id)); |
| 103 | } |
| 104 | } |
| 105 | protected function onAttributeChange($name, $value = null) |
| 106 | { |
| 107 | throw new \HTML_QuickForm2_InvalidArgumentException('Attribute \'' . $name . '\' is read-only'); |
| 108 | } |
| 109 | protected function setContainer(?\HTML_QuickForm2_Container $container = null) |
| 110 | { |
| 111 | throw new \HTML_QuickForm2_Exception('Form cannot be added to container'); |
| 112 | } |
| 113 | public function setId($id = null) |
| 114 | { |
| 115 | throw new \HTML_QuickForm2_InvalidArgumentException("Attribute 'id' is read-only"); |
| 116 | } |
| 117 | /** |
| 118 | * Adds a new data source to the form |
| 119 | * |
| 120 | * @param HTML_QuickForm2_DataSource Data source |
| 121 | */ |
| 122 | public function addDataSource(\HTML_QuickForm2_DataSource $datasource) |
| 123 | { |
| 124 | $this->datasources[] = $datasource; |
| 125 | $this->updateValue(); |
| 126 | } |
| 127 | /** |
| 128 | * Replaces the list of form's data sources with a completely new one |
| 129 | * |
| 130 | * @param array A new data source list |
| 131 | * @throws HTML_QuickForm2_InvalidArgumentException if given array |
| 132 | * contains something that is not a valid data source |
| 133 | */ |
| 134 | public function setDataSources(array $datasources) |
| 135 | { |
| 136 | foreach ($datasources as $ds) { |
| 137 | if (!$ds instanceof \HTML_QuickForm2_DataSource) { |
| 138 | throw new \HTML_QuickForm2_InvalidArgumentException('Array should contain only DataSource instances'); |
| 139 | } |
| 140 | } |
| 141 | $this->datasources = $datasources; |
| 142 | $this->updateValue(); |
| 143 | } |
| 144 | /** |
| 145 | * Returns the list of data sources attached to the form |
| 146 | * |
| 147 | * @return array |
| 148 | */ |
| 149 | public function getDataSources() |
| 150 | { |
| 151 | return $this->datasources; |
| 152 | } |
| 153 | public function getType() |
| 154 | { |
| 155 | return 'form'; |
| 156 | } |
| 157 | public function setValue($value) |
| 158 | { |
| 159 | throw new \HTML_QuickForm2_Exception('Not implemented'); |
| 160 | } |
| 161 | /** |
| 162 | * Performs the server-side validation |
| 163 | * |
| 164 | * @return boolean Whether all form's elements are valid |
| 165 | */ |
| 166 | public function validate() |
| 167 | { |
| 168 | $isSubmitted = \false; |
| 169 | foreach ($this->datasources as $ds) { |
| 170 | if ($ds instanceof \HTML_QuickForm2_DataSource_Submit) { |
| 171 | $isSubmitted = \true; |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | return $isSubmitted ? parent::validate() : \false; |
| 176 | } |
| 177 | /** |
| 178 | * Renders the form using the given renderer |
| 179 | * |
| 180 | * @param HTML_QuickForm2_Renderer Renderer instance |
| 181 | * @return HTML_QuickForm2_Renderer |
| 182 | */ |
| 183 | public function render(\HTML_QuickForm2_Renderer $renderer) |
| 184 | { |
| 185 | $renderer->startForm($this); |
| 186 | $renderer->getJavascriptBuilder()->startForm($this); |
| 187 | foreach ($this as $element) { |
| 188 | $element->render($renderer); |
| 189 | } |
| 190 | $renderer->finishForm($this); |
| 191 | return $renderer; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 |