@@ -12,464 +12,464 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Group extends Tag |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * The Container |
|
| 17 | - * |
|
| 18 | - * @var Container |
|
| 19 | - */ |
|
| 20 | - protected $app; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The current state of the group |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $state = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Whether the field should be displayed raw or not |
|
| 31 | - * |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $raw = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The group label |
|
| 38 | - * |
|
| 39 | - * @var Element |
|
| 40 | - */ |
|
| 41 | - protected $label; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The group help |
|
| 45 | - * |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - protected $help = array(); |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * An array of elements to preprend the field |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $prepend = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * An array of elements to append the field |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - protected $append = array(); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * The field validations to be checked for errors |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - protected $validations = array(); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The group's element |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $element = 'div'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Whether a custom group is opened or not |
|
| 80 | - * |
|
| 81 | - * @var boolean |
|
| 82 | - */ |
|
| 83 | - public static $opened = false; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * The custom group that is open |
|
| 87 | - * |
|
| 88 | - * @var Former\Form\Group |
|
| 89 | - */ |
|
| 90 | - public static $openGroup = null; |
|
| 91 | - |
|
| 92 | - //////////////////////////////////////////////////////////////////// |
|
| 93 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 94 | - //////////////////////////////////////////////////////////////////// |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Creates a group |
|
| 98 | - * |
|
| 99 | - * @param string $label Its label |
|
| 100 | - */ |
|
| 101 | - public function __construct(Container $app, $label, $validations = null) |
|
| 102 | - { |
|
| 103 | - // Get special classes |
|
| 104 | - $this->app = $app; |
|
| 105 | - $this->addClass($this->app['former.framework']->getGroupClasses()); |
|
| 106 | - |
|
| 107 | - // Invisible if Nude |
|
| 108 | - if ($this->app['former.framework']->is('Nude')) { |
|
| 109 | - $this->element = ''; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Set group label |
|
| 113 | - if ($label) { |
|
| 114 | - $this->setLabel($label); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // Set validations used to override groups own conclusions |
|
| 118 | - $this->validations = (array) $validations; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Prints out the opening of the Control Group |
|
| 123 | - * |
|
| 124 | - * @return string A control group opening tag |
|
| 125 | - */ |
|
| 126 | - public function __toString() |
|
| 127 | - { |
|
| 128 | - return $this->open().$this->getFormattedLabel(); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Opens a group |
|
| 133 | - * |
|
| 134 | - * @return string Opening tag |
|
| 135 | - */ |
|
| 136 | - public function open() |
|
| 137 | - { |
|
| 138 | - if ($this->getErrors()) { |
|
| 139 | - $this->state($this->app['former.framework']->errorState()); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // Retrieve state and append it to classes |
|
| 143 | - if ($this->state) { |
|
| 144 | - $this->addClass($this->state); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Required state |
|
| 148 | - if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) { |
|
| 149 | - $this->addClass($this->app['former']->getOption('required_class')); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return parent::open(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Set the contents of the current group |
|
| 157 | - * |
|
| 158 | - * @param string $contents The group contents |
|
| 159 | - * |
|
| 160 | - * @return string A group |
|
| 161 | - */ |
|
| 162 | - public function contents($contents) |
|
| 163 | - { |
|
| 164 | - return $this->wrap($contents, $this->getFormattedLabel()); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Wrap a Field with the current group |
|
| 169 | - * |
|
| 170 | - * @param \Former\Traits\Field $field A Field instance |
|
| 171 | - * |
|
| 172 | - * @return string A group |
|
| 173 | - */ |
|
| 174 | - public function wrapField($field) |
|
| 175 | - { |
|
| 176 | - $label = $this->getLabel($field); |
|
| 177 | - $field = $this->prependAppend($field); |
|
| 178 | - $field .= $this->getHelp(); |
|
| 179 | - |
|
| 180 | - return $this->wrap($field, $label); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - //////////////////////////////////////////////////////////////////// |
|
| 184 | - //////////////////////////// FIELD METHODS ///////////////////////// |
|
| 185 | - //////////////////////////////////////////////////////////////////// |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Set the state of the group |
|
| 189 | - * |
|
| 190 | - * @param string $state A Bootstrap state class |
|
| 191 | - */ |
|
| 192 | - public function state($state) |
|
| 193 | - { |
|
| 194 | - // Filter state |
|
| 195 | - $state = $this->app['former.framework']->filterState($state); |
|
| 196 | - |
|
| 197 | - $this->state = $state; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Set a class on the Group |
|
| 202 | - * |
|
| 203 | - * @param string $class The class to add |
|
| 204 | - */ |
|
| 205 | - public function addGroupClass($class) |
|
| 206 | - { |
|
| 207 | - $this->addClass($class); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Adds a label to the group |
|
| 212 | - * |
|
| 213 | - * @param string $label A label |
|
| 214 | - */ |
|
| 215 | - public function setLabel($label) |
|
| 216 | - { |
|
| 217 | - if (!$label instanceof Element) { |
|
| 218 | - $label = Helpers::translate($label); |
|
| 219 | - $label = Element::create('label', $label)->for($label); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $this->label = $label; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Get the formatted group label |
|
| 227 | - * |
|
| 228 | - * @return string|null |
|
| 229 | - */ |
|
| 230 | - public function getFormattedLabel() |
|
| 231 | - { |
|
| 232 | - if (!$this->label) { |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Disables the control group for the current field |
|
| 241 | - */ |
|
| 242 | - public function raw() |
|
| 243 | - { |
|
| 244 | - $this->raw = true; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Check if the current group is to be displayed or not |
|
| 249 | - * |
|
| 250 | - * @return boolean |
|
| 251 | - */ |
|
| 252 | - public function isRaw() |
|
| 253 | - { |
|
| 254 | - return (bool) $this->raw; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - //////////////////////////////////////////////////////////////////// |
|
| 258 | - ///////////////////////////// HELP BLOCKS ////////////////////////// |
|
| 259 | - //////////////////////////////////////////////////////////////////// |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Alias for inlineHelp |
|
| 263 | - * |
|
| 264 | - * @param string $help The help text |
|
| 265 | - * @param array $attributes Facultative attributes |
|
| 266 | - */ |
|
| 267 | - public function help($help, $attributes = array()) |
|
| 268 | - { |
|
| 269 | - return $this->inlineHelp($help, $attributes); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Add an inline help |
|
| 274 | - * |
|
| 275 | - * @param string $help The help text |
|
| 276 | - * @param array $attributes Facultative attributes |
|
| 277 | - */ |
|
| 278 | - public function inlineHelp($help, $attributes = array()) |
|
| 279 | - { |
|
| 280 | - // If no help text, do nothing |
|
| 281 | - if (!$help) { |
|
| 282 | - return false; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Add an block help |
|
| 290 | - * |
|
| 291 | - * @param string $help The help text |
|
| 292 | - * @param array $attributes Facultative attributes |
|
| 293 | - */ |
|
| 294 | - public function blockHelp($help, $attributes = array()) |
|
| 295 | - { |
|
| 296 | - // Reserved method |
|
| 297 | - if ($this->app['former.framework']->isnt('TwitterBootstrap') && |
|
| 298 | - $this->app['former.framework']->isnt('TwitterBootstrap3') && |
|
| 299 | - $this->app['former.framework']->isnt('TwitterBootstrap4') |
|
| 300 | - ) { |
|
| 301 | - throw new BadMethodCallException('This method is only available on the Bootstrap framework'); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // If no help text, do nothing |
|
| 305 | - if (!$help) { |
|
| 306 | - return false; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - //////////////////////////////////////////////////////////////////// |
|
| 313 | - ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
|
| 314 | - //////////////////////////////////////////////////////////////////// |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Prepend elements to the field |
|
| 318 | - */ |
|
| 319 | - public function prepend() |
|
| 320 | - { |
|
| 321 | - $this->placeAround(func_get_args(), 'prepend'); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Append elements to the field |
|
| 326 | - */ |
|
| 327 | - public function append() |
|
| 328 | - { |
|
| 329 | - $this->placeAround(func_get_args(), 'append'); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Prepends an icon to a field |
|
| 334 | - * |
|
| 335 | - * @param string $icon The icon to prepend |
|
| 336 | - * @param array $attributes Its attributes |
|
| 337 | - */ |
|
| 338 | - public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 339 | - { |
|
| 340 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 341 | - |
|
| 342 | - $this->prepend($icon); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Append an icon to a field |
|
| 347 | - * |
|
| 348 | - * @param string $icon The icon to prepend |
|
| 349 | - * @param array $attributes Its attributes |
|
| 350 | - */ |
|
| 351 | - public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 352 | - { |
|
| 353 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 354 | - |
|
| 355 | - $this->append($icon); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - //////////////////////////////////////////////////////////////////// |
|
| 359 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 360 | - //////////////////////////////////////////////////////////////////// |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Get the errors for the group |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - public function getErrors() |
|
| 368 | - { |
|
| 369 | - $errors = ''; |
|
| 370 | - |
|
| 371 | - if (!self::$opened) { |
|
| 372 | - |
|
| 373 | - // for non-custom groups, normal error handling applies |
|
| 374 | - $errors = $this->app['former']->getErrors(); |
|
| 375 | - } elseif (!empty($this->validations)) { |
|
| 376 | - |
|
| 377 | - // error handling only when validations specified for custom groups |
|
| 378 | - foreach ($this->validations as $validation) { |
|
| 379 | - $errors .= $this->app['former']->getErrors($validation); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - return $errors; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Wraps content in a group |
|
| 388 | - * |
|
| 389 | - * @param string $contents The content |
|
| 390 | - * @param string $label The label to add |
|
| 391 | - * |
|
| 392 | - * @return string A group |
|
| 393 | - */ |
|
| 394 | - public function wrap($contents, $label = null) |
|
| 395 | - { |
|
| 396 | - $group = $this->open(); |
|
| 397 | - $group .= $label; |
|
| 398 | - $group .= $this->app['former.framework']->wrapField($contents); |
|
| 399 | - $group .= $this->close(); |
|
| 400 | - |
|
| 401 | - return $group; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Prints out the current label |
|
| 406 | - * |
|
| 407 | - * @param string $field The field to create a label for |
|
| 408 | - * |
|
| 409 | - * @return string A <label> tag |
|
| 410 | - */ |
|
| 411 | - protected function getLabel($field = null) |
|
| 412 | - { |
|
| 413 | - // Don't create a label if none exist |
|
| 414 | - if (!$field or !$this->label) { |
|
| 415 | - return null; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - // Wrap label in framework classes |
|
| 419 | - $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 420 | - $this->label = $this->app['former.framework']->createLabelOf($field, $this->label); |
|
| 421 | - $this->label = $this->app['former.framework']->wrapLabel($this->label); |
|
| 422 | - |
|
| 423 | - return $this->label; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Prints out the current help |
|
| 428 | - * |
|
| 429 | - * @return string A .help-block or .help-inline |
|
| 430 | - */ |
|
| 431 | - protected function getHelp() |
|
| 432 | - { |
|
| 433 | - $inline = array_get($this->help, 'inline'); |
|
| 434 | - $block = array_get($this->help, 'block'); |
|
| 435 | - |
|
| 436 | - // Replace help text with error if any found |
|
| 437 | - $errors = $this->app['former']->getErrors(); |
|
| 438 | - if ($errors and $this->app['former']->getOption('error_messages')) { |
|
| 439 | - $inline = $this->app['former.framework']->createHelp($errors); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - return join(null, array($inline, $block)); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Format the field with prepended/appended elements |
|
| 447 | - * |
|
| 448 | - * @param Field $field The field to format |
|
| 449 | - * |
|
| 450 | - * @return string Field plus supplementary elements |
|
| 451 | - */ |
|
| 452 | - protected function prependAppend($field) |
|
| 453 | - { |
|
| 454 | - if (!$this->prepend and !$this->append) { |
|
| 455 | - return $field->render(); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Place elements around the field |
|
| 463 | - * |
|
| 464 | - * @param array $items An array of items to place |
|
| 465 | - * @param string $place Where they should end up (prepend|append) |
|
| 466 | - */ |
|
| 467 | - protected function placeAround($items, $place) |
|
| 468 | - { |
|
| 469 | - // Iterate over the items and place them where they should |
|
| 470 | - foreach ((array) $items as $item) { |
|
| 471 | - $item = $this->app['former.framework']->placeAround($item); |
|
| 472 | - $this->{$place}[] = $item; |
|
| 473 | - } |
|
| 474 | - } |
|
| 15 | + /** |
|
| 16 | + * The Container |
|
| 17 | + * |
|
| 18 | + * @var Container |
|
| 19 | + */ |
|
| 20 | + protected $app; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The current state of the group |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $state = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Whether the field should be displayed raw or not |
|
| 31 | + * |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $raw = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The group label |
|
| 38 | + * |
|
| 39 | + * @var Element |
|
| 40 | + */ |
|
| 41 | + protected $label; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The group help |
|
| 45 | + * |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + protected $help = array(); |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * An array of elements to preprend the field |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $prepend = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * An array of elements to append the field |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + protected $append = array(); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * The field validations to be checked for errors |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + protected $validations = array(); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The group's element |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $element = 'div'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Whether a custom group is opened or not |
|
| 80 | + * |
|
| 81 | + * @var boolean |
|
| 82 | + */ |
|
| 83 | + public static $opened = false; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * The custom group that is open |
|
| 87 | + * |
|
| 88 | + * @var Former\Form\Group |
|
| 89 | + */ |
|
| 90 | + public static $openGroup = null; |
|
| 91 | + |
|
| 92 | + //////////////////////////////////////////////////////////////////// |
|
| 93 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 94 | + //////////////////////////////////////////////////////////////////// |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Creates a group |
|
| 98 | + * |
|
| 99 | + * @param string $label Its label |
|
| 100 | + */ |
|
| 101 | + public function __construct(Container $app, $label, $validations = null) |
|
| 102 | + { |
|
| 103 | + // Get special classes |
|
| 104 | + $this->app = $app; |
|
| 105 | + $this->addClass($this->app['former.framework']->getGroupClasses()); |
|
| 106 | + |
|
| 107 | + // Invisible if Nude |
|
| 108 | + if ($this->app['former.framework']->is('Nude')) { |
|
| 109 | + $this->element = ''; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Set group label |
|
| 113 | + if ($label) { |
|
| 114 | + $this->setLabel($label); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // Set validations used to override groups own conclusions |
|
| 118 | + $this->validations = (array) $validations; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Prints out the opening of the Control Group |
|
| 123 | + * |
|
| 124 | + * @return string A control group opening tag |
|
| 125 | + */ |
|
| 126 | + public function __toString() |
|
| 127 | + { |
|
| 128 | + return $this->open().$this->getFormattedLabel(); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Opens a group |
|
| 133 | + * |
|
| 134 | + * @return string Opening tag |
|
| 135 | + */ |
|
| 136 | + public function open() |
|
| 137 | + { |
|
| 138 | + if ($this->getErrors()) { |
|
| 139 | + $this->state($this->app['former.framework']->errorState()); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // Retrieve state and append it to classes |
|
| 143 | + if ($this->state) { |
|
| 144 | + $this->addClass($this->state); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Required state |
|
| 148 | + if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) { |
|
| 149 | + $this->addClass($this->app['former']->getOption('required_class')); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return parent::open(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Set the contents of the current group |
|
| 157 | + * |
|
| 158 | + * @param string $contents The group contents |
|
| 159 | + * |
|
| 160 | + * @return string A group |
|
| 161 | + */ |
|
| 162 | + public function contents($contents) |
|
| 163 | + { |
|
| 164 | + return $this->wrap($contents, $this->getFormattedLabel()); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Wrap a Field with the current group |
|
| 169 | + * |
|
| 170 | + * @param \Former\Traits\Field $field A Field instance |
|
| 171 | + * |
|
| 172 | + * @return string A group |
|
| 173 | + */ |
|
| 174 | + public function wrapField($field) |
|
| 175 | + { |
|
| 176 | + $label = $this->getLabel($field); |
|
| 177 | + $field = $this->prependAppend($field); |
|
| 178 | + $field .= $this->getHelp(); |
|
| 179 | + |
|
| 180 | + return $this->wrap($field, $label); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + //////////////////////////////////////////////////////////////////// |
|
| 184 | + //////////////////////////// FIELD METHODS ///////////////////////// |
|
| 185 | + //////////////////////////////////////////////////////////////////// |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Set the state of the group |
|
| 189 | + * |
|
| 190 | + * @param string $state A Bootstrap state class |
|
| 191 | + */ |
|
| 192 | + public function state($state) |
|
| 193 | + { |
|
| 194 | + // Filter state |
|
| 195 | + $state = $this->app['former.framework']->filterState($state); |
|
| 196 | + |
|
| 197 | + $this->state = $state; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Set a class on the Group |
|
| 202 | + * |
|
| 203 | + * @param string $class The class to add |
|
| 204 | + */ |
|
| 205 | + public function addGroupClass($class) |
|
| 206 | + { |
|
| 207 | + $this->addClass($class); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Adds a label to the group |
|
| 212 | + * |
|
| 213 | + * @param string $label A label |
|
| 214 | + */ |
|
| 215 | + public function setLabel($label) |
|
| 216 | + { |
|
| 217 | + if (!$label instanceof Element) { |
|
| 218 | + $label = Helpers::translate($label); |
|
| 219 | + $label = Element::create('label', $label)->for($label); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $this->label = $label; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Get the formatted group label |
|
| 227 | + * |
|
| 228 | + * @return string|null |
|
| 229 | + */ |
|
| 230 | + public function getFormattedLabel() |
|
| 231 | + { |
|
| 232 | + if (!$this->label) { |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Disables the control group for the current field |
|
| 241 | + */ |
|
| 242 | + public function raw() |
|
| 243 | + { |
|
| 244 | + $this->raw = true; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Check if the current group is to be displayed or not |
|
| 249 | + * |
|
| 250 | + * @return boolean |
|
| 251 | + */ |
|
| 252 | + public function isRaw() |
|
| 253 | + { |
|
| 254 | + return (bool) $this->raw; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + //////////////////////////////////////////////////////////////////// |
|
| 258 | + ///////////////////////////// HELP BLOCKS ////////////////////////// |
|
| 259 | + //////////////////////////////////////////////////////////////////// |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Alias for inlineHelp |
|
| 263 | + * |
|
| 264 | + * @param string $help The help text |
|
| 265 | + * @param array $attributes Facultative attributes |
|
| 266 | + */ |
|
| 267 | + public function help($help, $attributes = array()) |
|
| 268 | + { |
|
| 269 | + return $this->inlineHelp($help, $attributes); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Add an inline help |
|
| 274 | + * |
|
| 275 | + * @param string $help The help text |
|
| 276 | + * @param array $attributes Facultative attributes |
|
| 277 | + */ |
|
| 278 | + public function inlineHelp($help, $attributes = array()) |
|
| 279 | + { |
|
| 280 | + // If no help text, do nothing |
|
| 281 | + if (!$help) { |
|
| 282 | + return false; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Add an block help |
|
| 290 | + * |
|
| 291 | + * @param string $help The help text |
|
| 292 | + * @param array $attributes Facultative attributes |
|
| 293 | + */ |
|
| 294 | + public function blockHelp($help, $attributes = array()) |
|
| 295 | + { |
|
| 296 | + // Reserved method |
|
| 297 | + if ($this->app['former.framework']->isnt('TwitterBootstrap') && |
|
| 298 | + $this->app['former.framework']->isnt('TwitterBootstrap3') && |
|
| 299 | + $this->app['former.framework']->isnt('TwitterBootstrap4') |
|
| 300 | + ) { |
|
| 301 | + throw new BadMethodCallException('This method is only available on the Bootstrap framework'); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // If no help text, do nothing |
|
| 305 | + if (!$help) { |
|
| 306 | + return false; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + //////////////////////////////////////////////////////////////////// |
|
| 313 | + ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
|
| 314 | + //////////////////////////////////////////////////////////////////// |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Prepend elements to the field |
|
| 318 | + */ |
|
| 319 | + public function prepend() |
|
| 320 | + { |
|
| 321 | + $this->placeAround(func_get_args(), 'prepend'); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Append elements to the field |
|
| 326 | + */ |
|
| 327 | + public function append() |
|
| 328 | + { |
|
| 329 | + $this->placeAround(func_get_args(), 'append'); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Prepends an icon to a field |
|
| 334 | + * |
|
| 335 | + * @param string $icon The icon to prepend |
|
| 336 | + * @param array $attributes Its attributes |
|
| 337 | + */ |
|
| 338 | + public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 339 | + { |
|
| 340 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 341 | + |
|
| 342 | + $this->prepend($icon); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Append an icon to a field |
|
| 347 | + * |
|
| 348 | + * @param string $icon The icon to prepend |
|
| 349 | + * @param array $attributes Its attributes |
|
| 350 | + */ |
|
| 351 | + public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 352 | + { |
|
| 353 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 354 | + |
|
| 355 | + $this->append($icon); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + //////////////////////////////////////////////////////////////////// |
|
| 359 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 360 | + //////////////////////////////////////////////////////////////////// |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Get the errors for the group |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + public function getErrors() |
|
| 368 | + { |
|
| 369 | + $errors = ''; |
|
| 370 | + |
|
| 371 | + if (!self::$opened) { |
|
| 372 | + |
|
| 373 | + // for non-custom groups, normal error handling applies |
|
| 374 | + $errors = $this->app['former']->getErrors(); |
|
| 375 | + } elseif (!empty($this->validations)) { |
|
| 376 | + |
|
| 377 | + // error handling only when validations specified for custom groups |
|
| 378 | + foreach ($this->validations as $validation) { |
|
| 379 | + $errors .= $this->app['former']->getErrors($validation); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + return $errors; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Wraps content in a group |
|
| 388 | + * |
|
| 389 | + * @param string $contents The content |
|
| 390 | + * @param string $label The label to add |
|
| 391 | + * |
|
| 392 | + * @return string A group |
|
| 393 | + */ |
|
| 394 | + public function wrap($contents, $label = null) |
|
| 395 | + { |
|
| 396 | + $group = $this->open(); |
|
| 397 | + $group .= $label; |
|
| 398 | + $group .= $this->app['former.framework']->wrapField($contents); |
|
| 399 | + $group .= $this->close(); |
|
| 400 | + |
|
| 401 | + return $group; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Prints out the current label |
|
| 406 | + * |
|
| 407 | + * @param string $field The field to create a label for |
|
| 408 | + * |
|
| 409 | + * @return string A <label> tag |
|
| 410 | + */ |
|
| 411 | + protected function getLabel($field = null) |
|
| 412 | + { |
|
| 413 | + // Don't create a label if none exist |
|
| 414 | + if (!$field or !$this->label) { |
|
| 415 | + return null; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + // Wrap label in framework classes |
|
| 419 | + $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 420 | + $this->label = $this->app['former.framework']->createLabelOf($field, $this->label); |
|
| 421 | + $this->label = $this->app['former.framework']->wrapLabel($this->label); |
|
| 422 | + |
|
| 423 | + return $this->label; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Prints out the current help |
|
| 428 | + * |
|
| 429 | + * @return string A .help-block or .help-inline |
|
| 430 | + */ |
|
| 431 | + protected function getHelp() |
|
| 432 | + { |
|
| 433 | + $inline = array_get($this->help, 'inline'); |
|
| 434 | + $block = array_get($this->help, 'block'); |
|
| 435 | + |
|
| 436 | + // Replace help text with error if any found |
|
| 437 | + $errors = $this->app['former']->getErrors(); |
|
| 438 | + if ($errors and $this->app['former']->getOption('error_messages')) { |
|
| 439 | + $inline = $this->app['former.framework']->createHelp($errors); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + return join(null, array($inline, $block)); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Format the field with prepended/appended elements |
|
| 447 | + * |
|
| 448 | + * @param Field $field The field to format |
|
| 449 | + * |
|
| 450 | + * @return string Field plus supplementary elements |
|
| 451 | + */ |
|
| 452 | + protected function prependAppend($field) |
|
| 453 | + { |
|
| 454 | + if (!$this->prepend and !$this->append) { |
|
| 455 | + return $field->render(); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Place elements around the field |
|
| 463 | + * |
|
| 464 | + * @param array $items An array of items to place |
|
| 465 | + * @param string $place Where they should end up (prepend|append) |
|
| 466 | + */ |
|
| 467 | + protected function placeAround($items, $place) |
|
| 468 | + { |
|
| 469 | + // Iterate over the items and place them where they should |
|
| 470 | + foreach ((array) $items as $item) { |
|
| 471 | + $item = $this->app['former.framework']->placeAround($item); |
|
| 472 | + $this->{$place}[] = $item; |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | 475 | } |