@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | // Checking for any supplementary classes |
| 149 | 149 | $modifiers = explode('_', $method); |
| 150 | - $method = array_pop($modifiers); |
|
| 150 | + $method = array_pop($modifiers); |
|
| 151 | 151 | |
| 152 | 152 | // Dispatch to the different Form\Fields |
| 153 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 153 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | 154 | $field->setModifiers($modifiers); |
| 155 | 155 | $field->addClass(''); |
| 156 | 156 | |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | $this->setOption('framework', $framework); |
| 351 | 351 | |
| 352 | 352 | $framework = $this->getFrameworkInstance($framework); |
| 353 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 353 | + $this->app->bind('former.framework', function($app) use ($framework) { |
|
| 354 | 354 | return $framework; |
| 355 | 355 | }); |
| 356 | 356 | } |
@@ -370,9 +370,9 @@ discard block |
||
| 370 | 370 | //get interfaces of the given framework |
| 371 | 371 | $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
| 372 | 372 | |
| 373 | - if(class_exists($formerClass)) { |
|
| 373 | + if (class_exists($formerClass)) { |
|
| 374 | 374 | $returnClass = $formerClass; |
| 375 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 375 | + } elseif (class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | 376 | // We have some outside class, lets return it. |
| 377 | 377 | $returnClass = $framework; |
| 378 | 378 | } else { |
@@ -14,490 +14,490 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Former |
| 16 | 16 | { |
| 17 | - // Instances |
|
| 18 | - //////////////////////////////////////////////////////////////////// |
|
| 19 | - |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The current environment |
|
| 23 | - * |
|
| 24 | - * @var \Illuminate\Container\Container |
|
| 25 | - */ |
|
| 26 | - protected $app; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Method Dispatcher |
|
| 30 | - * |
|
| 31 | - * @var MethodDispatcher |
|
| 32 | - */ |
|
| 33 | - protected $dispatch; |
|
| 34 | - |
|
| 35 | - // Informations |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The form's errors |
|
| 40 | - * |
|
| 41 | - * @var Message |
|
| 42 | - */ |
|
| 43 | - protected $errors; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * An array of rules to use |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - protected $rules = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * An array of field macros |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $macros = array(); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The labels created so far |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - public $labels = array(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The IDs created so far |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - public $ids = array(); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * A lookup table where the key is the input name, |
|
| 75 | - * and the value is number of times seen. This is |
|
| 76 | - * used to calculate unique ids. |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - public $names = array(); |
|
| 81 | - |
|
| 82 | - // Namespaces |
|
| 83 | - //////////////////////////////////////////////////////////////////// |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * The namespace of Form elements |
|
| 87 | - */ |
|
| 88 | - const FORMSPACE = 'Former\Form\\'; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * The namespace of fields |
|
| 92 | - */ |
|
| 93 | - const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Build a new Former instance |
|
| 97 | - * |
|
| 98 | - * @param Container $app |
|
| 99 | - * @param MethodDispatcher $dispatcher |
|
| 100 | - */ |
|
| 101 | - public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 102 | - { |
|
| 103 | - $this->app = $app; |
|
| 104 | - $this->dispatch = $dispatcher; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - //////////////////////////// INTERFACE ///////////////////////////// |
|
| 109 | - //////////////////////////////////////////////////////////////////// |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Acts as a router that redirects methods to all of Former classes |
|
| 113 | - * |
|
| 114 | - * @param string $method The method called |
|
| 115 | - * @param array $parameters An array of parameters |
|
| 116 | - * |
|
| 117 | - * @return mixed |
|
| 118 | - */ |
|
| 119 | - public function __call($method, $parameters) |
|
| 120 | - { |
|
| 121 | - // Dispatch to Form\Elements |
|
| 122 | - // Explicitly check false since closeGroup() may return an empty string |
|
| 123 | - if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 124 | - return $element; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // Dispatch to Form\Form |
|
| 128 | - if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 129 | - $this->app->instance('former.form', $form); |
|
| 130 | - |
|
| 131 | - return $this->app['former.form']; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Dispatch to Form\Group |
|
| 135 | - if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 136 | - return $group; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Dispatch to Form\Actions |
|
| 140 | - if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 141 | - return $actions; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Dispatch to macros |
|
| 145 | - if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 146 | - return $macro; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // Checking for any supplementary classes |
|
| 150 | - $modifiers = explode('_', $method); |
|
| 151 | - $method = array_pop($modifiers); |
|
| 152 | - |
|
| 153 | - // Dispatch to the different Form\Fields |
|
| 154 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 155 | - $field->setModifiers($modifiers); |
|
| 156 | - $field->addClass(''); |
|
| 157 | - |
|
| 158 | - // Else bind field |
|
| 159 | - $this->app->instance('former.field', $field); |
|
| 160 | - |
|
| 161 | - return $this->app['former.field']; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - //////////////////////////////////////////////////////////////////// |
|
| 165 | - //////////////////////////////// MACROS //////////////////////////// |
|
| 166 | - //////////////////////////////////////////////////////////////////// |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Register a macro with Former |
|
| 170 | - * |
|
| 171 | - * @param string $name The name of the macro |
|
| 172 | - * @param Callable $macro The macro itself |
|
| 173 | - * |
|
| 174 | - * @return mixed |
|
| 175 | - */ |
|
| 176 | - public function macro($name, $macro) |
|
| 177 | - { |
|
| 178 | - $this->macros[$name] = $macro; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Check if a macro exists |
|
| 183 | - * |
|
| 184 | - * @param string $name |
|
| 185 | - * |
|
| 186 | - * @return boolean |
|
| 187 | - */ |
|
| 188 | - public function hasMacro($name) |
|
| 189 | - { |
|
| 190 | - return isset($this->macros[$name]); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get a registered macro |
|
| 195 | - * |
|
| 196 | - * @param string $name |
|
| 197 | - * |
|
| 198 | - * @return Closure |
|
| 199 | - */ |
|
| 200 | - public function getMacro($name) |
|
| 201 | - { |
|
| 202 | - return $this->macros[$name]; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - //////////////////////////////////////////////////////////////////// |
|
| 206 | - ///////////////////////////// POPULATOR //////////////////////////// |
|
| 207 | - //////////////////////////////////////////////////////////////////// |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Add values to populate the array |
|
| 211 | - * |
|
| 212 | - * @param mixed $values Can be an Eloquent object or an array |
|
| 213 | - */ |
|
| 214 | - public function populate($values) |
|
| 215 | - { |
|
| 216 | - $this->app['former.populator']->replace($values); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Set the value of a particular field |
|
| 221 | - * |
|
| 222 | - * @param string $field The field's name |
|
| 223 | - * @param mixed $value Its new value |
|
| 224 | - */ |
|
| 225 | - public function populateField($field, $value) |
|
| 226 | - { |
|
| 227 | - $this->app['former.populator']->put($field, $value); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the value of a field |
|
| 232 | - * |
|
| 233 | - * @param string $field The field's name |
|
| 234 | - * @param null $fallback |
|
| 235 | - * |
|
| 236 | - * @return mixed |
|
| 237 | - */ |
|
| 238 | - public function getValue($field, $fallback = null) |
|
| 239 | - { |
|
| 240 | - return $this->app['former.populator']->get($field, $fallback); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Fetch a field value from both the new and old POST array |
|
| 245 | - * |
|
| 246 | - * @param string $name A field name |
|
| 247 | - * @param string $fallback A fallback if nothing was found |
|
| 248 | - * |
|
| 249 | - * @return string The results |
|
| 250 | - */ |
|
| 251 | - public function getPost($name, $fallback = null) |
|
| 252 | - { |
|
| 253 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 254 | - $name = trim($name, '.'); |
|
| 255 | - $oldValue = $this->app['request']->old($name, $fallback); |
|
| 256 | - |
|
| 257 | - return $this->app['request']->input($name, $oldValue, true); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - //////////////////////////////////////////////////////////////////// |
|
| 261 | - ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 262 | - //////////////////////////////////////////////////////////////////// |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Set the errors to use for validations |
|
| 266 | - * |
|
| 267 | - * @param Message $validator The result from a validation |
|
| 268 | - * |
|
| 269 | - * @return void |
|
| 270 | - */ |
|
| 271 | - public function withErrors($validator = null) |
|
| 272 | - { |
|
| 273 | - // Try to get the errors form the session |
|
| 274 | - if ($this->app['session']->has('errors')) { |
|
| 275 | - $this->errors = $this->app['session']->get('errors'); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // If we're given a raw Validator, go fetch the errors in it |
|
| 279 | - if ($validator instanceof Validator) { |
|
| 280 | - $this->errors = $validator->getMessageBag(); |
|
| 281 | - } else { |
|
| 282 | - if ($validator instanceof MessageBag) { |
|
| 283 | - $this->errors = $validator; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return $this->errors; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Add live validation rules |
|
| 292 | - * |
|
| 293 | - * @param array *$rules An array of Laravel rules |
|
| 294 | - * |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - public function withRules() |
|
| 298 | - { |
|
| 299 | - $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 300 | - |
|
| 301 | - // Parse the rules according to Laravel conventions |
|
| 302 | - foreach ($rules as $name => $fieldRules) { |
|
| 303 | - $expFieldRules = $fieldRules; |
|
| 304 | - if (!is_array($expFieldRules)) { |
|
| 305 | - if (is_object($expFieldRules)) { |
|
| 306 | - continue; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - $expFieldRules = explode('|', $expFieldRules); |
|
| 310 | - $expFieldRules = array_map('trim', $expFieldRules); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - foreach ($expFieldRules as $rule) { |
|
| 314 | - if (is_object($rule)) { |
|
| 315 | - continue; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $parameters = null; |
|
| 319 | - |
|
| 320 | - if (($colon = strpos($rule, ':')) !== false) { |
|
| 321 | - $rulename = substr($rule, 0, $colon); |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 325 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 326 | - */ |
|
| 327 | - if ($rulename !== 'regex') { |
|
| 328 | - $parameters = str_getcsv(substr($rule, $colon + 1)); |
|
| 329 | - } else { |
|
| 330 | - $parameters = [substr($rule, $colon + 1)]; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - // Exclude unsupported rules |
|
| 335 | - $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 336 | - |
|
| 337 | - // Store processed rule in Former's array |
|
| 338 | - if (!isset($parameters)) { |
|
| 339 | - $parameters = array(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $this->rules[$name][$rule] = $parameters; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Switch the framework used by Former |
|
| 349 | - * |
|
| 350 | - * @param string $framework The name of the framework to use |
|
| 351 | - */ |
|
| 352 | - public function framework($framework = null) |
|
| 353 | - { |
|
| 354 | - if (!$framework) { |
|
| 355 | - return $this->app['former.framework']->current(); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $this->setOption('framework', $framework); |
|
| 359 | - |
|
| 360 | - $framework = $this->getFrameworkInstance($framework); |
|
| 361 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 362 | - return $framework; |
|
| 363 | - }); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Get a new framework instance |
|
| 368 | - * |
|
| 369 | - * @param string $framework |
|
| 370 | - * |
|
| 371 | - * @throws Exceptions\InvalidFrameworkException |
|
| 372 | - * @return \Former\Interfaces\FrameworkInterface |
|
| 373 | - */ |
|
| 374 | - public function getFrameworkInstance($framework) |
|
| 375 | - { |
|
| 376 | - $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 377 | - |
|
| 378 | - //get interfaces of the given framework |
|
| 379 | - $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 380 | - |
|
| 381 | - if(class_exists($formerClass)) { |
|
| 382 | - $returnClass = $formerClass; |
|
| 383 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 384 | - // We have some outside class, lets return it. |
|
| 385 | - $returnClass = $framework; |
|
| 386 | - } else { |
|
| 387 | - throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - return new $returnClass($this->app); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Get an option from the config |
|
| 395 | - * |
|
| 396 | - * @param string $option The option |
|
| 397 | - * @param mixed $default Optional fallback |
|
| 398 | - * |
|
| 399 | - * @return mixed |
|
| 400 | - */ |
|
| 401 | - public function getOption($option, $default = null) |
|
| 402 | - { |
|
| 403 | - return $this->app['config']->get('former.'.$option, $default); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Set an option on the config |
|
| 408 | - * |
|
| 409 | - * @param string $option |
|
| 410 | - * @param string $value |
|
| 411 | - */ |
|
| 412 | - public function setOption($option, $value) |
|
| 413 | - { |
|
| 414 | - return $this->app['config']->set('former.'.$option, $value); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - //////////////////////////////////////////////////////////////////// |
|
| 418 | - ////////////////////////////// BUILDERS //////////////////////////// |
|
| 419 | - //////////////////////////////////////////////////////////////////// |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Closes a form |
|
| 423 | - * |
|
| 424 | - * @return string A form closing tag |
|
| 425 | - */ |
|
| 426 | - public function close() |
|
| 427 | - { |
|
| 428 | - if ($this->app->bound('former.form')) { |
|
| 429 | - $closing = $this->app['former.form']->close(); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - // Destroy instances |
|
| 433 | - $instances = array('former.form', 'former.form.framework'); |
|
| 434 | - foreach ($instances as $instance) { |
|
| 435 | - $this->app[$instance] = null; |
|
| 436 | - unset($this->app[$instance]); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // Reset populator |
|
| 440 | - $this->app['former.populator']->reset(); |
|
| 441 | - |
|
| 442 | - // Reset all values |
|
| 443 | - $this->errors = null; |
|
| 444 | - $this->rules = array(); |
|
| 445 | - |
|
| 446 | - return isset($closing) ? $closing : null; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - //////////////////////////////////////////////////////////////////// |
|
| 450 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 451 | - //////////////////////////////////////////////////////////////////// |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Get the errors for the current field |
|
| 455 | - * |
|
| 456 | - * @param string $name A field name |
|
| 457 | - * |
|
| 458 | - * @return string An error message |
|
| 459 | - */ |
|
| 460 | - public function getErrors($name = null) |
|
| 461 | - { |
|
| 462 | - // Get name and translate array notation |
|
| 463 | - if (!$name and $this->app['former.field']) { |
|
| 464 | - $name = $this->app['former.field']->getName(); |
|
| 465 | - |
|
| 466 | - // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 467 | - if (!$name) { |
|
| 468 | - return ''; |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if ($this->errors and $name) { |
|
| 473 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 474 | - $name = trim($name, '.'); |
|
| 475 | - |
|
| 476 | - return $this->errors->first($name); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - return $this->errors; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Get a rule from the Rules array |
|
| 484 | - * |
|
| 485 | - * @param string $name The field to fetch |
|
| 486 | - * |
|
| 487 | - * @return array An array of rules |
|
| 488 | - */ |
|
| 489 | - public function getRules($name) |
|
| 490 | - { |
|
| 491 | - // Check the rules for the name as given |
|
| 492 | - $ruleset = Arr::get($this->rules, $name); |
|
| 493 | - |
|
| 494 | - // If no rules found, convert to dot notation and try again |
|
| 495 | - if (is_null($ruleset)) { |
|
| 496 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 497 | - $name = trim($name, '.'); |
|
| 498 | - $ruleset = Arr::get($this->rules, $name); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - return $ruleset; |
|
| 502 | - } |
|
| 17 | + // Instances |
|
| 18 | + //////////////////////////////////////////////////////////////////// |
|
| 19 | + |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The current environment |
|
| 23 | + * |
|
| 24 | + * @var \Illuminate\Container\Container |
|
| 25 | + */ |
|
| 26 | + protected $app; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Method Dispatcher |
|
| 30 | + * |
|
| 31 | + * @var MethodDispatcher |
|
| 32 | + */ |
|
| 33 | + protected $dispatch; |
|
| 34 | + |
|
| 35 | + // Informations |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The form's errors |
|
| 40 | + * |
|
| 41 | + * @var Message |
|
| 42 | + */ |
|
| 43 | + protected $errors; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * An array of rules to use |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + protected $rules = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * An array of field macros |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $macros = array(); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The labels created so far |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + public $labels = array(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The IDs created so far |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + public $ids = array(); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * A lookup table where the key is the input name, |
|
| 75 | + * and the value is number of times seen. This is |
|
| 76 | + * used to calculate unique ids. |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + public $names = array(); |
|
| 81 | + |
|
| 82 | + // Namespaces |
|
| 83 | + //////////////////////////////////////////////////////////////////// |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * The namespace of Form elements |
|
| 87 | + */ |
|
| 88 | + const FORMSPACE = 'Former\Form\\'; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * The namespace of fields |
|
| 92 | + */ |
|
| 93 | + const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Build a new Former instance |
|
| 97 | + * |
|
| 98 | + * @param Container $app |
|
| 99 | + * @param MethodDispatcher $dispatcher |
|
| 100 | + */ |
|
| 101 | + public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 102 | + { |
|
| 103 | + $this->app = $app; |
|
| 104 | + $this->dispatch = $dispatcher; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + //////////////////////////// INTERFACE ///////////////////////////// |
|
| 109 | + //////////////////////////////////////////////////////////////////// |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Acts as a router that redirects methods to all of Former classes |
|
| 113 | + * |
|
| 114 | + * @param string $method The method called |
|
| 115 | + * @param array $parameters An array of parameters |
|
| 116 | + * |
|
| 117 | + * @return mixed |
|
| 118 | + */ |
|
| 119 | + public function __call($method, $parameters) |
|
| 120 | + { |
|
| 121 | + // Dispatch to Form\Elements |
|
| 122 | + // Explicitly check false since closeGroup() may return an empty string |
|
| 123 | + if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 124 | + return $element; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // Dispatch to Form\Form |
|
| 128 | + if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 129 | + $this->app->instance('former.form', $form); |
|
| 130 | + |
|
| 131 | + return $this->app['former.form']; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Dispatch to Form\Group |
|
| 135 | + if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 136 | + return $group; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Dispatch to Form\Actions |
|
| 140 | + if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 141 | + return $actions; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Dispatch to macros |
|
| 145 | + if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 146 | + return $macro; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // Checking for any supplementary classes |
|
| 150 | + $modifiers = explode('_', $method); |
|
| 151 | + $method = array_pop($modifiers); |
|
| 152 | + |
|
| 153 | + // Dispatch to the different Form\Fields |
|
| 154 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 155 | + $field->setModifiers($modifiers); |
|
| 156 | + $field->addClass(''); |
|
| 157 | + |
|
| 158 | + // Else bind field |
|
| 159 | + $this->app->instance('former.field', $field); |
|
| 160 | + |
|
| 161 | + return $this->app['former.field']; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + //////////////////////////////////////////////////////////////////// |
|
| 165 | + //////////////////////////////// MACROS //////////////////////////// |
|
| 166 | + //////////////////////////////////////////////////////////////////// |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Register a macro with Former |
|
| 170 | + * |
|
| 171 | + * @param string $name The name of the macro |
|
| 172 | + * @param Callable $macro The macro itself |
|
| 173 | + * |
|
| 174 | + * @return mixed |
|
| 175 | + */ |
|
| 176 | + public function macro($name, $macro) |
|
| 177 | + { |
|
| 178 | + $this->macros[$name] = $macro; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Check if a macro exists |
|
| 183 | + * |
|
| 184 | + * @param string $name |
|
| 185 | + * |
|
| 186 | + * @return boolean |
|
| 187 | + */ |
|
| 188 | + public function hasMacro($name) |
|
| 189 | + { |
|
| 190 | + return isset($this->macros[$name]); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get a registered macro |
|
| 195 | + * |
|
| 196 | + * @param string $name |
|
| 197 | + * |
|
| 198 | + * @return Closure |
|
| 199 | + */ |
|
| 200 | + public function getMacro($name) |
|
| 201 | + { |
|
| 202 | + return $this->macros[$name]; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + //////////////////////////////////////////////////////////////////// |
|
| 206 | + ///////////////////////////// POPULATOR //////////////////////////// |
|
| 207 | + //////////////////////////////////////////////////////////////////// |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Add values to populate the array |
|
| 211 | + * |
|
| 212 | + * @param mixed $values Can be an Eloquent object or an array |
|
| 213 | + */ |
|
| 214 | + public function populate($values) |
|
| 215 | + { |
|
| 216 | + $this->app['former.populator']->replace($values); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Set the value of a particular field |
|
| 221 | + * |
|
| 222 | + * @param string $field The field's name |
|
| 223 | + * @param mixed $value Its new value |
|
| 224 | + */ |
|
| 225 | + public function populateField($field, $value) |
|
| 226 | + { |
|
| 227 | + $this->app['former.populator']->put($field, $value); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the value of a field |
|
| 232 | + * |
|
| 233 | + * @param string $field The field's name |
|
| 234 | + * @param null $fallback |
|
| 235 | + * |
|
| 236 | + * @return mixed |
|
| 237 | + */ |
|
| 238 | + public function getValue($field, $fallback = null) |
|
| 239 | + { |
|
| 240 | + return $this->app['former.populator']->get($field, $fallback); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Fetch a field value from both the new and old POST array |
|
| 245 | + * |
|
| 246 | + * @param string $name A field name |
|
| 247 | + * @param string $fallback A fallback if nothing was found |
|
| 248 | + * |
|
| 249 | + * @return string The results |
|
| 250 | + */ |
|
| 251 | + public function getPost($name, $fallback = null) |
|
| 252 | + { |
|
| 253 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 254 | + $name = trim($name, '.'); |
|
| 255 | + $oldValue = $this->app['request']->old($name, $fallback); |
|
| 256 | + |
|
| 257 | + return $this->app['request']->input($name, $oldValue, true); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + //////////////////////////////////////////////////////////////////// |
|
| 261 | + ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 262 | + //////////////////////////////////////////////////////////////////// |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Set the errors to use for validations |
|
| 266 | + * |
|
| 267 | + * @param Message $validator The result from a validation |
|
| 268 | + * |
|
| 269 | + * @return void |
|
| 270 | + */ |
|
| 271 | + public function withErrors($validator = null) |
|
| 272 | + { |
|
| 273 | + // Try to get the errors form the session |
|
| 274 | + if ($this->app['session']->has('errors')) { |
|
| 275 | + $this->errors = $this->app['session']->get('errors'); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // If we're given a raw Validator, go fetch the errors in it |
|
| 279 | + if ($validator instanceof Validator) { |
|
| 280 | + $this->errors = $validator->getMessageBag(); |
|
| 281 | + } else { |
|
| 282 | + if ($validator instanceof MessageBag) { |
|
| 283 | + $this->errors = $validator; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return $this->errors; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Add live validation rules |
|
| 292 | + * |
|
| 293 | + * @param array *$rules An array of Laravel rules |
|
| 294 | + * |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + public function withRules() |
|
| 298 | + { |
|
| 299 | + $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 300 | + |
|
| 301 | + // Parse the rules according to Laravel conventions |
|
| 302 | + foreach ($rules as $name => $fieldRules) { |
|
| 303 | + $expFieldRules = $fieldRules; |
|
| 304 | + if (!is_array($expFieldRules)) { |
|
| 305 | + if (is_object($expFieldRules)) { |
|
| 306 | + continue; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $expFieldRules = explode('|', $expFieldRules); |
|
| 310 | + $expFieldRules = array_map('trim', $expFieldRules); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + foreach ($expFieldRules as $rule) { |
|
| 314 | + if (is_object($rule)) { |
|
| 315 | + continue; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $parameters = null; |
|
| 319 | + |
|
| 320 | + if (($colon = strpos($rule, ':')) !== false) { |
|
| 321 | + $rulename = substr($rule, 0, $colon); |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 325 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 326 | + */ |
|
| 327 | + if ($rulename !== 'regex') { |
|
| 328 | + $parameters = str_getcsv(substr($rule, $colon + 1)); |
|
| 329 | + } else { |
|
| 330 | + $parameters = [substr($rule, $colon + 1)]; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + // Exclude unsupported rules |
|
| 335 | + $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 336 | + |
|
| 337 | + // Store processed rule in Former's array |
|
| 338 | + if (!isset($parameters)) { |
|
| 339 | + $parameters = array(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $this->rules[$name][$rule] = $parameters; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Switch the framework used by Former |
|
| 349 | + * |
|
| 350 | + * @param string $framework The name of the framework to use |
|
| 351 | + */ |
|
| 352 | + public function framework($framework = null) |
|
| 353 | + { |
|
| 354 | + if (!$framework) { |
|
| 355 | + return $this->app['former.framework']->current(); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $this->setOption('framework', $framework); |
|
| 359 | + |
|
| 360 | + $framework = $this->getFrameworkInstance($framework); |
|
| 361 | + $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 362 | + return $framework; |
|
| 363 | + }); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Get a new framework instance |
|
| 368 | + * |
|
| 369 | + * @param string $framework |
|
| 370 | + * |
|
| 371 | + * @throws Exceptions\InvalidFrameworkException |
|
| 372 | + * @return \Former\Interfaces\FrameworkInterface |
|
| 373 | + */ |
|
| 374 | + public function getFrameworkInstance($framework) |
|
| 375 | + { |
|
| 376 | + $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 377 | + |
|
| 378 | + //get interfaces of the given framework |
|
| 379 | + $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 380 | + |
|
| 381 | + if(class_exists($formerClass)) { |
|
| 382 | + $returnClass = $formerClass; |
|
| 383 | + } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 384 | + // We have some outside class, lets return it. |
|
| 385 | + $returnClass = $framework; |
|
| 386 | + } else { |
|
| 387 | + throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + return new $returnClass($this->app); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Get an option from the config |
|
| 395 | + * |
|
| 396 | + * @param string $option The option |
|
| 397 | + * @param mixed $default Optional fallback |
|
| 398 | + * |
|
| 399 | + * @return mixed |
|
| 400 | + */ |
|
| 401 | + public function getOption($option, $default = null) |
|
| 402 | + { |
|
| 403 | + return $this->app['config']->get('former.'.$option, $default); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Set an option on the config |
|
| 408 | + * |
|
| 409 | + * @param string $option |
|
| 410 | + * @param string $value |
|
| 411 | + */ |
|
| 412 | + public function setOption($option, $value) |
|
| 413 | + { |
|
| 414 | + return $this->app['config']->set('former.'.$option, $value); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + //////////////////////////////////////////////////////////////////// |
|
| 418 | + ////////////////////////////// BUILDERS //////////////////////////// |
|
| 419 | + //////////////////////////////////////////////////////////////////// |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Closes a form |
|
| 423 | + * |
|
| 424 | + * @return string A form closing tag |
|
| 425 | + */ |
|
| 426 | + public function close() |
|
| 427 | + { |
|
| 428 | + if ($this->app->bound('former.form')) { |
|
| 429 | + $closing = $this->app['former.form']->close(); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + // Destroy instances |
|
| 433 | + $instances = array('former.form', 'former.form.framework'); |
|
| 434 | + foreach ($instances as $instance) { |
|
| 435 | + $this->app[$instance] = null; |
|
| 436 | + unset($this->app[$instance]); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // Reset populator |
|
| 440 | + $this->app['former.populator']->reset(); |
|
| 441 | + |
|
| 442 | + // Reset all values |
|
| 443 | + $this->errors = null; |
|
| 444 | + $this->rules = array(); |
|
| 445 | + |
|
| 446 | + return isset($closing) ? $closing : null; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + //////////////////////////////////////////////////////////////////// |
|
| 450 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 451 | + //////////////////////////////////////////////////////////////////// |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Get the errors for the current field |
|
| 455 | + * |
|
| 456 | + * @param string $name A field name |
|
| 457 | + * |
|
| 458 | + * @return string An error message |
|
| 459 | + */ |
|
| 460 | + public function getErrors($name = null) |
|
| 461 | + { |
|
| 462 | + // Get name and translate array notation |
|
| 463 | + if (!$name and $this->app['former.field']) { |
|
| 464 | + $name = $this->app['former.field']->getName(); |
|
| 465 | + |
|
| 466 | + // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 467 | + if (!$name) { |
|
| 468 | + return ''; |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if ($this->errors and $name) { |
|
| 473 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 474 | + $name = trim($name, '.'); |
|
| 475 | + |
|
| 476 | + return $this->errors->first($name); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return $this->errors; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Get a rule from the Rules array |
|
| 484 | + * |
|
| 485 | + * @param string $name The field to fetch |
|
| 486 | + * |
|
| 487 | + * @return array An array of rules |
|
| 488 | + */ |
|
| 489 | + public function getRules($name) |
|
| 490 | + { |
|
| 491 | + // Check the rules for the name as given |
|
| 492 | + $ruleset = Arr::get($this->rules, $name); |
|
| 493 | + |
|
| 494 | + // If no rules found, convert to dot notation and try again |
|
| 495 | + if (is_null($ruleset)) { |
|
| 496 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 497 | + $name = trim($name, '.'); |
|
| 498 | + $ruleset = Arr::get($this->rules, $name); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + return $ruleset; |
|
| 502 | + } |
|
| 503 | 503 | } |
@@ -139,7 +139,7 @@ |
||
| 139 | 139 | $classes = array_intersect($classes, $this->fields); |
| 140 | 140 | |
| 141 | 141 | // Prepend field type |
| 142 | - $classes = array_map(function ($class) { |
|
| 142 | + $classes = array_map(function($class) { |
|
| 143 | 143 | return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
| 144 | 144 | }, $classes); |
| 145 | 145 | |
@@ -345,7 +345,7 @@ |
||
| 345 | 345 | * @param string $text |
| 346 | 346 | * @param array $attributes |
| 347 | 347 | * |
| 348 | - * @return string |
|
| 348 | + * @return Element |
|
| 349 | 349 | */ |
| 350 | 350 | public function createValidationError($text, $attributes = array()) |
| 351 | 351 | { |
@@ -13,469 +13,469 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap4 extends Framework implements FrameworkInterface |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Form types that trigger special styling for this Framework |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'lg', |
|
| 30 | - 'sm', |
|
| 31 | - 'xs', |
|
| 32 | - 'block', |
|
| 33 | - 'link', |
|
| 34 | - 'primary', |
|
| 35 | - 'secondary', |
|
| 36 | - 'warning', |
|
| 37 | - 'danger', |
|
| 38 | - 'success', |
|
| 39 | - 'info', |
|
| 40 | - 'light', |
|
| 41 | - 'dark', |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The field sizes available |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - private $fields = array( |
|
| 50 | - 'lg', |
|
| 51 | - 'sm', |
|
| 52 | - // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | - // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | - // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | - // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | - // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | - // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | - // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | - // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | - ); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The field states available |
|
| 64 | - * |
|
| 65 | - * @var array |
|
| 66 | - */ |
|
| 67 | - protected $states = array( |
|
| 68 | - 'is-invalid', |
|
| 69 | - ); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The default HTML tag used for icons |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $iconTag = 'i'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * The default set for icon fonts |
|
| 80 | - * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | - * |
|
| 82 | - * @var string |
|
| 83 | - */ |
|
| 84 | - protected $iconSet = 'fa'; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * The default prefix icon names |
|
| 88 | - * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | - * |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 92 | - protected $iconPrefix = 'fa'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Create a new TwitterBootstrap instance |
|
| 96 | - * |
|
| 97 | - * @param \Illuminate\Container\Container $app |
|
| 98 | - */ |
|
| 99 | - public function __construct(Container $app) |
|
| 100 | - { |
|
| 101 | - $this->app = $app; |
|
| 102 | - $this->setFrameworkDefaults(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - //////////////////////////////////////////////////////////////////// |
|
| 106 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Filter buttons classes |
|
| 111 | - * |
|
| 112 | - * @param array $classes An array of classes |
|
| 113 | - * |
|
| 114 | - * @return string[] A filtered array |
|
| 115 | - */ |
|
| 116 | - public function filterButtonClasses($classes) |
|
| 117 | - { |
|
| 118 | - // Filter classes |
|
| 119 | - // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | - |
|
| 121 | - // Prepend button type |
|
| 122 | - $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | - $classes[] = 'btn'; |
|
| 124 | - |
|
| 125 | - return $classes; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Filter field classes |
|
| 130 | - * |
|
| 131 | - * @param array $classes An array of classes |
|
| 132 | - * |
|
| 133 | - * @return array A filtered array |
|
| 134 | - */ |
|
| 135 | - public function filterFieldClasses($classes) |
|
| 136 | - { |
|
| 137 | - // Filter classes |
|
| 138 | - $classes = array_intersect($classes, $this->fields); |
|
| 139 | - |
|
| 140 | - // Prepend field type |
|
| 141 | - $classes = array_map(function ($class) { |
|
| 142 | - return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | - }, $classes); |
|
| 144 | - |
|
| 145 | - return $classes; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | - //////////////////////////////////////////////////////////////////// |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Framework error state |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function errorState() |
|
| 158 | - { |
|
| 159 | - return 'is-invalid'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns corresponding inline class of a field |
|
| 164 | - * |
|
| 165 | - * @param Field $field |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function getInlineLabelClass($field) |
|
| 170 | - { |
|
| 171 | - $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | - if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | - $inlineClass = 'form-check-label'; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $inlineClass; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Set the fields width from a label width |
|
| 181 | - * |
|
| 182 | - * @param array $labelWidths |
|
| 183 | - */ |
|
| 184 | - protected function setFieldWidths($labelWidths) |
|
| 185 | - { |
|
| 186 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | - |
|
| 188 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | - if ($viewport) { |
|
| 191 | - $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | - $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | - $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns"; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | - $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | - $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - //////////////////////////////////////////////////////////////////// |
|
| 203 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Add classes to a field |
|
| 208 | - * |
|
| 209 | - * @param Field $field |
|
| 210 | - * @param array $classes The possible classes to add |
|
| 211 | - * |
|
| 212 | - * @return Field |
|
| 213 | - */ |
|
| 214 | - public function getFieldClasses(Field $field, $classes) |
|
| 215 | - { |
|
| 216 | - // Add inline class for checkables |
|
| 217 | - if ($field->isCheckable()) { |
|
| 218 | - // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | - $field->addClass('form-check-input'); |
|
| 220 | - $classes[] = 'form-check'; |
|
| 221 | - |
|
| 222 | - if (in_array('inline', $classes)) { |
|
| 223 | - $field->inline(); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Filter classes according to field type |
|
| 228 | - if ($field->isButton()) { |
|
| 229 | - $classes = $this->filterButtonClasses($classes); |
|
| 230 | - } else { |
|
| 231 | - $classes = $this->filterFieldClasses($classes); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Add form-control class for text-type, textarea and select fields |
|
| 235 | - // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | - if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array( |
|
| 237 | - 'file', |
|
| 238 | - 'plaintext', |
|
| 239 | - )) and !in_array('form-control', $classes) |
|
| 240 | - ) { |
|
| 241 | - $classes[] = 'form-control'; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - if ($this->app['former']->getErrors($field->getName())) { |
|
| 245 | - $classes[] = $this->errorState(); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return $this->addClassesToField($field, $classes); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Add group classes |
|
| 253 | - * |
|
| 254 | - * @return string A list of group classes |
|
| 255 | - */ |
|
| 256 | - public function getGroupClasses() |
|
| 257 | - { |
|
| 258 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 259 | - return 'form-group row'; |
|
| 260 | - } else { |
|
| 261 | - return 'form-group'; |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Add label classes |
|
| 267 | - * |
|
| 268 | - * @return string[] An array of attributes with the label class |
|
| 269 | - */ |
|
| 270 | - public function getLabelClasses() |
|
| 271 | - { |
|
| 272 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 273 | - return array('col-form-label', $this->labelWidth); |
|
| 274 | - } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 275 | - return array('sr-only'); |
|
| 276 | - } else { |
|
| 277 | - return array('col-form-label'); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Add uneditable field classes |
|
| 283 | - * |
|
| 284 | - * @return string An array of attributes with the uneditable class |
|
| 285 | - */ |
|
| 286 | - public function getUneditableClasses() |
|
| 287 | - { |
|
| 288 | - return ''; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Add plain text field classes |
|
| 293 | - * |
|
| 294 | - * @return string An array of attributes with the plain text class |
|
| 295 | - */ |
|
| 296 | - public function getPlainTextClasses() |
|
| 297 | - { |
|
| 298 | - return 'form-control-plaintext'; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Add form class |
|
| 303 | - * |
|
| 304 | - * @param string $type The type of form to add |
|
| 305 | - * |
|
| 306 | - * @return string|null |
|
| 307 | - */ |
|
| 308 | - public function getFormClasses($type) |
|
| 309 | - { |
|
| 310 | - return $type ? 'form-'.$type : null; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Add actions block class |
|
| 315 | - * |
|
| 316 | - * @return string|null |
|
| 317 | - */ |
|
| 318 | - public function getActionClasses() |
|
| 319 | - { |
|
| 320 | - if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 321 | - return 'form-group row'; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - return null; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - //////////////////////////////////////////////////////////////////// |
|
| 328 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 329 | - //////////////////////////////////////////////////////////////////// |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Render an help text |
|
| 333 | - * |
|
| 334 | - * @param string $text |
|
| 335 | - * @param array $attributes |
|
| 336 | - * |
|
| 337 | - * @return Element |
|
| 338 | - */ |
|
| 339 | - public function createHelp($text, $attributes = array()) |
|
| 340 | - { |
|
| 341 | - return Element::create('small', $text, $attributes)->addClass('text-muted'); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Render an validation error text |
|
| 346 | - * |
|
| 347 | - * @param string $text |
|
| 348 | - * @param array $attributes |
|
| 349 | - * |
|
| 350 | - * @return string |
|
| 351 | - */ |
|
| 352 | - public function createValidationError($text, $attributes = array()) |
|
| 353 | - { |
|
| 354 | - return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Render an help text |
|
| 359 | - * |
|
| 360 | - * @param string $text |
|
| 361 | - * @param array $attributes |
|
| 362 | - * |
|
| 363 | - * @return Element |
|
| 364 | - */ |
|
| 365 | - public function createBlockHelp($text, $attributes = array()) |
|
| 366 | - { |
|
| 367 | - return Element::create('small', $text, $attributes)->addClass('form-text text-muted'); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Render a disabled field |
|
| 372 | - * |
|
| 373 | - * @param Field $field |
|
| 374 | - * |
|
| 375 | - * @return Element |
|
| 376 | - */ |
|
| 377 | - public function createDisabledField(Field $field) |
|
| 378 | - { |
|
| 379 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Render a plain text field |
|
| 384 | - * |
|
| 385 | - * @param Field $field |
|
| 386 | - * |
|
| 387 | - * @return Element |
|
| 388 | - */ |
|
| 389 | - public function createPlainTextField(Field $field) |
|
| 390 | - { |
|
| 391 | - $label = $field->getLabel(); |
|
| 392 | - if ($label) { |
|
| 393 | - $label->for(''); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - //////////////////////////////////////////////////////////////////// |
|
| 400 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 401 | - //////////////////////////////////////////////////////////////////// |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Wrap an item to be prepended or appended to the current field |
|
| 405 | - * |
|
| 406 | - * @return Element A wrapped item |
|
| 407 | - */ |
|
| 408 | - public function placeAround($item, $place = null) |
|
| 409 | - { |
|
| 410 | - // Render object |
|
| 411 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 412 | - $item = $item->__toString(); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - $items = (array) $item; |
|
| 416 | - $element = ''; |
|
| 417 | - foreach ($items as $item) { |
|
| 418 | - $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 419 | - |
|
| 420 | - // Get class to use |
|
| 421 | - $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 422 | - |
|
| 423 | - $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - return Element::create('div', $element)->addClass('input-group-'.$place); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Wrap a field with prepended and appended items |
|
| 431 | - * |
|
| 432 | - * @param Field $field |
|
| 433 | - * @param array $prepend |
|
| 434 | - * @param array $append |
|
| 435 | - * |
|
| 436 | - * @return string A field concatented with prepended and/or appended items |
|
| 437 | - */ |
|
| 438 | - public function prependAppend($field, $prepend, $append) |
|
| 439 | - { |
|
| 440 | - $return = '<div class="input-group">'; |
|
| 441 | - $return .= join(null, $prepend); |
|
| 442 | - $return .= $field->render(); |
|
| 443 | - $return .= join(null, $append); |
|
| 444 | - $return .= '</div>'; |
|
| 445 | - |
|
| 446 | - return $return; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Wrap a field with potential additional tags |
|
| 451 | - * |
|
| 452 | - * @param Field $field |
|
| 453 | - * |
|
| 454 | - * @return Element A wrapped field |
|
| 455 | - */ |
|
| 456 | - public function wrapField($field) |
|
| 457 | - { |
|
| 458 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 459 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - return $field; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Wrap actions block with potential additional tags |
|
| 467 | - * |
|
| 468 | - * @param Actions $actions |
|
| 469 | - * |
|
| 470 | - * @return string A wrapped actions block |
|
| 471 | - */ |
|
| 472 | - public function wrapActions($actions) |
|
| 473 | - { |
|
| 474 | - // For horizontal forms, we wrap the actions in a div |
|
| 475 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 476 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - return $actions; |
|
| 480 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'lg', |
|
| 30 | + 'sm', |
|
| 31 | + 'xs', |
|
| 32 | + 'block', |
|
| 33 | + 'link', |
|
| 34 | + 'primary', |
|
| 35 | + 'secondary', |
|
| 36 | + 'warning', |
|
| 37 | + 'danger', |
|
| 38 | + 'success', |
|
| 39 | + 'info', |
|
| 40 | + 'light', |
|
| 41 | + 'dark', |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The field sizes available |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + private $fields = array( |
|
| 50 | + 'lg', |
|
| 51 | + 'sm', |
|
| 52 | + // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | + // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | + // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | + // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | + // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | + // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | + // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | + // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | + ); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The field states available |
|
| 64 | + * |
|
| 65 | + * @var array |
|
| 66 | + */ |
|
| 67 | + protected $states = array( |
|
| 68 | + 'is-invalid', |
|
| 69 | + ); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The default HTML tag used for icons |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $iconTag = 'i'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * The default set for icon fonts |
|
| 80 | + * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | + * |
|
| 82 | + * @var string |
|
| 83 | + */ |
|
| 84 | + protected $iconSet = 'fa'; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * The default prefix icon names |
|
| 88 | + * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | + * |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | + protected $iconPrefix = 'fa'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Create a new TwitterBootstrap instance |
|
| 96 | + * |
|
| 97 | + * @param \Illuminate\Container\Container $app |
|
| 98 | + */ |
|
| 99 | + public function __construct(Container $app) |
|
| 100 | + { |
|
| 101 | + $this->app = $app; |
|
| 102 | + $this->setFrameworkDefaults(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + //////////////////////////////////////////////////////////////////// |
|
| 106 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Filter buttons classes |
|
| 111 | + * |
|
| 112 | + * @param array $classes An array of classes |
|
| 113 | + * |
|
| 114 | + * @return string[] A filtered array |
|
| 115 | + */ |
|
| 116 | + public function filterButtonClasses($classes) |
|
| 117 | + { |
|
| 118 | + // Filter classes |
|
| 119 | + // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | + |
|
| 121 | + // Prepend button type |
|
| 122 | + $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | + $classes[] = 'btn'; |
|
| 124 | + |
|
| 125 | + return $classes; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Filter field classes |
|
| 130 | + * |
|
| 131 | + * @param array $classes An array of classes |
|
| 132 | + * |
|
| 133 | + * @return array A filtered array |
|
| 134 | + */ |
|
| 135 | + public function filterFieldClasses($classes) |
|
| 136 | + { |
|
| 137 | + // Filter classes |
|
| 138 | + $classes = array_intersect($classes, $this->fields); |
|
| 139 | + |
|
| 140 | + // Prepend field type |
|
| 141 | + $classes = array_map(function ($class) { |
|
| 142 | + return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | + }, $classes); |
|
| 144 | + |
|
| 145 | + return $classes; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | + //////////////////////////////////////////////////////////////////// |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Framework error state |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function errorState() |
|
| 158 | + { |
|
| 159 | + return 'is-invalid'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns corresponding inline class of a field |
|
| 164 | + * |
|
| 165 | + * @param Field $field |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function getInlineLabelClass($field) |
|
| 170 | + { |
|
| 171 | + $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | + if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | + $inlineClass = 'form-check-label'; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $inlineClass; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Set the fields width from a label width |
|
| 181 | + * |
|
| 182 | + * @param array $labelWidths |
|
| 183 | + */ |
|
| 184 | + protected function setFieldWidths($labelWidths) |
|
| 185 | + { |
|
| 186 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | + |
|
| 188 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | + if ($viewport) { |
|
| 191 | + $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | + $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | + $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns"; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | + $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | + $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + //////////////////////////////////////////////////////////////////// |
|
| 203 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Add classes to a field |
|
| 208 | + * |
|
| 209 | + * @param Field $field |
|
| 210 | + * @param array $classes The possible classes to add |
|
| 211 | + * |
|
| 212 | + * @return Field |
|
| 213 | + */ |
|
| 214 | + public function getFieldClasses(Field $field, $classes) |
|
| 215 | + { |
|
| 216 | + // Add inline class for checkables |
|
| 217 | + if ($field->isCheckable()) { |
|
| 218 | + // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | + $field->addClass('form-check-input'); |
|
| 220 | + $classes[] = 'form-check'; |
|
| 221 | + |
|
| 222 | + if (in_array('inline', $classes)) { |
|
| 223 | + $field->inline(); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Filter classes according to field type |
|
| 228 | + if ($field->isButton()) { |
|
| 229 | + $classes = $this->filterButtonClasses($classes); |
|
| 230 | + } else { |
|
| 231 | + $classes = $this->filterFieldClasses($classes); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Add form-control class for text-type, textarea and select fields |
|
| 235 | + // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | + if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array( |
|
| 237 | + 'file', |
|
| 238 | + 'plaintext', |
|
| 239 | + )) and !in_array('form-control', $classes) |
|
| 240 | + ) { |
|
| 241 | + $classes[] = 'form-control'; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + if ($this->app['former']->getErrors($field->getName())) { |
|
| 245 | + $classes[] = $this->errorState(); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return $this->addClassesToField($field, $classes); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Add group classes |
|
| 253 | + * |
|
| 254 | + * @return string A list of group classes |
|
| 255 | + */ |
|
| 256 | + public function getGroupClasses() |
|
| 257 | + { |
|
| 258 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 259 | + return 'form-group row'; |
|
| 260 | + } else { |
|
| 261 | + return 'form-group'; |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Add label classes |
|
| 267 | + * |
|
| 268 | + * @return string[] An array of attributes with the label class |
|
| 269 | + */ |
|
| 270 | + public function getLabelClasses() |
|
| 271 | + { |
|
| 272 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 273 | + return array('col-form-label', $this->labelWidth); |
|
| 274 | + } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 275 | + return array('sr-only'); |
|
| 276 | + } else { |
|
| 277 | + return array('col-form-label'); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Add uneditable field classes |
|
| 283 | + * |
|
| 284 | + * @return string An array of attributes with the uneditable class |
|
| 285 | + */ |
|
| 286 | + public function getUneditableClasses() |
|
| 287 | + { |
|
| 288 | + return ''; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Add plain text field classes |
|
| 293 | + * |
|
| 294 | + * @return string An array of attributes with the plain text class |
|
| 295 | + */ |
|
| 296 | + public function getPlainTextClasses() |
|
| 297 | + { |
|
| 298 | + return 'form-control-plaintext'; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Add form class |
|
| 303 | + * |
|
| 304 | + * @param string $type The type of form to add |
|
| 305 | + * |
|
| 306 | + * @return string|null |
|
| 307 | + */ |
|
| 308 | + public function getFormClasses($type) |
|
| 309 | + { |
|
| 310 | + return $type ? 'form-'.$type : null; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Add actions block class |
|
| 315 | + * |
|
| 316 | + * @return string|null |
|
| 317 | + */ |
|
| 318 | + public function getActionClasses() |
|
| 319 | + { |
|
| 320 | + if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 321 | + return 'form-group row'; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + return null; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + //////////////////////////////////////////////////////////////////// |
|
| 328 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 329 | + //////////////////////////////////////////////////////////////////// |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Render an help text |
|
| 333 | + * |
|
| 334 | + * @param string $text |
|
| 335 | + * @param array $attributes |
|
| 336 | + * |
|
| 337 | + * @return Element |
|
| 338 | + */ |
|
| 339 | + public function createHelp($text, $attributes = array()) |
|
| 340 | + { |
|
| 341 | + return Element::create('small', $text, $attributes)->addClass('text-muted'); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Render an validation error text |
|
| 346 | + * |
|
| 347 | + * @param string $text |
|
| 348 | + * @param array $attributes |
|
| 349 | + * |
|
| 350 | + * @return string |
|
| 351 | + */ |
|
| 352 | + public function createValidationError($text, $attributes = array()) |
|
| 353 | + { |
|
| 354 | + return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Render an help text |
|
| 359 | + * |
|
| 360 | + * @param string $text |
|
| 361 | + * @param array $attributes |
|
| 362 | + * |
|
| 363 | + * @return Element |
|
| 364 | + */ |
|
| 365 | + public function createBlockHelp($text, $attributes = array()) |
|
| 366 | + { |
|
| 367 | + return Element::create('small', $text, $attributes)->addClass('form-text text-muted'); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Render a disabled field |
|
| 372 | + * |
|
| 373 | + * @param Field $field |
|
| 374 | + * |
|
| 375 | + * @return Element |
|
| 376 | + */ |
|
| 377 | + public function createDisabledField(Field $field) |
|
| 378 | + { |
|
| 379 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Render a plain text field |
|
| 384 | + * |
|
| 385 | + * @param Field $field |
|
| 386 | + * |
|
| 387 | + * @return Element |
|
| 388 | + */ |
|
| 389 | + public function createPlainTextField(Field $field) |
|
| 390 | + { |
|
| 391 | + $label = $field->getLabel(); |
|
| 392 | + if ($label) { |
|
| 393 | + $label->for(''); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + //////////////////////////////////////////////////////////////////// |
|
| 400 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 401 | + //////////////////////////////////////////////////////////////////// |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Wrap an item to be prepended or appended to the current field |
|
| 405 | + * |
|
| 406 | + * @return Element A wrapped item |
|
| 407 | + */ |
|
| 408 | + public function placeAround($item, $place = null) |
|
| 409 | + { |
|
| 410 | + // Render object |
|
| 411 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 412 | + $item = $item->__toString(); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + $items = (array) $item; |
|
| 416 | + $element = ''; |
|
| 417 | + foreach ($items as $item) { |
|
| 418 | + $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 419 | + |
|
| 420 | + // Get class to use |
|
| 421 | + $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 422 | + |
|
| 423 | + $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + return Element::create('div', $element)->addClass('input-group-'.$place); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Wrap a field with prepended and appended items |
|
| 431 | + * |
|
| 432 | + * @param Field $field |
|
| 433 | + * @param array $prepend |
|
| 434 | + * @param array $append |
|
| 435 | + * |
|
| 436 | + * @return string A field concatented with prepended and/or appended items |
|
| 437 | + */ |
|
| 438 | + public function prependAppend($field, $prepend, $append) |
|
| 439 | + { |
|
| 440 | + $return = '<div class="input-group">'; |
|
| 441 | + $return .= join(null, $prepend); |
|
| 442 | + $return .= $field->render(); |
|
| 443 | + $return .= join(null, $append); |
|
| 444 | + $return .= '</div>'; |
|
| 445 | + |
|
| 446 | + return $return; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Wrap a field with potential additional tags |
|
| 451 | + * |
|
| 452 | + * @param Field $field |
|
| 453 | + * |
|
| 454 | + * @return Element A wrapped field |
|
| 455 | + */ |
|
| 456 | + public function wrapField($field) |
|
| 457 | + { |
|
| 458 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 459 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + return $field; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Wrap actions block with potential additional tags |
|
| 467 | + * |
|
| 468 | + * @param Actions $actions |
|
| 469 | + * |
|
| 470 | + * @return string A wrapped actions block |
|
| 471 | + */ |
|
| 472 | + public function wrapActions($actions) |
|
| 473 | + { |
|
| 474 | + // For horizontal forms, we wrap the actions in a div |
|
| 475 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 476 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return $actions; |
|
| 480 | + } |
|
| 481 | 481 | } |
@@ -12,239 +12,239 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class MethodDispatcher |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * The IoC Container |
|
| 17 | - * |
|
| 18 | - * @var Container |
|
| 19 | - */ |
|
| 20 | - protected $app; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * An array of fields repositories |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $repositories = array(); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Build a new Dispatcher |
|
| 31 | - * |
|
| 32 | - * @param Container $app |
|
| 33 | - * @param array $repositories |
|
| 34 | - */ |
|
| 35 | - public function __construct(Container $app, $repositories) |
|
| 36 | - { |
|
| 37 | - $this->app = $app; |
|
| 38 | - $this->repositories = (array) $repositories; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - //////////////////////////////////////////////////////////////////// |
|
| 42 | - ///////////////////////////// REPOSITORIES ///////////////////////// |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Add a fields repository |
|
| 47 | - * |
|
| 48 | - * @param string $repository |
|
| 49 | - * |
|
| 50 | - * @return $this |
|
| 51 | - */ |
|
| 52 | - public function addRepository($repository) |
|
| 53 | - { |
|
| 54 | - array_unshift($this->repositories, $repository); |
|
| 55 | - |
|
| 56 | - return $this; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - //////////////////////////////////////////////////////////////////// |
|
| 60 | - ///////////////////////////// DISPATCHERS ////////////////////////// |
|
| 61 | - //////////////////////////////////////////////////////////////////// |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Dispatch a call to a registered macro |
|
| 65 | - * |
|
| 66 | - * @param string $method The macro's name |
|
| 67 | - * @param array $parameters The macro's arguments |
|
| 68 | - * |
|
| 69 | - * @return mixed |
|
| 70 | - */ |
|
| 71 | - public function toMacros($method, $parameters) |
|
| 72 | - { |
|
| 73 | - if (!$this->app['former']->hasMacro($method)) { |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // Get and format macro |
|
| 78 | - $callback = $this->app['former']->getMacro($method); |
|
| 79 | - if ($callback instanceof Closure) { |
|
| 80 | - return call_user_func_array($callback, $parameters); |
|
| 81 | - } // Cancel if the macro is invalid |
|
| 82 | - elseif (!is_string($callback)) { |
|
| 83 | - return false; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - // Get class and method |
|
| 87 | - list($class, $method) = explode('@', $callback); |
|
| 88 | - $this->app->instance('Illuminate\Container\Container', $this->app); |
|
| 89 | - |
|
| 90 | - return call_user_func_array(array($this->app->make($class), $method), $parameters); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Dispatch a call over to Elements |
|
| 95 | - * |
|
| 96 | - * @param string $method The method called |
|
| 97 | - * @param array $parameters Its parameters |
|
| 98 | - * |
|
| 99 | - * @return string |
|
| 100 | - */ |
|
| 101 | - public function toElements($method, $parameters) |
|
| 102 | - { |
|
| 103 | - // Disregards if the method isn't an element |
|
| 104 | - if (!method_exists($elements = new Form\Elements($this->app, $this->app['session']), $method)) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return call_user_func_array(array($elements, $method), $parameters); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Dispatch a call over to Form |
|
| 113 | - * |
|
| 114 | - * @param string $method The method called |
|
| 115 | - * @param array $parameters Its parameters |
|
| 116 | - * |
|
| 117 | - * @return Form |
|
| 118 | - */ |
|
| 119 | - public function toForm($method, $parameters) |
|
| 120 | - { |
|
| 121 | - // Disregards if the method doesn't contain 'open' |
|
| 122 | - if (!Str::contains($method, 'open') and !Str::contains($method, 'Open')) { |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $form = new Form\Form($this->app, $this->app['url'], $this->app['former.populator']); |
|
| 127 | - |
|
| 128 | - return $form->openForm($method, $parameters); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Dispatch a call over to Group |
|
| 133 | - * |
|
| 134 | - * @param string $method The method called |
|
| 135 | - * @param array $parameters Its parameters |
|
| 136 | - * |
|
| 137 | - * @return Group |
|
| 138 | - */ |
|
| 139 | - public function toGroup($method, $parameters) |
|
| 140 | - { |
|
| 141 | - // Disregards if the method isn't "group" |
|
| 142 | - if ($method != 'group') { |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Create opener |
|
| 147 | - $group = new Form\Group( |
|
| 148 | - $this->app, |
|
| 149 | - Arr::get($parameters, 0, null), |
|
| 150 | - Arr::get($parameters, 1, null) |
|
| 151 | - ); |
|
| 152 | - |
|
| 153 | - // Set custom group as true |
|
| 154 | - Form\Group::$opened = true; |
|
| 155 | - |
|
| 156 | - // Set custom group reference |
|
| 157 | - Form\Group::$openGroup = $group; |
|
| 158 | - |
|
| 159 | - return $group; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Dispatch a call over to Actions |
|
| 164 | - * |
|
| 165 | - * @param string $method The method called |
|
| 166 | - * @param array $parameters Its parameters |
|
| 167 | - * |
|
| 168 | - * @return Actions |
|
| 169 | - */ |
|
| 170 | - public function toActions($method, $parameters) |
|
| 171 | - { |
|
| 172 | - if ($method != 'actions') { |
|
| 173 | - return false; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return new Form\Actions($this->app, $parameters); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Dispatch a call over to the Fields |
|
| 181 | - * |
|
| 182 | - * @param string $method The method called |
|
| 183 | - * @param array $parameters Its parameters |
|
| 184 | - * |
|
| 185 | - * @return Field |
|
| 186 | - */ |
|
| 187 | - public function toFields($method, $parameters) |
|
| 188 | - { |
|
| 189 | - // Listing parameters |
|
| 190 | - $class = $this->getClassFromMethod($method); |
|
| 191 | - $field = new $class( |
|
| 192 | - $this->app, |
|
| 193 | - $method, |
|
| 194 | - Arr::get($parameters, 0), |
|
| 195 | - Arr::get($parameters, 1), |
|
| 196 | - Arr::get($parameters, 2), |
|
| 197 | - Arr::get($parameters, 3), |
|
| 198 | - Arr::get($parameters, 4), |
|
| 199 | - Arr::get($parameters, 5) |
|
| 200 | - ); |
|
| 201 | - |
|
| 202 | - return $field; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - //////////////////////////////////////////////////////////////////// |
|
| 206 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 207 | - //////////////////////////////////////////////////////////////////// |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the correct class to call according to the created field |
|
| 211 | - * |
|
| 212 | - * @param string $method The field created |
|
| 213 | - * |
|
| 214 | - * @return string The correct class |
|
| 215 | - */ |
|
| 216 | - protected function getClassFromMethod($method) |
|
| 217 | - { |
|
| 218 | - // If the field's name directly match a class, call it |
|
| 219 | - $class = Str::singular(Str::title($method)); |
|
| 220 | - $studly_class = Str::singular(Str::studly($method)); |
|
| 221 | - foreach ($this->repositories as $repository) { |
|
| 222 | - if (class_exists($repository.$studly_class)) { |
|
| 223 | - return $repository.$studly_class; |
|
| 224 | - } else { |
|
| 225 | - if (class_exists($repository.$class)) { |
|
| 226 | - return $repository.$class; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // Else convert known fields to their classes |
|
| 232 | - switch ($method) { |
|
| 233 | - case 'submit': |
|
| 234 | - case 'link': |
|
| 235 | - case 'reset': |
|
| 236 | - $class = Former::FIELDSPACE.'Button'; |
|
| 237 | - break; |
|
| 238 | - |
|
| 239 | - case 'multiselect': |
|
| 240 | - $class = Former::FIELDSPACE.'Select'; |
|
| 241 | - break; |
|
| 242 | - |
|
| 243 | - default: |
|
| 244 | - $class = Former::FIELDSPACE.'Input'; |
|
| 245 | - break; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return $class; |
|
| 249 | - } |
|
| 15 | + /** |
|
| 16 | + * The IoC Container |
|
| 17 | + * |
|
| 18 | + * @var Container |
|
| 19 | + */ |
|
| 20 | + protected $app; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * An array of fields repositories |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $repositories = array(); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Build a new Dispatcher |
|
| 31 | + * |
|
| 32 | + * @param Container $app |
|
| 33 | + * @param array $repositories |
|
| 34 | + */ |
|
| 35 | + public function __construct(Container $app, $repositories) |
|
| 36 | + { |
|
| 37 | + $this->app = $app; |
|
| 38 | + $this->repositories = (array) $repositories; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + //////////////////////////////////////////////////////////////////// |
|
| 42 | + ///////////////////////////// REPOSITORIES ///////////////////////// |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Add a fields repository |
|
| 47 | + * |
|
| 48 | + * @param string $repository |
|
| 49 | + * |
|
| 50 | + * @return $this |
|
| 51 | + */ |
|
| 52 | + public function addRepository($repository) |
|
| 53 | + { |
|
| 54 | + array_unshift($this->repositories, $repository); |
|
| 55 | + |
|
| 56 | + return $this; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + //////////////////////////////////////////////////////////////////// |
|
| 60 | + ///////////////////////////// DISPATCHERS ////////////////////////// |
|
| 61 | + //////////////////////////////////////////////////////////////////// |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Dispatch a call to a registered macro |
|
| 65 | + * |
|
| 66 | + * @param string $method The macro's name |
|
| 67 | + * @param array $parameters The macro's arguments |
|
| 68 | + * |
|
| 69 | + * @return mixed |
|
| 70 | + */ |
|
| 71 | + public function toMacros($method, $parameters) |
|
| 72 | + { |
|
| 73 | + if (!$this->app['former']->hasMacro($method)) { |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // Get and format macro |
|
| 78 | + $callback = $this->app['former']->getMacro($method); |
|
| 79 | + if ($callback instanceof Closure) { |
|
| 80 | + return call_user_func_array($callback, $parameters); |
|
| 81 | + } // Cancel if the macro is invalid |
|
| 82 | + elseif (!is_string($callback)) { |
|
| 83 | + return false; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + // Get class and method |
|
| 87 | + list($class, $method) = explode('@', $callback); |
|
| 88 | + $this->app->instance('Illuminate\Container\Container', $this->app); |
|
| 89 | + |
|
| 90 | + return call_user_func_array(array($this->app->make($class), $method), $parameters); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Dispatch a call over to Elements |
|
| 95 | + * |
|
| 96 | + * @param string $method The method called |
|
| 97 | + * @param array $parameters Its parameters |
|
| 98 | + * |
|
| 99 | + * @return string |
|
| 100 | + */ |
|
| 101 | + public function toElements($method, $parameters) |
|
| 102 | + { |
|
| 103 | + // Disregards if the method isn't an element |
|
| 104 | + if (!method_exists($elements = new Form\Elements($this->app, $this->app['session']), $method)) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return call_user_func_array(array($elements, $method), $parameters); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Dispatch a call over to Form |
|
| 113 | + * |
|
| 114 | + * @param string $method The method called |
|
| 115 | + * @param array $parameters Its parameters |
|
| 116 | + * |
|
| 117 | + * @return Form |
|
| 118 | + */ |
|
| 119 | + public function toForm($method, $parameters) |
|
| 120 | + { |
|
| 121 | + // Disregards if the method doesn't contain 'open' |
|
| 122 | + if (!Str::contains($method, 'open') and !Str::contains($method, 'Open')) { |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $form = new Form\Form($this->app, $this->app['url'], $this->app['former.populator']); |
|
| 127 | + |
|
| 128 | + return $form->openForm($method, $parameters); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Dispatch a call over to Group |
|
| 133 | + * |
|
| 134 | + * @param string $method The method called |
|
| 135 | + * @param array $parameters Its parameters |
|
| 136 | + * |
|
| 137 | + * @return Group |
|
| 138 | + */ |
|
| 139 | + public function toGroup($method, $parameters) |
|
| 140 | + { |
|
| 141 | + // Disregards if the method isn't "group" |
|
| 142 | + if ($method != 'group') { |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Create opener |
|
| 147 | + $group = new Form\Group( |
|
| 148 | + $this->app, |
|
| 149 | + Arr::get($parameters, 0, null), |
|
| 150 | + Arr::get($parameters, 1, null) |
|
| 151 | + ); |
|
| 152 | + |
|
| 153 | + // Set custom group as true |
|
| 154 | + Form\Group::$opened = true; |
|
| 155 | + |
|
| 156 | + // Set custom group reference |
|
| 157 | + Form\Group::$openGroup = $group; |
|
| 158 | + |
|
| 159 | + return $group; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Dispatch a call over to Actions |
|
| 164 | + * |
|
| 165 | + * @param string $method The method called |
|
| 166 | + * @param array $parameters Its parameters |
|
| 167 | + * |
|
| 168 | + * @return Actions |
|
| 169 | + */ |
|
| 170 | + public function toActions($method, $parameters) |
|
| 171 | + { |
|
| 172 | + if ($method != 'actions') { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return new Form\Actions($this->app, $parameters); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Dispatch a call over to the Fields |
|
| 181 | + * |
|
| 182 | + * @param string $method The method called |
|
| 183 | + * @param array $parameters Its parameters |
|
| 184 | + * |
|
| 185 | + * @return Field |
|
| 186 | + */ |
|
| 187 | + public function toFields($method, $parameters) |
|
| 188 | + { |
|
| 189 | + // Listing parameters |
|
| 190 | + $class = $this->getClassFromMethod($method); |
|
| 191 | + $field = new $class( |
|
| 192 | + $this->app, |
|
| 193 | + $method, |
|
| 194 | + Arr::get($parameters, 0), |
|
| 195 | + Arr::get($parameters, 1), |
|
| 196 | + Arr::get($parameters, 2), |
|
| 197 | + Arr::get($parameters, 3), |
|
| 198 | + Arr::get($parameters, 4), |
|
| 199 | + Arr::get($parameters, 5) |
|
| 200 | + ); |
|
| 201 | + |
|
| 202 | + return $field; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + //////////////////////////////////////////////////////////////////// |
|
| 206 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 207 | + //////////////////////////////////////////////////////////////////// |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the correct class to call according to the created field |
|
| 211 | + * |
|
| 212 | + * @param string $method The field created |
|
| 213 | + * |
|
| 214 | + * @return string The correct class |
|
| 215 | + */ |
|
| 216 | + protected function getClassFromMethod($method) |
|
| 217 | + { |
|
| 218 | + // If the field's name directly match a class, call it |
|
| 219 | + $class = Str::singular(Str::title($method)); |
|
| 220 | + $studly_class = Str::singular(Str::studly($method)); |
|
| 221 | + foreach ($this->repositories as $repository) { |
|
| 222 | + if (class_exists($repository.$studly_class)) { |
|
| 223 | + return $repository.$studly_class; |
|
| 224 | + } else { |
|
| 225 | + if (class_exists($repository.$class)) { |
|
| 226 | + return $repository.$class; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // Else convert known fields to their classes |
|
| 232 | + switch ($method) { |
|
| 233 | + case 'submit': |
|
| 234 | + case 'link': |
|
| 235 | + case 'reset': |
|
| 236 | + $class = Former::FIELDSPACE.'Button'; |
|
| 237 | + break; |
|
| 238 | + |
|
| 239 | + case 'multiselect': |
|
| 240 | + $class = Former::FIELDSPACE.'Select'; |
|
| 241 | + break; |
|
| 242 | + |
|
| 243 | + default: |
|
| 244 | + $class = Former::FIELDSPACE.'Input'; |
|
| 245 | + break; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return $class; |
|
| 249 | + } |
|
| 250 | 250 | } |
@@ -11,171 +11,171 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Populator extends Collection |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Create a new collection. |
|
| 16 | - * |
|
| 17 | - * @param array|Model $items |
|
| 18 | - * |
|
| 19 | - * @return void |
|
| 20 | - */ |
|
| 21 | - public function __construct($items = array()) |
|
| 22 | - { |
|
| 23 | - $this->items = $items; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - //////////////////////////////////////////////////////////////////// |
|
| 27 | - ///////////////////////// INDIVIDUAL VALUES //////////////////////// |
|
| 28 | - //////////////////////////////////////////////////////////////////// |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Get the value of a field |
|
| 32 | - * |
|
| 33 | - * @param string $field The field's name |
|
| 34 | - * |
|
| 35 | - * @return mixed |
|
| 36 | - */ |
|
| 37 | - public function get($field, $fallback = null) |
|
| 38 | - { |
|
| 39 | - // Anonymous fields should not return any value |
|
| 40 | - if ($field == null) { |
|
| 41 | - return null; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - // Plain array |
|
| 45 | - if (is_array($this->items) and !Str::contains($field, '[')) { |
|
| 46 | - return parent::get($field, $fallback); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - // Transform the name into an array |
|
| 50 | - $value = $this->items; |
|
| 51 | - $field = $this->parseFieldAsArray($field); |
|
| 52 | - |
|
| 53 | - // Dive into the model |
|
| 54 | - foreach ($field as $relationship) { |
|
| 55 | - |
|
| 56 | - // Get attribute from model |
|
| 57 | - if (!is_array($value)) { |
|
| 58 | - $value = $this->getAttributeFromModel($value, $relationship, $fallback); |
|
| 59 | - |
|
| 60 | - continue; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - // Get attribute from model |
|
| 64 | - if (array_key_exists($relationship, $value)) { |
|
| 65 | - $value = $value[$relationship]; |
|
| 66 | - } else { |
|
| 67 | - // Check array for submodels that may contain the relationship |
|
| 68 | - $inSubmodel = false; |
|
| 69 | - |
|
| 70 | - foreach ($value as $key => $submodel) { |
|
| 71 | - $value[$key] = $this->getAttributeFromModel($submodel, $relationship, $fallback); |
|
| 72 | - |
|
| 73 | - if ($value[$key] !== $fallback) { |
|
| 74 | - $inSubmodel = true; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // If no submodels contained the relationship, return the fallback, not an array of fallbacks |
|
| 79 | - if (!$inSubmodel) { |
|
| 80 | - $value = $fallback; |
|
| 81 | - break; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $value; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - //////////////////////////////////////////////////////////////////// |
|
| 90 | - ///////////////////////////// SWAPPERS ///////////////////////////// |
|
| 91 | - //////////////////////////////////////////////////////////////////// |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Replace the items |
|
| 95 | - * |
|
| 96 | - * @param mixed $items |
|
| 97 | - * |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - public function replace($items) |
|
| 101 | - { |
|
| 102 | - $this->items = $items; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Reset the current values array |
|
| 107 | - * |
|
| 108 | - * @return void |
|
| 109 | - */ |
|
| 110 | - public function reset() |
|
| 111 | - { |
|
| 112 | - $this->items = array(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - //////////////////////////////////////////////////////////////////// |
|
| 116 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 117 | - //////////////////////////////////////////////////////////////////// |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Parses the name of a field to a tree of fields |
|
| 121 | - * |
|
| 122 | - * @param string $field The field's name |
|
| 123 | - * |
|
| 124 | - * @return array A tree of field |
|
| 125 | - */ |
|
| 126 | - protected function parseFieldAsArray($field) |
|
| 127 | - { |
|
| 128 | - if (Str::contains($field, '[]')) { |
|
| 129 | - return (array) $field; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Transform array notation to dot notation |
|
| 133 | - if (Str::contains($field, '[')) { |
|
| 134 | - $field = preg_replace("/[\[\]]/", '.', $field); |
|
| 135 | - $field = str_replace('..', '.', $field); |
|
| 136 | - $field = trim($field, '.'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Parse dot notation |
|
| 140 | - if (Str::contains($field, '.')) { |
|
| 141 | - $field = explode('.', $field); |
|
| 142 | - } else { |
|
| 143 | - $field = (array) $field; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return $field; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get an attribute from a model |
|
| 151 | - * |
|
| 152 | - * @param object $model The model |
|
| 153 | - * @param string $attribute The attribute's name |
|
| 154 | - * @param string $fallback Fallback value |
|
| 155 | - * |
|
| 156 | - * @return mixed |
|
| 157 | - */ |
|
| 158 | - public function getAttributeFromModel($model, $attribute, $fallback) |
|
| 159 | - { |
|
| 160 | - if ($model instanceof Model) { |
|
| 161 | - // Return fallback if attribute is null |
|
| 162 | - $value = $model->getAttribute($attribute); |
|
| 163 | - return is_null($value) ? $fallback : $value; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if ($model instanceof Collection) { |
|
| 167 | - return $model->get($attribute, $fallback); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (is_object($model) && method_exists($model, 'toArray')) { |
|
| 171 | - $model = $model->toArray(); |
|
| 172 | - } else { |
|
| 173 | - $model = (array) $model; |
|
| 174 | - } |
|
| 175 | - if (array_key_exists($attribute, $model)) { |
|
| 176 | - return $model[$attribute]; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $fallback; |
|
| 180 | - } |
|
| 14 | + /** |
|
| 15 | + * Create a new collection. |
|
| 16 | + * |
|
| 17 | + * @param array|Model $items |
|
| 18 | + * |
|
| 19 | + * @return void |
|
| 20 | + */ |
|
| 21 | + public function __construct($items = array()) |
|
| 22 | + { |
|
| 23 | + $this->items = $items; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + //////////////////////////////////////////////////////////////////// |
|
| 27 | + ///////////////////////// INDIVIDUAL VALUES //////////////////////// |
|
| 28 | + //////////////////////////////////////////////////////////////////// |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Get the value of a field |
|
| 32 | + * |
|
| 33 | + * @param string $field The field's name |
|
| 34 | + * |
|
| 35 | + * @return mixed |
|
| 36 | + */ |
|
| 37 | + public function get($field, $fallback = null) |
|
| 38 | + { |
|
| 39 | + // Anonymous fields should not return any value |
|
| 40 | + if ($field == null) { |
|
| 41 | + return null; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + // Plain array |
|
| 45 | + if (is_array($this->items) and !Str::contains($field, '[')) { |
|
| 46 | + return parent::get($field, $fallback); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + // Transform the name into an array |
|
| 50 | + $value = $this->items; |
|
| 51 | + $field = $this->parseFieldAsArray($field); |
|
| 52 | + |
|
| 53 | + // Dive into the model |
|
| 54 | + foreach ($field as $relationship) { |
|
| 55 | + |
|
| 56 | + // Get attribute from model |
|
| 57 | + if (!is_array($value)) { |
|
| 58 | + $value = $this->getAttributeFromModel($value, $relationship, $fallback); |
|
| 59 | + |
|
| 60 | + continue; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + // Get attribute from model |
|
| 64 | + if (array_key_exists($relationship, $value)) { |
|
| 65 | + $value = $value[$relationship]; |
|
| 66 | + } else { |
|
| 67 | + // Check array for submodels that may contain the relationship |
|
| 68 | + $inSubmodel = false; |
|
| 69 | + |
|
| 70 | + foreach ($value as $key => $submodel) { |
|
| 71 | + $value[$key] = $this->getAttributeFromModel($submodel, $relationship, $fallback); |
|
| 72 | + |
|
| 73 | + if ($value[$key] !== $fallback) { |
|
| 74 | + $inSubmodel = true; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // If no submodels contained the relationship, return the fallback, not an array of fallbacks |
|
| 79 | + if (!$inSubmodel) { |
|
| 80 | + $value = $fallback; |
|
| 81 | + break; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $value; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + //////////////////////////////////////////////////////////////////// |
|
| 90 | + ///////////////////////////// SWAPPERS ///////////////////////////// |
|
| 91 | + //////////////////////////////////////////////////////////////////// |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Replace the items |
|
| 95 | + * |
|
| 96 | + * @param mixed $items |
|
| 97 | + * |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + public function replace($items) |
|
| 101 | + { |
|
| 102 | + $this->items = $items; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Reset the current values array |
|
| 107 | + * |
|
| 108 | + * @return void |
|
| 109 | + */ |
|
| 110 | + public function reset() |
|
| 111 | + { |
|
| 112 | + $this->items = array(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + //////////////////////////////////////////////////////////////////// |
|
| 116 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 117 | + //////////////////////////////////////////////////////////////////// |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Parses the name of a field to a tree of fields |
|
| 121 | + * |
|
| 122 | + * @param string $field The field's name |
|
| 123 | + * |
|
| 124 | + * @return array A tree of field |
|
| 125 | + */ |
|
| 126 | + protected function parseFieldAsArray($field) |
|
| 127 | + { |
|
| 128 | + if (Str::contains($field, '[]')) { |
|
| 129 | + return (array) $field; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Transform array notation to dot notation |
|
| 133 | + if (Str::contains($field, '[')) { |
|
| 134 | + $field = preg_replace("/[\[\]]/", '.', $field); |
|
| 135 | + $field = str_replace('..', '.', $field); |
|
| 136 | + $field = trim($field, '.'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Parse dot notation |
|
| 140 | + if (Str::contains($field, '.')) { |
|
| 141 | + $field = explode('.', $field); |
|
| 142 | + } else { |
|
| 143 | + $field = (array) $field; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return $field; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get an attribute from a model |
|
| 151 | + * |
|
| 152 | + * @param object $model The model |
|
| 153 | + * @param string $attribute The attribute's name |
|
| 154 | + * @param string $fallback Fallback value |
|
| 155 | + * |
|
| 156 | + * @return mixed |
|
| 157 | + */ |
|
| 158 | + public function getAttributeFromModel($model, $attribute, $fallback) |
|
| 159 | + { |
|
| 160 | + if ($model instanceof Model) { |
|
| 161 | + // Return fallback if attribute is null |
|
| 162 | + $value = $model->getAttribute($attribute); |
|
| 163 | + return is_null($value) ? $fallback : $value; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if ($model instanceof Collection) { |
|
| 167 | + return $model->get($attribute, $fallback); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (is_object($model) && method_exists($model, 'toArray')) { |
|
| 171 | + $model = $model->toArray(); |
|
| 172 | + } else { |
|
| 173 | + $model = (array) $model; |
|
| 174 | + } |
|
| 175 | + if (array_key_exists($attribute, $model)) { |
|
| 176 | + return $model[$attribute]; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $fallback; |
|
| 180 | + } |
|
| 181 | 181 | } |
@@ -9,191 +9,191 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Helpers |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The IoC Container |
|
| 14 | - * |
|
| 15 | - * @var Container |
|
| 16 | - */ |
|
| 17 | - protected static $app; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Bind a Container to the Helpers class |
|
| 21 | - * |
|
| 22 | - * @param Container $app |
|
| 23 | - */ |
|
| 24 | - public static function setApp(Container $app) |
|
| 25 | - { |
|
| 26 | - static::$app = $app; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Encodes HTML |
|
| 31 | - * |
|
| 32 | - * @param string $value The string to encode |
|
| 33 | - * |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public static function encode($value) |
|
| 37 | - { |
|
| 38 | - return htmlentities($value, ENT_QUOTES, 'UTF-8', true); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - //////////////////////////////////////////////////////////////////// |
|
| 42 | - ///////////////////////// LOCALIZATION HELPERS ///////////////////// |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Translates a string by trying several fallbacks |
|
| 47 | - * |
|
| 48 | - * @param string $key The key to translate |
|
| 49 | - * @param string $fallback The ultimate fallback |
|
| 50 | - * |
|
| 51 | - * @return string A translated string |
|
| 52 | - */ |
|
| 53 | - public static function translate($key, $fallback = null) |
|
| 54 | - { |
|
| 55 | - // If nothing was given, return nothing, bitch |
|
| 56 | - if (!$key) { |
|
| 57 | - return null; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - // If no fallback, use the key |
|
| 61 | - if (!$fallback) { |
|
| 62 | - $fallback = $key; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Assure we don't already have a Lang object |
|
| 66 | - if (is_object($key) and method_exists($key, 'get')) { |
|
| 67 | - return $key->get(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $translation = null; |
|
| 71 | - $translateFrom = static::$app['former']->getOption('translate_from'); |
|
| 72 | - if (substr($translateFrom, -1) !== '/') { |
|
| 73 | - $translateFrom .= '.'; |
|
| 74 | - } |
|
| 75 | - $translateFrom .= $key; |
|
| 76 | - |
|
| 77 | - // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']] |
|
| 78 | - if (strpos($translateFrom, ']') !== false) { |
|
| 79 | - $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // Search for the key itself, if it is valid |
|
| 83 | - $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1; |
|
| 84 | - if ($validKey && static::$app['translator']->has($key)) { |
|
| 85 | - $translation = static::$app['translator']->get($key); |
|
| 86 | - } elseif (static::$app['translator']->has($translateFrom)) { |
|
| 87 | - $translation = static::$app['translator']->get($translateFrom); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // Replace by fallback if invalid |
|
| 91 | - if (!$translation or is_array($translation)) { |
|
| 92 | - $translation = $fallback; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Capitalize |
|
| 96 | - $capitalize = static::$app['former']->getOption('capitalize_translations'); |
|
| 97 | - |
|
| 98 | - return $capitalize ? ucfirst($translation) : $translation; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - //////////////////////////////////////////////////////////////////// |
|
| 102 | - ////////////////////////// DATABASE HELPERS //////////////////////// |
|
| 103 | - //////////////////////////////////////////////////////////////////// |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Transforms an array of models into an associative array |
|
| 107 | - * |
|
| 108 | - * @param array|object $query The array of results |
|
| 109 | - * @param string|function $text The value to use as text |
|
| 110 | - * @param string|array $attributes The data to use as attributes |
|
| 111 | - * |
|
| 112 | - * @return array A data array |
|
| 113 | - */ |
|
| 114 | - public static function queryToArray($query, $text = null, $attributes = null) |
|
| 115 | - { |
|
| 116 | - // Automatically fetch Lang objects for people who store translated options lists |
|
| 117 | - // Same of unfetched queries |
|
| 118 | - if (!$query instanceof Collection) { |
|
| 119 | - if ((is_object($query) || is_string($query)) && method_exists($query, 'get')) { |
|
| 120 | - $query = $query->get(); |
|
| 121 | - } |
|
| 122 | - if (!is_array($query)) { |
|
| 123 | - $query = (array) $query; |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - //Convert parametrs of old format to new format |
|
| 128 | - if (!is_callable($text)) { |
|
| 129 | - $optionTextValue = $text; |
|
| 130 | - $text = function ($model) use ($optionTextValue) { |
|
| 131 | - if ($optionTextValue and isset($model->$optionTextValue)) { |
|
| 132 | - return $model->$optionTextValue; |
|
| 133 | - } elseif ((is_object($model) || is_string($model)) && method_exists($model, '__toString')) { |
|
| 134 | - return $model->__toString(); |
|
| 135 | - } else { |
|
| 136 | - return null; |
|
| 137 | - } |
|
| 138 | - }; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if (!is_array($attributes)) { |
|
| 142 | - if (is_string($attributes)) { |
|
| 143 | - $attributes = ['value' => $attributes]; |
|
| 144 | - } else { |
|
| 145 | - $attributes = ['value' => null]; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if (!isset($attributes['value'])) { |
|
| 150 | - $attributes['value'] = null; |
|
| 151 | - } |
|
| 152 | - //------------------------------------------------- |
|
| 153 | - |
|
| 154 | - // Populates the new options |
|
| 155 | - foreach ($query as $model) { |
|
| 156 | - // If it's an array, convert to object |
|
| 157 | - if (is_array($model)) { |
|
| 158 | - $model = (object) $model; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Calculate option text |
|
| 162 | - $optionText = $text($model); |
|
| 163 | - |
|
| 164 | - // Skip if no text value found |
|
| 165 | - if (!$optionText) { |
|
| 166 | - continue; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - //Collect option attributes |
|
| 170 | - foreach ($attributes as $optionAttributeName => $modelAttributeName) { |
|
| 171 | - if (is_callable($modelAttributeName)) { |
|
| 172 | - $optionAttributeValue = $modelAttributeName($model); |
|
| 173 | - } elseif ($modelAttributeName and isset($model->$modelAttributeName)) { |
|
| 174 | - $optionAttributeValue = $model->$modelAttributeName; |
|
| 175 | - } elseif($optionAttributeName === 'value') { |
|
| 176 | - //For backward compatibility |
|
| 177 | - if (method_exists($model, 'getKey')) { |
|
| 178 | - $optionAttributeValue = $model->getKey(); |
|
| 179 | - } elseif (isset($model->id)) { |
|
| 180 | - $optionAttributeValue = $model->id; |
|
| 181 | - } else { |
|
| 182 | - $optionAttributeValue = $optionText; |
|
| 183 | - } |
|
| 184 | - } else { |
|
| 185 | - $optionAttributeValue = ''; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - //For backward compatibility |
|
| 189 | - if (count($attributes) === 1) { |
|
| 190 | - $array[$optionAttributeValue] = (string) $optionText; |
|
| 191 | - } else { |
|
| 192 | - $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue; |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return !empty($array) ? $array : $query; |
|
| 198 | - } |
|
| 12 | + /** |
|
| 13 | + * The IoC Container |
|
| 14 | + * |
|
| 15 | + * @var Container |
|
| 16 | + */ |
|
| 17 | + protected static $app; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Bind a Container to the Helpers class |
|
| 21 | + * |
|
| 22 | + * @param Container $app |
|
| 23 | + */ |
|
| 24 | + public static function setApp(Container $app) |
|
| 25 | + { |
|
| 26 | + static::$app = $app; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Encodes HTML |
|
| 31 | + * |
|
| 32 | + * @param string $value The string to encode |
|
| 33 | + * |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public static function encode($value) |
|
| 37 | + { |
|
| 38 | + return htmlentities($value, ENT_QUOTES, 'UTF-8', true); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + //////////////////////////////////////////////////////////////////// |
|
| 42 | + ///////////////////////// LOCALIZATION HELPERS ///////////////////// |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Translates a string by trying several fallbacks |
|
| 47 | + * |
|
| 48 | + * @param string $key The key to translate |
|
| 49 | + * @param string $fallback The ultimate fallback |
|
| 50 | + * |
|
| 51 | + * @return string A translated string |
|
| 52 | + */ |
|
| 53 | + public static function translate($key, $fallback = null) |
|
| 54 | + { |
|
| 55 | + // If nothing was given, return nothing, bitch |
|
| 56 | + if (!$key) { |
|
| 57 | + return null; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + // If no fallback, use the key |
|
| 61 | + if (!$fallback) { |
|
| 62 | + $fallback = $key; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Assure we don't already have a Lang object |
|
| 66 | + if (is_object($key) and method_exists($key, 'get')) { |
|
| 67 | + return $key->get(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $translation = null; |
|
| 71 | + $translateFrom = static::$app['former']->getOption('translate_from'); |
|
| 72 | + if (substr($translateFrom, -1) !== '/') { |
|
| 73 | + $translateFrom .= '.'; |
|
| 74 | + } |
|
| 75 | + $translateFrom .= $key; |
|
| 76 | + |
|
| 77 | + // Convert a[b[c]] to a.b.c for nested translations [a => [b => 'B!']] |
|
| 78 | + if (strpos($translateFrom, ']') !== false) { |
|
| 79 | + $translateFrom = str_replace(array(']', '['), array('', '.'), $translateFrom); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // Search for the key itself, if it is valid |
|
| 83 | + $validKey = preg_match("/^[a-z0-9_-]+([.][a-z0-9 _-]+)+$/i", $key) === 1; |
|
| 84 | + if ($validKey && static::$app['translator']->has($key)) { |
|
| 85 | + $translation = static::$app['translator']->get($key); |
|
| 86 | + } elseif (static::$app['translator']->has($translateFrom)) { |
|
| 87 | + $translation = static::$app['translator']->get($translateFrom); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // Replace by fallback if invalid |
|
| 91 | + if (!$translation or is_array($translation)) { |
|
| 92 | + $translation = $fallback; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Capitalize |
|
| 96 | + $capitalize = static::$app['former']->getOption('capitalize_translations'); |
|
| 97 | + |
|
| 98 | + return $capitalize ? ucfirst($translation) : $translation; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + //////////////////////////////////////////////////////////////////// |
|
| 102 | + ////////////////////////// DATABASE HELPERS //////////////////////// |
|
| 103 | + //////////////////////////////////////////////////////////////////// |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Transforms an array of models into an associative array |
|
| 107 | + * |
|
| 108 | + * @param array|object $query The array of results |
|
| 109 | + * @param string|function $text The value to use as text |
|
| 110 | + * @param string|array $attributes The data to use as attributes |
|
| 111 | + * |
|
| 112 | + * @return array A data array |
|
| 113 | + */ |
|
| 114 | + public static function queryToArray($query, $text = null, $attributes = null) |
|
| 115 | + { |
|
| 116 | + // Automatically fetch Lang objects for people who store translated options lists |
|
| 117 | + // Same of unfetched queries |
|
| 118 | + if (!$query instanceof Collection) { |
|
| 119 | + if ((is_object($query) || is_string($query)) && method_exists($query, 'get')) { |
|
| 120 | + $query = $query->get(); |
|
| 121 | + } |
|
| 122 | + if (!is_array($query)) { |
|
| 123 | + $query = (array) $query; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + //Convert parametrs of old format to new format |
|
| 128 | + if (!is_callable($text)) { |
|
| 129 | + $optionTextValue = $text; |
|
| 130 | + $text = function ($model) use ($optionTextValue) { |
|
| 131 | + if ($optionTextValue and isset($model->$optionTextValue)) { |
|
| 132 | + return $model->$optionTextValue; |
|
| 133 | + } elseif ((is_object($model) || is_string($model)) && method_exists($model, '__toString')) { |
|
| 134 | + return $model->__toString(); |
|
| 135 | + } else { |
|
| 136 | + return null; |
|
| 137 | + } |
|
| 138 | + }; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if (!is_array($attributes)) { |
|
| 142 | + if (is_string($attributes)) { |
|
| 143 | + $attributes = ['value' => $attributes]; |
|
| 144 | + } else { |
|
| 145 | + $attributes = ['value' => null]; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if (!isset($attributes['value'])) { |
|
| 150 | + $attributes['value'] = null; |
|
| 151 | + } |
|
| 152 | + //------------------------------------------------- |
|
| 153 | + |
|
| 154 | + // Populates the new options |
|
| 155 | + foreach ($query as $model) { |
|
| 156 | + // If it's an array, convert to object |
|
| 157 | + if (is_array($model)) { |
|
| 158 | + $model = (object) $model; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Calculate option text |
|
| 162 | + $optionText = $text($model); |
|
| 163 | + |
|
| 164 | + // Skip if no text value found |
|
| 165 | + if (!$optionText) { |
|
| 166 | + continue; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + //Collect option attributes |
|
| 170 | + foreach ($attributes as $optionAttributeName => $modelAttributeName) { |
|
| 171 | + if (is_callable($modelAttributeName)) { |
|
| 172 | + $optionAttributeValue = $modelAttributeName($model); |
|
| 173 | + } elseif ($modelAttributeName and isset($model->$modelAttributeName)) { |
|
| 174 | + $optionAttributeValue = $model->$modelAttributeName; |
|
| 175 | + } elseif($optionAttributeName === 'value') { |
|
| 176 | + //For backward compatibility |
|
| 177 | + if (method_exists($model, 'getKey')) { |
|
| 178 | + $optionAttributeValue = $model->getKey(); |
|
| 179 | + } elseif (isset($model->id)) { |
|
| 180 | + $optionAttributeValue = $model->id; |
|
| 181 | + } else { |
|
| 182 | + $optionAttributeValue = $optionText; |
|
| 183 | + } |
|
| 184 | + } else { |
|
| 185 | + $optionAttributeValue = ''; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + //For backward compatibility |
|
| 189 | + if (count($attributes) === 1) { |
|
| 190 | + $array[$optionAttributeValue] = (string) $optionText; |
|
| 191 | + } else { |
|
| 192 | + $array[$optionText][$optionAttributeName] = (string) $optionAttributeValue; |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return !empty($array) ? $array : $query; |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | //Convert parametrs of old format to new format |
| 128 | 128 | if (!is_callable($text)) { |
| 129 | 129 | $optionTextValue = $text; |
| 130 | - $text = function ($model) use ($optionTextValue) { |
|
| 130 | + $text = function($model) use ($optionTextValue) { |
|
| 131 | 131 | if ($optionTextValue and isset($model->$optionTextValue)) { |
| 132 | 132 | return $model->$optionTextValue; |
| 133 | 133 | } elseif ((is_object($model) || is_string($model)) && method_exists($model, '__toString')) { |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | $optionAttributeValue = $modelAttributeName($model); |
| 173 | 173 | } elseif ($modelAttributeName and isset($model->$modelAttributeName)) { |
| 174 | 174 | $optionAttributeValue = $model->$modelAttributeName; |
| 175 | - } elseif($optionAttributeName === 'value') { |
|
| 175 | + } elseif ($optionAttributeName === 'value') { |
|
| 176 | 176 | //For backward compatibility |
| 177 | 177 | if (method_exists($model, 'getKey')) { |
| 178 | 178 | $optionAttributeValue = $model->getKey(); |
@@ -9,31 +9,31 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Plaintext extends Field |
| 11 | 11 | { |
| 12 | - //////////////////////////////////////////////////////////////////// |
|
| 13 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 14 | - //////////////////////////////////////////////////////////////////// |
|
| 12 | + //////////////////////////////////////////////////////////////////// |
|
| 13 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 14 | + //////////////////////////////////////////////////////////////////// |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Prints out the current tag |
|
| 18 | - * |
|
| 19 | - * @return string A plain text tag |
|
| 20 | - */ |
|
| 21 | - public function render() |
|
| 22 | - { |
|
| 23 | - $this->addClass($this->app['former.framework']->getPlainTextClasses()); |
|
| 24 | - $this->setId(); |
|
| 25 | - if ($this->app['former']->getOption('escape_plaintext_value', true)) { |
|
| 26 | - $this->escapeValue(); |
|
| 27 | - } |
|
| 16 | + /** |
|
| 17 | + * Prints out the current tag |
|
| 18 | + * |
|
| 19 | + * @return string A plain text tag |
|
| 20 | + */ |
|
| 21 | + public function render() |
|
| 22 | + { |
|
| 23 | + $this->addClass($this->app['former.framework']->getPlainTextClasses()); |
|
| 24 | + $this->setId(); |
|
| 25 | + if ($this->app['former']->getOption('escape_plaintext_value', true)) { |
|
| 26 | + $this->escapeValue(); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - return $this->app['former.framework']->createPlainTextField($this); |
|
| 30 | - } |
|
| 29 | + return $this->app['former.framework']->createPlainTextField($this); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - protected function escapeValue() |
|
| 33 | - { |
|
| 34 | - $valueToEscape = $this->getValue(); |
|
| 35 | - $value = is_string($valueToEscape) || $valueToEscape instanceof HtmlString ? e($valueToEscape) : $valueToEscape; |
|
| 32 | + protected function escapeValue() |
|
| 33 | + { |
|
| 34 | + $valueToEscape = $this->getValue(); |
|
| 35 | + $value = is_string($valueToEscape) || $valueToEscape instanceof HtmlString ? e($valueToEscape) : $valueToEscape; |
|
| 36 | 36 | |
| 37 | - return $this->forceValue($value); |
|
| 38 | - } |
|
| 37 | + return $this->forceValue($value); |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -352,7 +352,7 @@ discard block |
||
| 352 | 352 | * @param string $text |
| 353 | 353 | * @param array $attributes |
| 354 | 354 | * |
| 355 | - * @return string |
|
| 355 | + * @return Element |
|
| 356 | 356 | */ |
| 357 | 357 | public function createValidationError($text, $attributes = array()) |
| 358 | 358 | { |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | /** |
| 409 | 409 | * Wrap an item to be prepended or appended to the current field |
| 410 | 410 | * |
| 411 | - * @return Element A wrapped item |
|
| 411 | + * @return string A wrapped item |
|
| 412 | 412 | */ |
| 413 | 413 | public function placeAround($item, $place = null) |
| 414 | 414 | { |
@@ -139,7 +139,7 @@ |
||
| 139 | 139 | $classes = array_intersect($classes, $this->fields); |
| 140 | 140 | |
| 141 | 141 | // Prepend field type |
| 142 | - $classes = array_map(function ($class) { |
|
| 142 | + $classes = array_map(function($class) { |
|
| 143 | 143 | return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
| 144 | 144 | }, $classes); |
| 145 | 145 | |
@@ -13,474 +13,474 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap5 extends Framework implements FrameworkInterface |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Form types that trigger special styling for this Framework |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'lg', |
|
| 30 | - 'sm', |
|
| 31 | - 'xs', |
|
| 32 | - 'block', |
|
| 33 | - 'link', |
|
| 34 | - 'primary', |
|
| 35 | - 'secondary', |
|
| 36 | - 'warning', |
|
| 37 | - 'danger', |
|
| 38 | - 'success', |
|
| 39 | - 'info', |
|
| 40 | - 'light', |
|
| 41 | - 'dark', |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The field sizes available |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - private $fields = array( |
|
| 50 | - 'lg', |
|
| 51 | - 'sm', |
|
| 52 | - // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | - // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | - // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | - // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | - // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | - // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | - // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | - // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | - ); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The field states available |
|
| 64 | - * |
|
| 65 | - * @var array |
|
| 66 | - */ |
|
| 67 | - protected $states = array( |
|
| 68 | - 'is-invalid', |
|
| 69 | - ); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The default HTML tag used for icons |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $iconTag = 'i'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * The default set for icon fonts |
|
| 80 | - * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | - * |
|
| 82 | - * @var string |
|
| 83 | - */ |
|
| 84 | - protected $iconSet = 'fa'; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * The default prefix icon names |
|
| 88 | - * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | - * |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 92 | - protected $iconPrefix = 'fa'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Create a new TwitterBootstrap instance |
|
| 96 | - * |
|
| 97 | - * @param \Illuminate\Container\Container $app |
|
| 98 | - */ |
|
| 99 | - public function __construct(Container $app) |
|
| 100 | - { |
|
| 101 | - $this->app = $app; |
|
| 102 | - $this->setFrameworkDefaults(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - //////////////////////////////////////////////////////////////////// |
|
| 106 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Filter buttons classes |
|
| 111 | - * |
|
| 112 | - * @param array $classes An array of classes |
|
| 113 | - * |
|
| 114 | - * @return string[] A filtered array |
|
| 115 | - */ |
|
| 116 | - public function filterButtonClasses($classes) |
|
| 117 | - { |
|
| 118 | - // Filter classes |
|
| 119 | - // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | - |
|
| 121 | - // Prepend button type |
|
| 122 | - $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | - $classes[] = 'btn'; |
|
| 124 | - |
|
| 125 | - return $classes; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Filter field classes |
|
| 130 | - * |
|
| 131 | - * @param array $classes An array of classes |
|
| 132 | - * |
|
| 133 | - * @return array A filtered array |
|
| 134 | - */ |
|
| 135 | - public function filterFieldClasses($classes) |
|
| 136 | - { |
|
| 137 | - // Filter classes |
|
| 138 | - $classes = array_intersect($classes, $this->fields); |
|
| 139 | - |
|
| 140 | - // Prepend field type |
|
| 141 | - $classes = array_map(function ($class) { |
|
| 142 | - return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | - }, $classes); |
|
| 144 | - |
|
| 145 | - return $classes; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | - //////////////////////////////////////////////////////////////////// |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Framework error state |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function errorState() |
|
| 158 | - { |
|
| 159 | - return 'is-invalid'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns corresponding inline class of a field |
|
| 164 | - * |
|
| 165 | - * @param Field $field |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function getInlineLabelClass($field) |
|
| 170 | - { |
|
| 171 | - $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | - if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | - $inlineClass = 'form-check-label'; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $inlineClass; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Set the fields width from a label width |
|
| 181 | - * |
|
| 182 | - * @param array $labelWidths |
|
| 183 | - */ |
|
| 184 | - protected function setFieldWidths($labelWidths) |
|
| 185 | - { |
|
| 186 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | - |
|
| 188 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | - if ($viewport) { |
|
| 191 | - $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | - $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | - $fieldOffsetClass .= " offset-$viewports[$viewport]-$columns"; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | - $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | - $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - //////////////////////////////////////////////////////////////////// |
|
| 203 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Add classes to a field |
|
| 208 | - * |
|
| 209 | - * @param Field $field |
|
| 210 | - * @param array $classes The possible classes to add |
|
| 211 | - * |
|
| 212 | - * @return Field |
|
| 213 | - */ |
|
| 214 | - public function getFieldClasses(Field $field, $classes) |
|
| 215 | - { |
|
| 216 | - // Add inline class for checkables |
|
| 217 | - if ($field->isCheckable()) { |
|
| 218 | - // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | - $field->addClass('form-check-input'); |
|
| 220 | - $classes[] = 'form-check'; |
|
| 221 | - |
|
| 222 | - if (in_array('inline', $classes)) { |
|
| 223 | - $field->inline(); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Filter classes according to field type |
|
| 228 | - if ($field->isButton()) { |
|
| 229 | - $classes = $this->filterButtonClasses($classes); |
|
| 230 | - } else { |
|
| 231 | - $classes = $this->filterFieldClasses($classes); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Add form-control class for text-type, textarea and file fields |
|
| 235 | - // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | - if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [ |
|
| 237 | - 'plaintext', |
|
| 238 | - 'select', |
|
| 239 | - ]) && !in_array('form-control', $classes) |
|
| 240 | - ) { |
|
| 241 | - $classes[] = 'form-control'; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // Add form-select class for select fields |
|
| 245 | - if ($field->getType() === 'select' && !in_array('form-select', $classes)) { |
|
| 246 | - $classes[] = 'form-select'; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ($this->app['former']->getErrors($field->getName())) { |
|
| 250 | - $classes[] = $this->errorState(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this->addClassesToField($field, $classes); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Add group classes |
|
| 258 | - * |
|
| 259 | - * @return string A list of group classes |
|
| 260 | - */ |
|
| 261 | - public function getGroupClasses() |
|
| 262 | - { |
|
| 263 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 264 | - return 'mb-3 row'; |
|
| 265 | - } else { |
|
| 266 | - return 'mb-3'; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Add label classes |
|
| 272 | - * |
|
| 273 | - * @return string[] An array of attributes with the label class |
|
| 274 | - */ |
|
| 275 | - public function getLabelClasses() |
|
| 276 | - { |
|
| 277 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | - return array('col-form-label', $this->labelWidth); |
|
| 279 | - } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 280 | - return array('visually-hidden'); |
|
| 281 | - } else { |
|
| 282 | - return array('form-label'); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Add uneditable field classes |
|
| 288 | - * |
|
| 289 | - * @return string An array of attributes with the uneditable class |
|
| 290 | - */ |
|
| 291 | - public function getUneditableClasses() |
|
| 292 | - { |
|
| 293 | - return ''; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Add plain text field classes |
|
| 298 | - * |
|
| 299 | - * @return string An array of attributes with the plain text class |
|
| 300 | - */ |
|
| 301 | - public function getPlainTextClasses() |
|
| 302 | - { |
|
| 303 | - return 'form-control-plaintext'; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Add form class |
|
| 308 | - * |
|
| 309 | - * @param string $type The type of form to add |
|
| 310 | - * |
|
| 311 | - * @return string|null |
|
| 312 | - */ |
|
| 313 | - public function getFormClasses($type) |
|
| 314 | - { |
|
| 315 | - return $type ? 'form-'.$type : null; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Add actions block class |
|
| 320 | - * |
|
| 321 | - * @return string|null |
|
| 322 | - */ |
|
| 323 | - public function getActionClasses() |
|
| 324 | - { |
|
| 325 | - if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 326 | - return 'mb-3 row'; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - return null; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - //////////////////////////////////////////////////////////////////// |
|
| 333 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 334 | - //////////////////////////////////////////////////////////////////// |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Render an help text |
|
| 338 | - * |
|
| 339 | - * @param string $text |
|
| 340 | - * @param array $attributes |
|
| 341 | - * |
|
| 342 | - * @return Element |
|
| 343 | - */ |
|
| 344 | - public function createHelp($text, $attributes = array()) |
|
| 345 | - { |
|
| 346 | - return Element::create('span', $text, $attributes)->addClass('form-text'); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Render an validation error text |
|
| 351 | - * |
|
| 352 | - * @param string $text |
|
| 353 | - * @param array $attributes |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - */ |
|
| 357 | - public function createValidationError($text, $attributes = array()) |
|
| 358 | - { |
|
| 359 | - return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Render an help text |
|
| 364 | - * |
|
| 365 | - * @param string $text |
|
| 366 | - * @param array $attributes |
|
| 367 | - * |
|
| 368 | - * @return Element |
|
| 369 | - */ |
|
| 370 | - public function createBlockHelp($text, $attributes = array()) |
|
| 371 | - { |
|
| 372 | - return Element::create('div', $text, $attributes)->addClass('form-text'); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Render a disabled field |
|
| 377 | - * |
|
| 378 | - * @param Field $field |
|
| 379 | - * |
|
| 380 | - * @return Element |
|
| 381 | - */ |
|
| 382 | - public function createDisabledField(Field $field) |
|
| 383 | - { |
|
| 384 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Render a plain text field |
|
| 389 | - * |
|
| 390 | - * @param Field $field |
|
| 391 | - * |
|
| 392 | - * @return Element |
|
| 393 | - */ |
|
| 394 | - public function createPlainTextField(Field $field) |
|
| 395 | - { |
|
| 396 | - $label = $field->getLabel(); |
|
| 397 | - if ($label) { |
|
| 398 | - $label->for(''); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - //////////////////////////////////////////////////////////////////// |
|
| 405 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 406 | - //////////////////////////////////////////////////////////////////// |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Wrap an item to be prepended or appended to the current field |
|
| 410 | - * |
|
| 411 | - * @return Element A wrapped item |
|
| 412 | - */ |
|
| 413 | - public function placeAround($item, $place = null) |
|
| 414 | - { |
|
| 415 | - // Render object |
|
| 416 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 417 | - $item = $item->__toString(); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - $items = (array) $item; |
|
| 421 | - $element = ''; |
|
| 422 | - foreach ($items as $item) { |
|
| 423 | - $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 424 | - |
|
| 425 | - // Get class to use |
|
| 426 | - $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 427 | - |
|
| 428 | - $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - return $element; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Wrap a field with prepended and appended items |
|
| 436 | - * |
|
| 437 | - * @param Field $field |
|
| 438 | - * @param array $prepend |
|
| 439 | - * @param array $append |
|
| 440 | - * |
|
| 441 | - * @return string A field concatented with prepended and/or appended items |
|
| 442 | - */ |
|
| 443 | - public function prependAppend($field, $prepend, $append) |
|
| 444 | - { |
|
| 445 | - $return = '<div class="input-group">'; |
|
| 446 | - $return .= join(null, $prepend); |
|
| 447 | - $return .= $field->render(); |
|
| 448 | - $return .= join(null, $append); |
|
| 449 | - $return .= '</div>'; |
|
| 450 | - |
|
| 451 | - return $return; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Wrap a field with potential additional tags |
|
| 456 | - * |
|
| 457 | - * @param Field $field |
|
| 458 | - * |
|
| 459 | - * @return Element A wrapped field |
|
| 460 | - */ |
|
| 461 | - public function wrapField($field) |
|
| 462 | - { |
|
| 463 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 464 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - return $field; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Wrap actions block with potential additional tags |
|
| 472 | - * |
|
| 473 | - * @param Actions $actions |
|
| 474 | - * |
|
| 475 | - * @return string A wrapped actions block |
|
| 476 | - */ |
|
| 477 | - public function wrapActions($actions) |
|
| 478 | - { |
|
| 479 | - // For horizontal forms, we wrap the actions in a div |
|
| 480 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 481 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - return $actions; |
|
| 485 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'lg', |
|
| 30 | + 'sm', |
|
| 31 | + 'xs', |
|
| 32 | + 'block', |
|
| 33 | + 'link', |
|
| 34 | + 'primary', |
|
| 35 | + 'secondary', |
|
| 36 | + 'warning', |
|
| 37 | + 'danger', |
|
| 38 | + 'success', |
|
| 39 | + 'info', |
|
| 40 | + 'light', |
|
| 41 | + 'dark', |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The field sizes available |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + private $fields = array( |
|
| 50 | + 'lg', |
|
| 51 | + 'sm', |
|
| 52 | + // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | + // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | + // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | + // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | + // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | + // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | + // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | + // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | + ); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The field states available |
|
| 64 | + * |
|
| 65 | + * @var array |
|
| 66 | + */ |
|
| 67 | + protected $states = array( |
|
| 68 | + 'is-invalid', |
|
| 69 | + ); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The default HTML tag used for icons |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $iconTag = 'i'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * The default set for icon fonts |
|
| 80 | + * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | + * |
|
| 82 | + * @var string |
|
| 83 | + */ |
|
| 84 | + protected $iconSet = 'fa'; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * The default prefix icon names |
|
| 88 | + * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | + * |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | + protected $iconPrefix = 'fa'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Create a new TwitterBootstrap instance |
|
| 96 | + * |
|
| 97 | + * @param \Illuminate\Container\Container $app |
|
| 98 | + */ |
|
| 99 | + public function __construct(Container $app) |
|
| 100 | + { |
|
| 101 | + $this->app = $app; |
|
| 102 | + $this->setFrameworkDefaults(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + //////////////////////////////////////////////////////////////////// |
|
| 106 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Filter buttons classes |
|
| 111 | + * |
|
| 112 | + * @param array $classes An array of classes |
|
| 113 | + * |
|
| 114 | + * @return string[] A filtered array |
|
| 115 | + */ |
|
| 116 | + public function filterButtonClasses($classes) |
|
| 117 | + { |
|
| 118 | + // Filter classes |
|
| 119 | + // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | + |
|
| 121 | + // Prepend button type |
|
| 122 | + $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | + $classes[] = 'btn'; |
|
| 124 | + |
|
| 125 | + return $classes; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Filter field classes |
|
| 130 | + * |
|
| 131 | + * @param array $classes An array of classes |
|
| 132 | + * |
|
| 133 | + * @return array A filtered array |
|
| 134 | + */ |
|
| 135 | + public function filterFieldClasses($classes) |
|
| 136 | + { |
|
| 137 | + // Filter classes |
|
| 138 | + $classes = array_intersect($classes, $this->fields); |
|
| 139 | + |
|
| 140 | + // Prepend field type |
|
| 141 | + $classes = array_map(function ($class) { |
|
| 142 | + return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | + }, $classes); |
|
| 144 | + |
|
| 145 | + return $classes; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | + //////////////////////////////////////////////////////////////////// |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Framework error state |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function errorState() |
|
| 158 | + { |
|
| 159 | + return 'is-invalid'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns corresponding inline class of a field |
|
| 164 | + * |
|
| 165 | + * @param Field $field |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function getInlineLabelClass($field) |
|
| 170 | + { |
|
| 171 | + $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | + if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | + $inlineClass = 'form-check-label'; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $inlineClass; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Set the fields width from a label width |
|
| 181 | + * |
|
| 182 | + * @param array $labelWidths |
|
| 183 | + */ |
|
| 184 | + protected function setFieldWidths($labelWidths) |
|
| 185 | + { |
|
| 186 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | + |
|
| 188 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | + if ($viewport) { |
|
| 191 | + $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | + $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | + $fieldOffsetClass .= " offset-$viewports[$viewport]-$columns"; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | + $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | + $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + //////////////////////////////////////////////////////////////////// |
|
| 203 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Add classes to a field |
|
| 208 | + * |
|
| 209 | + * @param Field $field |
|
| 210 | + * @param array $classes The possible classes to add |
|
| 211 | + * |
|
| 212 | + * @return Field |
|
| 213 | + */ |
|
| 214 | + public function getFieldClasses(Field $field, $classes) |
|
| 215 | + { |
|
| 216 | + // Add inline class for checkables |
|
| 217 | + if ($field->isCheckable()) { |
|
| 218 | + // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | + $field->addClass('form-check-input'); |
|
| 220 | + $classes[] = 'form-check'; |
|
| 221 | + |
|
| 222 | + if (in_array('inline', $classes)) { |
|
| 223 | + $field->inline(); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Filter classes according to field type |
|
| 228 | + if ($field->isButton()) { |
|
| 229 | + $classes = $this->filterButtonClasses($classes); |
|
| 230 | + } else { |
|
| 231 | + $classes = $this->filterFieldClasses($classes); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Add form-control class for text-type, textarea and file fields |
|
| 235 | + // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | + if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [ |
|
| 237 | + 'plaintext', |
|
| 238 | + 'select', |
|
| 239 | + ]) && !in_array('form-control', $classes) |
|
| 240 | + ) { |
|
| 241 | + $classes[] = 'form-control'; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // Add form-select class for select fields |
|
| 245 | + if ($field->getType() === 'select' && !in_array('form-select', $classes)) { |
|
| 246 | + $classes[] = 'form-select'; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ($this->app['former']->getErrors($field->getName())) { |
|
| 250 | + $classes[] = $this->errorState(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this->addClassesToField($field, $classes); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Add group classes |
|
| 258 | + * |
|
| 259 | + * @return string A list of group classes |
|
| 260 | + */ |
|
| 261 | + public function getGroupClasses() |
|
| 262 | + { |
|
| 263 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 264 | + return 'mb-3 row'; |
|
| 265 | + } else { |
|
| 266 | + return 'mb-3'; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Add label classes |
|
| 272 | + * |
|
| 273 | + * @return string[] An array of attributes with the label class |
|
| 274 | + */ |
|
| 275 | + public function getLabelClasses() |
|
| 276 | + { |
|
| 277 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | + return array('col-form-label', $this->labelWidth); |
|
| 279 | + } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 280 | + return array('visually-hidden'); |
|
| 281 | + } else { |
|
| 282 | + return array('form-label'); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Add uneditable field classes |
|
| 288 | + * |
|
| 289 | + * @return string An array of attributes with the uneditable class |
|
| 290 | + */ |
|
| 291 | + public function getUneditableClasses() |
|
| 292 | + { |
|
| 293 | + return ''; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Add plain text field classes |
|
| 298 | + * |
|
| 299 | + * @return string An array of attributes with the plain text class |
|
| 300 | + */ |
|
| 301 | + public function getPlainTextClasses() |
|
| 302 | + { |
|
| 303 | + return 'form-control-plaintext'; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Add form class |
|
| 308 | + * |
|
| 309 | + * @param string $type The type of form to add |
|
| 310 | + * |
|
| 311 | + * @return string|null |
|
| 312 | + */ |
|
| 313 | + public function getFormClasses($type) |
|
| 314 | + { |
|
| 315 | + return $type ? 'form-'.$type : null; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Add actions block class |
|
| 320 | + * |
|
| 321 | + * @return string|null |
|
| 322 | + */ |
|
| 323 | + public function getActionClasses() |
|
| 324 | + { |
|
| 325 | + if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 326 | + return 'mb-3 row'; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + return null; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + //////////////////////////////////////////////////////////////////// |
|
| 333 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 334 | + //////////////////////////////////////////////////////////////////// |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Render an help text |
|
| 338 | + * |
|
| 339 | + * @param string $text |
|
| 340 | + * @param array $attributes |
|
| 341 | + * |
|
| 342 | + * @return Element |
|
| 343 | + */ |
|
| 344 | + public function createHelp($text, $attributes = array()) |
|
| 345 | + { |
|
| 346 | + return Element::create('span', $text, $attributes)->addClass('form-text'); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Render an validation error text |
|
| 351 | + * |
|
| 352 | + * @param string $text |
|
| 353 | + * @param array $attributes |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + */ |
|
| 357 | + public function createValidationError($text, $attributes = array()) |
|
| 358 | + { |
|
| 359 | + return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Render an help text |
|
| 364 | + * |
|
| 365 | + * @param string $text |
|
| 366 | + * @param array $attributes |
|
| 367 | + * |
|
| 368 | + * @return Element |
|
| 369 | + */ |
|
| 370 | + public function createBlockHelp($text, $attributes = array()) |
|
| 371 | + { |
|
| 372 | + return Element::create('div', $text, $attributes)->addClass('form-text'); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Render a disabled field |
|
| 377 | + * |
|
| 378 | + * @param Field $field |
|
| 379 | + * |
|
| 380 | + * @return Element |
|
| 381 | + */ |
|
| 382 | + public function createDisabledField(Field $field) |
|
| 383 | + { |
|
| 384 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Render a plain text field |
|
| 389 | + * |
|
| 390 | + * @param Field $field |
|
| 391 | + * |
|
| 392 | + * @return Element |
|
| 393 | + */ |
|
| 394 | + public function createPlainTextField(Field $field) |
|
| 395 | + { |
|
| 396 | + $label = $field->getLabel(); |
|
| 397 | + if ($label) { |
|
| 398 | + $label->for(''); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + //////////////////////////////////////////////////////////////////// |
|
| 405 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 406 | + //////////////////////////////////////////////////////////////////// |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Wrap an item to be prepended or appended to the current field |
|
| 410 | + * |
|
| 411 | + * @return Element A wrapped item |
|
| 412 | + */ |
|
| 413 | + public function placeAround($item, $place = null) |
|
| 414 | + { |
|
| 415 | + // Render object |
|
| 416 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 417 | + $item = $item->__toString(); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + $items = (array) $item; |
|
| 421 | + $element = ''; |
|
| 422 | + foreach ($items as $item) { |
|
| 423 | + $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 424 | + |
|
| 425 | + // Get class to use |
|
| 426 | + $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 427 | + |
|
| 428 | + $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + return $element; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Wrap a field with prepended and appended items |
|
| 436 | + * |
|
| 437 | + * @param Field $field |
|
| 438 | + * @param array $prepend |
|
| 439 | + * @param array $append |
|
| 440 | + * |
|
| 441 | + * @return string A field concatented with prepended and/or appended items |
|
| 442 | + */ |
|
| 443 | + public function prependAppend($field, $prepend, $append) |
|
| 444 | + { |
|
| 445 | + $return = '<div class="input-group">'; |
|
| 446 | + $return .= join(null, $prepend); |
|
| 447 | + $return .= $field->render(); |
|
| 448 | + $return .= join(null, $append); |
|
| 449 | + $return .= '</div>'; |
|
| 450 | + |
|
| 451 | + return $return; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Wrap a field with potential additional tags |
|
| 456 | + * |
|
| 457 | + * @param Field $field |
|
| 458 | + * |
|
| 459 | + * @return Element A wrapped field |
|
| 460 | + */ |
|
| 461 | + public function wrapField($field) |
|
| 462 | + { |
|
| 463 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 464 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + return $field; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Wrap actions block with potential additional tags |
|
| 472 | + * |
|
| 473 | + * @param Actions $actions |
|
| 474 | + * |
|
| 475 | + * @return string A wrapped actions block |
|
| 476 | + */ |
|
| 477 | + public function wrapActions($actions) |
|
| 478 | + { |
|
| 479 | + // For horizontal forms, we wrap the actions in a div |
|
| 480 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 481 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + return $actions; |
|
| 485 | + } |
|
| 486 | 486 | } |
@@ -1,240 +1,240 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | 2 | |
| 3 | - // Markup |
|
| 4 | - //////////////////////////////////////////////////////////////////// |
|
| 5 | - |
|
| 6 | - // Whether labels should be automatically computed from name |
|
| 7 | - 'automatic_label' => true, |
|
| 8 | - |
|
| 9 | - // The default form type |
|
| 10 | - 'default_form_type' => 'horizontal', |
|
| 11 | - |
|
| 12 | - // Whether Former should escape HTML tags of 'plaintext' fields value |
|
| 13 | - // Enabled by default |
|
| 14 | - // |
|
| 15 | - // Instead of disabled this option, you should use the 'HtmlString' class: |
|
| 16 | - // Former::plaintext('text') |
|
| 17 | - // ->forceValue( |
|
| 18 | - // new Illuminate\Support\HtmlString('<b>your HTML data</b>') |
|
| 19 | - // ) |
|
| 20 | - 'escape_plaintext_value' => true, |
|
| 21 | - |
|
| 22 | - // Validation |
|
| 23 | - //////////////////////////////////////////////////////////////////// |
|
| 24 | - |
|
| 25 | - // Whether Former should fetch errors from Session |
|
| 26 | - 'fetch_errors' => true, |
|
| 27 | - |
|
| 28 | - // Whether Former should try to apply Validator rules as attributes |
|
| 29 | - 'live_validation' => true, |
|
| 30 | - |
|
| 31 | - // Whether Former should automatically fetch error messages and |
|
| 32 | - // display them next to the matching fields |
|
| 33 | - 'error_messages' => true, |
|
| 34 | - |
|
| 35 | - // Checkables |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - |
|
| 38 | - // Whether checkboxes should always be present in the POST data, |
|
| 39 | - // no matter if you checked them or not |
|
| 40 | - 'push_checkboxes' => false, |
|
| 41 | - |
|
| 42 | - // The value a checkbox will have in the POST array if unchecked |
|
| 43 | - 'unchecked_value' => 0, |
|
| 44 | - |
|
| 45 | - // Required fields |
|
| 46 | - //////////////////////////////////////////////////////////////////// |
|
| 47 | - |
|
| 48 | - // The class to be added to required fields |
|
| 49 | - 'required_class' => 'required', |
|
| 50 | - |
|
| 51 | - // A facultative text to append to the labels of required fields |
|
| 52 | - 'required_text' => '<sup>*</sup>', |
|
| 53 | - |
|
| 54 | - // Translations |
|
| 55 | - //////////////////////////////////////////////////////////////////// |
|
| 56 | - |
|
| 57 | - // Where Former should look for translations |
|
| 58 | - 'translate_from' => 'validation.attributes', |
|
| 59 | - |
|
| 60 | - // Whether text that comes out of the translated |
|
| 61 | - // should be capitalized (ex: email => Email) automatically |
|
| 62 | - 'capitalize_translations' => true, |
|
| 63 | - |
|
| 64 | - // An array of attributes to automatically translate |
|
| 65 | - 'translatable' => array( |
|
| 66 | - 'help', |
|
| 67 | - 'inlineHelp', |
|
| 68 | - 'blockHelp', |
|
| 69 | - 'placeholder', |
|
| 70 | - 'data_placeholder', |
|
| 71 | - 'label', |
|
| 72 | - ), |
|
| 73 | - |
|
| 74 | - // Framework |
|
| 75 | - //////////////////////////////////////////////////////////////////// |
|
| 76 | - |
|
| 77 | - // The framework to be used by Former |
|
| 78 | - 'framework' => 'TwitterBootstrap3', |
|
| 79 | - |
|
| 80 | - 'TwitterBootstrap5' => array( |
|
| 81 | - |
|
| 82 | - // Map Former-supported viewports to Bootstrap 5 equivalents |
|
| 83 | - 'viewports' => array( |
|
| 84 | - 'large' => 'lg', |
|
| 85 | - 'medium' => 'md', |
|
| 86 | - 'small' => 'sm', |
|
| 87 | - 'mini' => 'xs', |
|
| 88 | - ), |
|
| 89 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 90 | - 'labelWidths' => array( |
|
| 91 | - 'large' => 2, |
|
| 92 | - 'small' => 4, |
|
| 93 | - ), |
|
| 94 | - // HTML markup and classes used by Bootstrap 5 for icons |
|
| 95 | - 'icon' => array( |
|
| 96 | - 'tag' => 'i', |
|
| 97 | - 'set' => 'fa', |
|
| 98 | - 'prefix' => 'fa', |
|
| 99 | - ), |
|
| 100 | - |
|
| 101 | - ), |
|
| 102 | - |
|
| 103 | - 'TwitterBootstrap4' => array( |
|
| 104 | - |
|
| 105 | - // Map Former-supported viewports to Bootstrap 4 equivalents |
|
| 106 | - 'viewports' => array( |
|
| 107 | - 'large' => 'lg', |
|
| 108 | - 'medium' => 'md', |
|
| 109 | - 'small' => 'sm', |
|
| 110 | - 'mini' => 'xs', |
|
| 111 | - ), |
|
| 112 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 113 | - 'labelWidths' => array( |
|
| 114 | - 'large' => 2, |
|
| 115 | - 'small' => 4, |
|
| 116 | - ), |
|
| 117 | - // HTML markup and classes used by Bootstrap 4 for icons |
|
| 118 | - 'icon' => array( |
|
| 119 | - 'tag' => 'i', |
|
| 120 | - 'set' => 'fa', |
|
| 121 | - 'prefix' => 'fa', |
|
| 122 | - ), |
|
| 123 | - |
|
| 124 | - ), |
|
| 125 | - |
|
| 126 | - 'TwitterBootstrap3' => array( |
|
| 127 | - |
|
| 128 | - // Map Former-supported viewports to Bootstrap 3 equivalents |
|
| 129 | - 'viewports' => array( |
|
| 130 | - 'large' => 'lg', |
|
| 131 | - 'medium' => 'md', |
|
| 132 | - 'small' => 'sm', |
|
| 133 | - 'mini' => 'xs', |
|
| 134 | - ), |
|
| 135 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 136 | - 'labelWidths' => array( |
|
| 137 | - 'large' => 2, |
|
| 138 | - 'small' => 4, |
|
| 139 | - ), |
|
| 140 | - // HTML markup and classes used by Bootstrap 3 for icons |
|
| 141 | - 'icon' => array( |
|
| 142 | - 'tag' => 'span', |
|
| 143 | - 'set' => 'glyphicon', |
|
| 144 | - 'prefix' => 'glyphicon', |
|
| 145 | - ), |
|
| 146 | - |
|
| 147 | - ), |
|
| 148 | - |
|
| 149 | - 'Nude' => array( // No-framework markup |
|
| 150 | - 'icon' => array( |
|
| 151 | - 'tag' => 'i', |
|
| 152 | - 'set' => null, |
|
| 153 | - 'prefix' => 'icon', |
|
| 154 | - ), |
|
| 155 | - ), |
|
| 156 | - |
|
| 157 | - 'TwitterBootstrap' => array( // Twitter Bootstrap version 2 |
|
| 158 | - 'icon' => array( |
|
| 159 | - 'tag' => 'i', |
|
| 160 | - 'set' => null, |
|
| 161 | - 'prefix' => 'icon', |
|
| 162 | - ), |
|
| 163 | - ), |
|
| 164 | - |
|
| 165 | - 'ZurbFoundation5' => array( |
|
| 166 | - // Map Former-supported viewports to Foundation 5 equivalents |
|
| 167 | - 'viewports' => array( |
|
| 168 | - 'large' => 'large', |
|
| 169 | - 'medium' => null, |
|
| 170 | - 'small' => 'small', |
|
| 171 | - 'mini' => null, |
|
| 172 | - ), |
|
| 173 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 174 | - 'labelWidths' => array( |
|
| 175 | - 'small' => 3, |
|
| 176 | - ), |
|
| 177 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 178 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 179 | - // HTML markup and classes used by Foundation 5 for icons |
|
| 180 | - 'icon' => array( |
|
| 181 | - 'tag' => 'i', |
|
| 182 | - 'set' => null, |
|
| 183 | - 'prefix' => 'fi', |
|
| 184 | - ), |
|
| 185 | - // CSS for inline validation errors |
|
| 186 | - 'error_classes' => array('class' => 'error'), |
|
| 187 | - ), |
|
| 188 | - |
|
| 189 | - 'ZurbFoundation4' => array( |
|
| 190 | - // Foundation 4 also has an experimental "medium" breakpoint |
|
| 191 | - // explained at http://foundation.zurb.com/docs/components/grid.html |
|
| 192 | - 'viewports' => array( |
|
| 193 | - 'large' => 'large', |
|
| 194 | - 'medium' => null, |
|
| 195 | - 'small' => 'small', |
|
| 196 | - 'mini' => null, |
|
| 197 | - ), |
|
| 198 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 199 | - 'labelWidths' => array( |
|
| 200 | - 'small' => 3, |
|
| 201 | - ), |
|
| 202 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 203 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 204 | - // HTML markup and classes used by Foundation 4 for icons |
|
| 205 | - 'icon' => array( |
|
| 206 | - 'tag' => 'i', |
|
| 207 | - 'set' => 'general', |
|
| 208 | - 'prefix' => 'foundicon', |
|
| 209 | - ), |
|
| 210 | - // CSS for inline validation errors |
|
| 211 | - 'error_classes' => array('class' => 'alert-box radius warning'), |
|
| 212 | - ), |
|
| 213 | - |
|
| 214 | - 'ZurbFoundation' => array( // Foundation 3 |
|
| 215 | - 'viewports' => array( |
|
| 216 | - 'large' => '', |
|
| 217 | - 'medium' => null, |
|
| 218 | - 'small' => 'mobile-', |
|
| 219 | - 'mini' => null, |
|
| 220 | - ), |
|
| 221 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 222 | - 'labelWidths' => array( |
|
| 223 | - 'large' => 2, |
|
| 224 | - 'small' => 4, |
|
| 225 | - ), |
|
| 226 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 227 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 228 | - // HTML markup and classes used by Foundation 3 for icons |
|
| 229 | - 'icon' => array( |
|
| 230 | - 'tag' => 'i', |
|
| 231 | - 'set' => null, |
|
| 232 | - 'prefix' => 'fi', |
|
| 233 | - ), |
|
| 234 | - // CSS for inline validation errors |
|
| 235 | - // should work for Zurb 2 and 3 |
|
| 236 | - 'error_classes' => array('class' => 'alert-box alert error'), |
|
| 237 | - ), |
|
| 3 | + // Markup |
|
| 4 | + //////////////////////////////////////////////////////////////////// |
|
| 5 | + |
|
| 6 | + // Whether labels should be automatically computed from name |
|
| 7 | + 'automatic_label' => true, |
|
| 8 | + |
|
| 9 | + // The default form type |
|
| 10 | + 'default_form_type' => 'horizontal', |
|
| 11 | + |
|
| 12 | + // Whether Former should escape HTML tags of 'plaintext' fields value |
|
| 13 | + // Enabled by default |
|
| 14 | + // |
|
| 15 | + // Instead of disabled this option, you should use the 'HtmlString' class: |
|
| 16 | + // Former::plaintext('text') |
|
| 17 | + // ->forceValue( |
|
| 18 | + // new Illuminate\Support\HtmlString('<b>your HTML data</b>') |
|
| 19 | + // ) |
|
| 20 | + 'escape_plaintext_value' => true, |
|
| 21 | + |
|
| 22 | + // Validation |
|
| 23 | + //////////////////////////////////////////////////////////////////// |
|
| 24 | + |
|
| 25 | + // Whether Former should fetch errors from Session |
|
| 26 | + 'fetch_errors' => true, |
|
| 27 | + |
|
| 28 | + // Whether Former should try to apply Validator rules as attributes |
|
| 29 | + 'live_validation' => true, |
|
| 30 | + |
|
| 31 | + // Whether Former should automatically fetch error messages and |
|
| 32 | + // display them next to the matching fields |
|
| 33 | + 'error_messages' => true, |
|
| 34 | + |
|
| 35 | + // Checkables |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + |
|
| 38 | + // Whether checkboxes should always be present in the POST data, |
|
| 39 | + // no matter if you checked them or not |
|
| 40 | + 'push_checkboxes' => false, |
|
| 41 | + |
|
| 42 | + // The value a checkbox will have in the POST array if unchecked |
|
| 43 | + 'unchecked_value' => 0, |
|
| 44 | + |
|
| 45 | + // Required fields |
|
| 46 | + //////////////////////////////////////////////////////////////////// |
|
| 47 | + |
|
| 48 | + // The class to be added to required fields |
|
| 49 | + 'required_class' => 'required', |
|
| 50 | + |
|
| 51 | + // A facultative text to append to the labels of required fields |
|
| 52 | + 'required_text' => '<sup>*</sup>', |
|
| 53 | + |
|
| 54 | + // Translations |
|
| 55 | + //////////////////////////////////////////////////////////////////// |
|
| 56 | + |
|
| 57 | + // Where Former should look for translations |
|
| 58 | + 'translate_from' => 'validation.attributes', |
|
| 59 | + |
|
| 60 | + // Whether text that comes out of the translated |
|
| 61 | + // should be capitalized (ex: email => Email) automatically |
|
| 62 | + 'capitalize_translations' => true, |
|
| 63 | + |
|
| 64 | + // An array of attributes to automatically translate |
|
| 65 | + 'translatable' => array( |
|
| 66 | + 'help', |
|
| 67 | + 'inlineHelp', |
|
| 68 | + 'blockHelp', |
|
| 69 | + 'placeholder', |
|
| 70 | + 'data_placeholder', |
|
| 71 | + 'label', |
|
| 72 | + ), |
|
| 73 | + |
|
| 74 | + // Framework |
|
| 75 | + //////////////////////////////////////////////////////////////////// |
|
| 76 | + |
|
| 77 | + // The framework to be used by Former |
|
| 78 | + 'framework' => 'TwitterBootstrap3', |
|
| 79 | + |
|
| 80 | + 'TwitterBootstrap5' => array( |
|
| 81 | + |
|
| 82 | + // Map Former-supported viewports to Bootstrap 5 equivalents |
|
| 83 | + 'viewports' => array( |
|
| 84 | + 'large' => 'lg', |
|
| 85 | + 'medium' => 'md', |
|
| 86 | + 'small' => 'sm', |
|
| 87 | + 'mini' => 'xs', |
|
| 88 | + ), |
|
| 89 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 90 | + 'labelWidths' => array( |
|
| 91 | + 'large' => 2, |
|
| 92 | + 'small' => 4, |
|
| 93 | + ), |
|
| 94 | + // HTML markup and classes used by Bootstrap 5 for icons |
|
| 95 | + 'icon' => array( |
|
| 96 | + 'tag' => 'i', |
|
| 97 | + 'set' => 'fa', |
|
| 98 | + 'prefix' => 'fa', |
|
| 99 | + ), |
|
| 100 | + |
|
| 101 | + ), |
|
| 102 | + |
|
| 103 | + 'TwitterBootstrap4' => array( |
|
| 104 | + |
|
| 105 | + // Map Former-supported viewports to Bootstrap 4 equivalents |
|
| 106 | + 'viewports' => array( |
|
| 107 | + 'large' => 'lg', |
|
| 108 | + 'medium' => 'md', |
|
| 109 | + 'small' => 'sm', |
|
| 110 | + 'mini' => 'xs', |
|
| 111 | + ), |
|
| 112 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 113 | + 'labelWidths' => array( |
|
| 114 | + 'large' => 2, |
|
| 115 | + 'small' => 4, |
|
| 116 | + ), |
|
| 117 | + // HTML markup and classes used by Bootstrap 4 for icons |
|
| 118 | + 'icon' => array( |
|
| 119 | + 'tag' => 'i', |
|
| 120 | + 'set' => 'fa', |
|
| 121 | + 'prefix' => 'fa', |
|
| 122 | + ), |
|
| 123 | + |
|
| 124 | + ), |
|
| 125 | + |
|
| 126 | + 'TwitterBootstrap3' => array( |
|
| 127 | + |
|
| 128 | + // Map Former-supported viewports to Bootstrap 3 equivalents |
|
| 129 | + 'viewports' => array( |
|
| 130 | + 'large' => 'lg', |
|
| 131 | + 'medium' => 'md', |
|
| 132 | + 'small' => 'sm', |
|
| 133 | + 'mini' => 'xs', |
|
| 134 | + ), |
|
| 135 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 136 | + 'labelWidths' => array( |
|
| 137 | + 'large' => 2, |
|
| 138 | + 'small' => 4, |
|
| 139 | + ), |
|
| 140 | + // HTML markup and classes used by Bootstrap 3 for icons |
|
| 141 | + 'icon' => array( |
|
| 142 | + 'tag' => 'span', |
|
| 143 | + 'set' => 'glyphicon', |
|
| 144 | + 'prefix' => 'glyphicon', |
|
| 145 | + ), |
|
| 146 | + |
|
| 147 | + ), |
|
| 148 | + |
|
| 149 | + 'Nude' => array( // No-framework markup |
|
| 150 | + 'icon' => array( |
|
| 151 | + 'tag' => 'i', |
|
| 152 | + 'set' => null, |
|
| 153 | + 'prefix' => 'icon', |
|
| 154 | + ), |
|
| 155 | + ), |
|
| 156 | + |
|
| 157 | + 'TwitterBootstrap' => array( // Twitter Bootstrap version 2 |
|
| 158 | + 'icon' => array( |
|
| 159 | + 'tag' => 'i', |
|
| 160 | + 'set' => null, |
|
| 161 | + 'prefix' => 'icon', |
|
| 162 | + ), |
|
| 163 | + ), |
|
| 164 | + |
|
| 165 | + 'ZurbFoundation5' => array( |
|
| 166 | + // Map Former-supported viewports to Foundation 5 equivalents |
|
| 167 | + 'viewports' => array( |
|
| 168 | + 'large' => 'large', |
|
| 169 | + 'medium' => null, |
|
| 170 | + 'small' => 'small', |
|
| 171 | + 'mini' => null, |
|
| 172 | + ), |
|
| 173 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 174 | + 'labelWidths' => array( |
|
| 175 | + 'small' => 3, |
|
| 176 | + ), |
|
| 177 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 178 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 179 | + // HTML markup and classes used by Foundation 5 for icons |
|
| 180 | + 'icon' => array( |
|
| 181 | + 'tag' => 'i', |
|
| 182 | + 'set' => null, |
|
| 183 | + 'prefix' => 'fi', |
|
| 184 | + ), |
|
| 185 | + // CSS for inline validation errors |
|
| 186 | + 'error_classes' => array('class' => 'error'), |
|
| 187 | + ), |
|
| 188 | + |
|
| 189 | + 'ZurbFoundation4' => array( |
|
| 190 | + // Foundation 4 also has an experimental "medium" breakpoint |
|
| 191 | + // explained at http://foundation.zurb.com/docs/components/grid.html |
|
| 192 | + 'viewports' => array( |
|
| 193 | + 'large' => 'large', |
|
| 194 | + 'medium' => null, |
|
| 195 | + 'small' => 'small', |
|
| 196 | + 'mini' => null, |
|
| 197 | + ), |
|
| 198 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 199 | + 'labelWidths' => array( |
|
| 200 | + 'small' => 3, |
|
| 201 | + ), |
|
| 202 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 203 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 204 | + // HTML markup and classes used by Foundation 4 for icons |
|
| 205 | + 'icon' => array( |
|
| 206 | + 'tag' => 'i', |
|
| 207 | + 'set' => 'general', |
|
| 208 | + 'prefix' => 'foundicon', |
|
| 209 | + ), |
|
| 210 | + // CSS for inline validation errors |
|
| 211 | + 'error_classes' => array('class' => 'alert-box radius warning'), |
|
| 212 | + ), |
|
| 213 | + |
|
| 214 | + 'ZurbFoundation' => array( // Foundation 3 |
|
| 215 | + 'viewports' => array( |
|
| 216 | + 'large' => '', |
|
| 217 | + 'medium' => null, |
|
| 218 | + 'small' => 'mobile-', |
|
| 219 | + 'mini' => null, |
|
| 220 | + ), |
|
| 221 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 222 | + 'labelWidths' => array( |
|
| 223 | + 'large' => 2, |
|
| 224 | + 'small' => 4, |
|
| 225 | + ), |
|
| 226 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 227 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 228 | + // HTML markup and classes used by Foundation 3 for icons |
|
| 229 | + 'icon' => array( |
|
| 230 | + 'tag' => 'i', |
|
| 231 | + 'set' => null, |
|
| 232 | + 'prefix' => 'fi', |
|
| 233 | + ), |
|
| 234 | + // CSS for inline validation errors |
|
| 235 | + // should work for Zurb 2 and 3 |
|
| 236 | + 'error_classes' => array('class' => 'alert-box alert error'), |
|
| 237 | + ), |
|
| 238 | 238 | |
| 239 | 239 | |
| 240 | 240 | ); |
@@ -14,570 +14,570 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | abstract class Checkable extends Field |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Renders the checkables as inline |
|
| 19 | - * |
|
| 20 | - * @var boolean |
|
| 21 | - */ |
|
| 22 | - protected $inline = false; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Add a text to a single element |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $text = null; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Renders the checkables as grouped |
|
| 33 | - * |
|
| 34 | - * @var boolean |
|
| 35 | - */ |
|
| 36 | - protected $grouped = false; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The checkable items currently stored |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $items = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The type of checkable item |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - protected $checkable = null; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * An array of checked items |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $checked = array(); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The checkable currently being focused on |
|
| 61 | - * |
|
| 62 | - * @var integer |
|
| 63 | - */ |
|
| 64 | - protected $focus = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Whether this particular checkable is to be pushed |
|
| 68 | - * |
|
| 69 | - * @var boolean |
|
| 70 | - */ |
|
| 71 | - protected $isPushed = null; |
|
| 72 | - |
|
| 73 | - //////////////////////////////////////////////////////////////////// |
|
| 74 | - //////////////////////////// CORE METHODS ////////////////////////// |
|
| 75 | - //////////////////////////////////////////////////////////////////// |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Build a new checkable |
|
| 79 | - * |
|
| 80 | - * @param Container $app |
|
| 81 | - * @param string $type |
|
| 82 | - * @param array $name |
|
| 83 | - * @param $label |
|
| 84 | - * @param $value |
|
| 85 | - * @param $attributes |
|
| 86 | - */ |
|
| 87 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 88 | - { |
|
| 89 | - // Unify auto and chained methods of grouping checkboxes |
|
| 90 | - if (Str::endsWith($name, '[]')) { |
|
| 91 | - $name = substr($name, 0, -2); |
|
| 92 | - $this->grouped(); |
|
| 93 | - } |
|
| 94 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 95 | - |
|
| 96 | - if (is_array($this->value)) { |
|
| 97 | - $this->items($this->value); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Apply methods to focused checkable |
|
| 103 | - * |
|
| 104 | - * @param string $method |
|
| 105 | - * @param array $parameters |
|
| 106 | - * |
|
| 107 | - * @return $this |
|
| 108 | - */ |
|
| 109 | - public function __call($method, $parameters) |
|
| 110 | - { |
|
| 111 | - $focused = $this->setOnFocused('attributes.'.$method, Arr::get($parameters, 0)); |
|
| 112 | - if ($focused) { |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return parent::__call($method, $parameters); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Prints out the currently stored checkables |
|
| 121 | - */ |
|
| 122 | - public function render() |
|
| 123 | - { |
|
| 124 | - $html = null; |
|
| 125 | - |
|
| 126 | - $this->setFieldClasses(); |
|
| 127 | - |
|
| 128 | - // Multiple items |
|
| 129 | - if ($this->items) { |
|
| 130 | - unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 131 | - foreach ($this->items as $key => $item) { |
|
| 132 | - $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 133 | - $html .= $this->createCheckable($item, $value); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return $html; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Single item |
|
| 140 | - return $this->createCheckable(array( |
|
| 141 | - 'name' => $this->name, |
|
| 142 | - 'label' => $this->text, |
|
| 143 | - 'value' => $this->value, |
|
| 144 | - 'attributes' => $this->attributes, |
|
| 145 | - )); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 150 | - //////////////////////////////////////////////////////////////////// |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Focus on a particular checkable |
|
| 154 | - * |
|
| 155 | - * @param integer $on The checkable to focus on |
|
| 156 | - * |
|
| 157 | - * @return $this |
|
| 158 | - */ |
|
| 159 | - public function on($on) |
|
| 160 | - { |
|
| 161 | - if (!isset($this->items[$on])) { |
|
| 162 | - return $this; |
|
| 163 | - } else { |
|
| 164 | - $this->focus = $on; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $this; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Set the checkables as inline |
|
| 172 | - */ |
|
| 173 | - public function inline($isInline = true) |
|
| 174 | - { |
|
| 175 | - $this->inline = $isInline; |
|
| 176 | - |
|
| 177 | - return $this; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Set the checkables as stacked |
|
| 182 | - */ |
|
| 183 | - public function stacked($isStacked = true) |
|
| 184 | - { |
|
| 185 | - $this->inline = !$isStacked; |
|
| 186 | - |
|
| 187 | - return $this; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Set the checkables as grouped |
|
| 192 | - */ |
|
| 193 | - public function grouped($isGrouped = true) |
|
| 194 | - { |
|
| 195 | - $this->grouped = $isGrouped; |
|
| 196 | - |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Add text to a single checkable |
|
| 202 | - * |
|
| 203 | - * @param string $text The checkable label |
|
| 204 | - * |
|
| 205 | - * @return $this |
|
| 206 | - */ |
|
| 207 | - public function text($text) |
|
| 208 | - { |
|
| 209 | - // Translate and format |
|
| 210 | - $text = Helpers::translate($text); |
|
| 211 | - |
|
| 212 | - // Apply on focused if any |
|
| 213 | - $focused = $this->setOnFocused('label', $text); |
|
| 214 | - if ($focused) { |
|
| 215 | - return $this; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $this->text = $text; |
|
| 219 | - |
|
| 220 | - return $this; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Push this particular checkbox |
|
| 225 | - * |
|
| 226 | - * @param boolean $pushed |
|
| 227 | - * |
|
| 228 | - * @return $this |
|
| 229 | - */ |
|
| 230 | - public function push($pushed = true) |
|
| 231 | - { |
|
| 232 | - $this->isPushed = $pushed; |
|
| 233 | - |
|
| 234 | - return $this; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Check a specific item |
|
| 239 | - * |
|
| 240 | - * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 241 | - * |
|
| 242 | - * @return $this |
|
| 243 | - */ |
|
| 244 | - public function check($checked = true) |
|
| 245 | - { |
|
| 246 | - // If we're setting all the checked items at once |
|
| 247 | - if (is_array($checked)) { |
|
| 248 | - $this->checked = $checked; |
|
| 249 | - // Checking an item in particular |
|
| 250 | - } elseif (is_string($checked) or is_int($checked)) { |
|
| 251 | - $this->checked[$checked] = true; |
|
| 252 | - // Only setting a single item |
|
| 253 | - } else { |
|
| 254 | - $this->checked[$this->name] = (bool) $checked; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $this; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Check if the checkables are inline |
|
| 263 | - * |
|
| 264 | - * @return boolean |
|
| 265 | - */ |
|
| 266 | - public function isInline() |
|
| 267 | - { |
|
| 268 | - return $this->inline; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - //////////////////////////////////////////////////////////////////// |
|
| 272 | - ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 273 | - //////////////////////////////////////////////////////////////////// |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Creates a series of checkable items |
|
| 277 | - * |
|
| 278 | - * @param array $_items Items to create |
|
| 279 | - */ |
|
| 280 | - protected function items($_items) |
|
| 281 | - { |
|
| 282 | - // If passing an array |
|
| 283 | - if (sizeof($_items) == 1 and |
|
| 284 | - isset($_items[0]) and |
|
| 285 | - is_array($_items[0]) |
|
| 286 | - ) { |
|
| 287 | - $_items = $_items[0]; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - // Fetch models if that's what we were passed |
|
| 291 | - if (isset($_items[0]) and is_object($_items[0])) { |
|
| 292 | - $_items = Helpers::queryToArray($_items); |
|
| 293 | - $_items = array_flip($_items); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // Iterate through items, assign a name and a label to each |
|
| 297 | - $count = 0; |
|
| 298 | - foreach ($_items as $label => $name) { |
|
| 299 | - |
|
| 300 | - // Define a fallback name in case none is found |
|
| 301 | - $fallback = $this->isCheckbox() |
|
| 302 | - ? $this->name.'_'.$count |
|
| 303 | - : $this->name; |
|
| 304 | - |
|
| 305 | - // Grouped fields |
|
| 306 | - if ($this->isGrouped()) { |
|
| 307 | - $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 308 | - $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // If we haven't any name defined for the checkable, try to compute some |
|
| 312 | - if (!is_string($label) and !is_array($name)) { |
|
| 313 | - $label = $name; |
|
| 314 | - $name = $fallback; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // If we gave custom information on the item, add them |
|
| 318 | - if (is_array($name)) { |
|
| 319 | - $attributes = $name; |
|
| 320 | - $name = Arr::get($attributes, 'name', $fallback); |
|
| 321 | - unset($attributes['name']); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - // Store all informations we have in an array |
|
| 325 | - $item = array( |
|
| 326 | - 'name' => $name, |
|
| 327 | - 'label' => Helpers::translate($label), |
|
| 328 | - 'count' => $count, |
|
| 329 | - ); |
|
| 330 | - if (isset($attributes)) { |
|
| 331 | - $item['attributes'] = $attributes; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $this->items[] = $item; |
|
| 335 | - $count++; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Renders a checkable |
|
| 341 | - * |
|
| 342 | - * @param string|array $item A checkable item |
|
| 343 | - * @param integer $fallbackValue A fallback value if none is set |
|
| 344 | - * |
|
| 345 | - * @return string |
|
| 346 | - */ |
|
| 347 | - protected function createCheckable($item, $fallbackValue = 1) |
|
| 348 | - { |
|
| 349 | - // Extract informations |
|
| 350 | - extract($item); |
|
| 351 | - |
|
| 352 | - // Set default values |
|
| 353 | - if (!isset($attributes)) { |
|
| 354 | - $attributes = array(); |
|
| 355 | - } |
|
| 356 | - if (isset($attributes['value'])) { |
|
| 357 | - $value = $attributes['value']; |
|
| 358 | - } |
|
| 359 | - if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 360 | - $value = $fallbackValue; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // If inline items, add class |
|
| 364 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 365 | - |
|
| 366 | - // In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if |
|
| 367 | - // rendering inline. |
|
| 368 | - $class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline; |
|
| 369 | - |
|
| 370 | - // Merge custom attributes with global attributes |
|
| 371 | - $attributes = array_merge($this->attributes, $attributes); |
|
| 372 | - if (!isset($attributes['id'])) { |
|
| 373 | - $attributes['id'] = $name.$this->unique($name); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - // Create field |
|
| 377 | - $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes); |
|
| 378 | - if ($this->isChecked($item, $value)) { |
|
| 379 | - $field->checked('checked'); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - // Add hidden checkbox if requested |
|
| 383 | - if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 384 | - if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 385 | - $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 386 | - |
|
| 387 | - // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 388 | - // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 389 | - $this->app->instance('former.field', $this); |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - // If no label to wrap, return plain checkable |
|
| 394 | - if (!$label) { |
|
| 395 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 396 | - } elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) { |
|
| 397 | - // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 398 | - $labelClass = 'form-check-label'; |
|
| 399 | - $element = $field . Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render(); |
|
| 400 | - |
|
| 401 | - $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 402 | - |
|
| 403 | - $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 404 | - } else { |
|
| 405 | - // Original way is to add the 'input' inside the 'label' |
|
| 406 | - $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 410 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 411 | - $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 412 | - if ($this->getAttribute('disabled')) { |
|
| 413 | - $wrapper->addClass('disabled'); |
|
| 414 | - } |
|
| 415 | - $element = $wrapper->render(); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - // Return the field |
|
| 419 | - return $element; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - //////////////////////////////////////////////////////////////////// |
|
| 423 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 424 | - //////////////////////////////////////////////////////////////////// |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Generate an unique ID for a field |
|
| 428 | - * |
|
| 429 | - * @param string $name The field's name |
|
| 430 | - * |
|
| 431 | - * @return string A field number to use |
|
| 432 | - */ |
|
| 433 | - protected function unique($name) |
|
| 434 | - { |
|
| 435 | - $this->app['former']->labels[] = $name; |
|
| 436 | - |
|
| 437 | - // Count number of fields with the same ID |
|
| 438 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 439 | - return $label == $name; |
|
| 440 | - }); |
|
| 441 | - $unique = sizeof($where); |
|
| 442 | - |
|
| 443 | - // In case the field doesn't need to be numbered |
|
| 444 | - if ($unique < 2 or empty($this->items)) { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - return $unique; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Set something on the currently focused checkable |
|
| 453 | - * |
|
| 454 | - * @param string $attribute The key to set |
|
| 455 | - * @param string $value Its value |
|
| 456 | - * |
|
| 457 | - * @return $this|bool |
|
| 458 | - */ |
|
| 459 | - protected function setOnFocused($attribute, $value) |
|
| 460 | - { |
|
| 461 | - if (is_null($this->focus)) { |
|
| 462 | - return false; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - $this->items[$this->focus] = Arr::set($this->items[$this->focus], $attribute, $value); |
|
| 466 | - |
|
| 467 | - return $this; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Check if a checkable is checked |
|
| 472 | - * |
|
| 473 | - * @return boolean Checked or not |
|
| 474 | - */ |
|
| 475 | - protected function isChecked($item = null, $value = null) |
|
| 476 | - { |
|
| 477 | - if (isset($item['name'])) { |
|
| 478 | - $name = $item['name']; |
|
| 479 | - } |
|
| 480 | - if (empty($name)) { |
|
| 481 | - $name = $this->name; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 485 | - // Or if it's a single radio, simply see if we called check |
|
| 486 | - if ($this->isCheckbox() or |
|
| 487 | - !$this->isCheckbox() and !$this->items |
|
| 488 | - ) { |
|
| 489 | - $checked = Arr::get($this->checked, $name, false); |
|
| 490 | - |
|
| 491 | - // If there are multiple, search for the value |
|
| 492 | - // as the name are the same between radios |
|
| 493 | - } else { |
|
| 494 | - $checked = Arr::get($this->checked, $value, false); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - // Check the values and POST array |
|
| 498 | - if ($this->isGrouped()) { |
|
| 499 | - // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 500 | - $groupIndex = self::getGroupIndexFromItem($item); |
|
| 501 | - |
|
| 502 | - // Search using the bare name, not the individual item name |
|
| 503 | - $post = $this->app['former']->getPost($this->name); |
|
| 504 | - $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 505 | - |
|
| 506 | - if (isset($post[$groupIndex])) { |
|
| 507 | - $post = $post[$groupIndex]; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 512 | - * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 513 | - * |
|
| 514 | - * array( |
|
| 515 | - * 'name' = 'roles[foo]', |
|
| 516 | - * 'value' => 'foo', |
|
| 517 | - * ) |
|
| 518 | - */ |
|
| 519 | - if ($static instanceof Collection) { |
|
| 520 | - // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 521 | - foreach ($static as $staticItem) { |
|
| 522 | - $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 523 | - if ($staticItemValue == $groupIndex) { |
|
| 524 | - $static = $staticItemValue; |
|
| 525 | - break; |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - } else if (isset($static[$groupIndex])) { |
|
| 529 | - $static = $static[$groupIndex]; |
|
| 530 | - } |
|
| 531 | - } else { |
|
| 532 | - $post = $this->app['former']->getPost($name); |
|
| 533 | - $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 537 | - $isChecked = ($post == $value); |
|
| 538 | - } elseif (!is_null($static)) { |
|
| 539 | - $isChecked = ($static == $value); |
|
| 540 | - } else { |
|
| 541 | - $isChecked = $checked; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - return $isChecked ? true : false; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Check if the current element is a checkbox |
|
| 549 | - * |
|
| 550 | - * @return boolean Checkbox or radio |
|
| 551 | - */ |
|
| 552 | - protected function isCheckbox() |
|
| 553 | - { |
|
| 554 | - return $this->checkable == 'checkbox'; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Check if the checkables are grouped or not |
|
| 559 | - * |
|
| 560 | - * @return boolean |
|
| 561 | - */ |
|
| 562 | - protected function isGrouped() |
|
| 563 | - { |
|
| 564 | - return |
|
| 565 | - $this->grouped == true or |
|
| 566 | - strpos($this->name, '[]') !== false; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * @param array $item The item array, containing at least name and count keys. |
|
| 571 | - * |
|
| 572 | - * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 573 | - */ |
|
| 574 | - public static function getGroupIndexFromItem($item) |
|
| 575 | - { |
|
| 576 | - $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 577 | - if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 578 | - return $item['count']; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - return $groupIndex; |
|
| 582 | - } |
|
| 17 | + /** |
|
| 18 | + * Renders the checkables as inline |
|
| 19 | + * |
|
| 20 | + * @var boolean |
|
| 21 | + */ |
|
| 22 | + protected $inline = false; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Add a text to a single element |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $text = null; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Renders the checkables as grouped |
|
| 33 | + * |
|
| 34 | + * @var boolean |
|
| 35 | + */ |
|
| 36 | + protected $grouped = false; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The checkable items currently stored |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $items = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The type of checkable item |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + protected $checkable = null; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * An array of checked items |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $checked = array(); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The checkable currently being focused on |
|
| 61 | + * |
|
| 62 | + * @var integer |
|
| 63 | + */ |
|
| 64 | + protected $focus = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Whether this particular checkable is to be pushed |
|
| 68 | + * |
|
| 69 | + * @var boolean |
|
| 70 | + */ |
|
| 71 | + protected $isPushed = null; |
|
| 72 | + |
|
| 73 | + //////////////////////////////////////////////////////////////////// |
|
| 74 | + //////////////////////////// CORE METHODS ////////////////////////// |
|
| 75 | + //////////////////////////////////////////////////////////////////// |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Build a new checkable |
|
| 79 | + * |
|
| 80 | + * @param Container $app |
|
| 81 | + * @param string $type |
|
| 82 | + * @param array $name |
|
| 83 | + * @param $label |
|
| 84 | + * @param $value |
|
| 85 | + * @param $attributes |
|
| 86 | + */ |
|
| 87 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 88 | + { |
|
| 89 | + // Unify auto and chained methods of grouping checkboxes |
|
| 90 | + if (Str::endsWith($name, '[]')) { |
|
| 91 | + $name = substr($name, 0, -2); |
|
| 92 | + $this->grouped(); |
|
| 93 | + } |
|
| 94 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 95 | + |
|
| 96 | + if (is_array($this->value)) { |
|
| 97 | + $this->items($this->value); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Apply methods to focused checkable |
|
| 103 | + * |
|
| 104 | + * @param string $method |
|
| 105 | + * @param array $parameters |
|
| 106 | + * |
|
| 107 | + * @return $this |
|
| 108 | + */ |
|
| 109 | + public function __call($method, $parameters) |
|
| 110 | + { |
|
| 111 | + $focused = $this->setOnFocused('attributes.'.$method, Arr::get($parameters, 0)); |
|
| 112 | + if ($focused) { |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return parent::__call($method, $parameters); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Prints out the currently stored checkables |
|
| 121 | + */ |
|
| 122 | + public function render() |
|
| 123 | + { |
|
| 124 | + $html = null; |
|
| 125 | + |
|
| 126 | + $this->setFieldClasses(); |
|
| 127 | + |
|
| 128 | + // Multiple items |
|
| 129 | + if ($this->items) { |
|
| 130 | + unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 131 | + foreach ($this->items as $key => $item) { |
|
| 132 | + $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 133 | + $html .= $this->createCheckable($item, $value); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return $html; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Single item |
|
| 140 | + return $this->createCheckable(array( |
|
| 141 | + 'name' => $this->name, |
|
| 142 | + 'label' => $this->text, |
|
| 143 | + 'value' => $this->value, |
|
| 144 | + 'attributes' => $this->attributes, |
|
| 145 | + )); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 150 | + //////////////////////////////////////////////////////////////////// |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Focus on a particular checkable |
|
| 154 | + * |
|
| 155 | + * @param integer $on The checkable to focus on |
|
| 156 | + * |
|
| 157 | + * @return $this |
|
| 158 | + */ |
|
| 159 | + public function on($on) |
|
| 160 | + { |
|
| 161 | + if (!isset($this->items[$on])) { |
|
| 162 | + return $this; |
|
| 163 | + } else { |
|
| 164 | + $this->focus = $on; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $this; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Set the checkables as inline |
|
| 172 | + */ |
|
| 173 | + public function inline($isInline = true) |
|
| 174 | + { |
|
| 175 | + $this->inline = $isInline; |
|
| 176 | + |
|
| 177 | + return $this; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Set the checkables as stacked |
|
| 182 | + */ |
|
| 183 | + public function stacked($isStacked = true) |
|
| 184 | + { |
|
| 185 | + $this->inline = !$isStacked; |
|
| 186 | + |
|
| 187 | + return $this; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Set the checkables as grouped |
|
| 192 | + */ |
|
| 193 | + public function grouped($isGrouped = true) |
|
| 194 | + { |
|
| 195 | + $this->grouped = $isGrouped; |
|
| 196 | + |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Add text to a single checkable |
|
| 202 | + * |
|
| 203 | + * @param string $text The checkable label |
|
| 204 | + * |
|
| 205 | + * @return $this |
|
| 206 | + */ |
|
| 207 | + public function text($text) |
|
| 208 | + { |
|
| 209 | + // Translate and format |
|
| 210 | + $text = Helpers::translate($text); |
|
| 211 | + |
|
| 212 | + // Apply on focused if any |
|
| 213 | + $focused = $this->setOnFocused('label', $text); |
|
| 214 | + if ($focused) { |
|
| 215 | + return $this; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $this->text = $text; |
|
| 219 | + |
|
| 220 | + return $this; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Push this particular checkbox |
|
| 225 | + * |
|
| 226 | + * @param boolean $pushed |
|
| 227 | + * |
|
| 228 | + * @return $this |
|
| 229 | + */ |
|
| 230 | + public function push($pushed = true) |
|
| 231 | + { |
|
| 232 | + $this->isPushed = $pushed; |
|
| 233 | + |
|
| 234 | + return $this; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Check a specific item |
|
| 239 | + * |
|
| 240 | + * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 241 | + * |
|
| 242 | + * @return $this |
|
| 243 | + */ |
|
| 244 | + public function check($checked = true) |
|
| 245 | + { |
|
| 246 | + // If we're setting all the checked items at once |
|
| 247 | + if (is_array($checked)) { |
|
| 248 | + $this->checked = $checked; |
|
| 249 | + // Checking an item in particular |
|
| 250 | + } elseif (is_string($checked) or is_int($checked)) { |
|
| 251 | + $this->checked[$checked] = true; |
|
| 252 | + // Only setting a single item |
|
| 253 | + } else { |
|
| 254 | + $this->checked[$this->name] = (bool) $checked; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $this; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Check if the checkables are inline |
|
| 263 | + * |
|
| 264 | + * @return boolean |
|
| 265 | + */ |
|
| 266 | + public function isInline() |
|
| 267 | + { |
|
| 268 | + return $this->inline; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + //////////////////////////////////////////////////////////////////// |
|
| 272 | + ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 273 | + //////////////////////////////////////////////////////////////////// |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Creates a series of checkable items |
|
| 277 | + * |
|
| 278 | + * @param array $_items Items to create |
|
| 279 | + */ |
|
| 280 | + protected function items($_items) |
|
| 281 | + { |
|
| 282 | + // If passing an array |
|
| 283 | + if (sizeof($_items) == 1 and |
|
| 284 | + isset($_items[0]) and |
|
| 285 | + is_array($_items[0]) |
|
| 286 | + ) { |
|
| 287 | + $_items = $_items[0]; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + // Fetch models if that's what we were passed |
|
| 291 | + if (isset($_items[0]) and is_object($_items[0])) { |
|
| 292 | + $_items = Helpers::queryToArray($_items); |
|
| 293 | + $_items = array_flip($_items); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // Iterate through items, assign a name and a label to each |
|
| 297 | + $count = 0; |
|
| 298 | + foreach ($_items as $label => $name) { |
|
| 299 | + |
|
| 300 | + // Define a fallback name in case none is found |
|
| 301 | + $fallback = $this->isCheckbox() |
|
| 302 | + ? $this->name.'_'.$count |
|
| 303 | + : $this->name; |
|
| 304 | + |
|
| 305 | + // Grouped fields |
|
| 306 | + if ($this->isGrouped()) { |
|
| 307 | + $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 308 | + $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // If we haven't any name defined for the checkable, try to compute some |
|
| 312 | + if (!is_string($label) and !is_array($name)) { |
|
| 313 | + $label = $name; |
|
| 314 | + $name = $fallback; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // If we gave custom information on the item, add them |
|
| 318 | + if (is_array($name)) { |
|
| 319 | + $attributes = $name; |
|
| 320 | + $name = Arr::get($attributes, 'name', $fallback); |
|
| 321 | + unset($attributes['name']); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + // Store all informations we have in an array |
|
| 325 | + $item = array( |
|
| 326 | + 'name' => $name, |
|
| 327 | + 'label' => Helpers::translate($label), |
|
| 328 | + 'count' => $count, |
|
| 329 | + ); |
|
| 330 | + if (isset($attributes)) { |
|
| 331 | + $item['attributes'] = $attributes; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $this->items[] = $item; |
|
| 335 | + $count++; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Renders a checkable |
|
| 341 | + * |
|
| 342 | + * @param string|array $item A checkable item |
|
| 343 | + * @param integer $fallbackValue A fallback value if none is set |
|
| 344 | + * |
|
| 345 | + * @return string |
|
| 346 | + */ |
|
| 347 | + protected function createCheckable($item, $fallbackValue = 1) |
|
| 348 | + { |
|
| 349 | + // Extract informations |
|
| 350 | + extract($item); |
|
| 351 | + |
|
| 352 | + // Set default values |
|
| 353 | + if (!isset($attributes)) { |
|
| 354 | + $attributes = array(); |
|
| 355 | + } |
|
| 356 | + if (isset($attributes['value'])) { |
|
| 357 | + $value = $attributes['value']; |
|
| 358 | + } |
|
| 359 | + if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 360 | + $value = $fallbackValue; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // If inline items, add class |
|
| 364 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 365 | + |
|
| 366 | + // In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if |
|
| 367 | + // rendering inline. |
|
| 368 | + $class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline; |
|
| 369 | + |
|
| 370 | + // Merge custom attributes with global attributes |
|
| 371 | + $attributes = array_merge($this->attributes, $attributes); |
|
| 372 | + if (!isset($attributes['id'])) { |
|
| 373 | + $attributes['id'] = $name.$this->unique($name); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + // Create field |
|
| 377 | + $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes); |
|
| 378 | + if ($this->isChecked($item, $value)) { |
|
| 379 | + $field->checked('checked'); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + // Add hidden checkbox if requested |
|
| 383 | + if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 384 | + if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 385 | + $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 386 | + |
|
| 387 | + // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 388 | + // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 389 | + $this->app->instance('former.field', $this); |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + // If no label to wrap, return plain checkable |
|
| 394 | + if (!$label) { |
|
| 395 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 396 | + } elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) { |
|
| 397 | + // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 398 | + $labelClass = 'form-check-label'; |
|
| 399 | + $element = $field . Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render(); |
|
| 400 | + |
|
| 401 | + $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 402 | + |
|
| 403 | + $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 404 | + } else { |
|
| 405 | + // Original way is to add the 'input' inside the 'label' |
|
| 406 | + $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 410 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 411 | + $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 412 | + if ($this->getAttribute('disabled')) { |
|
| 413 | + $wrapper->addClass('disabled'); |
|
| 414 | + } |
|
| 415 | + $element = $wrapper->render(); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + // Return the field |
|
| 419 | + return $element; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + //////////////////////////////////////////////////////////////////// |
|
| 423 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 424 | + //////////////////////////////////////////////////////////////////// |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Generate an unique ID for a field |
|
| 428 | + * |
|
| 429 | + * @param string $name The field's name |
|
| 430 | + * |
|
| 431 | + * @return string A field number to use |
|
| 432 | + */ |
|
| 433 | + protected function unique($name) |
|
| 434 | + { |
|
| 435 | + $this->app['former']->labels[] = $name; |
|
| 436 | + |
|
| 437 | + // Count number of fields with the same ID |
|
| 438 | + $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 439 | + return $label == $name; |
|
| 440 | + }); |
|
| 441 | + $unique = sizeof($where); |
|
| 442 | + |
|
| 443 | + // In case the field doesn't need to be numbered |
|
| 444 | + if ($unique < 2 or empty($this->items)) { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + return $unique; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Set something on the currently focused checkable |
|
| 453 | + * |
|
| 454 | + * @param string $attribute The key to set |
|
| 455 | + * @param string $value Its value |
|
| 456 | + * |
|
| 457 | + * @return $this|bool |
|
| 458 | + */ |
|
| 459 | + protected function setOnFocused($attribute, $value) |
|
| 460 | + { |
|
| 461 | + if (is_null($this->focus)) { |
|
| 462 | + return false; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + $this->items[$this->focus] = Arr::set($this->items[$this->focus], $attribute, $value); |
|
| 466 | + |
|
| 467 | + return $this; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Check if a checkable is checked |
|
| 472 | + * |
|
| 473 | + * @return boolean Checked or not |
|
| 474 | + */ |
|
| 475 | + protected function isChecked($item = null, $value = null) |
|
| 476 | + { |
|
| 477 | + if (isset($item['name'])) { |
|
| 478 | + $name = $item['name']; |
|
| 479 | + } |
|
| 480 | + if (empty($name)) { |
|
| 481 | + $name = $this->name; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 485 | + // Or if it's a single radio, simply see if we called check |
|
| 486 | + if ($this->isCheckbox() or |
|
| 487 | + !$this->isCheckbox() and !$this->items |
|
| 488 | + ) { |
|
| 489 | + $checked = Arr::get($this->checked, $name, false); |
|
| 490 | + |
|
| 491 | + // If there are multiple, search for the value |
|
| 492 | + // as the name are the same between radios |
|
| 493 | + } else { |
|
| 494 | + $checked = Arr::get($this->checked, $value, false); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + // Check the values and POST array |
|
| 498 | + if ($this->isGrouped()) { |
|
| 499 | + // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 500 | + $groupIndex = self::getGroupIndexFromItem($item); |
|
| 501 | + |
|
| 502 | + // Search using the bare name, not the individual item name |
|
| 503 | + $post = $this->app['former']->getPost($this->name); |
|
| 504 | + $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 505 | + |
|
| 506 | + if (isset($post[$groupIndex])) { |
|
| 507 | + $post = $post[$groupIndex]; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 512 | + * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 513 | + * |
|
| 514 | + * array( |
|
| 515 | + * 'name' = 'roles[foo]', |
|
| 516 | + * 'value' => 'foo', |
|
| 517 | + * ) |
|
| 518 | + */ |
|
| 519 | + if ($static instanceof Collection) { |
|
| 520 | + // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 521 | + foreach ($static as $staticItem) { |
|
| 522 | + $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 523 | + if ($staticItemValue == $groupIndex) { |
|
| 524 | + $static = $staticItemValue; |
|
| 525 | + break; |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + } else if (isset($static[$groupIndex])) { |
|
| 529 | + $static = $static[$groupIndex]; |
|
| 530 | + } |
|
| 531 | + } else { |
|
| 532 | + $post = $this->app['former']->getPost($name); |
|
| 533 | + $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 537 | + $isChecked = ($post == $value); |
|
| 538 | + } elseif (!is_null($static)) { |
|
| 539 | + $isChecked = ($static == $value); |
|
| 540 | + } else { |
|
| 541 | + $isChecked = $checked; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + return $isChecked ? true : false; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Check if the current element is a checkbox |
|
| 549 | + * |
|
| 550 | + * @return boolean Checkbox or radio |
|
| 551 | + */ |
|
| 552 | + protected function isCheckbox() |
|
| 553 | + { |
|
| 554 | + return $this->checkable == 'checkbox'; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Check if the checkables are grouped or not |
|
| 559 | + * |
|
| 560 | + * @return boolean |
|
| 561 | + */ |
|
| 562 | + protected function isGrouped() |
|
| 563 | + { |
|
| 564 | + return |
|
| 565 | + $this->grouped == true or |
|
| 566 | + strpos($this->name, '[]') !== false; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * @param array $item The item array, containing at least name and count keys. |
|
| 571 | + * |
|
| 572 | + * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 573 | + */ |
|
| 574 | + public static function getGroupIndexFromItem($item) |
|
| 575 | + { |
|
| 576 | + $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 577 | + if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 578 | + return $item['count']; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + return $groupIndex; |
|
| 582 | + } |
|
| 583 | 583 | } |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | |
| 366 | 366 | // In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if |
| 367 | 367 | // rendering inline. |
| 368 | - $class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline; |
|
| 368 | + $class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline; |
|
| 369 | 369 | |
| 370 | 370 | // Merge custom attributes with global attributes |
| 371 | 371 | $attributes = array_merge($this->attributes, $attributes); |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | } elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) { |
| 397 | 397 | // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
| 398 | 398 | $labelClass = 'form-check-label'; |
| 399 | - $element = $field . Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render(); |
|
| 399 | + $element = $field.Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render(); |
|
| 400 | 400 | |
| 401 | 401 | $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
| 402 | 402 | |
@@ -435,7 +435,7 @@ discard block |
||
| 435 | 435 | $this->app['former']->labels[] = $name; |
| 436 | 436 | |
| 437 | 437 | // Count number of fields with the same ID |
| 438 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 438 | + $where = array_filter($this->app['former']->labels, function($label) use ($name) { |
|
| 439 | 439 | return $label == $name; |
| 440 | 440 | }); |
| 441 | 441 | $unique = sizeof($where); |