symphonycms /
symphony-2
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @package toolkit |
||||
| 4 | */ |
||||
| 5 | /** |
||||
| 6 | * Widget is a utility class that offers a number miscellaneous of |
||||
| 7 | * functions to help generate common HTML Forms elements as XMLElement |
||||
| 8 | * objects for inclusion in Symphony backend pages. |
||||
| 9 | */ |
||||
| 10 | class Widget |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * Generates a XMLElement representation of `<label>` |
||||
| 14 | * |
||||
| 15 | * @param string $name (optional) |
||||
| 16 | * The text for the resulting `<label>` |
||||
| 17 | * @param XMLElement $child (optional) |
||||
| 18 | * An XMLElement that this <label> will become the parent of. |
||||
| 19 | * Commonly used with `<input>`. |
||||
| 20 | * @param string $class (optional) |
||||
| 21 | * The class attribute of the resulting `<label>` |
||||
| 22 | * @param string $id (optional) |
||||
| 23 | * The id attribute of the resulting `<label>` |
||||
| 24 | * @param array $attributes (optional) |
||||
| 25 | * Any additional attributes can be included in an associative array with |
||||
| 26 | * the key being the name and the value being the value of the attribute. |
||||
| 27 | * Attributes set from this array will override existing attributes |
||||
| 28 | * set by previous params. |
||||
| 29 | * @throws InvalidArgumentException |
||||
| 30 | * @return XMLElement |
||||
| 31 | */ |
||||
| 32 | public static function Label($name = null, XMLElement $child = null, $class = null, $id = null, array $attributes = null) |
||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 33 | { |
||||
| 34 | General::ensureType(array( |
||||
| 35 | 'name' => array('var' => $name, 'type' => 'string', 'optional' => true), |
||||
| 36 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 37 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true) |
||||
| 38 | )); |
||||
| 39 | |||||
| 40 | $obj = new XMLElement('label', ($name ? $name : null)); |
||||
|
0 ignored issues
–
show
|
|||||
| 41 | |||||
| 42 | if (is_object($child)) { |
||||
| 43 | $obj->appendChild($child); |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | if ($class) { |
||||
| 47 | $obj->setAttribute('class', $class); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | if ($id) { |
||||
| 51 | $obj->setAttribute('id', $id); |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 55 | $obj->setAttributeArray($attributes); |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | return $obj; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Generates a XMLElement representation of `<input>` |
||||
| 63 | * |
||||
| 64 | * @param string $name |
||||
| 65 | * The name attribute of the resulting `<input>` |
||||
| 66 | * @param string $value (optional) |
||||
| 67 | * The value attribute of the resulting `<input>` |
||||
| 68 | * @param string $type |
||||
| 69 | * The type attribute for this `<input>`, defaults to "text". |
||||
| 70 | * @param array $attributes (optional) |
||||
| 71 | * Any additional attributes can be included in an associative array with |
||||
| 72 | * the key being the name and the value being the value of the attribute. |
||||
| 73 | * Attributes set from this array will override existing attributes |
||||
| 74 | * set by previous params. |
||||
| 75 | * @throws InvalidArgumentException |
||||
| 76 | * @return XMLElement |
||||
| 77 | */ |
||||
| 78 | public static function Input($name, $value = null, $type = 'text', array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 79 | { |
||||
| 80 | General::ensureType(array( |
||||
| 81 | 'name' => array('var' => $name, 'type' => 'string'), |
||||
| 82 | 'value' => array('var' => $value, 'type' => 'string', 'optional' => true), |
||||
| 83 | 'type' => array('var' => $type, 'type' => 'string', 'optional' => true), |
||||
| 84 | )); |
||||
| 85 | |||||
| 86 | $obj = new XMLElement('input'); |
||||
| 87 | $obj->setAttribute('name', $name); |
||||
| 88 | |||||
| 89 | if ($type) { |
||||
| 90 | $obj->setAttribute('type', $type); |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | if (strlen($value) !== 0) { |
||||
| 94 | $obj->setAttribute('value', $value); |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 98 | $obj->setAttributeArray($attributes); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | return $obj; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Generates a XMLElement representation of a `<input type='checkbox'>`. This also |
||||
| 106 | * includes the actual label of the Checkbox and any help text if required. Note that |
||||
| 107 | * this includes two input fields, one is the hidden 'no' value and the other |
||||
| 108 | * is the actual checkbox ('yes' value). This is provided so if the checkbox is |
||||
| 109 | * not checked, 'no' is still sent in the form request for this `$name`. |
||||
| 110 | * |
||||
| 111 | * @since Symphony 2.5.2 |
||||
| 112 | * @param string $name |
||||
| 113 | * The name attribute of the resulting checkbox |
||||
| 114 | * @param string $value |
||||
| 115 | * The value attribute of the resulting checkbox |
||||
| 116 | * @param string $description |
||||
| 117 | * This will be localisable and displayed after the checkbox when |
||||
| 118 | * generated. |
||||
| 119 | * @param XMLElement $wrapper |
||||
| 120 | * Passed by reference, if this is provided the elements will be automatically |
||||
| 121 | * added to the wrapper, otherwise they will just be returned. |
||||
| 122 | * @param string $help (optional) |
||||
| 123 | * A help message to show below the checkbox. |
||||
| 124 | * @throws InvalidArgumentException |
||||
| 125 | * @return XMLElement |
||||
| 126 | * The markup for the label and the checkbox. |
||||
| 127 | */ |
||||
| 128 | public static function Checkbox($name, $value, $description = null, XMLElement &$wrapper = null, $help = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 129 | { |
||||
| 130 | General::ensureType(array( |
||||
| 131 | 'name' => array('var' => $name, 'type' => 'string'), |
||||
| 132 | 'value' => array('var' => $value, 'type' => 'string', 'optional' => true), |
||||
| 133 | 'description' => array('var' => $description, 'type' => 'string'), |
||||
| 134 | 'help' => array('var' => $help, 'type' => 'string', 'optional' => true), |
||||
| 135 | )); |
||||
| 136 | |||||
| 137 | // Build the label |
||||
| 138 | $label = Widget::Label(); |
||||
| 139 | if ($help) { |
||||
| 140 | $label->addClass('inline-help'); |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | // Add the 'no' default option to the label, or to the wrapper if it's provided |
||||
| 144 | $default_hidden = Widget::Input($name, 'no', 'hidden'); |
||||
| 145 | if (is_null($wrapper)) { |
||||
| 146 | $label->appendChild($default_hidden); |
||||
| 147 | } else { |
||||
| 148 | $wrapper->appendChild($default_hidden); |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | // Include the actual checkbox. |
||||
| 152 | $input = Widget::Input($name, 'yes', 'checkbox'); |
||||
| 153 | if ($value === 'yes') { |
||||
| 154 | $input->setAttribute('checked', 'checked'); |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | // Build the checkbox, then label, then help |
||||
| 158 | $label->setValue(__('%s ' . $description . ' %s', array( |
||||
| 159 | $input->generate(), |
||||
| 160 | ($help) ? ' <i>(' . $help . ')</i>' : '' |
||||
| 161 | ))); |
||||
| 162 | |||||
| 163 | // If a wrapper was given, add the label to it |
||||
| 164 | if (!is_null($wrapper)) { |
||||
| 165 | $wrapper->appendChild($label); |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | return $label; |
||||
| 169 | } |
||||
| 170 | |||||
| 171 | /** |
||||
| 172 | * Generates a XMLElement representation of `<textarea>` |
||||
| 173 | * |
||||
| 174 | * @param string $name |
||||
| 175 | * The name of the resulting `<textarea>` |
||||
| 176 | * @param integer $rows (optional) |
||||
| 177 | * The height of the `<textarea>`, using the rows attribute. Defaults to 15 |
||||
| 178 | * @param integer $cols (optional) |
||||
| 179 | * The width of the `<textarea>`, using the cols attribute. Defaults to 50. |
||||
| 180 | * @param string $value (optional) |
||||
| 181 | * The content to be displayed inside the `<textarea>` |
||||
| 182 | * @param array $attributes (optional) |
||||
| 183 | * Any additional attributes can be included in an associative array with |
||||
| 184 | * the key being the name and the value being the value of the attribute. |
||||
| 185 | * Attributes set from this array will override existing attributes |
||||
| 186 | * set by previous params. |
||||
| 187 | * @throws InvalidArgumentException |
||||
| 188 | * @return XMLElement |
||||
| 189 | */ |
||||
| 190 | public static function Textarea($name, $rows = 15, $cols = 50, $value = null, array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 191 | { |
||||
| 192 | General::ensureType(array( |
||||
| 193 | 'name' => array('var' => $name, 'type' => 'string'), |
||||
| 194 | 'rows' => array('var' => $rows, 'type' => 'int'), |
||||
| 195 | 'cols' => array('var' => $cols, 'type' => 'int'), |
||||
| 196 | 'value' => array('var' => $value, 'type' => 'string', 'optional' => true) |
||||
| 197 | )); |
||||
| 198 | |||||
| 199 | $obj = new XMLElement('textarea', $value); |
||||
| 200 | |||||
| 201 | $obj->setSelfClosingTag(false); |
||||
| 202 | |||||
| 203 | $obj->setAttribute('name', $name); |
||||
| 204 | $obj->setAttribute('rows', $rows); |
||||
| 205 | $obj->setAttribute('cols', $cols); |
||||
| 206 | |||||
| 207 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 208 | $obj->setAttributeArray($attributes); |
||||
| 209 | } |
||||
| 210 | |||||
| 211 | return $obj; |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | /** |
||||
| 215 | * Generates a XMLElement representation of `<a>` |
||||
| 216 | * |
||||
| 217 | * @param string $value |
||||
| 218 | * The text of the resulting `<a>` |
||||
| 219 | * @param string $href |
||||
| 220 | * The href attribute of the resulting `<a>` |
||||
| 221 | * @param string $title (optional) |
||||
| 222 | * The title attribute of the resulting `<a>` |
||||
| 223 | * @param string $class (optional) |
||||
| 224 | * The class attribute of the resulting `<a>` |
||||
| 225 | * @param string $id (optional) |
||||
| 226 | * The id attribute of the resulting `<a>` |
||||
| 227 | * @param array $attributes (optional) |
||||
| 228 | * Any additional attributes can be included in an associative array with |
||||
| 229 | * the key being the name and the value being the value of the attribute. |
||||
| 230 | * Attributes set from this array will override existing attributes |
||||
| 231 | * set by previous params. |
||||
| 232 | * @throws InvalidArgumentException |
||||
| 233 | * @return XMLElement |
||||
| 234 | */ |
||||
| 235 | public static function Anchor($value, $href, $title = null, $class = null, $id = null, array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 236 | { |
||||
| 237 | General::ensureType(array( |
||||
| 238 | 'value' => array('var' => $value, 'type' => 'string'), |
||||
| 239 | 'href' => array('var' => $href, 'type' => 'string'), |
||||
| 240 | 'title' => array('var' => $title, 'type' => 'string', 'optional' => true), |
||||
| 241 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 242 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true) |
||||
| 243 | )); |
||||
| 244 | |||||
| 245 | $obj = new XMLElement('a', $value); |
||||
| 246 | $obj->setAttribute('href', $href); |
||||
| 247 | |||||
| 248 | if ($title) { |
||||
| 249 | $obj->setAttribute('title', $title); |
||||
| 250 | } |
||||
| 251 | |||||
| 252 | if ($class) { |
||||
| 253 | $obj->setAttribute('class', $class); |
||||
| 254 | } |
||||
| 255 | |||||
| 256 | if ($id) { |
||||
| 257 | $obj->setAttribute('id', $id); |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 261 | $obj->setAttributeArray($attributes); |
||||
| 262 | } |
||||
| 263 | |||||
| 264 | return $obj; |
||||
| 265 | } |
||||
| 266 | |||||
| 267 | /** |
||||
| 268 | * Generates a XMLElement representation of `<form>` |
||||
| 269 | * |
||||
| 270 | * @param string $action |
||||
| 271 | * The text of the resulting `<form>` |
||||
| 272 | * @param string $method |
||||
| 273 | * The method attribute of the resulting `<form>`. Defaults to "post". |
||||
| 274 | * @param string $class (optional) |
||||
| 275 | * The class attribute of the resulting `<form>` |
||||
| 276 | * @param string $id (optional) |
||||
| 277 | * The id attribute of the resulting `<form>` |
||||
| 278 | * @param array $attributes (optional) |
||||
| 279 | * Any additional attributes can be included in an associative array with |
||||
| 280 | * the key being the name and the value being the value of the attribute. |
||||
| 281 | * Attributes set from this array will override existing attributes |
||||
| 282 | * set by previous params. |
||||
| 283 | * @throws InvalidArgumentException |
||||
| 284 | * @return XMLElement |
||||
| 285 | */ |
||||
| 286 | public static function Form($action = null, $method = 'post', $class = null, $id = null, array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 287 | { |
||||
| 288 | General::ensureType(array( |
||||
| 289 | 'action' => array('var' => $action, 'type' => 'string', 'optional' => true), |
||||
| 290 | 'method' => array('var' => $method, 'type' => 'string'), |
||||
| 291 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 292 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true) |
||||
| 293 | )); |
||||
| 294 | |||||
| 295 | $obj = new XMLElement('form'); |
||||
| 296 | $obj->setAttribute('action', $action); |
||||
| 297 | $obj->setAttribute('method', $method); |
||||
| 298 | |||||
| 299 | if ($class) { |
||||
| 300 | $obj->setAttribute('class', $class); |
||||
| 301 | } |
||||
| 302 | |||||
| 303 | if ($id) { |
||||
| 304 | $obj->setAttribute('id', $id); |
||||
| 305 | } |
||||
| 306 | |||||
| 307 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 308 | $obj->setAttributeArray($attributes); |
||||
| 309 | } |
||||
| 310 | |||||
| 311 | return $obj; |
||||
| 312 | } |
||||
| 313 | |||||
| 314 | /** |
||||
| 315 | * Generates a XMLElement representation of `<table>` |
||||
| 316 | * This is a simple way to create generic Symphony table wrapper |
||||
| 317 | * |
||||
| 318 | * @param XMLElement $header |
||||
| 319 | * An XMLElement containing the `<thead>`. See Widget::TableHead |
||||
| 320 | * @param XMLElement $footer |
||||
| 321 | * An XMLElement containing the `<tfoot>` |
||||
| 322 | * @param XMLElement $body |
||||
| 323 | * An XMLElement containing the `<tbody>`. See Widget::TableBody |
||||
| 324 | * @param string $class (optional) |
||||
| 325 | * The class attribute of the resulting `<table>` |
||||
| 326 | * @param string $id (optional) |
||||
| 327 | * The id attribute of the resulting `<table>` |
||||
| 328 | * @param array $attributes (optional) |
||||
| 329 | * Any additional attributes can be included in an associative array with |
||||
| 330 | * the key being the name and the value being the value of the attribute. |
||||
| 331 | * Attributes set from this array will override existing attributes |
||||
| 332 | * set by previous params. |
||||
| 333 | * @throws InvalidArgumentException |
||||
| 334 | * @return XMLElement |
||||
| 335 | */ |
||||
| 336 | public static function Table(XMLElement $header = null, XMLElement $footer = null, XMLElement $body = null, $class = null, $id = null, Array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 337 | { |
||||
| 338 | General::ensureType(array( |
||||
| 339 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 340 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true) |
||||
| 341 | )); |
||||
| 342 | |||||
| 343 | $obj = new XMLElement('table'); |
||||
| 344 | |||||
| 345 | if ($class) { |
||||
| 346 | $obj->setAttribute('class', $class); |
||||
| 347 | } |
||||
| 348 | |||||
| 349 | if ($id) { |
||||
| 350 | $obj->setAttribute('id', $id); |
||||
| 351 | } |
||||
| 352 | |||||
| 353 | if ($header) { |
||||
| 354 | $obj->appendChild($header); |
||||
| 355 | } |
||||
| 356 | |||||
| 357 | if ($footer) { |
||||
| 358 | $obj->appendChild($footer); |
||||
| 359 | } |
||||
| 360 | |||||
| 361 | if ($body) { |
||||
| 362 | $obj->appendChild($body); |
||||
| 363 | } |
||||
| 364 | |||||
| 365 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 366 | $obj->setAttributeArray($attributes); |
||||
| 367 | } |
||||
| 368 | |||||
| 369 | return $obj; |
||||
| 370 | } |
||||
| 371 | |||||
| 372 | /** |
||||
| 373 | * Generates a XMLElement representation of `<thead>` from an array |
||||
| 374 | * containing column names and any other attributes. |
||||
| 375 | * |
||||
| 376 | * @param array $columns |
||||
| 377 | * An array of column arrays, where the first item is the name of the |
||||
| 378 | * column, the second is the scope attribute, and the third is an array |
||||
| 379 | * of possible attributes. |
||||
| 380 | * ` |
||||
| 381 | * array( |
||||
| 382 | * array('Column Name', 'scope', array('class'=>'th-class')) |
||||
| 383 | * ) |
||||
| 384 | * ` |
||||
| 385 | * @return XMLElement |
||||
| 386 | */ |
||||
| 387 | public static function TableHead(array $columns = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 388 | { |
||||
| 389 | $thead = new XMLElement('thead'); |
||||
| 390 | $tr = new XMLElement('tr'); |
||||
| 391 | |||||
| 392 | if (is_array($columns) && !empty($columns)) { |
||||
| 393 | foreach ($columns as $col) { |
||||
| 394 | $th = new XMLElement('th'); |
||||
| 395 | |||||
| 396 | if (is_object($col[0])) { |
||||
| 397 | $th->appendChild($col[0]); |
||||
| 398 | } else { |
||||
| 399 | $th->setValue($col[0]); |
||||
| 400 | } |
||||
| 401 | |||||
| 402 | if ($col[1] && $col[1] != '') { |
||||
| 403 | $th->setAttribute('scope', $col[1]); |
||||
| 404 | } |
||||
| 405 | |||||
| 406 | if (!empty($col[2]) && is_array($col[2])) { |
||||
| 407 | $th->setAttributeArray($col[2]); |
||||
| 408 | } |
||||
| 409 | |||||
| 410 | $tr->appendChild($th); |
||||
| 411 | } |
||||
| 412 | } |
||||
| 413 | |||||
| 414 | $thead->appendChild($tr); |
||||
| 415 | |||||
| 416 | return $thead; |
||||
| 417 | } |
||||
| 418 | |||||
| 419 | /** |
||||
| 420 | * Generates a XMLElement representation of `<tbody>` from an array |
||||
| 421 | * containing `<tr>` XMLElements |
||||
| 422 | * |
||||
| 423 | * @see toolkit.Widget#TableRow() |
||||
| 424 | * @param array $rows |
||||
| 425 | * An array of XMLElements of `<tr>`'s. |
||||
| 426 | * @param string $class (optional) |
||||
| 427 | * The class attribute of the resulting `<tbody>` |
||||
| 428 | * @param string $id (optional) |
||||
| 429 | * The id attribute of the resulting `<tbody>` |
||||
| 430 | * @param array $attributes (optional) |
||||
| 431 | * Any additional attributes can be included in an associative array with |
||||
| 432 | * the key being the name and the value being the value of the attribute. |
||||
| 433 | * Attributes set from this array will override existing attributes |
||||
| 434 | * set by previous params. |
||||
| 435 | * @throws InvalidArgumentException |
||||
| 436 | * @return XMLElement |
||||
| 437 | */ |
||||
| 438 | public static function TableBody(array $rows, $class = null, $id = null, array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 439 | { |
||||
| 440 | General::ensureType(array( |
||||
| 441 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 442 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true) |
||||
| 443 | )); |
||||
| 444 | |||||
| 445 | $tbody = new XMLElement('tbody'); |
||||
| 446 | |||||
| 447 | if ($class) { |
||||
| 448 | $tbody->setAttribute('class', $class); |
||||
| 449 | } |
||||
| 450 | |||||
| 451 | if ($id) { |
||||
| 452 | $tbody->setAttribute('id', $id); |
||||
| 453 | } |
||||
| 454 | |||||
| 455 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 456 | $tbody->setAttributeArray($attributes); |
||||
| 457 | } |
||||
| 458 | |||||
| 459 | foreach ($rows as $row) { |
||||
| 460 | $tbody->appendChild($row); |
||||
| 461 | } |
||||
| 462 | |||||
| 463 | return $tbody; |
||||
| 464 | } |
||||
| 465 | |||||
| 466 | /** |
||||
| 467 | * Generates a XMLElement representation of `<tr>` from an array |
||||
| 468 | * containing column names and any other attributes. |
||||
| 469 | * |
||||
| 470 | * @param array $cells |
||||
| 471 | * An array of XMLElements of `<td>`'s. See Widget::TableData |
||||
| 472 | * @param string $class (optional) |
||||
| 473 | * The class attribute of the resulting `<tr>` |
||||
| 474 | * @param string $id (optional) |
||||
| 475 | * The id attribute of the resulting `<tr>` |
||||
| 476 | * @param integer $rowspan (optional) |
||||
| 477 | * The rowspan attribute of the resulting `<tr>` |
||||
| 478 | * @param array $attributes (optional) |
||||
| 479 | * Any additional attributes can be included in an associative array with |
||||
| 480 | * the key being the name and the value being the value of the attribute. |
||||
| 481 | * Attributes set from this array will override existing attributes |
||||
| 482 | * set by previous params. |
||||
| 483 | * @throws InvalidArgumentException |
||||
| 484 | * @return XMLElement |
||||
| 485 | */ |
||||
| 486 | public static function TableRow(array $cells, $class = null, $id = null, $rowspan = null, Array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 487 | { |
||||
| 488 | General::ensureType(array( |
||||
| 489 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 490 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true), |
||||
| 491 | 'rowspan' => array('var' => $rowspan, 'type' => 'int', 'optional' => true) |
||||
| 492 | )); |
||||
| 493 | |||||
| 494 | $tr = new XMLElement('tr'); |
||||
| 495 | |||||
| 496 | if ($class) { |
||||
| 497 | $tr->setAttribute('class', $class); |
||||
| 498 | } |
||||
| 499 | |||||
| 500 | if ($id) { |
||||
| 501 | $tr->setAttribute('id', $id); |
||||
| 502 | } |
||||
| 503 | |||||
| 504 | if ($rowspan) { |
||||
|
0 ignored issues
–
show
The expression
$rowspan of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||||
| 505 | $tr->setAttribute('rowspan', $rowspan); |
||||
| 506 | } |
||||
| 507 | |||||
| 508 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 509 | $tr->setAttributeArray($attributes); |
||||
| 510 | } |
||||
| 511 | |||||
| 512 | foreach ($cells as $cell) { |
||||
| 513 | $tr->appendChild($cell); |
||||
| 514 | } |
||||
| 515 | |||||
| 516 | return $tr; |
||||
| 517 | } |
||||
| 518 | |||||
| 519 | /** |
||||
| 520 | * Generates a XMLElement representation of a `<td>`. |
||||
| 521 | * |
||||
| 522 | * @param XMLElement|string $value |
||||
| 523 | * Either an XMLElement object, or a string for the value of the |
||||
| 524 | * resulting `<td>` |
||||
| 525 | * @param string $class (optional) |
||||
| 526 | * The class attribute of the resulting `<td>` |
||||
| 527 | * @param string $id (optional) |
||||
| 528 | * The id attribute of the resulting `<td>` |
||||
| 529 | * @param integer $colspan (optional) |
||||
| 530 | * The colspan attribute of the resulting `<td>` |
||||
| 531 | * @param array $attributes (optional) |
||||
| 532 | * Any additional attributes can be included in an associative array with |
||||
| 533 | * the key being the name and the value being the value of the attribute. |
||||
| 534 | * Attributes set from this array will override existing attributes |
||||
| 535 | * set by previous params. |
||||
| 536 | * @throws InvalidArgumentException |
||||
| 537 | * @return XMLElement |
||||
| 538 | */ |
||||
| 539 | public static function TableData($value, $class = null, $id = null, $colspan = null, Array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 540 | { |
||||
| 541 | General::ensureType(array( |
||||
| 542 | 'class' => array('var' => $class, 'type' => 'string', 'optional' => true), |
||||
| 543 | 'id' => array('var' => $id, 'type' => 'string', 'optional' => true), |
||||
| 544 | 'colspan' => array('var' => $colspan, 'type' => 'int', 'optional' => true) |
||||
| 545 | )); |
||||
| 546 | |||||
| 547 | if (is_object($value)) { |
||||
| 548 | $td = new XMLElement('td'); |
||||
| 549 | $td->appendChild($value); |
||||
| 550 | } else { |
||||
| 551 | $td = new XMLElement('td', $value); |
||||
| 552 | } |
||||
| 553 | |||||
| 554 | if ($class) { |
||||
| 555 | $td->setAttribute('class', $class); |
||||
| 556 | } |
||||
| 557 | |||||
| 558 | if ($id) { |
||||
| 559 | $td->setAttribute('id', $id); |
||||
| 560 | } |
||||
| 561 | |||||
| 562 | if ($colspan) { |
||||
|
0 ignored issues
–
show
The expression
$colspan of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||||
| 563 | $td->setAttribute('colspan', $colspan); |
||||
| 564 | } |
||||
| 565 | |||||
| 566 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 567 | $td->setAttributeArray($attributes); |
||||
| 568 | } |
||||
| 569 | |||||
| 570 | return $td; |
||||
| 571 | } |
||||
| 572 | |||||
| 573 | /** |
||||
| 574 | * Generates a XMLElement representation of a `<time>` |
||||
| 575 | * |
||||
| 576 | * @since Symphony 2.3 |
||||
| 577 | * @param string $string |
||||
| 578 | * A string containing date and time, defaults to the current date and time |
||||
| 579 | * @param string $format (optional) |
||||
| 580 | * A valid PHP date format, defaults to `__SYM_TIME_FORMAT__` |
||||
| 581 | * @param boolean $pubdate (optional) |
||||
| 582 | * A flag to make the given date a publish date |
||||
| 583 | * @return XMLElement |
||||
| 584 | */ |
||||
| 585 | public static function Time($string = 'now', $format = __SYM_TIME_FORMAT__, $pubdate = false) |
||||
|
0 ignored issues
–
show
|
|||||
| 586 | { |
||||
| 587 | // Parse date |
||||
| 588 | $date = DateTimeObj::parse($string); |
||||
| 589 | |||||
| 590 | // Create element |
||||
| 591 | $obj = new XMLElement('time', Lang::localizeDate($date->format($format))); |
||||
| 592 | $obj->setAttribute('datetime', $date->format(DateTime::ISO8601)); |
||||
| 593 | $obj->setAttribute('utc', $date->format('U')); |
||||
| 594 | |||||
| 595 | // Pubdate? |
||||
| 596 | if ($pubdate === true) { |
||||
| 597 | $obj->setAttribute('pubdate', 'pubdate'); |
||||
| 598 | } |
||||
| 599 | |||||
| 600 | return $obj; |
||||
| 601 | } |
||||
| 602 | |||||
| 603 | /** |
||||
| 604 | * Generates a XMLElement representation of a `<select>`. This uses |
||||
| 605 | * the private function `__SelectBuildOption()` to build XMLElements of |
||||
| 606 | * options given the `$options` array. |
||||
| 607 | * |
||||
| 608 | * @see toolkit.Widget::__SelectBuildOption() |
||||
| 609 | * @param string $name |
||||
| 610 | * The name attribute of the resulting `<select>` |
||||
| 611 | * @param array $options (optional) |
||||
| 612 | * An array containing the data for each `<option>` for this |
||||
| 613 | * `<select>`. If the array is associative, it is assumed that |
||||
| 614 | * `<optgroup>` are to be created, otherwise it's an array of the |
||||
| 615 | * containing the option data. If no options are provided an empty |
||||
| 616 | * `<select>` XMLElement is returned. |
||||
| 617 | * ` |
||||
| 618 | * array( |
||||
| 619 | * array($value, $selected, $desc, $class, $id, $attr) |
||||
| 620 | * ) |
||||
| 621 | * array( |
||||
| 622 | * array('label' => 'Optgroup', 'data-label' => 'optgroup', 'options' = array( |
||||
| 623 | * array($value, $selected, $desc, $class, $id, $attr) |
||||
| 624 | * ) |
||||
| 625 | * ) |
||||
| 626 | * ` |
||||
| 627 | * @param array $attributes (optional) |
||||
| 628 | * Any additional attributes can be included in an associative array with |
||||
| 629 | * the key being the name and the value being the value of the attribute. |
||||
| 630 | * Attributes set from this array will override existing attributes |
||||
| 631 | * set by previous params. |
||||
| 632 | * @throws InvalidArgumentException |
||||
| 633 | * @return XMLElement |
||||
| 634 | */ |
||||
| 635 | public static function Select($name, array $options = null, array $attributes = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 636 | { |
||||
| 637 | General::ensureType(array( |
||||
| 638 | 'name' => array('var' => $name, 'type' => 'string') |
||||
| 639 | )); |
||||
| 640 | |||||
| 641 | $obj = new XMLElement('select'); |
||||
| 642 | $obj->setAttribute('name', $name); |
||||
| 643 | |||||
| 644 | $obj->setSelfClosingTag(false); |
||||
| 645 | |||||
| 646 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 647 | $obj->setAttributeArray($attributes); |
||||
| 648 | } |
||||
| 649 | |||||
| 650 | if (!is_array($options) || empty($options)) { |
||||
| 651 | if (!isset($attributes['disabled'])) { |
||||
| 652 | $obj->setAttribute('disabled', 'disabled'); |
||||
| 653 | } |
||||
| 654 | |||||
| 655 | return $obj; |
||||
| 656 | } |
||||
| 657 | |||||
| 658 | foreach ($options as $o) { |
||||
| 659 | // Optgroup |
||||
| 660 | if (isset($o['label'])) { |
||||
| 661 | $optgroup = new XMLElement('optgroup'); |
||||
| 662 | $optgroup->setAttribute('label', $o['label']); |
||||
| 663 | |||||
| 664 | if (isset($o['data-label'])) { |
||||
| 665 | $optgroup->setAttribute('data-label', $o['data-label']); |
||||
| 666 | } |
||||
| 667 | |||||
| 668 | foreach ($o['options'] as $option) { |
||||
| 669 | $optgroup->appendChild( |
||||
| 670 | Widget::__SelectBuildOption($option) |
||||
| 671 | ); |
||||
| 672 | } |
||||
| 673 | |||||
| 674 | $obj->appendChild($optgroup); |
||||
| 675 | } else { |
||||
| 676 | $obj->appendChild(Widget::__SelectBuildOption($o)); |
||||
| 677 | } |
||||
| 678 | } |
||||
| 679 | |||||
| 680 | return $obj; |
||||
| 681 | } |
||||
| 682 | |||||
| 683 | /** |
||||
| 684 | * This function is used internally by the `Widget::Select()` to build |
||||
| 685 | * an XMLElement of an `<option>` from an array. |
||||
| 686 | * |
||||
| 687 | * @param array $option |
||||
| 688 | * An array containing the data a single `<option>` for this |
||||
| 689 | * `<select>`. The array can contain the following params: |
||||
| 690 | * string $value |
||||
| 691 | * The value attribute of the resulting `<option>` |
||||
| 692 | * boolean $selected |
||||
| 693 | * Whether this `<option>` should be selected |
||||
| 694 | * string $desc (optional) |
||||
| 695 | * The text of the resulting `<option>`. If omitted $value will |
||||
| 696 | * be used a default. |
||||
| 697 | * string $class (optional) |
||||
| 698 | * The class attribute of the resulting `<option>` |
||||
| 699 | * string $id (optional) |
||||
| 700 | * The id attribute of the resulting `<option>` |
||||
| 701 | * array $attributes (optional) |
||||
| 702 | * Any additional attributes can be included in an associative |
||||
| 703 | * array with the key being the name and the value being the |
||||
| 704 | * value of the attribute. Attributes set from this array |
||||
| 705 | * will override existing attributes set by previous params. |
||||
| 706 | * `array( |
||||
| 707 | * array('one-shot', false, 'One Shot', 'my-option') |
||||
| 708 | * )` |
||||
| 709 | * @return XMLElement |
||||
| 710 | */ |
||||
| 711 | private static function __SelectBuildOption($option) |
||||
| 712 | { |
||||
| 713 | list($value, $selected, $desc, $class, $id, $attributes) = array_pad($option, 6, null); |
||||
| 714 | |||||
| 715 | if (!$desc) { |
||||
| 716 | $desc = $value; |
||||
| 717 | } |
||||
| 718 | |||||
| 719 | $obj = new XMLElement('option', "$desc"); |
||||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $desc instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||||
| 720 | $obj->setSelfClosingTag(false); |
||||
| 721 | $obj->setAttribute('value', "$value"); |
||||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $value instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||||
| 722 | |||||
| 723 | if (!empty($class)) { |
||||
| 724 | $obj->setAttribute('class', $class); |
||||
| 725 | } |
||||
| 726 | |||||
| 727 | if (!empty($id)) { |
||||
| 728 | $obj->setAttribute('id', $id); |
||||
| 729 | } |
||||
| 730 | |||||
| 731 | if (is_array($attributes) && !empty($attributes)) { |
||||
| 732 | $obj->setAttributeArray($attributes); |
||||
| 733 | } |
||||
| 734 | |||||
| 735 | if ($selected) { |
||||
| 736 | $obj->setAttribute('selected', 'selected'); |
||||
| 737 | } |
||||
| 738 | |||||
| 739 | return $obj; |
||||
| 740 | } |
||||
| 741 | |||||
| 742 | /** |
||||
| 743 | * Generates a XMLElement representation of a `<fieldset>` containing |
||||
| 744 | * the "With selected…" menu. This uses the private function `__SelectBuildOption()` |
||||
| 745 | * to build `XMLElement`'s of options given the `$options` array. |
||||
| 746 | * |
||||
| 747 | * @since Symphony 2.3 |
||||
| 748 | * @see toolkit.Widget::__SelectBuildOption() |
||||
| 749 | * @param array $options (optional) |
||||
| 750 | * An array containing the data for each `<option>` for this |
||||
| 751 | * `<select>`. If the array is associative, it is assumed that |
||||
| 752 | * `<optgroup>` are to be created, otherwise it's an array of the |
||||
| 753 | * containing the option data. If no options are provided an empty |
||||
| 754 | * `<select>` XMLElement is returned. |
||||
| 755 | * ` |
||||
| 756 | * array( |
||||
| 757 | * array($value, $selected, $desc, $class, $id, $attr) |
||||
| 758 | * ) |
||||
| 759 | * array( |
||||
| 760 | * array('label' => 'Optgroup', 'options' = array( |
||||
| 761 | * array($value, $selected, $desc, $class, $id, $attr) |
||||
| 762 | * ) |
||||
| 763 | * ) |
||||
| 764 | * ` |
||||
| 765 | * @throws InvalidArgumentException |
||||
| 766 | * @return XMLElement |
||||
| 767 | */ |
||||
| 768 | public static function Apply(array $options = null) |
||||
|
0 ignored issues
–
show
|
|||||
| 769 | { |
||||
| 770 | $fieldset = new XMLElement('fieldset', null, array('class' => 'apply')); |
||||
| 771 | $div = new XMLElement('div'); |
||||
| 772 | $div->appendChild(Widget::Label(__('Actions'), null, 'accessible', null, array( |
||||
| 773 | 'for' => 'with-selected' |
||||
| 774 | ))); |
||||
| 775 | $div->appendChild(Widget::Select('with-selected', $options, array( |
||||
| 776 | 'id' => 'with-selected' |
||||
| 777 | ))); |
||||
| 778 | $fieldset->appendChild($div); |
||||
| 779 | $fieldset->appendChild(new XMLElement('button', __('Apply'), array('name' => 'action[apply]', 'type' => 'submit'))); |
||||
| 780 | |||||
| 781 | return $fieldset; |
||||
| 782 | } |
||||
| 783 | |||||
| 784 | /** |
||||
| 785 | * Will wrap a `<div>` around a desired element to trigger the default |
||||
| 786 | * Symphony error styling. |
||||
| 787 | * |
||||
| 788 | * @since Symphony 2.3 |
||||
| 789 | * @param XMLElement $element |
||||
| 790 | * The element that should be wrapped with an error |
||||
| 791 | * @param string $message |
||||
| 792 | * The text for this error. This will be appended after the $element, |
||||
| 793 | * but inside the wrapping `<div>` |
||||
| 794 | * @throws InvalidArgumentException |
||||
| 795 | * @return XMLElement |
||||
| 796 | */ |
||||
| 797 | public static function Error(XMLElement $element, $message) |
||||
| 798 | { |
||||
| 799 | General::ensureType(array( |
||||
| 800 | 'message' => array('var' => $message, 'type' => 'string') |
||||
| 801 | )); |
||||
| 802 | |||||
| 803 | $div = new XMLElement('div'); |
||||
| 804 | $div->setAttributeArray(array('class' => 'invalid')); |
||||
| 805 | |||||
| 806 | $div->appendChild($element); |
||||
| 807 | $div->appendChild(new XMLElement('p', $message)); |
||||
| 808 | |||||
| 809 | return $div; |
||||
| 810 | } |
||||
| 811 | |||||
| 812 | /** |
||||
| 813 | * Generates a XMLElement representation of a Symphony drawer widget. |
||||
| 814 | * A widget is identified by it's `$label`, and it's contents is defined |
||||
| 815 | * by the `XMLElement`, `$content`. |
||||
| 816 | * |
||||
| 817 | * @since Symphony 2.3 |
||||
| 818 | * @param string $id |
||||
| 819 | * The id attribute for this drawer |
||||
| 820 | * @param string $label |
||||
| 821 | * A name for this drawer |
||||
| 822 | * @param XMLElement $content |
||||
| 823 | * An XMLElement containing the HTML that should be contained inside |
||||
| 824 | * the drawer. |
||||
| 825 | * @param string $default_state |
||||
| 826 | * This parameter defines whether the drawer will be open or closed by |
||||
| 827 | * default. It defaults to closed. |
||||
| 828 | * @param string $context |
||||
| 829 | * @param array $attributes (optional) |
||||
| 830 | * Any additional attributes can be included in an associative array with |
||||
| 831 | * the key being the name and the value being the value of the attribute. |
||||
| 832 | * Attributes set from this array will override existing attributes |
||||
| 833 | * set by previous params, except the `id` attribute. |
||||
| 834 | * @return XMLElement |
||||
| 835 | */ |
||||
| 836 | public static function Drawer($id = '', $label = '', XMLElement $content = null, $default_state = 'closed', $context = '', array $attributes = array()) |
||||
|
0 ignored issues
–
show
|
|||||
| 837 | { |
||||
| 838 | $id = Lang::createHandle($id); |
||||
| 839 | |||||
| 840 | $contents = new XMLElement('div', $content, array( |
||||
| 841 | 'class' => 'contents' |
||||
| 842 | )); |
||||
| 843 | $contents->setElementStyle('html'); |
||||
| 844 | |||||
| 845 | $drawer = new XMLElement('div', $contents, $attributes); |
||||
| 846 | $drawer->setAttribute('data-default-state', $default_state); |
||||
| 847 | $drawer->setAttribute('data-context', $context); |
||||
| 848 | $drawer->setAttribute('data-label', $label); |
||||
| 849 | $drawer->setAttribute('data-interactive', 'data-interactive'); |
||||
| 850 | $drawer->addClass('drawer'); |
||||
| 851 | $drawer->setAttribute('id', 'drawer-' . $id); |
||||
| 852 | |||||
| 853 | return $drawer; |
||||
| 854 | } |
||||
| 855 | |||||
| 856 | /** |
||||
| 857 | * Generates a XMLElement representation of a Symphony calendar. |
||||
| 858 | * |
||||
| 859 | * @since Symphony 2.6 |
||||
| 860 | * @param boolean $time |
||||
| 861 | * Wheather or not to display the time, defaults to true |
||||
| 862 | * @return XMLElement |
||||
| 863 | */ |
||||
| 864 | public static function Calendar($time = true) |
||||
|
0 ignored issues
–
show
|
|||||
| 865 | { |
||||
| 866 | $calendar = new XMLElement('div'); |
||||
| 867 | $calendar->setAttribute('class', 'calendar'); |
||||
| 868 | |||||
| 869 | $date = DateTimeObj::convertDateToMoment(DateTimeObj::getSetting('date_format')); |
||||
|
0 ignored issues
–
show
It seems like
DateTimeObj::getSetting('date_format') can also be of type array; however, parameter $format of DateTimeObj::convertDateToMoment() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 870 | if ($date) { |
||||
| 871 | if ($time === true) { |
||||
| 872 | $separator = DateTimeObj::getSetting('datetime_separator'); |
||||
| 873 | $time = DateTimeObj::convertTimeToMoment(DateTimeObj::getSetting('time_format')); |
||||
|
0 ignored issues
–
show
It seems like
DateTimeObj::getSetting('time_format') can also be of type array; however, parameter $format of DateTimeObj::convertTimeToMoment() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 874 | |||||
| 875 | $calendar->setAttribute('data-calendar', 'datetime'); |
||||
| 876 | $calendar->setAttribute('data-format', $date . $separator . $time); |
||||
|
0 ignored issues
–
show
Are you sure
$separator of type array|null|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 877 | } else { |
||||
| 878 | $calendar->setAttribute('data-calendar', 'date'); |
||||
| 879 | $calendar->setAttribute('data-format', $date); |
||||
| 880 | } |
||||
| 881 | } |
||||
| 882 | |||||
| 883 | return $calendar; |
||||
| 884 | } |
||||
| 885 | } |
||||
| 886 |