@@ -11,407 +11,407 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Form extends FormerObject |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * The IoC Container |
|
| 16 | - * |
|
| 17 | - * @var Container |
|
| 18 | - */ |
|
| 19 | - protected $app; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The URL generator |
|
| 23 | - * |
|
| 24 | - * @var UrlGenerator |
|
| 25 | - */ |
|
| 26 | - protected $url; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Populator |
|
| 30 | - * |
|
| 31 | - * @var Populator |
|
| 32 | - */ |
|
| 33 | - protected $populator; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The Form type |
|
| 37 | - * |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - protected $type = null; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The destination of the current form |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - protected $action; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The form method |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $method; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Whether the form should be secured or not |
|
| 58 | - * |
|
| 59 | - * @var boolean |
|
| 60 | - */ |
|
| 61 | - protected $secure; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The form element |
|
| 65 | - * |
|
| 66 | - * @var string |
|
| 67 | - */ |
|
| 68 | - protected $element = 'form'; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * A list of injected properties |
|
| 72 | - * |
|
| 73 | - * @var array |
|
| 74 | - */ |
|
| 75 | - protected $injectedProperties = array('method', 'action'); |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Whether a form is opened or not |
|
| 79 | - * |
|
| 80 | - * @var boolean |
|
| 81 | - */ |
|
| 82 | - protected static $opened = false; |
|
| 83 | - |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 86 | - //////////////////////////////////////////////////////////////////// |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Build a new Form instance |
|
| 90 | - * |
|
| 91 | - * @param UrlGenerator $url |
|
| 92 | - */ |
|
| 93 | - public function __construct(Container $app, $url, Populator $populator) |
|
| 94 | - { |
|
| 95 | - $this->app = $app; |
|
| 96 | - $this->url = $url; |
|
| 97 | - $this->populator = $populator; |
|
| 98 | - |
|
| 99 | - $this->app->singleton('former.form.framework', function($app) { |
|
| 100 | - return clone $app['former.framework']; |
|
| 101 | - }); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Opens up magically a form |
|
| 106 | - * |
|
| 107 | - * @param string $type The form type asked |
|
| 108 | - * @param array $parameters Parameters passed |
|
| 109 | - * |
|
| 110 | - * @return Form A form opening tag |
|
| 111 | - */ |
|
| 112 | - public function openForm($type, $parameters) |
|
| 113 | - { |
|
| 114 | - $action = array_get($parameters, 0); |
|
| 115 | - $method = array_get($parameters, 1, 'POST'); |
|
| 116 | - $attributes = array_get($parameters, 2, array()); |
|
| 117 | - $secure = array_get($parameters, 3, null); |
|
| 118 | - |
|
| 119 | - // Fetch errors if asked for |
|
| 120 | - if ($this->app['former']->getOption('fetch_errors')) { |
|
| 121 | - $this->app['former']->withErrors(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - // Open the form |
|
| 125 | - $this->action($action); |
|
| 126 | - $this->attributes = $attributes; |
|
| 127 | - $this->method = strtoupper($method); |
|
| 128 | - $this->secure = $secure; |
|
| 129 | - |
|
| 130 | - // Add any effect of the form type |
|
| 131 | - $type = Str::snake($type); |
|
| 132 | - $this->type = $this->applyType($type); |
|
| 133 | - |
|
| 134 | - // Add enctype |
|
| 135 | - if (!array_key_exists('accept-charset', $attributes)) { |
|
| 136 | - $this->attributes['accept-charset'] = 'utf-8'; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Add supplementary classes |
|
| 140 | - if ($this->type !== 'raw') { |
|
| 141 | - $this->addClass($this->app['former.form.framework']->getFormClasses($this->type)); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $this; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Closes a Form |
|
| 149 | - * |
|
| 150 | - * @return string A closing <form> tag |
|
| 151 | - */ |
|
| 152 | - public function close() |
|
| 153 | - { |
|
| 154 | - static::$opened = false; |
|
| 155 | - |
|
| 156 | - // Add token if necessary |
|
| 157 | - $closing = '</form>'; |
|
| 158 | - if ($this->method != 'GET') { |
|
| 159 | - $closing = $this->app['former']->token().$closing; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $closing; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - //////////////////////////////////////////////////////////////////// |
|
| 166 | - //////////////////////////// STATIC HELPERS //////////////////////// |
|
| 167 | - //////////////////////////////////////////////////////////////////// |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Whether a form is currently opened or not |
|
| 171 | - * |
|
| 172 | - * @return boolean |
|
| 173 | - */ |
|
| 174 | - public static function hasInstanceOpened() |
|
| 175 | - { |
|
| 176 | - return static::$opened; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - //////////////////////////////////////////////////////////////////// |
|
| 180 | - /////////////////////////////// SETTER ///////////////////////////// |
|
| 181 | - //////////////////////////////////////////////////////////////////// |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Change the form's action |
|
| 185 | - * |
|
| 186 | - * @param string $action The new action |
|
| 187 | - * |
|
| 188 | - * @return $this |
|
| 189 | - */ |
|
| 190 | - public function action($action) |
|
| 191 | - { |
|
| 192 | - $this->action = $action ? $this->url->to($action, array(), $this->secure) : null; |
|
| 193 | - |
|
| 194 | - return $this; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Change the form's method |
|
| 199 | - * |
|
| 200 | - * @param string $method The method to use |
|
| 201 | - * |
|
| 202 | - * @return $this |
|
| 203 | - */ |
|
| 204 | - public function method($method) |
|
| 205 | - { |
|
| 206 | - $this->method = strtoupper($method); |
|
| 207 | - |
|
| 208 | - return $this; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Whether the form should be secure |
|
| 213 | - * |
|
| 214 | - * @param boolean $secure Secure or not |
|
| 215 | - * |
|
| 216 | - * @return $this |
|
| 217 | - */ |
|
| 218 | - public function secure($secure = true) |
|
| 219 | - { |
|
| 220 | - $this->secure = $secure; |
|
| 221 | - |
|
| 222 | - return $this; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Change the form's action and method to a route |
|
| 227 | - * |
|
| 228 | - * @param string $name The name of the route to use |
|
| 229 | - * @param array $params Any route parameters |
|
| 230 | - * |
|
| 231 | - * @return Form |
|
| 232 | - */ |
|
| 233 | - public function route($name, $params = array()) |
|
| 234 | - { |
|
| 235 | - return $this->setRouteOrAction($name, $params, 'route'); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Change the form's action to a controller method |
|
| 240 | - * |
|
| 241 | - * @param string $name The controller and method |
|
| 242 | - * @param array $params Any method parameters |
|
| 243 | - * |
|
| 244 | - * @return Form |
|
| 245 | - */ |
|
| 246 | - public function controller($name, $params = array()) |
|
| 247 | - { |
|
| 248 | - return $this->setRouteOrAction($name, $params, 'action'); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Outputs the current form opened |
|
| 253 | - * |
|
| 254 | - * @return string A <form> opening tag |
|
| 255 | - */ |
|
| 256 | - public function __toString() |
|
| 257 | - { |
|
| 258 | - // Mark the form as opened |
|
| 259 | - static::$opened = true; |
|
| 260 | - |
|
| 261 | - // Add name to attributes |
|
| 262 | - $this->attributes['name'] = $this->name; |
|
| 263 | - |
|
| 264 | - // Add spoof method |
|
| 265 | - if (in_array($this->method, array('PUT', 'PATCH', 'DELETE'))) { |
|
| 266 | - $spoof = $this->app['former']->hidden('_method', $this->method); |
|
| 267 | - $this->method = 'POST'; |
|
| 268 | - } else { |
|
| 269 | - $spoof = null; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return $this->open().$spoof; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - //////////////////////////////////////////////////////////////////// |
|
| 276 | - ////////////////////////// PUBLIC HELPERS ////////////////////////// |
|
| 277 | - //////////////////////////////////////////////////////////////////// |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Alias for $this->app['former']->withRules |
|
| 281 | - */ |
|
| 282 | - public function rules() |
|
| 283 | - { |
|
| 284 | - call_user_func_array(array($this->app['former'], 'withRules'), func_get_args()); |
|
| 285 | - |
|
| 286 | - return $this; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Populate a form with specific values |
|
| 291 | - * |
|
| 292 | - * @param array|object $values |
|
| 293 | - * |
|
| 294 | - * @return $this |
|
| 295 | - */ |
|
| 296 | - public function populate($values) |
|
| 297 | - { |
|
| 298 | - $this->populator->replace($values); |
|
| 299 | - |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Get the Populator binded to the Form |
|
| 305 | - * |
|
| 306 | - * @return Populator |
|
| 307 | - */ |
|
| 308 | - public function getPopulator() |
|
| 309 | - { |
|
| 310 | - return $this->populator; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - //////////////////////////////////////////////////////////////////// |
|
| 314 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 315 | - //////////////////////////////////////////////////////////////////// |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Find the method of a route by its _uses or name |
|
| 319 | - * |
|
| 320 | - * @param string $name |
|
| 321 | - * |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - protected function findRouteMethod($name) |
|
| 325 | - { |
|
| 326 | - if (!$this->app->bound('router')) { |
|
| 327 | - return; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - // Get string by name |
|
| 331 | - if (!Str::contains($name, '@')) { |
|
| 332 | - $routes = $this->app['router']->getRoutes(); |
|
| 333 | - $route = method_exists($routes, 'getByName') ? $routes->getByName($name) : $routes->get($name); |
|
| 334 | - // Get string by uses |
|
| 335 | - } else { |
|
| 336 | - foreach ($this->app['router']->getRoutes() as $route) { |
|
| 337 | - $routeUses = method_exists($route, 'getOption') ? $route->getOption('_uses') : array_get($route->getAction(), 'controller'); |
|
| 338 | - if ($action = $routeUses) { |
|
| 339 | - if ($action == $name) { |
|
| 340 | - break; |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Get method |
|
| 347 | - $methods = method_exists($route, 'getMethods') ? $route->getMethods() : $route->methods(); |
|
| 348 | - $method = array_get($methods, 0); |
|
| 349 | - |
|
| 350 | - return $method; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @param string $name |
|
| 355 | - * @param $params |
|
| 356 | - * @param string $type |
|
| 357 | - * |
|
| 358 | - * @return $this |
|
| 359 | - */ |
|
| 360 | - protected function setRouteOrAction($name, $params, $type) |
|
| 361 | - { |
|
| 362 | - // Set the form action |
|
| 363 | - $this->action = $this->url->$type($name, $params); |
|
| 364 | - |
|
| 365 | - // Set the proper method |
|
| 366 | - if ($method = $this->findRouteMethod($name)) { |
|
| 367 | - $this->method($method); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - return $this; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Apply various parameters according to form type |
|
| 375 | - * |
|
| 376 | - * @param string $type The original form type provided |
|
| 377 | - * |
|
| 378 | - * @return string The final form type |
|
| 379 | - */ |
|
| 380 | - private function applyType($type) |
|
| 381 | - { |
|
| 382 | - // If classic form |
|
| 383 | - if ($type == 'open') { |
|
| 384 | - return $this->app['former']->getOption('default_form_type'); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // Look for HTTPS form |
|
| 388 | - if (Str::contains($type, 'secure')) { |
|
| 389 | - $type = str_replace('secure', '', $type); |
|
| 390 | - $this->secure = true; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - // Look for file form |
|
| 394 | - if (Str::contains($type, 'for_files')) { |
|
| 395 | - $type = str_replace('for_files', '', $type); |
|
| 396 | - $this->attributes['enctype'] = 'multipart/form-data'; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - // Calculate form type |
|
| 400 | - $type = str_replace('open', '', $type); |
|
| 401 | - $type = trim($type, '_'); |
|
| 402 | - |
|
| 403 | - // If raw form |
|
| 404 | - if ($type == 'raw') { |
|
| 405 | - $this->app->bind('former.form.framework', function($app) { |
|
| 406 | - return $app['former']->getFrameworkInstance('Nude'); |
|
| 407 | - }); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - // Use default form type if the one provided is invalid |
|
| 411 | - if ($type !== 'raw' and !in_array($type, $this->app['former.form.framework']->availableTypes())) { |
|
| 412 | - $type = $this->app['former']->getOption('default_form_type'); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - return $type; |
|
| 416 | - } |
|
| 14 | + /** |
|
| 15 | + * The IoC Container |
|
| 16 | + * |
|
| 17 | + * @var Container |
|
| 18 | + */ |
|
| 19 | + protected $app; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The URL generator |
|
| 23 | + * |
|
| 24 | + * @var UrlGenerator |
|
| 25 | + */ |
|
| 26 | + protected $url; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Populator |
|
| 30 | + * |
|
| 31 | + * @var Populator |
|
| 32 | + */ |
|
| 33 | + protected $populator; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The Form type |
|
| 37 | + * |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + protected $type = null; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The destination of the current form |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + protected $action; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The form method |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $method; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Whether the form should be secured or not |
|
| 58 | + * |
|
| 59 | + * @var boolean |
|
| 60 | + */ |
|
| 61 | + protected $secure; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The form element |
|
| 65 | + * |
|
| 66 | + * @var string |
|
| 67 | + */ |
|
| 68 | + protected $element = 'form'; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * A list of injected properties |
|
| 72 | + * |
|
| 73 | + * @var array |
|
| 74 | + */ |
|
| 75 | + protected $injectedProperties = array('method', 'action'); |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Whether a form is opened or not |
|
| 79 | + * |
|
| 80 | + * @var boolean |
|
| 81 | + */ |
|
| 82 | + protected static $opened = false; |
|
| 83 | + |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 86 | + //////////////////////////////////////////////////////////////////// |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Build a new Form instance |
|
| 90 | + * |
|
| 91 | + * @param UrlGenerator $url |
|
| 92 | + */ |
|
| 93 | + public function __construct(Container $app, $url, Populator $populator) |
|
| 94 | + { |
|
| 95 | + $this->app = $app; |
|
| 96 | + $this->url = $url; |
|
| 97 | + $this->populator = $populator; |
|
| 98 | + |
|
| 99 | + $this->app->singleton('former.form.framework', function($app) { |
|
| 100 | + return clone $app['former.framework']; |
|
| 101 | + }); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Opens up magically a form |
|
| 106 | + * |
|
| 107 | + * @param string $type The form type asked |
|
| 108 | + * @param array $parameters Parameters passed |
|
| 109 | + * |
|
| 110 | + * @return Form A form opening tag |
|
| 111 | + */ |
|
| 112 | + public function openForm($type, $parameters) |
|
| 113 | + { |
|
| 114 | + $action = array_get($parameters, 0); |
|
| 115 | + $method = array_get($parameters, 1, 'POST'); |
|
| 116 | + $attributes = array_get($parameters, 2, array()); |
|
| 117 | + $secure = array_get($parameters, 3, null); |
|
| 118 | + |
|
| 119 | + // Fetch errors if asked for |
|
| 120 | + if ($this->app['former']->getOption('fetch_errors')) { |
|
| 121 | + $this->app['former']->withErrors(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + // Open the form |
|
| 125 | + $this->action($action); |
|
| 126 | + $this->attributes = $attributes; |
|
| 127 | + $this->method = strtoupper($method); |
|
| 128 | + $this->secure = $secure; |
|
| 129 | + |
|
| 130 | + // Add any effect of the form type |
|
| 131 | + $type = Str::snake($type); |
|
| 132 | + $this->type = $this->applyType($type); |
|
| 133 | + |
|
| 134 | + // Add enctype |
|
| 135 | + if (!array_key_exists('accept-charset', $attributes)) { |
|
| 136 | + $this->attributes['accept-charset'] = 'utf-8'; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Add supplementary classes |
|
| 140 | + if ($this->type !== 'raw') { |
|
| 141 | + $this->addClass($this->app['former.form.framework']->getFormClasses($this->type)); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $this; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Closes a Form |
|
| 149 | + * |
|
| 150 | + * @return string A closing <form> tag |
|
| 151 | + */ |
|
| 152 | + public function close() |
|
| 153 | + { |
|
| 154 | + static::$opened = false; |
|
| 155 | + |
|
| 156 | + // Add token if necessary |
|
| 157 | + $closing = '</form>'; |
|
| 158 | + if ($this->method != 'GET') { |
|
| 159 | + $closing = $this->app['former']->token().$closing; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $closing; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + //////////////////////////////////////////////////////////////////// |
|
| 166 | + //////////////////////////// STATIC HELPERS //////////////////////// |
|
| 167 | + //////////////////////////////////////////////////////////////////// |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Whether a form is currently opened or not |
|
| 171 | + * |
|
| 172 | + * @return boolean |
|
| 173 | + */ |
|
| 174 | + public static function hasInstanceOpened() |
|
| 175 | + { |
|
| 176 | + return static::$opened; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + //////////////////////////////////////////////////////////////////// |
|
| 180 | + /////////////////////////////// SETTER ///////////////////////////// |
|
| 181 | + //////////////////////////////////////////////////////////////////// |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Change the form's action |
|
| 185 | + * |
|
| 186 | + * @param string $action The new action |
|
| 187 | + * |
|
| 188 | + * @return $this |
|
| 189 | + */ |
|
| 190 | + public function action($action) |
|
| 191 | + { |
|
| 192 | + $this->action = $action ? $this->url->to($action, array(), $this->secure) : null; |
|
| 193 | + |
|
| 194 | + return $this; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Change the form's method |
|
| 199 | + * |
|
| 200 | + * @param string $method The method to use |
|
| 201 | + * |
|
| 202 | + * @return $this |
|
| 203 | + */ |
|
| 204 | + public function method($method) |
|
| 205 | + { |
|
| 206 | + $this->method = strtoupper($method); |
|
| 207 | + |
|
| 208 | + return $this; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Whether the form should be secure |
|
| 213 | + * |
|
| 214 | + * @param boolean $secure Secure or not |
|
| 215 | + * |
|
| 216 | + * @return $this |
|
| 217 | + */ |
|
| 218 | + public function secure($secure = true) |
|
| 219 | + { |
|
| 220 | + $this->secure = $secure; |
|
| 221 | + |
|
| 222 | + return $this; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Change the form's action and method to a route |
|
| 227 | + * |
|
| 228 | + * @param string $name The name of the route to use |
|
| 229 | + * @param array $params Any route parameters |
|
| 230 | + * |
|
| 231 | + * @return Form |
|
| 232 | + */ |
|
| 233 | + public function route($name, $params = array()) |
|
| 234 | + { |
|
| 235 | + return $this->setRouteOrAction($name, $params, 'route'); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Change the form's action to a controller method |
|
| 240 | + * |
|
| 241 | + * @param string $name The controller and method |
|
| 242 | + * @param array $params Any method parameters |
|
| 243 | + * |
|
| 244 | + * @return Form |
|
| 245 | + */ |
|
| 246 | + public function controller($name, $params = array()) |
|
| 247 | + { |
|
| 248 | + return $this->setRouteOrAction($name, $params, 'action'); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Outputs the current form opened |
|
| 253 | + * |
|
| 254 | + * @return string A <form> opening tag |
|
| 255 | + */ |
|
| 256 | + public function __toString() |
|
| 257 | + { |
|
| 258 | + // Mark the form as opened |
|
| 259 | + static::$opened = true; |
|
| 260 | + |
|
| 261 | + // Add name to attributes |
|
| 262 | + $this->attributes['name'] = $this->name; |
|
| 263 | + |
|
| 264 | + // Add spoof method |
|
| 265 | + if (in_array($this->method, array('PUT', 'PATCH', 'DELETE'))) { |
|
| 266 | + $spoof = $this->app['former']->hidden('_method', $this->method); |
|
| 267 | + $this->method = 'POST'; |
|
| 268 | + } else { |
|
| 269 | + $spoof = null; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return $this->open().$spoof; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + //////////////////////////////////////////////////////////////////// |
|
| 276 | + ////////////////////////// PUBLIC HELPERS ////////////////////////// |
|
| 277 | + //////////////////////////////////////////////////////////////////// |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Alias for $this->app['former']->withRules |
|
| 281 | + */ |
|
| 282 | + public function rules() |
|
| 283 | + { |
|
| 284 | + call_user_func_array(array($this->app['former'], 'withRules'), func_get_args()); |
|
| 285 | + |
|
| 286 | + return $this; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Populate a form with specific values |
|
| 291 | + * |
|
| 292 | + * @param array|object $values |
|
| 293 | + * |
|
| 294 | + * @return $this |
|
| 295 | + */ |
|
| 296 | + public function populate($values) |
|
| 297 | + { |
|
| 298 | + $this->populator->replace($values); |
|
| 299 | + |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Get the Populator binded to the Form |
|
| 305 | + * |
|
| 306 | + * @return Populator |
|
| 307 | + */ |
|
| 308 | + public function getPopulator() |
|
| 309 | + { |
|
| 310 | + return $this->populator; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + //////////////////////////////////////////////////////////////////// |
|
| 314 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 315 | + //////////////////////////////////////////////////////////////////// |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Find the method of a route by its _uses or name |
|
| 319 | + * |
|
| 320 | + * @param string $name |
|
| 321 | + * |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + protected function findRouteMethod($name) |
|
| 325 | + { |
|
| 326 | + if (!$this->app->bound('router')) { |
|
| 327 | + return; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + // Get string by name |
|
| 331 | + if (!Str::contains($name, '@')) { |
|
| 332 | + $routes = $this->app['router']->getRoutes(); |
|
| 333 | + $route = method_exists($routes, 'getByName') ? $routes->getByName($name) : $routes->get($name); |
|
| 334 | + // Get string by uses |
|
| 335 | + } else { |
|
| 336 | + foreach ($this->app['router']->getRoutes() as $route) { |
|
| 337 | + $routeUses = method_exists($route, 'getOption') ? $route->getOption('_uses') : array_get($route->getAction(), 'controller'); |
|
| 338 | + if ($action = $routeUses) { |
|
| 339 | + if ($action == $name) { |
|
| 340 | + break; |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Get method |
|
| 347 | + $methods = method_exists($route, 'getMethods') ? $route->getMethods() : $route->methods(); |
|
| 348 | + $method = array_get($methods, 0); |
|
| 349 | + |
|
| 350 | + return $method; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @param string $name |
|
| 355 | + * @param $params |
|
| 356 | + * @param string $type |
|
| 357 | + * |
|
| 358 | + * @return $this |
|
| 359 | + */ |
|
| 360 | + protected function setRouteOrAction($name, $params, $type) |
|
| 361 | + { |
|
| 362 | + // Set the form action |
|
| 363 | + $this->action = $this->url->$type($name, $params); |
|
| 364 | + |
|
| 365 | + // Set the proper method |
|
| 366 | + if ($method = $this->findRouteMethod($name)) { |
|
| 367 | + $this->method($method); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + return $this; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Apply various parameters according to form type |
|
| 375 | + * |
|
| 376 | + * @param string $type The original form type provided |
|
| 377 | + * |
|
| 378 | + * @return string The final form type |
|
| 379 | + */ |
|
| 380 | + private function applyType($type) |
|
| 381 | + { |
|
| 382 | + // If classic form |
|
| 383 | + if ($type == 'open') { |
|
| 384 | + return $this->app['former']->getOption('default_form_type'); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // Look for HTTPS form |
|
| 388 | + if (Str::contains($type, 'secure')) { |
|
| 389 | + $type = str_replace('secure', '', $type); |
|
| 390 | + $this->secure = true; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + // Look for file form |
|
| 394 | + if (Str::contains($type, 'for_files')) { |
|
| 395 | + $type = str_replace('for_files', '', $type); |
|
| 396 | + $this->attributes['enctype'] = 'multipart/form-data'; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + // Calculate form type |
|
| 400 | + $type = str_replace('open', '', $type); |
|
| 401 | + $type = trim($type, '_'); |
|
| 402 | + |
|
| 403 | + // If raw form |
|
| 404 | + if ($type == 'raw') { |
|
| 405 | + $this->app->bind('former.form.framework', function($app) { |
|
| 406 | + return $app['former']->getFrameworkInstance('Nude'); |
|
| 407 | + }); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + // Use default form type if the one provided is invalid |
|
| 411 | + if ($type !== 'raw' and !in_array($type, $this->app['former.form.framework']->availableTypes())) { |
|
| 412 | + $type = $this->app['former']->getOption('default_form_type'); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + return $type; |
|
| 416 | + } |
|
| 417 | 417 | } |
@@ -10,105 +10,105 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Button extends Field |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The Button default element |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $element = 'input'; |
|
| 13 | + /** |
|
| 14 | + * The Button default element |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $element = 'input'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Default value for self-closing |
|
| 22 | - * |
|
| 23 | - * @var boolean |
|
| 24 | - */ |
|
| 25 | - protected $isSelfClosing = true; |
|
| 20 | + /** |
|
| 21 | + * Default value for self-closing |
|
| 22 | + * |
|
| 23 | + * @var boolean |
|
| 24 | + */ |
|
| 25 | + protected $isSelfClosing = true; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * A list of class properties to be added to attributes |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $injectedProperties = array( |
|
| 33 | - 'name', |
|
| 34 | - 'type', |
|
| 35 | - 'value', |
|
| 36 | - ); |
|
| 27 | + /** |
|
| 28 | + * A list of class properties to be added to attributes |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $injectedProperties = array( |
|
| 33 | + 'name', |
|
| 34 | + 'type', |
|
| 35 | + 'value', |
|
| 36 | + ); |
|
| 37 | 37 | |
| 38 | - //////////////////////////////////////////////////////////////////// |
|
| 39 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 40 | - //////////////////////////////////////////////////////////////////// |
|
| 38 | + //////////////////////////////////////////////////////////////////// |
|
| 39 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 40 | + //////////////////////////////////////////////////////////////////// |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Easier arguments order for button fields |
|
| 44 | - * |
|
| 45 | - * @param Container $app The Container |
|
| 46 | - * @param string $type button/submit/reset/etc |
|
| 47 | - * @param string $value The text of the button |
|
| 48 | - * @param string $link Its link |
|
| 49 | - * @param array $attributes Its attributes |
|
| 50 | - */ |
|
| 51 | - public function __construct(Container $app, $type, $value, $link, $attributes) |
|
| 52 | - { |
|
| 53 | - $this->app = $app; |
|
| 54 | - $this->attributes = (array) $attributes; |
|
| 55 | - $this->type = $type; |
|
| 56 | - $this->value($value); |
|
| 42 | + /** |
|
| 43 | + * Easier arguments order for button fields |
|
| 44 | + * |
|
| 45 | + * @param Container $app The Container |
|
| 46 | + * @param string $type button/submit/reset/etc |
|
| 47 | + * @param string $value The text of the button |
|
| 48 | + * @param string $link Its link |
|
| 49 | + * @param array $attributes Its attributes |
|
| 50 | + */ |
|
| 51 | + public function __construct(Container $app, $type, $value, $link, $attributes) |
|
| 52 | + { |
|
| 53 | + $this->app = $app; |
|
| 54 | + $this->attributes = (array) $attributes; |
|
| 55 | + $this->type = $type; |
|
| 56 | + $this->value($value); |
|
| 57 | 57 | |
| 58 | - // Set correct element for the various button patterns |
|
| 59 | - switch ($type) { |
|
| 60 | - case 'button': |
|
| 61 | - $this->element = 'button'; |
|
| 62 | - $this->isSelfClosing = false; |
|
| 63 | - break; |
|
| 64 | - case 'link': |
|
| 65 | - $this->type = null; |
|
| 66 | - $this->element = 'a'; |
|
| 67 | - $this->attributes['href'] = $link; |
|
| 68 | - $this->isSelfClosing = false; |
|
| 69 | - break; |
|
| 70 | - } |
|
| 71 | - } |
|
| 58 | + // Set correct element for the various button patterns |
|
| 59 | + switch ($type) { |
|
| 60 | + case 'button': |
|
| 61 | + $this->element = 'button'; |
|
| 62 | + $this->isSelfClosing = false; |
|
| 63 | + break; |
|
| 64 | + case 'link': |
|
| 65 | + $this->type = null; |
|
| 66 | + $this->element = 'a'; |
|
| 67 | + $this->attributes['href'] = $link; |
|
| 68 | + $this->isSelfClosing = false; |
|
| 69 | + break; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - //////////////////////////////////////////////////////////////////// |
|
| 74 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 75 | - //////////////////////////////////////////////////////////////////// |
|
| 73 | + //////////////////////////////////////////////////////////////////// |
|
| 74 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 75 | + //////////////////////////////////////////////////////////////////// |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Check if the field is a button |
|
| 79 | - * |
|
| 80 | - * @return boolean |
|
| 81 | - */ |
|
| 82 | - public function isButton() |
|
| 83 | - { |
|
| 84 | - return true; |
|
| 85 | - } |
|
| 77 | + /** |
|
| 78 | + * Check if the field is a button |
|
| 79 | + * |
|
| 80 | + * @return boolean |
|
| 81 | + */ |
|
| 82 | + public function isButton() |
|
| 83 | + { |
|
| 84 | + return true; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Prepend an icon to the button |
|
| 89 | - * |
|
| 90 | - * @param string $icon |
|
| 91 | - * @param array $attributes |
|
| 92 | - * |
|
| 93 | - * @return self |
|
| 94 | - */ |
|
| 95 | - public function icon($icon, $attributes = array()) |
|
| 96 | - { |
|
| 97 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes); |
|
| 98 | - $this->value = $icon.' '.$this->value; |
|
| 87 | + /** |
|
| 88 | + * Prepend an icon to the button |
|
| 89 | + * |
|
| 90 | + * @param string $icon |
|
| 91 | + * @param array $attributes |
|
| 92 | + * |
|
| 93 | + * @return self |
|
| 94 | + */ |
|
| 95 | + public function icon($icon, $attributes = array()) |
|
| 96 | + { |
|
| 97 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes); |
|
| 98 | + $this->value = $icon.' '.$this->value; |
|
| 99 | 99 | |
| 100 | - return $this; |
|
| 101 | - } |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Hijack Former's Object model value method |
|
| 105 | - * |
|
| 106 | - * @param string $value The new button text |
|
| 107 | - */ |
|
| 108 | - public function value($value) |
|
| 109 | - { |
|
| 110 | - $this->value = Helpers::translate($value); |
|
| 103 | + /** |
|
| 104 | + * Hijack Former's Object model value method |
|
| 105 | + * |
|
| 106 | + * @param string $value The new button text |
|
| 107 | + */ |
|
| 108 | + public function value($value) |
|
| 109 | + { |
|
| 110 | + $this->value = Helpers::translate($value); |
|
| 111 | 111 | |
| 112 | - return $this; |
|
| 113 | - } |
|
| 112 | + return $this; |
|
| 113 | + } |
|
| 114 | 114 | } |
@@ -8,28 +8,28 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Checkbox extends Checkable |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The current checkable type |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $checkable = 'checkbox'; |
|
| 11 | + /** |
|
| 12 | + * The current checkable type |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $checkable = 'checkbox'; |
|
| 17 | 17 | |
| 18 | - //////////////////////////////////////////////////////////////////// |
|
| 19 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 20 | - //////////////////////////////////////////////////////////////////// |
|
| 18 | + //////////////////////////////////////////////////////////////////// |
|
| 19 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 20 | + //////////////////////////////////////////////////////////////////// |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Create a serie of checkboxes |
|
| 24 | - */ |
|
| 25 | - public function checkboxes() |
|
| 26 | - { |
|
| 27 | - if ($this->isGrouped()) { |
|
| 28 | - // Remove any possible items added by the Populator. |
|
| 29 | - $this->items = array(); |
|
| 30 | - } |
|
| 31 | - $this->items(func_get_args()); |
|
| 22 | + /** |
|
| 23 | + * Create a serie of checkboxes |
|
| 24 | + */ |
|
| 25 | + public function checkboxes() |
|
| 26 | + { |
|
| 27 | + if ($this->isGrouped()) { |
|
| 28 | + // Remove any possible items added by the Populator. |
|
| 29 | + $this->items = array(); |
|
| 30 | + } |
|
| 31 | + $this->items(func_get_args()); |
|
| 32 | 32 | |
| 33 | - return $this; |
|
| 34 | - } |
|
| 33 | + return $this; |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -8,24 +8,24 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Radio extends Checkable |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The current checkable type |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $checkable = 'radio'; |
|
| 11 | + /** |
|
| 12 | + * The current checkable type |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $checkable = 'radio'; |
|
| 17 | 17 | |
| 18 | - //////////////////////////////////////////////////////////////////// |
|
| 19 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 20 | - //////////////////////////////////////////////////////////////////// |
|
| 18 | + //////////////////////////////////////////////////////////////////// |
|
| 19 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 20 | + //////////////////////////////////////////////////////////////////// |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Create a serie of radios |
|
| 24 | - */ |
|
| 25 | - public function radios() |
|
| 26 | - { |
|
| 27 | - $this->items(func_get_args()); |
|
| 22 | + /** |
|
| 23 | + * Create a serie of radios |
|
| 24 | + */ |
|
| 25 | + public function radios() |
|
| 26 | + { |
|
| 27 | + $this->items(func_get_args()); |
|
| 28 | 28 | |
| 29 | - return $this; |
|
| 30 | - } |
|
| 29 | + return $this; |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -8,20 +8,20 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Plaintext extends Field |
| 10 | 10 | { |
| 11 | - //////////////////////////////////////////////////////////////////// |
|
| 12 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 13 | - //////////////////////////////////////////////////////////////////// |
|
| 11 | + //////////////////////////////////////////////////////////////////// |
|
| 12 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 13 | + //////////////////////////////////////////////////////////////////// |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Prints out the current tag |
|
| 17 | - * |
|
| 18 | - * @return string A plain text tag |
|
| 19 | - */ |
|
| 20 | - public function render() |
|
| 21 | - { |
|
| 22 | - $this->addClass($this->app['former.framework']->getPlainTextClasses()); |
|
| 23 | - $this->setId(); |
|
| 15 | + /** |
|
| 16 | + * Prints out the current tag |
|
| 17 | + * |
|
| 18 | + * @return string A plain text tag |
|
| 19 | + */ |
|
| 20 | + public function render() |
|
| 21 | + { |
|
| 22 | + $this->addClass($this->app['former.framework']->getPlainTextClasses()); |
|
| 23 | + $this->setId(); |
|
| 24 | 24 | |
| 25 | - return $this->app['former.framework']->createPlainTextField($this); |
|
| 26 | - } |
|
| 25 | + return $this->app['former.framework']->createPlainTextField($this); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -12,31 +12,31 @@ |
||
| 12 | 12 | class Hidden extends Field |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - //////////////////////////////////////////////////////////////////// |
|
| 16 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 17 | - //////////////////////////////////////////////////////////////////// |
|
| 15 | + //////////////////////////////////////////////////////////////////// |
|
| 16 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 17 | + //////////////////////////////////////////////////////////////////// |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Easier arguments order for hidden fields |
|
| 21 | - * |
|
| 22 | - * @param Container $app The Container |
|
| 23 | - * @param string $type hidden |
|
| 24 | - * @param string $name Field names |
|
| 25 | - * @param string $value Its value |
|
| 26 | - * @param array $attributes Attributes |
|
| 27 | - */ |
|
| 28 | - public function __construct(Container $app, $type, $name, $value, $attributes) |
|
| 29 | - { |
|
| 30 | - parent::__construct($app, $type, $name, '', $value, $attributes); |
|
| 31 | - } |
|
| 19 | + /** |
|
| 20 | + * Easier arguments order for hidden fields |
|
| 21 | + * |
|
| 22 | + * @param Container $app The Container |
|
| 23 | + * @param string $type hidden |
|
| 24 | + * @param string $name Field names |
|
| 25 | + * @param string $value Its value |
|
| 26 | + * @param array $attributes Attributes |
|
| 27 | + */ |
|
| 28 | + public function __construct(Container $app, $type, $name, $value, $attributes) |
|
| 29 | + { |
|
| 30 | + parent::__construct($app, $type, $name, '', $value, $attributes); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Outputs a hidden field |
|
| 35 | - * |
|
| 36 | - * @return string An <input type="hidden" /> |
|
| 37 | - */ |
|
| 38 | - public function render() |
|
| 39 | - { |
|
| 40 | - return HtmlInput::create('hidden', $this->name, Helpers::encode($this->value), $this->attributes)->render(); |
|
| 41 | - } |
|
| 33 | + /** |
|
| 34 | + * Outputs a hidden field |
|
| 35 | + * |
|
| 36 | + * @return string An <input type="hidden" /> |
|
| 37 | + */ |
|
| 38 | + public function render() |
|
| 39 | + { |
|
| 40 | + return HtmlInput::create('hidden', $this->name, Helpers::encode($this->value), $this->attributes)->render(); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -10,146 +10,146 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Input extends Field |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Current datalist stored |
|
| 15 | - * |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $datalist = array(); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Properties to be injected as attributes |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $injectedProperties = array('type', 'name', 'value'); |
|
| 26 | - |
|
| 27 | - //////////////////////////////////////////////////////////////////// |
|
| 28 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 29 | - //////////////////////////////////////////////////////////////////// |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Build an input field |
|
| 33 | - * |
|
| 34 | - * @param Container $app The Container |
|
| 35 | - * @param string $type The input type |
|
| 36 | - * @param string $name Field name |
|
| 37 | - * @param string $label Its label |
|
| 38 | - * @param string $value Its value |
|
| 39 | - * @param array $attributes Attributes |
|
| 40 | - */ |
|
| 41 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 42 | - { |
|
| 43 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 44 | - |
|
| 45 | - // Multiple models population |
|
| 46 | - if (is_array($this->value)) { |
|
| 47 | - $values = array(); |
|
| 48 | - foreach ($this->value as $value) { |
|
| 49 | - $values[] = is_object($value) ? $value->__toString() : $value; |
|
| 50 | - } |
|
| 51 | - if (isset($values)) { |
|
| 52 | - $this->value = implode(', ', $values); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Prints out the current tag |
|
| 59 | - * |
|
| 60 | - * @return string An input tag |
|
| 61 | - */ |
|
| 62 | - public function render() |
|
| 63 | - { |
|
| 64 | - // Particular case of the search element |
|
| 65 | - if ($this->isOfType('search')) { |
|
| 66 | - $this->asSearch(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->setId(); |
|
| 70 | - |
|
| 71 | - // Render main input |
|
| 72 | - $input = parent::render(); |
|
| 73 | - |
|
| 74 | - // If we have a datalist to append, print it out |
|
| 75 | - if ($this->datalist) { |
|
| 76 | - $input .= $this->createDatalist($this->list, $this->datalist); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $input; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Adds a datalist to the current field |
|
| 88 | - * |
|
| 89 | - * @param array $datalist An array to use a source |
|
| 90 | - * @param string $value The field to use as value |
|
| 91 | - * @param string $key The field to use as key |
|
| 92 | - */ |
|
| 93 | - public function useDatalist($datalist, $value = null, $key = null) |
|
| 94 | - { |
|
| 95 | - $datalist = Helpers::queryToArray($datalist, $value, $key); |
|
| 96 | - |
|
| 97 | - $list = $this->list ?: 'datalist_'.$this->name; |
|
| 98 | - |
|
| 99 | - // Create the link to the datalist |
|
| 100 | - $this->list = $list; |
|
| 101 | - $this->datalist = $datalist; |
|
| 102 | - |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Add range to the input |
|
| 108 | - * |
|
| 109 | - * @param integer $min |
|
| 110 | - * @param integer $max |
|
| 111 | - * |
|
| 112 | - * @return self |
|
| 113 | - */ |
|
| 114 | - public function range($min, $max) |
|
| 115 | - { |
|
| 116 | - $this->min($min); |
|
| 117 | - $this->max($max); |
|
| 118 | - |
|
| 119 | - return $this; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - //////////////////////////////////////////////////////////////////// |
|
| 123 | - /////////////////////////////// HELPERS //////////////////////////// |
|
| 124 | - //////////////////////////////////////////////////////////////////// |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Render a text element as a search element |
|
| 128 | - */ |
|
| 129 | - private function asSearch() |
|
| 130 | - { |
|
| 131 | - $this->type = 'text'; |
|
| 132 | - $this->addClass('search-query'); |
|
| 133 | - |
|
| 134 | - return $this; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Renders a datalist |
|
| 139 | - * |
|
| 140 | - * @param string $id The datalist's id attribute |
|
| 141 | - * @param array $values Its values |
|
| 142 | - * |
|
| 143 | - * @return string A <datalist> tag |
|
| 144 | - */ |
|
| 145 | - private function createDatalist($id, $values) |
|
| 146 | - { |
|
| 147 | - $datalist = '<datalist id="'.$id.'">'; |
|
| 148 | - foreach ($values as $key => $value) { |
|
| 149 | - $datalist .= '<option value="'.$value.'">'.$key.'</option>'; |
|
| 150 | - } |
|
| 151 | - $datalist .= '</datalist>'; |
|
| 152 | - |
|
| 153 | - return $datalist; |
|
| 154 | - } |
|
| 13 | + /** |
|
| 14 | + * Current datalist stored |
|
| 15 | + * |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $datalist = array(); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Properties to be injected as attributes |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $injectedProperties = array('type', 'name', 'value'); |
|
| 26 | + |
|
| 27 | + //////////////////////////////////////////////////////////////////// |
|
| 28 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 29 | + //////////////////////////////////////////////////////////////////// |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Build an input field |
|
| 33 | + * |
|
| 34 | + * @param Container $app The Container |
|
| 35 | + * @param string $type The input type |
|
| 36 | + * @param string $name Field name |
|
| 37 | + * @param string $label Its label |
|
| 38 | + * @param string $value Its value |
|
| 39 | + * @param array $attributes Attributes |
|
| 40 | + */ |
|
| 41 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 42 | + { |
|
| 43 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 44 | + |
|
| 45 | + // Multiple models population |
|
| 46 | + if (is_array($this->value)) { |
|
| 47 | + $values = array(); |
|
| 48 | + foreach ($this->value as $value) { |
|
| 49 | + $values[] = is_object($value) ? $value->__toString() : $value; |
|
| 50 | + } |
|
| 51 | + if (isset($values)) { |
|
| 52 | + $this->value = implode(', ', $values); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Prints out the current tag |
|
| 59 | + * |
|
| 60 | + * @return string An input tag |
|
| 61 | + */ |
|
| 62 | + public function render() |
|
| 63 | + { |
|
| 64 | + // Particular case of the search element |
|
| 65 | + if ($this->isOfType('search')) { |
|
| 66 | + $this->asSearch(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->setId(); |
|
| 70 | + |
|
| 71 | + // Render main input |
|
| 72 | + $input = parent::render(); |
|
| 73 | + |
|
| 74 | + // If we have a datalist to append, print it out |
|
| 75 | + if ($this->datalist) { |
|
| 76 | + $input .= $this->createDatalist($this->list, $this->datalist); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $input; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Adds a datalist to the current field |
|
| 88 | + * |
|
| 89 | + * @param array $datalist An array to use a source |
|
| 90 | + * @param string $value The field to use as value |
|
| 91 | + * @param string $key The field to use as key |
|
| 92 | + */ |
|
| 93 | + public function useDatalist($datalist, $value = null, $key = null) |
|
| 94 | + { |
|
| 95 | + $datalist = Helpers::queryToArray($datalist, $value, $key); |
|
| 96 | + |
|
| 97 | + $list = $this->list ?: 'datalist_'.$this->name; |
|
| 98 | + |
|
| 99 | + // Create the link to the datalist |
|
| 100 | + $this->list = $list; |
|
| 101 | + $this->datalist = $datalist; |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Add range to the input |
|
| 108 | + * |
|
| 109 | + * @param integer $min |
|
| 110 | + * @param integer $max |
|
| 111 | + * |
|
| 112 | + * @return self |
|
| 113 | + */ |
|
| 114 | + public function range($min, $max) |
|
| 115 | + { |
|
| 116 | + $this->min($min); |
|
| 117 | + $this->max($max); |
|
| 118 | + |
|
| 119 | + return $this; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + //////////////////////////////////////////////////////////////////// |
|
| 123 | + /////////////////////////////// HELPERS //////////////////////////// |
|
| 124 | + //////////////////////////////////////////////////////////////////// |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Render a text element as a search element |
|
| 128 | + */ |
|
| 129 | + private function asSearch() |
|
| 130 | + { |
|
| 131 | + $this->type = 'text'; |
|
| 132 | + $this->addClass('search-query'); |
|
| 133 | + |
|
| 134 | + return $this; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Renders a datalist |
|
| 139 | + * |
|
| 140 | + * @param string $id The datalist's id attribute |
|
| 141 | + * @param array $values Its values |
|
| 142 | + * |
|
| 143 | + * @return string A <datalist> tag |
|
| 144 | + */ |
|
| 145 | + private function createDatalist($id, $values) |
|
| 146 | + { |
|
| 147 | + $datalist = '<datalist id="'.$id.'">'; |
|
| 148 | + foreach ($values as $key => $value) { |
|
| 149 | + $datalist .= '<option value="'.$value.'">'.$key.'</option>'; |
|
| 150 | + } |
|
| 151 | + $datalist .= '</datalist>'; |
|
| 152 | + |
|
| 153 | + return $datalist; |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -9,21 +9,21 @@ |
||
| 9 | 9 | class Uneditable extends Field |
| 10 | 10 | { |
| 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 An uneditable input tag |
|
| 20 | - */ |
|
| 21 | - public function render() |
|
| 22 | - { |
|
| 23 | - $this->addClass($this->app['former.framework']->getUneditableClasses()); |
|
| 16 | + /** |
|
| 17 | + * Prints out the current tag |
|
| 18 | + * |
|
| 19 | + * @return string An uneditable input tag |
|
| 20 | + */ |
|
| 21 | + public function render() |
|
| 22 | + { |
|
| 23 | + $this->addClass($this->app['former.framework']->getUneditableClasses()); |
|
| 24 | 24 | |
| 25 | - $this->setId(); |
|
| 25 | + $this->setId(); |
|
| 26 | 26 | |
| 27 | - return $this->app['former.framework']->createDisabledField($this); |
|
| 28 | - } |
|
| 27 | + return $this->app['former.framework']->createDisabledField($this); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -12,127 +12,127 @@ |
||
| 12 | 12 | class File extends Field |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The maximum file size |
|
| 17 | - * |
|
| 18 | - * @var integer |
|
| 19 | - */ |
|
| 20 | - private $maxSize; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * An array of mime groups to use as shortcuts |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private $mimeGroups = array('audio', 'video', 'image'); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * A list of properties to be injected in the attributes |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - protected $injectedProperties = array('type', 'name'); |
|
| 35 | - |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 38 | - //////////////////////////////////////////////////////////////////// |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Easier arguments order for hidden fields |
|
| 42 | - * |
|
| 43 | - * @param Container $app The Illuminate Container |
|
| 44 | - * @param string $type file |
|
| 45 | - * @param string $name Field name |
|
| 46 | - * @param string $label Its label |
|
| 47 | - * @param string $value Its value |
|
| 48 | - * @param array $attributes Attributes |
|
| 49 | - */ |
|
| 50 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 51 | - { |
|
| 52 | - // Multiple files field |
|
| 53 | - if ($type == 'files') { |
|
| 54 | - $attributes['multiple'] = 'true'; |
|
| 55 | - $type = 'file'; |
|
| 56 | - $name = $name.'[]'; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Prints out the current tag |
|
| 64 | - * |
|
| 65 | - * @return string An input file tag |
|
| 66 | - */ |
|
| 67 | - public function render() |
|
| 68 | - { |
|
| 69 | - // Maximum file size |
|
| 70 | - $hidden = $this->maxSize |
|
| 71 | - ? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize) |
|
| 72 | - : null; |
|
| 73 | - |
|
| 74 | - return $hidden.parent::render(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - //////////////////////////////////////////////////////////////////// |
|
| 78 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 79 | - //////////////////////////////////////////////////////////////////// |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Set which types of files are accepted by the file input |
|
| 83 | - */ |
|
| 84 | - public function accept() |
|
| 85 | - { |
|
| 86 | - $mimes = array(); |
|
| 87 | - |
|
| 88 | - // Transform all extensions/groups to mime types |
|
| 89 | - foreach (func_get_args() as $mime) { |
|
| 90 | - |
|
| 91 | - // Shortcuts and extensions |
|
| 92 | - if (in_array($mime, $this->mimeGroups)) { |
|
| 93 | - $mime .= '/*'; |
|
| 94 | - } |
|
| 95 | - $mime = LaravelFile::mime($mime, $mime); |
|
| 96 | - |
|
| 97 | - $mimes[] = $mime; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Add accept attribute by concatenating the mimes |
|
| 101 | - $this->attributes['accept'] = implode(',', $mimes); |
|
| 102 | - |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Set a maximum size for files |
|
| 108 | - * |
|
| 109 | - * @param integer $size A maximum size |
|
| 110 | - * @param string $units The size's unit |
|
| 111 | - */ |
|
| 112 | - public function max($size, $units = 'KB') |
|
| 113 | - { |
|
| 114 | - // Bytes or bits ? |
|
| 115 | - $unit = substr($units, -1); |
|
| 116 | - $base = 1024; |
|
| 117 | - if ($unit == 'b') { |
|
| 118 | - $size = $size / 8; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Convert |
|
| 122 | - switch ($units[0]) { |
|
| 123 | - case 'K': |
|
| 124 | - $size = $size * $base; |
|
| 125 | - break; |
|
| 126 | - case 'M': |
|
| 127 | - $size = $size * pow($base, 2); |
|
| 128 | - break; |
|
| 129 | - case 'G': |
|
| 130 | - $size = $size * pow($base, 3); |
|
| 131 | - break; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $this->maxSize = (int) $size; |
|
| 135 | - |
|
| 136 | - return $this; |
|
| 137 | - } |
|
| 15 | + /** |
|
| 16 | + * The maximum file size |
|
| 17 | + * |
|
| 18 | + * @var integer |
|
| 19 | + */ |
|
| 20 | + private $maxSize; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * An array of mime groups to use as shortcuts |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private $mimeGroups = array('audio', 'video', 'image'); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * A list of properties to be injected in the attributes |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + protected $injectedProperties = array('type', 'name'); |
|
| 35 | + |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 38 | + //////////////////////////////////////////////////////////////////// |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Easier arguments order for hidden fields |
|
| 42 | + * |
|
| 43 | + * @param Container $app The Illuminate Container |
|
| 44 | + * @param string $type file |
|
| 45 | + * @param string $name Field name |
|
| 46 | + * @param string $label Its label |
|
| 47 | + * @param string $value Its value |
|
| 48 | + * @param array $attributes Attributes |
|
| 49 | + */ |
|
| 50 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 51 | + { |
|
| 52 | + // Multiple files field |
|
| 53 | + if ($type == 'files') { |
|
| 54 | + $attributes['multiple'] = 'true'; |
|
| 55 | + $type = 'file'; |
|
| 56 | + $name = $name.'[]'; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Prints out the current tag |
|
| 64 | + * |
|
| 65 | + * @return string An input file tag |
|
| 66 | + */ |
|
| 67 | + public function render() |
|
| 68 | + { |
|
| 69 | + // Maximum file size |
|
| 70 | + $hidden = $this->maxSize |
|
| 71 | + ? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize) |
|
| 72 | + : null; |
|
| 73 | + |
|
| 74 | + return $hidden.parent::render(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + //////////////////////////////////////////////////////////////////// |
|
| 78 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 79 | + //////////////////////////////////////////////////////////////////// |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Set which types of files are accepted by the file input |
|
| 83 | + */ |
|
| 84 | + public function accept() |
|
| 85 | + { |
|
| 86 | + $mimes = array(); |
|
| 87 | + |
|
| 88 | + // Transform all extensions/groups to mime types |
|
| 89 | + foreach (func_get_args() as $mime) { |
|
| 90 | + |
|
| 91 | + // Shortcuts and extensions |
|
| 92 | + if (in_array($mime, $this->mimeGroups)) { |
|
| 93 | + $mime .= '/*'; |
|
| 94 | + } |
|
| 95 | + $mime = LaravelFile::mime($mime, $mime); |
|
| 96 | + |
|
| 97 | + $mimes[] = $mime; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Add accept attribute by concatenating the mimes |
|
| 101 | + $this->attributes['accept'] = implode(',', $mimes); |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Set a maximum size for files |
|
| 108 | + * |
|
| 109 | + * @param integer $size A maximum size |
|
| 110 | + * @param string $units The size's unit |
|
| 111 | + */ |
|
| 112 | + public function max($size, $units = 'KB') |
|
| 113 | + { |
|
| 114 | + // Bytes or bits ? |
|
| 115 | + $unit = substr($units, -1); |
|
| 116 | + $base = 1024; |
|
| 117 | + if ($unit == 'b') { |
|
| 118 | + $size = $size / 8; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Convert |
|
| 122 | + switch ($units[0]) { |
|
| 123 | + case 'K': |
|
| 124 | + $size = $size * $base; |
|
| 125 | + break; |
|
| 126 | + case 'M': |
|
| 127 | + $size = $size * pow($base, 2); |
|
| 128 | + break; |
|
| 129 | + case 'G': |
|
| 130 | + $size = $size * pow($base, 3); |
|
| 131 | + break; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $this->maxSize = (int) $size; |
|
| 135 | + |
|
| 136 | + return $this; |
|
| 137 | + } |
|
| 138 | 138 | } |