1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class HtmlFormBasic
* @brief Create html form elements
*
* This class creates html form elements.
* Create an instance of an form element and set the input elements inline .
* The class supports setting all form elements and allows you to configure all attributes programatically.
* The parsed form object is returned as string.
*
* @par Example of an array with further attributes
* @code
* $attrArray = array('class' => 'Classname');
* @endcode
* @par Example: Creating a form element
* @code
* // Get the Instance for a new form element and set an action attribute
* $form = new HtmlFormBasic('test.php');
* // XHTML determines that the input elements are inline elements of a block element
* // so we need somthing like a div Block. In this example we use a fieldset
* $form->addFieldSet();
* // we can define a label for the input element with reference ID
* $form->addLabel('Field_1', 'ID_1');
* // set an input element like a text field. All valid types are supported
* // you can define further attributes as associative array and set as parameter in correct position
* $form->addSimpleInput('text', 'Input_1', 'ID_1', 'Value_1', $attrArray);
* // add a linebreak
* $form->linebreak();
* // next label
* $form->addLabel('Radio_1', 'ID_2');
* // next element is a radio button
* $form->addSimpleInput('radio', 'Radio_1', 'ID_2', 'Value_Radio');
* // add a linebreak
* $form->linebreak();
* // Define a select box
* $form->addSelect('Select_Name', 'ID_3', $attrArray);
* // now we can also specify an optiongroup
* $form->addOptionGroup('Group_1', 'ID_4', $attrArray);
* // define options
* $form->addOption('Option_Value_1', 'Option_Label_1');
* $form->addOption('Option_Value_2', 'Option_Label_2');
* $form->addOption('Option_Value_3', 'Option_Label_3');
* // end of option group
* $form->closeOptionGroup();
* // end of select box
* $form->closeSelect();
* // add a linebreak
* $form->linebreak();
* // example of a text area
* $form->addTextArea('Textarea', '4', '4', 'Input please ...', 'ID_5', $attrArray);
* // close open fieldset block
* $form->closeFieldSet();
* // print the form
* echo $form->getHtmlForm();
* @endcode
*/
class HtmlFormBasic extends HtmlElement
{
/**
* Constructor creates the element
*
* @param string $action Optional action attribute of the form
* @param string $id Id of the form
* @param string $method Get/Post (Default "get" if not defined)
* @param string $event Optional event handler
* @param string $script Optional script or function called from event handler
*/
public function __construct($action = null, $id = null, $method = 'get', $event = null, $script = null)
{
parent::__construct('form', true);
// set action attribute
if ($action !== null)
{
$this->addAttribute('action', $action);
}
if ($id !== null)
{
$this->addAttribute('id', $id);
}
if ($method !== null)
{
$this->addAttribute('method', $method);
}
if ($event !== null && $script !== null)
{
$this->addAttribute($event, $script);
}
}
/**
* Add a fieldset.
* @param string $legend Description for optional legend element as string
* @param string $id Optional ID
*/
public function addFieldSet($legend = null, $id = null)
{
if ($id !== null)
{
$this->addParentElement('fieldset');
}
else
{
$this->addParentElement('fieldset', 'id', $id);
}
if ($legend !== null)
{
$this->addLegend($legend);
}
}
/**
* Add a input field with attribute properties.
* @param string $type Type of input field e.g. 'text'
* @param string $name Name of the input field
* @param string $id Optional ID for the input
* @param string $value Value of the field (Default: empty)
* @param string[] $arrAttributes Further attributes as array with key/value pairs
*/
public function addSimpleInput($type, $name, $id = null, $value = '', array $arrAttributes = null)
{
$this->addElement('input', '', '', '', true);
// set all attributes
$this->addAttribute('type', $type);
$this->addAttribute('name', $name);
if ($id !== null)
{
$this->addAttribute('id', $id);
}
$this->addAttribute('value', $value);
// Check optional attributes in associative array and set all attributes
if ($arrAttributes !== null)
{
$this->setAttributesFromArray($arrAttributes);
}
$this->addData(' ', true);
}
/**
* Add a label to the input field.
* @param string $string Value of the label as string
* @param string $refId
* @param string $attribute
*/
public function addLabel($string = '', $refId = null, $attribute = 'for')
{
$this->addElement('label');
if ($refId !== null)
{
$this->addAttribute($attribute, $refId);
}
$this->addData($string);
}
/**
* Add a legend element in current fieldset.
* @param string $legend Data for the element as string
*/
public function addLegend($legend)
{
$this->addElement('legend', '', '', $legend);
}
/**
* Add inline element into current division.
* @param string $value Option value
* @param string $label Label of the option
* @param string $id Optional Id of the option
* @param bool $selected Mark as selected (Default: false)
* @param bool $disable Disable option (optional)
*/
public function addOption($value, $label, $id = null, $selected = false, $disable = false)
{
$this->addElement('option');
// set attributes
$this->addAttribute('value', $value);
if ($id !== null)
{
$this->addAttribute('id', $id);
}
if ($selected)
{
$this->addAttribute('selected', 'selected');
}
if ($disable)
{
$this->addAttribute('disabled', 'disabled');
}
// add label
$this->addData($label);
}
/**
* Add an option group.
* @param string $label Label of the option group
* @param string $id Optional Id of the group
* @param string[] $arrAttributes Further attributes as array with key/value pairs
* @param bool $disable Disable option group (Default: false)
*/
public function addOptionGroup($label, $id = null, array $arrAttributes = null, $disable = false)
{
$this->addParentElement('optgroup');
// set attributes
$this->addAttribute('label', $label);
if ($id !== null)
{
$this->addAttribute('id', $id);
}
// Check optional attributes in associative array and set all attributes
if ($arrAttributes !== null)
{
$this->setAttributesFromArray($arrAttributes);
}
if ($disable)
{
$this->addAttribute('disabled', 'disabled');
}
}
/**
* Add an option group.
* @param string $name Name of the select
* @param string $id Optional Id of the select
* @param string[] $arrAttributes Further attributes as array with key/value pairs
* @param bool $disable Disable select (Default: false)
*/
public function addSelect($name, $id = null, array $arrAttributes = null, $disable = false)
{
$this->addParentElement('select', 'name', $name);
// set attributes
if ($id !== null)
{
$this->addAttribute('id', $id);
}
// Check optional attributes in associative array and set all attributes
if ($arrAttributes !== null)
{
$this->setAttributesFromArray($arrAttributes);
}
if ($disable)
{
$this->addAttribute('disabled', 'disabled');
}
}
/**
* Adds a button to the form.
* @param string $name Name of the button
* @param string $type Type attribute (Allowed: submit, reset, button (Default: button))
* @param string $value Value of the button
* @param string $id Optional ID for the button
* @param string $link If set a javascript click event with a page load to this link
* will be attached to the button.
*/
public function addSimpleButton($name, $type = 'button', $value, $id = null, $link = null)
{
$this->addElement('button');
if ($id !== null)
{
$this->addAttribute('id', $id);
}
// if link is set then add a onclick event
if ($link !== null)
{
$this->addAttribute('onclick', 'self.location.href=\'' . $link . '\'');
}
$this->addAttribute('name', $name);
$this->addAttribute('type', $type);
$this->addData($value);
}
/**
* Add a text area.
* @param string $name Name of the text area
* @param int $rows Number of rows
* @param int $cols Number of cols
* @param string $text Text as content
* @param string $id Optional Id
* @param string[] $arrAttributes Further attributes as array with key/value pairs
* @param bool $disable Disable text area (Default: false)
*/
public function addTextArea($name, $rows, $cols, $text = '', $id = null, array $arrAttributes = null, $disable = false)
{
$this->addElement('textarea');
// set attributes
$this->addAttribute('name', $name);
$this->addAttribute('rows', $rows);
$this->addAttribute('cols', $cols);
if ($id !== null)
{
$this->addAttribute('id', $id);
}
// Check optional attributes in associative array and set all attributes
if ($arrAttributes !== null)
{
$this->setAttributesFromArray($arrAttributes);
}
if ($disable)
{
$this->addAttribute('disabled', 'disabled');
}
$this->addData($text);
}
/**
* @par Close current fieldset.
*/
public function closeFieldSet()
{
$this->closeParentElement('fieldset');
}
/**
* @par Close current option group.
*/
public function closeOptionGroup()
{
$this->closeParentElement('optgroup');
}
/**
* @par Close current select.
*/
public function closeSelect()
{
$this->closeParentElement('select');
}
/**
* Get the full parsed html form
* @return string Returns the validated html form as string
*/
public function getHtmlForm()
{
return $this->getHtmlElement();
}
}