@@ -239,7 +239,7 @@ |
||
| 239 | 239 | */ |
| 240 | 240 | protected function prependWith($classes, $with) |
| 241 | 241 | { |
| 242 | - return array_map(function ($class) use ($with) { |
|
| 242 | + return array_map(function($class) use ($with) { |
|
| 243 | 243 | return $with.$class; |
| 244 | 244 | }, $classes); |
| 245 | 245 | } |
@@ -8,305 +8,305 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | abstract class Framework |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The Container |
|
| 13 | - * |
|
| 14 | - * @var Container |
|
| 15 | - */ |
|
| 16 | - protected $app; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Form types that trigger special styling |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $availableTypes = array(); |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * The field states available |
|
| 27 | - * |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $states = array(); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The default label width (for horizontal forms) |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $labelWidth; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The default field width (for horizontal forms) |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - protected $fieldWidth; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The default offset for fields (for horizontal form fields |
|
| 48 | - * with no label, so usually equal to the default label width) |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $fieldOffset; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The default HTML tag used for icons |
|
| 56 | - * |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - protected $iconTag; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The default set for icon fonts |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $iconSet; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * The default prefix icon names |
|
| 70 | - * |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - protected $iconPrefix; |
|
| 74 | - |
|
| 75 | - //////////////////////////////////////////////////////////////////// |
|
| 76 | - //////////////////////// CURRENT FRAMEWORK ///////////////////////// |
|
| 77 | - //////////////////////////////////////////////////////////////////// |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get the name of the current framework |
|
| 81 | - * |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 84 | - public function current() |
|
| 85 | - { |
|
| 86 | - return basename(str_replace('\\', '/', get_class($this))); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Check if the current framework matches something |
|
| 91 | - * |
|
| 92 | - * @param string $framework |
|
| 93 | - * |
|
| 94 | - * @return boolean |
|
| 95 | - */ |
|
| 96 | - public function is($framework) |
|
| 97 | - { |
|
| 98 | - return $framework == $this->current(); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Check if the current framework doesn't match something |
|
| 103 | - * |
|
| 104 | - * @param string $framework |
|
| 105 | - * |
|
| 106 | - * @return boolean |
|
| 107 | - */ |
|
| 108 | - public function isnt($framework) |
|
| 109 | - { |
|
| 110 | - return $framework != $this->current(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - //////////////////////////////////////////////////////////////////// |
|
| 114 | - /////////////////////////// COMMON METHODS ///////////////////////// |
|
| 115 | - //////////////////////////////////////////////////////////////////// |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * List form types triggered special styling form current framework |
|
| 119 | - * |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function availableTypes() |
|
| 123 | - { |
|
| 124 | - return $this->availableTypes; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Filter a field state |
|
| 129 | - * |
|
| 130 | - * @param string $state |
|
| 131 | - * |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function filterState($state) |
|
| 135 | - { |
|
| 136 | - // Filter out wrong states |
|
| 137 | - return in_array($state, $this->states) ? $state : null; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Framework error state |
|
| 142 | - * |
|
| 143 | - * @return string |
|
| 144 | - */ |
|
| 145 | - public function errorState() |
|
| 146 | - { |
|
| 147 | - return 'error'; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Returns corresponding inline class of a field |
|
| 152 | - * |
|
| 153 | - * @param Field $field |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function getInlineLabelClass($field) |
|
| 158 | - { |
|
| 159 | - return 'inline'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Set framework defaults from its config file |
|
| 164 | - */ |
|
| 165 | - protected function setFrameworkDefaults() |
|
| 166 | - { |
|
| 167 | - $this->setFieldWidths($this->getFrameworkOption('labelWidths')); |
|
| 168 | - $this->setIconDefaults(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param string $widths |
|
| 173 | - */ |
|
| 174 | - protected function setFieldWidths($widths) |
|
| 175 | - { |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Override framework defaults for icons with config values where set |
|
| 180 | - */ |
|
| 181 | - protected function setIconDefaults() |
|
| 182 | - { |
|
| 183 | - $this->iconTag = $this->getFrameworkOption('icon.tag'); |
|
| 184 | - $this->iconSet = $this->getFrameworkOption('icon.set'); |
|
| 185 | - $this->iconPrefix = $this->getFrameworkOption('icon.prefix'); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Render an icon |
|
| 190 | - * |
|
| 191 | - * @param array $attributes Its general attributes |
|
| 192 | - * |
|
| 193 | - * @return string |
|
| 194 | - */ |
|
| 195 | - public function createIcon($iconType, $attributes = array(), $iconSettings = array()) |
|
| 196 | - { |
|
| 197 | - // Check for empty icons |
|
| 198 | - if (!$iconType) { |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // icon settings can be overridden for a specific icon |
|
| 203 | - $tag = array_get($iconSettings, 'tag', $this->iconTag); |
|
| 204 | - $set = array_get($iconSettings, 'set', $this->iconSet); |
|
| 205 | - $prefix = array_get($iconSettings, 'prefix', $this->iconPrefix); |
|
| 206 | - |
|
| 207 | - return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType"); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - //////////////////////////////////////////////////////////////////// |
|
| 211 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 212 | - //////////////////////////////////////////////////////////////////// |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Add classes to a field |
|
| 216 | - * |
|
| 217 | - * @param Field $field |
|
| 218 | - * @param array $classes |
|
| 219 | - * |
|
| 220 | - * @return \Former\Traits\Field |
|
| 221 | - */ |
|
| 222 | - protected function addClassesToField($field, $classes) |
|
| 223 | - { |
|
| 224 | - // If we found any class, add them |
|
| 225 | - if ($classes) { |
|
| 226 | - $field->addClass(implode(' ', $classes)); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return $field; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Prepend an array of classes with a string |
|
| 234 | - * |
|
| 235 | - * @param array $classes The classes to prepend |
|
| 236 | - * @param string $with The string to prepend them with |
|
| 237 | - * |
|
| 238 | - * @return array A prepended array |
|
| 239 | - */ |
|
| 240 | - protected function prependWith($classes, $with) |
|
| 241 | - { |
|
| 242 | - return array_map(function ($class) use ($with) { |
|
| 243 | - return $with.$class; |
|
| 244 | - }, $classes); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Create a label for a field |
|
| 249 | - * |
|
| 250 | - * @param Field $field |
|
| 251 | - * @param Element $label The field label if non provided |
|
| 252 | - * |
|
| 253 | - * @return string A label |
|
| 254 | - */ |
|
| 255 | - public function createLabelOf(Field $field, Element $label = null) |
|
| 256 | - { |
|
| 257 | - // Get the label and its informations |
|
| 258 | - if (!$label) { |
|
| 259 | - $label = $field->getLabel(); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // Get label "for" |
|
| 263 | - $for = $field->id ?: $field->getName(); |
|
| 264 | - |
|
| 265 | - // Get label text |
|
| 266 | - $text = $label->getValue(); |
|
| 267 | - if (!$text) { |
|
| 268 | - return false; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // Append required text |
|
| 272 | - if ($field->isRequired()) { |
|
| 273 | - $text .= $this->app['former']->getOption('required_text'); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - // Render plain label if checkable, else a classic one |
|
| 277 | - $label->setValue($text); |
|
| 278 | - if (!$field->isCheckable()) { |
|
| 279 | - $label->for($for); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - return $label; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Get an option for the current framework |
|
| 287 | - * |
|
| 288 | - * @param string $option |
|
| 289 | - * |
|
| 290 | - * @return string |
|
| 291 | - */ |
|
| 292 | - protected function getFrameworkOption($option) |
|
| 293 | - { |
|
| 294 | - return $this->app['config']->get("former.{$this->current()}.$option"); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - //////////////////////////////////////////////////////////////////// |
|
| 298 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 299 | - //////////////////////////////////////////////////////////////////// |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Wraps all label contents with potential additional tags. |
|
| 303 | - * |
|
| 304 | - * @param string $label |
|
| 305 | - * |
|
| 306 | - * @return string A wrapped label |
|
| 307 | - */ |
|
| 308 | - public function wrapLabel($label) |
|
| 309 | - { |
|
| 310 | - return $label; |
|
| 311 | - } |
|
| 11 | + /** |
|
| 12 | + * The Container |
|
| 13 | + * |
|
| 14 | + * @var Container |
|
| 15 | + */ |
|
| 16 | + protected $app; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Form types that trigger special styling |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $availableTypes = array(); |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * The field states available |
|
| 27 | + * |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $states = array(); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The default label width (for horizontal forms) |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $labelWidth; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The default field width (for horizontal forms) |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + protected $fieldWidth; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The default offset for fields (for horizontal form fields |
|
| 48 | + * with no label, so usually equal to the default label width) |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $fieldOffset; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The default HTML tag used for icons |
|
| 56 | + * |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + protected $iconTag; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The default set for icon fonts |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $iconSet; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * The default prefix icon names |
|
| 70 | + * |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + protected $iconPrefix; |
|
| 74 | + |
|
| 75 | + //////////////////////////////////////////////////////////////////// |
|
| 76 | + //////////////////////// CURRENT FRAMEWORK ///////////////////////// |
|
| 77 | + //////////////////////////////////////////////////////////////////// |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get the name of the current framework |
|
| 81 | + * |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | + public function current() |
|
| 85 | + { |
|
| 86 | + return basename(str_replace('\\', '/', get_class($this))); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Check if the current framework matches something |
|
| 91 | + * |
|
| 92 | + * @param string $framework |
|
| 93 | + * |
|
| 94 | + * @return boolean |
|
| 95 | + */ |
|
| 96 | + public function is($framework) |
|
| 97 | + { |
|
| 98 | + return $framework == $this->current(); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Check if the current framework doesn't match something |
|
| 103 | + * |
|
| 104 | + * @param string $framework |
|
| 105 | + * |
|
| 106 | + * @return boolean |
|
| 107 | + */ |
|
| 108 | + public function isnt($framework) |
|
| 109 | + { |
|
| 110 | + return $framework != $this->current(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + //////////////////////////////////////////////////////////////////// |
|
| 114 | + /////////////////////////// COMMON METHODS ///////////////////////// |
|
| 115 | + //////////////////////////////////////////////////////////////////// |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * List form types triggered special styling form current framework |
|
| 119 | + * |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function availableTypes() |
|
| 123 | + { |
|
| 124 | + return $this->availableTypes; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Filter a field state |
|
| 129 | + * |
|
| 130 | + * @param string $state |
|
| 131 | + * |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function filterState($state) |
|
| 135 | + { |
|
| 136 | + // Filter out wrong states |
|
| 137 | + return in_array($state, $this->states) ? $state : null; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Framework error state |
|
| 142 | + * |
|
| 143 | + * @return string |
|
| 144 | + */ |
|
| 145 | + public function errorState() |
|
| 146 | + { |
|
| 147 | + return 'error'; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Returns corresponding inline class of a field |
|
| 152 | + * |
|
| 153 | + * @param Field $field |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function getInlineLabelClass($field) |
|
| 158 | + { |
|
| 159 | + return 'inline'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Set framework defaults from its config file |
|
| 164 | + */ |
|
| 165 | + protected function setFrameworkDefaults() |
|
| 166 | + { |
|
| 167 | + $this->setFieldWidths($this->getFrameworkOption('labelWidths')); |
|
| 168 | + $this->setIconDefaults(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param string $widths |
|
| 173 | + */ |
|
| 174 | + protected function setFieldWidths($widths) |
|
| 175 | + { |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Override framework defaults for icons with config values where set |
|
| 180 | + */ |
|
| 181 | + protected function setIconDefaults() |
|
| 182 | + { |
|
| 183 | + $this->iconTag = $this->getFrameworkOption('icon.tag'); |
|
| 184 | + $this->iconSet = $this->getFrameworkOption('icon.set'); |
|
| 185 | + $this->iconPrefix = $this->getFrameworkOption('icon.prefix'); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Render an icon |
|
| 190 | + * |
|
| 191 | + * @param array $attributes Its general attributes |
|
| 192 | + * |
|
| 193 | + * @return string |
|
| 194 | + */ |
|
| 195 | + public function createIcon($iconType, $attributes = array(), $iconSettings = array()) |
|
| 196 | + { |
|
| 197 | + // Check for empty icons |
|
| 198 | + if (!$iconType) { |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // icon settings can be overridden for a specific icon |
|
| 203 | + $tag = array_get($iconSettings, 'tag', $this->iconTag); |
|
| 204 | + $set = array_get($iconSettings, 'set', $this->iconSet); |
|
| 205 | + $prefix = array_get($iconSettings, 'prefix', $this->iconPrefix); |
|
| 206 | + |
|
| 207 | + return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType"); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + //////////////////////////////////////////////////////////////////// |
|
| 211 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 212 | + //////////////////////////////////////////////////////////////////// |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Add classes to a field |
|
| 216 | + * |
|
| 217 | + * @param Field $field |
|
| 218 | + * @param array $classes |
|
| 219 | + * |
|
| 220 | + * @return \Former\Traits\Field |
|
| 221 | + */ |
|
| 222 | + protected function addClassesToField($field, $classes) |
|
| 223 | + { |
|
| 224 | + // If we found any class, add them |
|
| 225 | + if ($classes) { |
|
| 226 | + $field->addClass(implode(' ', $classes)); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return $field; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Prepend an array of classes with a string |
|
| 234 | + * |
|
| 235 | + * @param array $classes The classes to prepend |
|
| 236 | + * @param string $with The string to prepend them with |
|
| 237 | + * |
|
| 238 | + * @return array A prepended array |
|
| 239 | + */ |
|
| 240 | + protected function prependWith($classes, $with) |
|
| 241 | + { |
|
| 242 | + return array_map(function ($class) use ($with) { |
|
| 243 | + return $with.$class; |
|
| 244 | + }, $classes); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Create a label for a field |
|
| 249 | + * |
|
| 250 | + * @param Field $field |
|
| 251 | + * @param Element $label The field label if non provided |
|
| 252 | + * |
|
| 253 | + * @return string A label |
|
| 254 | + */ |
|
| 255 | + public function createLabelOf(Field $field, Element $label = null) |
|
| 256 | + { |
|
| 257 | + // Get the label and its informations |
|
| 258 | + if (!$label) { |
|
| 259 | + $label = $field->getLabel(); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // Get label "for" |
|
| 263 | + $for = $field->id ?: $field->getName(); |
|
| 264 | + |
|
| 265 | + // Get label text |
|
| 266 | + $text = $label->getValue(); |
|
| 267 | + if (!$text) { |
|
| 268 | + return false; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // Append required text |
|
| 272 | + if ($field->isRequired()) { |
|
| 273 | + $text .= $this->app['former']->getOption('required_text'); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + // Render plain label if checkable, else a classic one |
|
| 277 | + $label->setValue($text); |
|
| 278 | + if (!$field->isCheckable()) { |
|
| 279 | + $label->for($for); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + return $label; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Get an option for the current framework |
|
| 287 | + * |
|
| 288 | + * @param string $option |
|
| 289 | + * |
|
| 290 | + * @return string |
|
| 291 | + */ |
|
| 292 | + protected function getFrameworkOption($option) |
|
| 293 | + { |
|
| 294 | + return $this->app['config']->get("former.{$this->current()}.$option"); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + //////////////////////////////////////////////////////////////////// |
|
| 298 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 299 | + //////////////////////////////////////////////////////////////////// |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Wraps all label contents with potential additional tags. |
|
| 303 | + * |
|
| 304 | + * @param string $label |
|
| 305 | + * |
|
| 306 | + * @return string A wrapped label |
|
| 307 | + */ |
|
| 308 | + public function wrapLabel($label) |
|
| 309 | + { |
|
| 310 | + return $label; |
|
| 311 | + } |
|
| 312 | 312 | } |
@@ -411,7 +411,7 @@ |
||
| 411 | 411 | $this->app['former']->labels[] = $name; |
| 412 | 412 | |
| 413 | 413 | // Count number of fields with the same ID |
| 414 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 414 | + $where = array_filter($this->app['former']->labels, function($label) use ($name) { |
|
| 415 | 415 | return $label == $name; |
| 416 | 416 | }); |
| 417 | 417 | $unique = sizeof($where); |
@@ -12,548 +12,548 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class Checkable extends Field |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Renders the checkables as inline |
|
| 17 | - * |
|
| 18 | - * @var boolean |
|
| 19 | - */ |
|
| 20 | - protected $inline = false; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Add a text to a single element |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $text = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Renders the checkables as grouped |
|
| 31 | - * |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $grouped = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The checkable items currently stored |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $items = array(); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The type of checkable item |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $checkable = null; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * An array of checked items |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $checked = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The checkable currently being focused on |
|
| 59 | - * |
|
| 60 | - * @var integer |
|
| 61 | - */ |
|
| 62 | - protected $focus = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Whether this particular checkable is to be pushed |
|
| 66 | - * |
|
| 67 | - * @var boolean |
|
| 68 | - */ |
|
| 69 | - protected $isPushed = null; |
|
| 70 | - |
|
| 71 | - //////////////////////////////////////////////////////////////////// |
|
| 72 | - //////////////////////////// CORE METHODS ////////////////////////// |
|
| 73 | - //////////////////////////////////////////////////////////////////// |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Build a new checkable |
|
| 77 | - * |
|
| 78 | - * @param Container $app |
|
| 79 | - * @param string $type |
|
| 80 | - * @param array $name |
|
| 81 | - * @param $label |
|
| 82 | - * @param $value |
|
| 83 | - * @param $attributes |
|
| 84 | - */ |
|
| 85 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 86 | - { |
|
| 87 | - // Unify auto and chained methods of grouping checkboxes |
|
| 88 | - if (ends_with($name, '[]')) { |
|
| 89 | - $name = substr($name, 0, -2); |
|
| 90 | - $this->grouped(); |
|
| 91 | - } |
|
| 92 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 93 | - |
|
| 94 | - if (is_array($this->value)) { |
|
| 95 | - $this->items($this->value); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Apply methods to focused checkable |
|
| 101 | - * |
|
| 102 | - * @param string $method |
|
| 103 | - * @param array $parameters |
|
| 104 | - * |
|
| 105 | - * @return $this |
|
| 106 | - */ |
|
| 107 | - public function __call($method, $parameters) |
|
| 108 | - { |
|
| 109 | - $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0)); |
|
| 110 | - if ($focused) { |
|
| 111 | - return $this; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return parent::__call($method, $parameters); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Prints out the currently stored checkables |
|
| 119 | - */ |
|
| 120 | - public function render() |
|
| 121 | - { |
|
| 122 | - $html = null; |
|
| 123 | - |
|
| 124 | - // Multiple items |
|
| 125 | - if ($this->items) { |
|
| 126 | - unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 127 | - foreach ($this->items as $key => $item) { |
|
| 128 | - $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 129 | - $html .= $this->createCheckable($item, $value); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $html; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Single item |
|
| 136 | - return $this->createCheckable(array( |
|
| 137 | - 'name' => $this->name, |
|
| 138 | - 'label' => $this->text, |
|
| 139 | - 'value' => $this->value, |
|
| 140 | - 'attributes' => $this->attributes, |
|
| 141 | - )); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - //////////////////////////////////////////////////////////////////// |
|
| 145 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 146 | - //////////////////////////////////////////////////////////////////// |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Focus on a particular checkable |
|
| 150 | - * |
|
| 151 | - * @param integer $on The checkable to focus on |
|
| 152 | - * |
|
| 153 | - * @return $this |
|
| 154 | - */ |
|
| 155 | - public function on($on) |
|
| 156 | - { |
|
| 157 | - if (!isset($this->items[$on])) { |
|
| 158 | - return $this; |
|
| 159 | - } else { |
|
| 160 | - $this->focus = $on; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $this; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Set the checkables as inline |
|
| 168 | - */ |
|
| 169 | - public function inline() |
|
| 170 | - { |
|
| 171 | - $this->inline = true; |
|
| 172 | - |
|
| 173 | - return $this; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Set the checkables as stacked |
|
| 178 | - */ |
|
| 179 | - public function stacked() |
|
| 180 | - { |
|
| 181 | - $this->inline = false; |
|
| 182 | - |
|
| 183 | - return $this; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Set the checkables as grouped |
|
| 188 | - */ |
|
| 189 | - public function grouped() |
|
| 190 | - { |
|
| 191 | - $this->grouped = true; |
|
| 192 | - |
|
| 193 | - return $this; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Add text to a single checkable |
|
| 198 | - * |
|
| 199 | - * @param string $text The checkable label |
|
| 200 | - * |
|
| 201 | - * @return $this |
|
| 202 | - */ |
|
| 203 | - public function text($text) |
|
| 204 | - { |
|
| 205 | - // Translate and format |
|
| 206 | - $text = Helpers::translate($text); |
|
| 207 | - |
|
| 208 | - // Apply on focused if any |
|
| 209 | - $focused = $this->setOnFocused('label', $text); |
|
| 210 | - if ($focused) { |
|
| 211 | - return $this; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $this->text = $text; |
|
| 215 | - |
|
| 216 | - return $this; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Push this particular checkbox |
|
| 221 | - * |
|
| 222 | - * @param boolean $pushed |
|
| 223 | - * |
|
| 224 | - * @return $this |
|
| 225 | - */ |
|
| 226 | - public function push($pushed = true) |
|
| 227 | - { |
|
| 228 | - $this->isPushed = $pushed; |
|
| 229 | - |
|
| 230 | - return $this; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Check a specific item |
|
| 235 | - * |
|
| 236 | - * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 237 | - * |
|
| 238 | - * @return $this |
|
| 239 | - */ |
|
| 240 | - public function check($checked = true) |
|
| 241 | - { |
|
| 242 | - // If we're setting all the checked items at once |
|
| 243 | - if (is_array($checked)) { |
|
| 244 | - $this->checked = $checked; |
|
| 245 | - // Checking an item in particular |
|
| 246 | - } elseif (is_string($checked) or is_int($checked)) { |
|
| 247 | - $this->checked[$checked] = true; |
|
| 248 | - // Only setting a single item |
|
| 249 | - } else { |
|
| 250 | - $this->checked[$this->name] = (bool) $checked; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - //////////////////////////////////////////////////////////////////// |
|
| 257 | - ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 258 | - //////////////////////////////////////////////////////////////////// |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Creates a series of checkable items |
|
| 262 | - * |
|
| 263 | - * @param array $_items Items to create |
|
| 264 | - */ |
|
| 265 | - protected function items($_items) |
|
| 266 | - { |
|
| 267 | - // If passing an array |
|
| 268 | - if (sizeof($_items) == 1 and |
|
| 269 | - isset($_items[0]) and |
|
| 270 | - is_array($_items[0]) |
|
| 271 | - ) { |
|
| 272 | - $_items = $_items[0]; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // Fetch models if that's what we were passed |
|
| 276 | - if (isset($_items[0]) and is_object($_items[0])) { |
|
| 277 | - $_items = Helpers::queryToArray($_items); |
|
| 278 | - $_items = array_flip($_items); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // Iterate through items, assign a name and a label to each |
|
| 282 | - $count = 0; |
|
| 283 | - foreach ($_items as $label => $name) { |
|
| 284 | - |
|
| 285 | - // Define a fallback name in case none is found |
|
| 286 | - $fallback = $this->isCheckbox() |
|
| 287 | - ? $this->name.'_'.$count |
|
| 288 | - : $this->name; |
|
| 289 | - |
|
| 290 | - // Grouped fields |
|
| 291 | - if ($this->isGrouped()) { |
|
| 292 | - $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 293 | - $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // If we haven't any name defined for the checkable, try to compute some |
|
| 297 | - if (!is_string($label) and !is_array($name)) { |
|
| 298 | - $label = $name; |
|
| 299 | - $name = $fallback; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - // If we gave custom information on the item, add them |
|
| 303 | - if (is_array($name)) { |
|
| 304 | - $attributes = $name; |
|
| 305 | - $name = array_get($attributes, 'name', $fallback); |
|
| 306 | - unset($attributes['name']); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - // Store all informations we have in an array |
|
| 310 | - $item = array( |
|
| 311 | - 'name' => $name, |
|
| 312 | - 'label' => Helpers::translate($label), |
|
| 313 | - 'count' => $count, |
|
| 314 | - ); |
|
| 315 | - if (isset($attributes)) { |
|
| 316 | - $item['attributes'] = $attributes; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - $this->items[] = $item; |
|
| 320 | - $count++; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Renders a checkable |
|
| 326 | - * |
|
| 327 | - * @param string|array $item A checkable item |
|
| 328 | - * @param integer $fallbackValue A fallback value if none is set |
|
| 329 | - * |
|
| 330 | - * @return string |
|
| 331 | - */ |
|
| 332 | - protected function createCheckable($item, $fallbackValue = 1) |
|
| 333 | - { |
|
| 334 | - // Extract informations |
|
| 335 | - extract($item); |
|
| 336 | - |
|
| 337 | - // Set default values |
|
| 338 | - if (!isset($attributes)) { |
|
| 339 | - $attributes = array(); |
|
| 340 | - } |
|
| 341 | - if (isset($attributes['value'])) { |
|
| 342 | - $value = $attributes['value']; |
|
| 343 | - } |
|
| 344 | - if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 345 | - $value = $fallbackValue; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - // If inline items, add class |
|
| 349 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 350 | - |
|
| 351 | - // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 352 | - // rendering inline. |
|
| 353 | - $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $this->checkable.$isInline; |
|
| 354 | - |
|
| 355 | - // Merge custom attributes with global attributes |
|
| 356 | - $attributes = array_merge($this->attributes, $attributes); |
|
| 357 | - if (!isset($attributes['id'])) { |
|
| 358 | - $attributes['id'] = $name.$this->unique($name); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - // Create field |
|
| 362 | - $field = Input::create($this->checkable, $name, $value, $attributes); |
|
| 363 | - if ($this->isChecked($item, $value)) { |
|
| 364 | - $field->checked('checked'); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - // Add hidden checkbox if requested |
|
| 368 | - if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 369 | - if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 370 | - $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 371 | - |
|
| 372 | - // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 373 | - // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 374 | - $this->app->instance('former.field', $this); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // If no label to wrap, return plain checkable |
|
| 379 | - if (!$label) { |
|
| 380 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 381 | - } else { |
|
| 382 | - $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 386 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 387 | - $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 388 | - if ($this->getAttribute('disabled')) { |
|
| 389 | - $wrapper->addClass('disabled'); |
|
| 390 | - } |
|
| 391 | - $element = $wrapper->render(); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - // Return the field |
|
| 395 | - return $element; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - //////////////////////////////////////////////////////////////////// |
|
| 399 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 400 | - //////////////////////////////////////////////////////////////////// |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Generate an unique ID for a field |
|
| 404 | - * |
|
| 405 | - * @param string $name The field's name |
|
| 406 | - * |
|
| 407 | - * @return string A field number to use |
|
| 408 | - */ |
|
| 409 | - protected function unique($name) |
|
| 410 | - { |
|
| 411 | - $this->app['former']->labels[] = $name; |
|
| 412 | - |
|
| 413 | - // Count number of fields with the same ID |
|
| 414 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 415 | - return $label == $name; |
|
| 416 | - }); |
|
| 417 | - $unique = sizeof($where); |
|
| 418 | - |
|
| 419 | - // In case the field doesn't need to be numbered |
|
| 420 | - if ($unique < 2 or empty($this->items)) { |
|
| 421 | - return false; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - return $unique; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Set something on the currently focused checkable |
|
| 429 | - * |
|
| 430 | - * @param string $attribute The key to set |
|
| 431 | - * @param string $value Its value |
|
| 432 | - * |
|
| 433 | - * @return $this|bool |
|
| 434 | - */ |
|
| 435 | - protected function setOnFocused($attribute, $value) |
|
| 436 | - { |
|
| 437 | - if (is_null($this->focus)) { |
|
| 438 | - return false; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value); |
|
| 442 | - |
|
| 443 | - return $this; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * Check if a checkable is checked |
|
| 448 | - * |
|
| 449 | - * @return boolean Checked or not |
|
| 450 | - */ |
|
| 451 | - protected function isChecked($item = null, $value = null) |
|
| 452 | - { |
|
| 453 | - if (isset($item['name'])) { |
|
| 454 | - $name = $item['name']; |
|
| 455 | - } |
|
| 456 | - if (empty($name)) { |
|
| 457 | - $name = $this->name; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 461 | - // Or if it's a single radio, simply see if we called check |
|
| 462 | - if ($this->isCheckbox() or |
|
| 463 | - !$this->isCheckbox() and !$this->items |
|
| 464 | - ) { |
|
| 465 | - $checked = array_get($this->checked, $name, false); |
|
| 466 | - |
|
| 467 | - // If there are multiple, search for the value |
|
| 468 | - // as the name are the same between radios |
|
| 469 | - } else { |
|
| 470 | - $checked = array_get($this->checked, $value, false); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // Check the values and POST array |
|
| 474 | - if ($this->isGrouped()) { |
|
| 475 | - // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 476 | - $groupIndex = self::getGroupIndexFromItem($item); |
|
| 477 | - |
|
| 478 | - // Search using the bare name, not the individual item name |
|
| 479 | - $post = $this->app['former']->getPost($this->name); |
|
| 480 | - $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 481 | - |
|
| 482 | - if (isset($post[$groupIndex])) { |
|
| 483 | - $post = $post[$groupIndex]; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 488 | - * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 489 | - * |
|
| 490 | - * array( |
|
| 491 | - * 'name' = 'roles[foo]', |
|
| 492 | - * 'value' => 'foo', |
|
| 493 | - * ) |
|
| 494 | - */ |
|
| 495 | - if ($static instanceof Collection) { |
|
| 496 | - // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 497 | - foreach ($static as $staticItem) { |
|
| 498 | - $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 499 | - if ($staticItemValue == $groupIndex) { |
|
| 500 | - $static = $staticItemValue; |
|
| 501 | - break; |
|
| 502 | - } |
|
| 503 | - } |
|
| 504 | - } else if (isset($static[$groupIndex])) { |
|
| 505 | - $static = $static[$groupIndex]; |
|
| 506 | - } |
|
| 507 | - } else { |
|
| 508 | - $post = $this->app['former']->getPost($name); |
|
| 509 | - $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 513 | - $isChecked = ($post == $value); |
|
| 514 | - } elseif (!is_null($static)) { |
|
| 515 | - $isChecked = ($static == $value); |
|
| 516 | - } else { |
|
| 517 | - $isChecked = $checked; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - return $isChecked ? true : false; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Check if the current element is a checkbox |
|
| 525 | - * |
|
| 526 | - * @return boolean Checkbox or radio |
|
| 527 | - */ |
|
| 528 | - protected function isCheckbox() |
|
| 529 | - { |
|
| 530 | - return $this->checkable == 'checkbox'; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - /** |
|
| 534 | - * Check if the checkables are grouped or not |
|
| 535 | - * |
|
| 536 | - * @return boolean |
|
| 537 | - */ |
|
| 538 | - protected function isGrouped() |
|
| 539 | - { |
|
| 540 | - return |
|
| 541 | - $this->grouped == true or |
|
| 542 | - strpos($this->name, '[]') !== false; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * @param array $item The item array, containing at least name and count keys. |
|
| 547 | - * |
|
| 548 | - * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 549 | - */ |
|
| 550 | - public static function getGroupIndexFromItem($item) |
|
| 551 | - { |
|
| 552 | - $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 553 | - if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 554 | - return $item['count']; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - return $groupIndex; |
|
| 558 | - } |
|
| 15 | + /** |
|
| 16 | + * Renders the checkables as inline |
|
| 17 | + * |
|
| 18 | + * @var boolean |
|
| 19 | + */ |
|
| 20 | + protected $inline = false; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Add a text to a single element |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $text = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Renders the checkables as grouped |
|
| 31 | + * |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $grouped = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The checkable items currently stored |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $items = array(); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The type of checkable item |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $checkable = null; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * An array of checked items |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $checked = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The checkable currently being focused on |
|
| 59 | + * |
|
| 60 | + * @var integer |
|
| 61 | + */ |
|
| 62 | + protected $focus = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Whether this particular checkable is to be pushed |
|
| 66 | + * |
|
| 67 | + * @var boolean |
|
| 68 | + */ |
|
| 69 | + protected $isPushed = null; |
|
| 70 | + |
|
| 71 | + //////////////////////////////////////////////////////////////////// |
|
| 72 | + //////////////////////////// CORE METHODS ////////////////////////// |
|
| 73 | + //////////////////////////////////////////////////////////////////// |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Build a new checkable |
|
| 77 | + * |
|
| 78 | + * @param Container $app |
|
| 79 | + * @param string $type |
|
| 80 | + * @param array $name |
|
| 81 | + * @param $label |
|
| 82 | + * @param $value |
|
| 83 | + * @param $attributes |
|
| 84 | + */ |
|
| 85 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 86 | + { |
|
| 87 | + // Unify auto and chained methods of grouping checkboxes |
|
| 88 | + if (ends_with($name, '[]')) { |
|
| 89 | + $name = substr($name, 0, -2); |
|
| 90 | + $this->grouped(); |
|
| 91 | + } |
|
| 92 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 93 | + |
|
| 94 | + if (is_array($this->value)) { |
|
| 95 | + $this->items($this->value); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Apply methods to focused checkable |
|
| 101 | + * |
|
| 102 | + * @param string $method |
|
| 103 | + * @param array $parameters |
|
| 104 | + * |
|
| 105 | + * @return $this |
|
| 106 | + */ |
|
| 107 | + public function __call($method, $parameters) |
|
| 108 | + { |
|
| 109 | + $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0)); |
|
| 110 | + if ($focused) { |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return parent::__call($method, $parameters); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Prints out the currently stored checkables |
|
| 119 | + */ |
|
| 120 | + public function render() |
|
| 121 | + { |
|
| 122 | + $html = null; |
|
| 123 | + |
|
| 124 | + // Multiple items |
|
| 125 | + if ($this->items) { |
|
| 126 | + unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 127 | + foreach ($this->items as $key => $item) { |
|
| 128 | + $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 129 | + $html .= $this->createCheckable($item, $value); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $html; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Single item |
|
| 136 | + return $this->createCheckable(array( |
|
| 137 | + 'name' => $this->name, |
|
| 138 | + 'label' => $this->text, |
|
| 139 | + 'value' => $this->value, |
|
| 140 | + 'attributes' => $this->attributes, |
|
| 141 | + )); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + //////////////////////////////////////////////////////////////////// |
|
| 145 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 146 | + //////////////////////////////////////////////////////////////////// |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Focus on a particular checkable |
|
| 150 | + * |
|
| 151 | + * @param integer $on The checkable to focus on |
|
| 152 | + * |
|
| 153 | + * @return $this |
|
| 154 | + */ |
|
| 155 | + public function on($on) |
|
| 156 | + { |
|
| 157 | + if (!isset($this->items[$on])) { |
|
| 158 | + return $this; |
|
| 159 | + } else { |
|
| 160 | + $this->focus = $on; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $this; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Set the checkables as inline |
|
| 168 | + */ |
|
| 169 | + public function inline() |
|
| 170 | + { |
|
| 171 | + $this->inline = true; |
|
| 172 | + |
|
| 173 | + return $this; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Set the checkables as stacked |
|
| 178 | + */ |
|
| 179 | + public function stacked() |
|
| 180 | + { |
|
| 181 | + $this->inline = false; |
|
| 182 | + |
|
| 183 | + return $this; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Set the checkables as grouped |
|
| 188 | + */ |
|
| 189 | + public function grouped() |
|
| 190 | + { |
|
| 191 | + $this->grouped = true; |
|
| 192 | + |
|
| 193 | + return $this; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Add text to a single checkable |
|
| 198 | + * |
|
| 199 | + * @param string $text The checkable label |
|
| 200 | + * |
|
| 201 | + * @return $this |
|
| 202 | + */ |
|
| 203 | + public function text($text) |
|
| 204 | + { |
|
| 205 | + // Translate and format |
|
| 206 | + $text = Helpers::translate($text); |
|
| 207 | + |
|
| 208 | + // Apply on focused if any |
|
| 209 | + $focused = $this->setOnFocused('label', $text); |
|
| 210 | + if ($focused) { |
|
| 211 | + return $this; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $this->text = $text; |
|
| 215 | + |
|
| 216 | + return $this; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Push this particular checkbox |
|
| 221 | + * |
|
| 222 | + * @param boolean $pushed |
|
| 223 | + * |
|
| 224 | + * @return $this |
|
| 225 | + */ |
|
| 226 | + public function push($pushed = true) |
|
| 227 | + { |
|
| 228 | + $this->isPushed = $pushed; |
|
| 229 | + |
|
| 230 | + return $this; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Check a specific item |
|
| 235 | + * |
|
| 236 | + * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 237 | + * |
|
| 238 | + * @return $this |
|
| 239 | + */ |
|
| 240 | + public function check($checked = true) |
|
| 241 | + { |
|
| 242 | + // If we're setting all the checked items at once |
|
| 243 | + if (is_array($checked)) { |
|
| 244 | + $this->checked = $checked; |
|
| 245 | + // Checking an item in particular |
|
| 246 | + } elseif (is_string($checked) or is_int($checked)) { |
|
| 247 | + $this->checked[$checked] = true; |
|
| 248 | + // Only setting a single item |
|
| 249 | + } else { |
|
| 250 | + $this->checked[$this->name] = (bool) $checked; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + //////////////////////////////////////////////////////////////////// |
|
| 257 | + ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 258 | + //////////////////////////////////////////////////////////////////// |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Creates a series of checkable items |
|
| 262 | + * |
|
| 263 | + * @param array $_items Items to create |
|
| 264 | + */ |
|
| 265 | + protected function items($_items) |
|
| 266 | + { |
|
| 267 | + // If passing an array |
|
| 268 | + if (sizeof($_items) == 1 and |
|
| 269 | + isset($_items[0]) and |
|
| 270 | + is_array($_items[0]) |
|
| 271 | + ) { |
|
| 272 | + $_items = $_items[0]; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // Fetch models if that's what we were passed |
|
| 276 | + if (isset($_items[0]) and is_object($_items[0])) { |
|
| 277 | + $_items = Helpers::queryToArray($_items); |
|
| 278 | + $_items = array_flip($_items); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // Iterate through items, assign a name and a label to each |
|
| 282 | + $count = 0; |
|
| 283 | + foreach ($_items as $label => $name) { |
|
| 284 | + |
|
| 285 | + // Define a fallback name in case none is found |
|
| 286 | + $fallback = $this->isCheckbox() |
|
| 287 | + ? $this->name.'_'.$count |
|
| 288 | + : $this->name; |
|
| 289 | + |
|
| 290 | + // Grouped fields |
|
| 291 | + if ($this->isGrouped()) { |
|
| 292 | + $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 293 | + $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // If we haven't any name defined for the checkable, try to compute some |
|
| 297 | + if (!is_string($label) and !is_array($name)) { |
|
| 298 | + $label = $name; |
|
| 299 | + $name = $fallback; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + // If we gave custom information on the item, add them |
|
| 303 | + if (is_array($name)) { |
|
| 304 | + $attributes = $name; |
|
| 305 | + $name = array_get($attributes, 'name', $fallback); |
|
| 306 | + unset($attributes['name']); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + // Store all informations we have in an array |
|
| 310 | + $item = array( |
|
| 311 | + 'name' => $name, |
|
| 312 | + 'label' => Helpers::translate($label), |
|
| 313 | + 'count' => $count, |
|
| 314 | + ); |
|
| 315 | + if (isset($attributes)) { |
|
| 316 | + $item['attributes'] = $attributes; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + $this->items[] = $item; |
|
| 320 | + $count++; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Renders a checkable |
|
| 326 | + * |
|
| 327 | + * @param string|array $item A checkable item |
|
| 328 | + * @param integer $fallbackValue A fallback value if none is set |
|
| 329 | + * |
|
| 330 | + * @return string |
|
| 331 | + */ |
|
| 332 | + protected function createCheckable($item, $fallbackValue = 1) |
|
| 333 | + { |
|
| 334 | + // Extract informations |
|
| 335 | + extract($item); |
|
| 336 | + |
|
| 337 | + // Set default values |
|
| 338 | + if (!isset($attributes)) { |
|
| 339 | + $attributes = array(); |
|
| 340 | + } |
|
| 341 | + if (isset($attributes['value'])) { |
|
| 342 | + $value = $attributes['value']; |
|
| 343 | + } |
|
| 344 | + if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 345 | + $value = $fallbackValue; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + // If inline items, add class |
|
| 349 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 350 | + |
|
| 351 | + // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 352 | + // rendering inline. |
|
| 353 | + $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $this->checkable.$isInline; |
|
| 354 | + |
|
| 355 | + // Merge custom attributes with global attributes |
|
| 356 | + $attributes = array_merge($this->attributes, $attributes); |
|
| 357 | + if (!isset($attributes['id'])) { |
|
| 358 | + $attributes['id'] = $name.$this->unique($name); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + // Create field |
|
| 362 | + $field = Input::create($this->checkable, $name, $value, $attributes); |
|
| 363 | + if ($this->isChecked($item, $value)) { |
|
| 364 | + $field->checked('checked'); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + // Add hidden checkbox if requested |
|
| 368 | + if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 369 | + if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 370 | + $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 371 | + |
|
| 372 | + // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 373 | + // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 374 | + $this->app->instance('former.field', $this); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // If no label to wrap, return plain checkable |
|
| 379 | + if (!$label) { |
|
| 380 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 381 | + } else { |
|
| 382 | + $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 386 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 387 | + $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 388 | + if ($this->getAttribute('disabled')) { |
|
| 389 | + $wrapper->addClass('disabled'); |
|
| 390 | + } |
|
| 391 | + $element = $wrapper->render(); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + // Return the field |
|
| 395 | + return $element; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + //////////////////////////////////////////////////////////////////// |
|
| 399 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 400 | + //////////////////////////////////////////////////////////////////// |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Generate an unique ID for a field |
|
| 404 | + * |
|
| 405 | + * @param string $name The field's name |
|
| 406 | + * |
|
| 407 | + * @return string A field number to use |
|
| 408 | + */ |
|
| 409 | + protected function unique($name) |
|
| 410 | + { |
|
| 411 | + $this->app['former']->labels[] = $name; |
|
| 412 | + |
|
| 413 | + // Count number of fields with the same ID |
|
| 414 | + $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 415 | + return $label == $name; |
|
| 416 | + }); |
|
| 417 | + $unique = sizeof($where); |
|
| 418 | + |
|
| 419 | + // In case the field doesn't need to be numbered |
|
| 420 | + if ($unique < 2 or empty($this->items)) { |
|
| 421 | + return false; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + return $unique; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Set something on the currently focused checkable |
|
| 429 | + * |
|
| 430 | + * @param string $attribute The key to set |
|
| 431 | + * @param string $value Its value |
|
| 432 | + * |
|
| 433 | + * @return $this|bool |
|
| 434 | + */ |
|
| 435 | + protected function setOnFocused($attribute, $value) |
|
| 436 | + { |
|
| 437 | + if (is_null($this->focus)) { |
|
| 438 | + return false; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value); |
|
| 442 | + |
|
| 443 | + return $this; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * Check if a checkable is checked |
|
| 448 | + * |
|
| 449 | + * @return boolean Checked or not |
|
| 450 | + */ |
|
| 451 | + protected function isChecked($item = null, $value = null) |
|
| 452 | + { |
|
| 453 | + if (isset($item['name'])) { |
|
| 454 | + $name = $item['name']; |
|
| 455 | + } |
|
| 456 | + if (empty($name)) { |
|
| 457 | + $name = $this->name; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 461 | + // Or if it's a single radio, simply see if we called check |
|
| 462 | + if ($this->isCheckbox() or |
|
| 463 | + !$this->isCheckbox() and !$this->items |
|
| 464 | + ) { |
|
| 465 | + $checked = array_get($this->checked, $name, false); |
|
| 466 | + |
|
| 467 | + // If there are multiple, search for the value |
|
| 468 | + // as the name are the same between radios |
|
| 469 | + } else { |
|
| 470 | + $checked = array_get($this->checked, $value, false); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // Check the values and POST array |
|
| 474 | + if ($this->isGrouped()) { |
|
| 475 | + // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 476 | + $groupIndex = self::getGroupIndexFromItem($item); |
|
| 477 | + |
|
| 478 | + // Search using the bare name, not the individual item name |
|
| 479 | + $post = $this->app['former']->getPost($this->name); |
|
| 480 | + $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 481 | + |
|
| 482 | + if (isset($post[$groupIndex])) { |
|
| 483 | + $post = $post[$groupIndex]; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 488 | + * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 489 | + * |
|
| 490 | + * array( |
|
| 491 | + * 'name' = 'roles[foo]', |
|
| 492 | + * 'value' => 'foo', |
|
| 493 | + * ) |
|
| 494 | + */ |
|
| 495 | + if ($static instanceof Collection) { |
|
| 496 | + // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 497 | + foreach ($static as $staticItem) { |
|
| 498 | + $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 499 | + if ($staticItemValue == $groupIndex) { |
|
| 500 | + $static = $staticItemValue; |
|
| 501 | + break; |
|
| 502 | + } |
|
| 503 | + } |
|
| 504 | + } else if (isset($static[$groupIndex])) { |
|
| 505 | + $static = $static[$groupIndex]; |
|
| 506 | + } |
|
| 507 | + } else { |
|
| 508 | + $post = $this->app['former']->getPost($name); |
|
| 509 | + $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 513 | + $isChecked = ($post == $value); |
|
| 514 | + } elseif (!is_null($static)) { |
|
| 515 | + $isChecked = ($static == $value); |
|
| 516 | + } else { |
|
| 517 | + $isChecked = $checked; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + return $isChecked ? true : false; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Check if the current element is a checkbox |
|
| 525 | + * |
|
| 526 | + * @return boolean Checkbox or radio |
|
| 527 | + */ |
|
| 528 | + protected function isCheckbox() |
|
| 529 | + { |
|
| 530 | + return $this->checkable == 'checkbox'; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + /** |
|
| 534 | + * Check if the checkables are grouped or not |
|
| 535 | + * |
|
| 536 | + * @return boolean |
|
| 537 | + */ |
|
| 538 | + protected function isGrouped() |
|
| 539 | + { |
|
| 540 | + return |
|
| 541 | + $this->grouped == true or |
|
| 542 | + strpos($this->name, '[]') !== false; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * @param array $item The item array, containing at least name and count keys. |
|
| 547 | + * |
|
| 548 | + * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 549 | + */ |
|
| 550 | + public static function getGroupIndexFromItem($item) |
|
| 551 | + { |
|
| 552 | + $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 553 | + if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 554 | + return $item['count']; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + return $groupIndex; |
|
| 558 | + } |
|
| 559 | 559 | } |
@@ -275,7 +275,7 @@ |
||
| 275 | 275 | |
| 276 | 276 | // If we have a rule with a value |
| 277 | 277 | if (($colon = strpos($rule, ':')) !== false) { |
| 278 | - $rulename = substr($rule, 0, $colon) ; |
|
| 278 | + $rulename = substr($rule, 0, $colon); |
|
| 279 | 279 | |
| 280 | 280 | /** |
| 281 | 281 | * Regular expressions may contain commas and should not be divided by str_getcsv. |
@@ -16,247 +16,247 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Field extends FormerObject implements FieldInterface |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The IoC Container |
|
| 21 | - * |
|
| 22 | - * @var Container |
|
| 23 | - */ |
|
| 24 | - protected $app; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The Form instance |
|
| 28 | - * |
|
| 29 | - * @var Former\Form |
|
| 30 | - */ |
|
| 31 | - protected $form; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A label for the field (if not using Bootstrap) |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $label; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The field's group |
|
| 42 | - * |
|
| 43 | - * @var Group |
|
| 44 | - */ |
|
| 45 | - protected $group; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The field's default element |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $element = 'input'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Whether the Field is self-closing or not |
|
| 56 | - * |
|
| 57 | - * @var boolean |
|
| 58 | - */ |
|
| 59 | - protected $isSelfClosing = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The field's bind destination |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $bind; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get the current framework instance |
|
| 70 | - * |
|
| 71 | - * @return Framework |
|
| 72 | - */ |
|
| 73 | - protected function currentFramework() |
|
| 74 | - { |
|
| 75 | - if ($this->app->bound('former.form.framework')) { |
|
| 76 | - return $this->app['former.form.framework']; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $this->app['former.framework']; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - ///////////////////////////// INTERFACE //////////////////////////// |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Set up a Field instance |
|
| 88 | - * |
|
| 89 | - * @param string $type A field type |
|
| 90 | - */ |
|
| 91 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 92 | - { |
|
| 93 | - // Set base parameters |
|
| 94 | - $this->app = $app; |
|
| 95 | - $this->type = $type; |
|
| 96 | - $this->value = $value; |
|
| 97 | - $this->setAttributes($attributes); |
|
| 98 | - $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 99 | - |
|
| 100 | - // Compute and translate label |
|
| 101 | - $this->automaticLabels($name, $label); |
|
| 102 | - |
|
| 103 | - // Repopulate field |
|
| 104 | - if ($type != 'password' && $name !== '_token') { |
|
| 105 | - $this->value = $this->repopulate(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Apply Live validation rules |
|
| 109 | - if ($this->app['former']->getOption('live_validation')) { |
|
| 110 | - $rules = new LiveValidation($this); |
|
| 111 | - $rules->apply($this->getRules()); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Bind the Group class |
|
| 115 | - $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 116 | - $groupClass = Former::FORMSPACE.$groupClass; |
|
| 117 | - |
|
| 118 | - $this->group = new $groupClass($this->app, $this->label); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Redirect calls to the group if necessary |
|
| 123 | - * |
|
| 124 | - * @param string $method |
|
| 125 | - */ |
|
| 126 | - public function __call($method, $parameters) |
|
| 127 | - { |
|
| 128 | - // Translate attributes |
|
| 129 | - $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 130 | - if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 131 | - $parameters[0] = Helpers::translate($parameters[0]); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Redirect calls to the Control Group |
|
| 135 | - if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) { |
|
| 136 | - $method = str_replace('onGroup', '', $method); |
|
| 137 | - $method = lcfirst($method); |
|
| 138 | - |
|
| 139 | - call_user_func_array(array($this->group, $method), $parameters); |
|
| 140 | - |
|
| 141 | - return $this; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return parent::__call($method, $parameters); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Prints out the field, wrapped in its group |
|
| 149 | - * |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function wrapAndRender() |
|
| 153 | - { |
|
| 154 | - // Dry syntax (hidden fields, plain fields) |
|
| 155 | - if ($this->isUnwrappable()) { |
|
| 156 | - $html = $this->render(); |
|
| 157 | - // Control group syntax |
|
| 158 | - } elseif (Form::hasInstanceOpened()) { |
|
| 159 | - $html = $this->group->wrapField($this); |
|
| 160 | - // Classic syntax |
|
| 161 | - } else { |
|
| 162 | - $html = $this->currentFramework()->createLabelOf($this); |
|
| 163 | - $html .= $this->render(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return $html; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Prints out the field |
|
| 171 | - * |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public function __toString() |
|
| 175 | - { |
|
| 176 | - return $this->wrapAndRender(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - //////////////////////////////////////////////////////////////////// |
|
| 180 | - ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 181 | - //////////////////////////////////////////////////////////////////// |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Whether the current field is required or not |
|
| 185 | - * |
|
| 186 | - * @return boolean |
|
| 187 | - */ |
|
| 188 | - public function isRequired() |
|
| 189 | - { |
|
| 190 | - return isset($this->attributes['required']); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Check if a field is unwrappable (no label) |
|
| 195 | - * |
|
| 196 | - * @return boolean |
|
| 197 | - */ |
|
| 198 | - public function isUnwrappable() |
|
| 199 | - { |
|
| 200 | - return |
|
| 201 | - ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 202 | - ($this->form and $this->isOfType('inline')) or |
|
| 203 | - $this->isButton() or |
|
| 204 | - $this->isOfType('hidden') or |
|
| 205 | - \Former\Form\Group::$opened or |
|
| 206 | - $this->group and $this->group->isRaw(); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Check if field is a checkbox or a radio |
|
| 211 | - * |
|
| 212 | - * @return boolean |
|
| 213 | - */ |
|
| 214 | - public function isCheckable() |
|
| 215 | - { |
|
| 216 | - return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios'); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Check if the field is a button |
|
| 221 | - * |
|
| 222 | - * @return boolean |
|
| 223 | - */ |
|
| 224 | - public function isButton() |
|
| 225 | - { |
|
| 226 | - return false; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Get the rules applied to the current field |
|
| 231 | - * |
|
| 232 | - * @return array An array of rules |
|
| 233 | - */ |
|
| 234 | - public function getRules() |
|
| 235 | - { |
|
| 236 | - return $this->app['former']->getRules($this->name); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - //////////////////////////////////////////////////////////////////// |
|
| 240 | - //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 241 | - //////////////////////////////////////////////////////////////////// |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Apply a Live Validation rule by chaining |
|
| 245 | - * |
|
| 246 | - * @param string $rule The rule |
|
| 247 | - */ |
|
| 248 | - public function rule($rule) |
|
| 249 | - { |
|
| 250 | - $parameters = func_get_args(); |
|
| 251 | - array_shift($parameters); |
|
| 252 | - |
|
| 253 | - $live = new LiveValidation($this); |
|
| 254 | - $live->apply(array( |
|
| 255 | - $rule => $parameters, |
|
| 256 | - )); |
|
| 257 | - |
|
| 258 | - return $this; |
|
| 259 | - } |
|
| 19 | + /** |
|
| 20 | + * The IoC Container |
|
| 21 | + * |
|
| 22 | + * @var Container |
|
| 23 | + */ |
|
| 24 | + protected $app; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The Form instance |
|
| 28 | + * |
|
| 29 | + * @var Former\Form |
|
| 30 | + */ |
|
| 31 | + protected $form; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A label for the field (if not using Bootstrap) |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $label; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The field's group |
|
| 42 | + * |
|
| 43 | + * @var Group |
|
| 44 | + */ |
|
| 45 | + protected $group; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The field's default element |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $element = 'input'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Whether the Field is self-closing or not |
|
| 56 | + * |
|
| 57 | + * @var boolean |
|
| 58 | + */ |
|
| 59 | + protected $isSelfClosing = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The field's bind destination |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $bind; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get the current framework instance |
|
| 70 | + * |
|
| 71 | + * @return Framework |
|
| 72 | + */ |
|
| 73 | + protected function currentFramework() |
|
| 74 | + { |
|
| 75 | + if ($this->app->bound('former.form.framework')) { |
|
| 76 | + return $this->app['former.form.framework']; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $this->app['former.framework']; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + ///////////////////////////// INTERFACE //////////////////////////// |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Set up a Field instance |
|
| 88 | + * |
|
| 89 | + * @param string $type A field type |
|
| 90 | + */ |
|
| 91 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 92 | + { |
|
| 93 | + // Set base parameters |
|
| 94 | + $this->app = $app; |
|
| 95 | + $this->type = $type; |
|
| 96 | + $this->value = $value; |
|
| 97 | + $this->setAttributes($attributes); |
|
| 98 | + $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 99 | + |
|
| 100 | + // Compute and translate label |
|
| 101 | + $this->automaticLabels($name, $label); |
|
| 102 | + |
|
| 103 | + // Repopulate field |
|
| 104 | + if ($type != 'password' && $name !== '_token') { |
|
| 105 | + $this->value = $this->repopulate(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Apply Live validation rules |
|
| 109 | + if ($this->app['former']->getOption('live_validation')) { |
|
| 110 | + $rules = new LiveValidation($this); |
|
| 111 | + $rules->apply($this->getRules()); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Bind the Group class |
|
| 115 | + $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 116 | + $groupClass = Former::FORMSPACE.$groupClass; |
|
| 117 | + |
|
| 118 | + $this->group = new $groupClass($this->app, $this->label); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Redirect calls to the group if necessary |
|
| 123 | + * |
|
| 124 | + * @param string $method |
|
| 125 | + */ |
|
| 126 | + public function __call($method, $parameters) |
|
| 127 | + { |
|
| 128 | + // Translate attributes |
|
| 129 | + $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 130 | + if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 131 | + $parameters[0] = Helpers::translate($parameters[0]); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Redirect calls to the Control Group |
|
| 135 | + if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) { |
|
| 136 | + $method = str_replace('onGroup', '', $method); |
|
| 137 | + $method = lcfirst($method); |
|
| 138 | + |
|
| 139 | + call_user_func_array(array($this->group, $method), $parameters); |
|
| 140 | + |
|
| 141 | + return $this; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return parent::__call($method, $parameters); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Prints out the field, wrapped in its group |
|
| 149 | + * |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function wrapAndRender() |
|
| 153 | + { |
|
| 154 | + // Dry syntax (hidden fields, plain fields) |
|
| 155 | + if ($this->isUnwrappable()) { |
|
| 156 | + $html = $this->render(); |
|
| 157 | + // Control group syntax |
|
| 158 | + } elseif (Form::hasInstanceOpened()) { |
|
| 159 | + $html = $this->group->wrapField($this); |
|
| 160 | + // Classic syntax |
|
| 161 | + } else { |
|
| 162 | + $html = $this->currentFramework()->createLabelOf($this); |
|
| 163 | + $html .= $this->render(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return $html; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Prints out the field |
|
| 171 | + * |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public function __toString() |
|
| 175 | + { |
|
| 176 | + return $this->wrapAndRender(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + //////////////////////////////////////////////////////////////////// |
|
| 180 | + ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 181 | + //////////////////////////////////////////////////////////////////// |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Whether the current field is required or not |
|
| 185 | + * |
|
| 186 | + * @return boolean |
|
| 187 | + */ |
|
| 188 | + public function isRequired() |
|
| 189 | + { |
|
| 190 | + return isset($this->attributes['required']); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Check if a field is unwrappable (no label) |
|
| 195 | + * |
|
| 196 | + * @return boolean |
|
| 197 | + */ |
|
| 198 | + public function isUnwrappable() |
|
| 199 | + { |
|
| 200 | + return |
|
| 201 | + ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 202 | + ($this->form and $this->isOfType('inline')) or |
|
| 203 | + $this->isButton() or |
|
| 204 | + $this->isOfType('hidden') or |
|
| 205 | + \Former\Form\Group::$opened or |
|
| 206 | + $this->group and $this->group->isRaw(); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Check if field is a checkbox or a radio |
|
| 211 | + * |
|
| 212 | + * @return boolean |
|
| 213 | + */ |
|
| 214 | + public function isCheckable() |
|
| 215 | + { |
|
| 216 | + return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios'); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Check if the field is a button |
|
| 221 | + * |
|
| 222 | + * @return boolean |
|
| 223 | + */ |
|
| 224 | + public function isButton() |
|
| 225 | + { |
|
| 226 | + return false; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Get the rules applied to the current field |
|
| 231 | + * |
|
| 232 | + * @return array An array of rules |
|
| 233 | + */ |
|
| 234 | + public function getRules() |
|
| 235 | + { |
|
| 236 | + return $this->app['former']->getRules($this->name); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + //////////////////////////////////////////////////////////////////// |
|
| 240 | + //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 241 | + //////////////////////////////////////////////////////////////////// |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Apply a Live Validation rule by chaining |
|
| 245 | + * |
|
| 246 | + * @param string $rule The rule |
|
| 247 | + */ |
|
| 248 | + public function rule($rule) |
|
| 249 | + { |
|
| 250 | + $parameters = func_get_args(); |
|
| 251 | + array_shift($parameters); |
|
| 252 | + |
|
| 253 | + $live = new LiveValidation($this); |
|
| 254 | + $live->apply(array( |
|
| 255 | + $rule => $parameters, |
|
| 256 | + )); |
|
| 257 | + |
|
| 258 | + return $this; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | 261 | /** |
| 262 | 262 | * Apply multiple rules passed as a string. |
@@ -278,9 +278,9 @@ discard block |
||
| 278 | 278 | $rulename = substr($rule, 0, $colon) ; |
| 279 | 279 | |
| 280 | 280 | /** |
| 281 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 282 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 283 | - */ |
|
| 281 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 282 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 283 | + */ |
|
| 284 | 284 | if ($rulename !== 'regex') { |
| 285 | 285 | $parameters = str_getcsv(substr($rule, $colon + 1)); |
| 286 | 286 | } else { |
@@ -302,149 +302,149 @@ discard block |
||
| 302 | 302 | return $this; |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - /** |
|
| 306 | - * Adds a label to the group/field |
|
| 307 | - * |
|
| 308 | - * @param string $text A label |
|
| 309 | - * @param array $attributes The label's attributes |
|
| 310 | - * |
|
| 311 | - * @return Field A field |
|
| 312 | - */ |
|
| 313 | - public function label($text, $attributes = array()) |
|
| 314 | - { |
|
| 315 | - // Create the Label element |
|
| 316 | - $for = $this->id ?: $this->name; |
|
| 317 | - $label = $this->app['former']->label($text, $for, $attributes); |
|
| 318 | - |
|
| 319 | - // Set label |
|
| 320 | - $this->label = $label; |
|
| 321 | - if ($this->group) { |
|
| 322 | - $this->group->setLabel($label); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - return $this; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Set the Field value no matter what |
|
| 330 | - * |
|
| 331 | - * @param string $value A new value |
|
| 332 | - */ |
|
| 333 | - public function forceValue($value) |
|
| 334 | - { |
|
| 335 | - $this->value = $value; |
|
| 336 | - |
|
| 337 | - return $this; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 342 | - * |
|
| 343 | - * @param string $value A new value |
|
| 344 | - */ |
|
| 345 | - public function value($value) |
|
| 346 | - { |
|
| 347 | - // Check if we already have a value stored for this field or in POST data |
|
| 348 | - $already = $this->repopulate(); |
|
| 349 | - |
|
| 350 | - if (!$already) { |
|
| 351 | - $this->value = $value; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - return $this; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Change the field's name |
|
| 359 | - * |
|
| 360 | - * @param string $name The new name |
|
| 361 | - */ |
|
| 362 | - public function name($name) |
|
| 363 | - { |
|
| 364 | - $this->name = $name; |
|
| 365 | - |
|
| 366 | - // Also relink the label to the new name |
|
| 367 | - $this->label($name); |
|
| 368 | - |
|
| 369 | - return $this; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Get the field's labels |
|
| 374 | - * |
|
| 375 | - * @return string |
|
| 376 | - */ |
|
| 377 | - public function getLabel() |
|
| 378 | - { |
|
| 379 | - return $this->label; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Change the field's bind destination |
|
| 384 | - * |
|
| 385 | - * @param $destination |
|
| 386 | - */ |
|
| 387 | - public function bind($destination) { |
|
| 388 | - $this->bind = $destination; |
|
| 389 | - if ($this->type != 'password') { |
|
| 390 | - $this->value = $this->repopulate(); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - return $this; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - //////////////////////////////////////////////////////////////////// |
|
| 397 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 398 | - //////////////////////////////////////////////////////////////////// |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Use values stored in Former to populate the current field |
|
| 402 | - */ |
|
| 403 | - private function repopulate($fallback = null) |
|
| 404 | - { |
|
| 405 | - // Get values from POST, populated, and manually set value |
|
| 406 | - $post = $this->app['former']->getPost($this->name); |
|
| 407 | - $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 408 | - $populate = $populator->get($this->bind ?: $this->name); |
|
| 409 | - |
|
| 410 | - // Assign a priority to each |
|
| 411 | - if (!is_null($post)) { |
|
| 412 | - return $post; |
|
| 413 | - } |
|
| 414 | - if (!is_null($populate)) { |
|
| 415 | - return $populate; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - return $fallback ?: $this->value; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Ponders a label and a field name, and tries to get the best out of it |
|
| 423 | - * |
|
| 424 | - * @param string $label A label |
|
| 425 | - * @param string $name A field name |
|
| 426 | - * |
|
| 427 | - * @return false|null A label and a field name |
|
| 428 | - */ |
|
| 429 | - private function automaticLabels($name, $label) |
|
| 430 | - { |
|
| 431 | - // Disabled automatic labels |
|
| 432 | - if (!$this->app['former']->getOption('automatic_label')) { |
|
| 433 | - $this->name = $name; |
|
| 434 | - $this->label($label); |
|
| 435 | - |
|
| 436 | - return false; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // Check for the two possibilities |
|
| 440 | - if ($label and is_null($name)) { |
|
| 441 | - $name = Str::slug($label); |
|
| 442 | - } elseif (is_null($label) and $name) { |
|
| 443 | - $label = preg_replace('/\[\]$/', '', $name); |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - // Save values |
|
| 447 | - $this->name = $name; |
|
| 448 | - $this->label($label); |
|
| 449 | - } |
|
| 305 | + /** |
|
| 306 | + * Adds a label to the group/field |
|
| 307 | + * |
|
| 308 | + * @param string $text A label |
|
| 309 | + * @param array $attributes The label's attributes |
|
| 310 | + * |
|
| 311 | + * @return Field A field |
|
| 312 | + */ |
|
| 313 | + public function label($text, $attributes = array()) |
|
| 314 | + { |
|
| 315 | + // Create the Label element |
|
| 316 | + $for = $this->id ?: $this->name; |
|
| 317 | + $label = $this->app['former']->label($text, $for, $attributes); |
|
| 318 | + |
|
| 319 | + // Set label |
|
| 320 | + $this->label = $label; |
|
| 321 | + if ($this->group) { |
|
| 322 | + $this->group->setLabel($label); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + return $this; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Set the Field value no matter what |
|
| 330 | + * |
|
| 331 | + * @param string $value A new value |
|
| 332 | + */ |
|
| 333 | + public function forceValue($value) |
|
| 334 | + { |
|
| 335 | + $this->value = $value; |
|
| 336 | + |
|
| 337 | + return $this; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 342 | + * |
|
| 343 | + * @param string $value A new value |
|
| 344 | + */ |
|
| 345 | + public function value($value) |
|
| 346 | + { |
|
| 347 | + // Check if we already have a value stored for this field or in POST data |
|
| 348 | + $already = $this->repopulate(); |
|
| 349 | + |
|
| 350 | + if (!$already) { |
|
| 351 | + $this->value = $value; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + return $this; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Change the field's name |
|
| 359 | + * |
|
| 360 | + * @param string $name The new name |
|
| 361 | + */ |
|
| 362 | + public function name($name) |
|
| 363 | + { |
|
| 364 | + $this->name = $name; |
|
| 365 | + |
|
| 366 | + // Also relink the label to the new name |
|
| 367 | + $this->label($name); |
|
| 368 | + |
|
| 369 | + return $this; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Get the field's labels |
|
| 374 | + * |
|
| 375 | + * @return string |
|
| 376 | + */ |
|
| 377 | + public function getLabel() |
|
| 378 | + { |
|
| 379 | + return $this->label; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Change the field's bind destination |
|
| 384 | + * |
|
| 385 | + * @param $destination |
|
| 386 | + */ |
|
| 387 | + public function bind($destination) { |
|
| 388 | + $this->bind = $destination; |
|
| 389 | + if ($this->type != 'password') { |
|
| 390 | + $this->value = $this->repopulate(); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + return $this; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + //////////////////////////////////////////////////////////////////// |
|
| 397 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 398 | + //////////////////////////////////////////////////////////////////// |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Use values stored in Former to populate the current field |
|
| 402 | + */ |
|
| 403 | + private function repopulate($fallback = null) |
|
| 404 | + { |
|
| 405 | + // Get values from POST, populated, and manually set value |
|
| 406 | + $post = $this->app['former']->getPost($this->name); |
|
| 407 | + $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 408 | + $populate = $populator->get($this->bind ?: $this->name); |
|
| 409 | + |
|
| 410 | + // Assign a priority to each |
|
| 411 | + if (!is_null($post)) { |
|
| 412 | + return $post; |
|
| 413 | + } |
|
| 414 | + if (!is_null($populate)) { |
|
| 415 | + return $populate; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + return $fallback ?: $this->value; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Ponders a label and a field name, and tries to get the best out of it |
|
| 423 | + * |
|
| 424 | + * @param string $label A label |
|
| 425 | + * @param string $name A field name |
|
| 426 | + * |
|
| 427 | + * @return false|null A label and a field name |
|
| 428 | + */ |
|
| 429 | + private function automaticLabels($name, $label) |
|
| 430 | + { |
|
| 431 | + // Disabled automatic labels |
|
| 432 | + if (!$this->app['former']->getOption('automatic_label')) { |
|
| 433 | + $this->name = $name; |
|
| 434 | + $this->label($label); |
|
| 435 | + |
|
| 436 | + return false; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // Check for the two possibilities |
|
| 440 | + if ($label and is_null($name)) { |
|
| 441 | + $name = Str::slug($label); |
|
| 442 | + } elseif (is_null($label) and $name) { |
|
| 443 | + $label = preg_replace('/\[\]$/', '', $name); |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + // Save values |
|
| 447 | + $this->name = $name; |
|
| 448 | + $this->label($label); |
|
| 449 | + } |
|
| 450 | 450 | } |
@@ -76,7 +76,7 @@ |
||
| 76 | 76 | if (array_key_exists($name, $names)) { |
| 77 | 77 | $count = $names[$name] + 1; |
| 78 | 78 | $names[$name] = $count; |
| 79 | - return $name . '-' . $count; |
|
| 79 | + return $name.'-'.$count; |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | $names[$name] = 1; |
@@ -10,147 +10,147 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class FormerObject extends Element |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The field's name |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $name; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The field type |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $type; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A list of class properties to be added to attributes |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $injectedProperties = array('name'); |
|
| 33 | - |
|
| 34 | - //////////////////////////////////////////////////////////////////// |
|
| 35 | - /////////////////////////// ID AND LABELS ////////////////////////// |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create an unique ID and return it |
|
| 40 | - * |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function getCreatedId() |
|
| 44 | - { |
|
| 45 | - $this->setId(); |
|
| 46 | - |
|
| 47 | - return $this->attributes['id']; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Set the matching ID on a field if possible |
|
| 52 | - */ |
|
| 53 | - protected function setId() |
|
| 54 | - { |
|
| 55 | - if (!array_key_exists('id', $this->attributes) and |
|
| 56 | - in_array($this->name, $this->app['former']->labels) |
|
| 57 | - ) { |
|
| 58 | - // Set and save the field's ID |
|
| 59 | - $id = $this->getUniqueId($this->name); |
|
| 60 | - $this->attributes['id'] = $id; |
|
| 61 | - $this->app['former']->ids[] = $id; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get an unique ID for a field from its name |
|
| 67 | - * |
|
| 68 | - * @param string $name |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - protected function getUniqueId($name) |
|
| 73 | - { |
|
| 74 | - $names = &$this->app['former']->names; |
|
| 75 | - |
|
| 76 | - if (array_key_exists($name, $names)) { |
|
| 77 | - $count = $names[$name] + 1; |
|
| 78 | - $names[$name] = $count; |
|
| 79 | - return $name . '-' . $count; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $names[$name] = 1; |
|
| 83 | - return $name; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Render the FormerObject and set its id |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function render() |
|
| 92 | - { |
|
| 93 | - // Set the proper ID according to the label |
|
| 94 | - $this->setId(); |
|
| 95 | - |
|
| 96 | - // Encode HTML value |
|
| 97 | - $isButton = ($this instanceof Field) ? $this->isButton() : false; |
|
| 98 | - if (!$isButton and is_string($this->value)) { |
|
| 99 | - $this->value = Helpers::encode($this->value); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - return parent::render(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - //////////////////////////////////////////////////////////////////// |
|
| 106 | - ////////////////////////////// GETTERS ///////////////////////////// |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get the object's name |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function getName() |
|
| 115 | - { |
|
| 116 | - return $this->name; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - //////////////////////////////////////////////////////////////////// |
|
| 120 | - //////////////////////////// OBJECT TYPE /////////////////////////// |
|
| 121 | - //////////////////////////////////////////////////////////////////// |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Get the object's type |
|
| 125 | - * |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 128 | - public function getType() |
|
| 129 | - { |
|
| 130 | - return $this->type; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Change a object's type |
|
| 135 | - * |
|
| 136 | - * @param string $type |
|
| 137 | - */ |
|
| 138 | - public function setType($type) |
|
| 139 | - { |
|
| 140 | - $this->type = $type; |
|
| 141 | - |
|
| 142 | - return $this; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Check if an object is of a certain type |
|
| 147 | - * |
|
| 148 | - * @return boolean |
|
| 149 | - */ |
|
| 150 | - public function isOfType() |
|
| 151 | - { |
|
| 152 | - $types = func_get_args(); |
|
| 153 | - |
|
| 154 | - return in_array($this->type, $types); |
|
| 155 | - } |
|
| 13 | + /** |
|
| 14 | + * The field's name |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $name; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The field type |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $type; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A list of class properties to be added to attributes |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $injectedProperties = array('name'); |
|
| 33 | + |
|
| 34 | + //////////////////////////////////////////////////////////////////// |
|
| 35 | + /////////////////////////// ID AND LABELS ////////////////////////// |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create an unique ID and return it |
|
| 40 | + * |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function getCreatedId() |
|
| 44 | + { |
|
| 45 | + $this->setId(); |
|
| 46 | + |
|
| 47 | + return $this->attributes['id']; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Set the matching ID on a field if possible |
|
| 52 | + */ |
|
| 53 | + protected function setId() |
|
| 54 | + { |
|
| 55 | + if (!array_key_exists('id', $this->attributes) and |
|
| 56 | + in_array($this->name, $this->app['former']->labels) |
|
| 57 | + ) { |
|
| 58 | + // Set and save the field's ID |
|
| 59 | + $id = $this->getUniqueId($this->name); |
|
| 60 | + $this->attributes['id'] = $id; |
|
| 61 | + $this->app['former']->ids[] = $id; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get an unique ID for a field from its name |
|
| 67 | + * |
|
| 68 | + * @param string $name |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + protected function getUniqueId($name) |
|
| 73 | + { |
|
| 74 | + $names = &$this->app['former']->names; |
|
| 75 | + |
|
| 76 | + if (array_key_exists($name, $names)) { |
|
| 77 | + $count = $names[$name] + 1; |
|
| 78 | + $names[$name] = $count; |
|
| 79 | + return $name . '-' . $count; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $names[$name] = 1; |
|
| 83 | + return $name; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Render the FormerObject and set its id |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function render() |
|
| 92 | + { |
|
| 93 | + // Set the proper ID according to the label |
|
| 94 | + $this->setId(); |
|
| 95 | + |
|
| 96 | + // Encode HTML value |
|
| 97 | + $isButton = ($this instanceof Field) ? $this->isButton() : false; |
|
| 98 | + if (!$isButton and is_string($this->value)) { |
|
| 99 | + $this->value = Helpers::encode($this->value); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + return parent::render(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + //////////////////////////////////////////////////////////////////// |
|
| 106 | + ////////////////////////////// GETTERS ///////////////////////////// |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get the object's name |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function getName() |
|
| 115 | + { |
|
| 116 | + return $this->name; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + //////////////////////////////////////////////////////////////////// |
|
| 120 | + //////////////////////////// OBJECT TYPE /////////////////////////// |
|
| 121 | + //////////////////////////////////////////////////////////////////// |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Get the object's type |
|
| 125 | + * |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | + public function getType() |
|
| 129 | + { |
|
| 130 | + return $this->type; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Change a object's type |
|
| 135 | + * |
|
| 136 | + * @param string $type |
|
| 137 | + */ |
|
| 138 | + public function setType($type) |
|
| 139 | + { |
|
| 140 | + $this->type = $type; |
|
| 141 | + |
|
| 142 | + return $this; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Check if an object is of a certain type |
|
| 147 | + * |
|
| 148 | + * @return boolean |
|
| 149 | + */ |
|
| 150 | + public function isOfType() |
|
| 151 | + { |
|
| 152 | + $types = func_get_args(); |
|
| 153 | + |
|
| 154 | + return in_array($this->type, $types); |
|
| 155 | + } |
|
| 156 | 156 | } |
@@ -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,437 +13,437 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap3 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 | - 'default', |
|
| 35 | - 'primary', |
|
| 36 | - 'warning', |
|
| 37 | - 'danger', |
|
| 38 | - 'success', |
|
| 39 | - 'info', |
|
| 40 | - ); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The field sizes available |
|
| 44 | - * |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - private $fields = array( |
|
| 48 | - 'lg', |
|
| 49 | - 'sm', |
|
| 50 | - // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 51 | - // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 52 | - // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 53 | - // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 54 | - // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 55 | - // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 56 | - // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 57 | - // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The field states available |
|
| 62 | - * |
|
| 63 | - * @var array |
|
| 64 | - */ |
|
| 65 | - protected $states = array( |
|
| 66 | - 'has-warning', |
|
| 67 | - 'has-error', |
|
| 68 | - 'has-success', |
|
| 69 | - ); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The default HTML tag used for icons |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $iconTag = 'span'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * The default set for icon fonts |
|
| 80 | - * By default Bootstrap 3 offers only 'glyphicon' |
|
| 81 | - * See Former docs to use 'social' and 'filetypes' sets for specific icons. |
|
| 82 | - * |
|
| 83 | - * @var string |
|
| 84 | - */ |
|
| 85 | - protected $iconSet = 'glyphicon'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * The default prefix icon names |
|
| 89 | - * "icon" works for Bootstrap 2 and Font-awesome |
|
| 90 | - * |
|
| 91 | - * @var string |
|
| 92 | - */ |
|
| 93 | - protected $iconPrefix = 'glyphicon'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Create a new TwitterBootstrap instance |
|
| 97 | - * |
|
| 98 | - * @param \Illuminate\Container\Container $app |
|
| 99 | - */ |
|
| 100 | - public function __construct(Container $app) |
|
| 101 | - { |
|
| 102 | - $this->app = $app; |
|
| 103 | - $this->setFrameworkDefaults(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - //////////////////////////////////////////////////////////////////// |
|
| 107 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 108 | - //////////////////////////////////////////////////////////////////// |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Filter buttons classes |
|
| 112 | - * |
|
| 113 | - * @param array $classes An array of classes |
|
| 114 | - * |
|
| 115 | - * @return string[] A filtered array |
|
| 116 | - */ |
|
| 117 | - public function filterButtonClasses($classes) |
|
| 118 | - { |
|
| 119 | - // Filter classes |
|
| 120 | - // $classes = array_intersect($classes, $this->buttons); |
|
| 121 | - |
|
| 122 | - // Prepend button type |
|
| 123 | - $classes = $this->prependWith($classes, 'btn-'); |
|
| 124 | - $classes[] = 'btn'; |
|
| 125 | - |
|
| 126 | - return $classes; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Filter field classes |
|
| 131 | - * |
|
| 132 | - * @param array $classes An array of classes |
|
| 133 | - * |
|
| 134 | - * @return array A filtered array |
|
| 135 | - */ |
|
| 136 | - public function filterFieldClasses($classes) |
|
| 137 | - { |
|
| 138 | - // Filter classes |
|
| 139 | - $classes = array_intersect($classes, $this->fields); |
|
| 140 | - |
|
| 141 | - // Prepend field type |
|
| 142 | - $classes = array_map(function ($class) { |
|
| 143 | - return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 144 | - }, $classes); |
|
| 145 | - |
|
| 146 | - return $classes; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - //////////////////////////////////////////////////////////////////// |
|
| 150 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 151 | - //////////////////////////////////////////////////////////////////// |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Framework error state |
|
| 155 | - * |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function errorState() |
|
| 159 | - { |
|
| 160 | - return 'has-error'; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Returns corresponding inline class of a field |
|
| 165 | - * |
|
| 166 | - * @param Field $field |
|
| 167 | - * |
|
| 168 | - * @return string |
|
| 169 | - */ |
|
| 170 | - public function getInlineLabelClass($field) |
|
| 171 | - { |
|
| 172 | - $inlineClass = parent::getInlineLabelClass($field); |
|
| 173 | - if ($field->isOfType('checkbox', 'checkboxes')) { |
|
| 174 | - $inlineClass = 'checkbox-'.$inlineClass; |
|
| 175 | - } elseif ($field->isOfType('radio', 'radios')) { |
|
| 176 | - $inlineClass = 'radio-'.$inlineClass; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $inlineClass; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Set the fields width from a label width |
|
| 184 | - * |
|
| 185 | - * @param array $labelWidths |
|
| 186 | - */ |
|
| 187 | - protected function setFieldWidths($labelWidths) |
|
| 188 | - { |
|
| 189 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 190 | - |
|
| 191 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 192 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 193 | - if ($viewport) { |
|
| 194 | - $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 195 | - $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 196 | - $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns"; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $this->labelWidth = ltrim($labelWidthClass); |
|
| 201 | - $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 202 | - $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - //////////////////////////////////////////////////////////////////// |
|
| 206 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 207 | - //////////////////////////////////////////////////////////////////// |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Add classes to a field |
|
| 211 | - * |
|
| 212 | - * @param Field $field |
|
| 213 | - * @param array $classes The possible classes to add |
|
| 214 | - * |
|
| 215 | - * @return Field |
|
| 216 | - */ |
|
| 217 | - public function getFieldClasses(Field $field, $classes) |
|
| 218 | - { |
|
| 219 | - // Add inline class for checkables |
|
| 220 | - if ($field->isCheckable() and in_array('inline', $classes)) { |
|
| 221 | - $field->inline(); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // Filter classes according to field type |
|
| 225 | - if ($field->isButton()) { |
|
| 226 | - $classes = $this->filterButtonClasses($classes); |
|
| 227 | - } else { |
|
| 228 | - $classes = $this->filterFieldClasses($classes); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // Add form-control class for text-type, textarea and select fields |
|
| 232 | - // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 233 | - if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array( |
|
| 234 | - 'file', |
|
| 235 | - 'plaintext', |
|
| 236 | - )) and !in_array('form-control', $classes) |
|
| 237 | - ) { |
|
| 238 | - $classes[] = 'form-control'; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return $this->addClassesToField($field, $classes); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Add group classes |
|
| 246 | - * |
|
| 247 | - * @return string A list of group classes |
|
| 248 | - */ |
|
| 249 | - public function getGroupClasses() |
|
| 250 | - { |
|
| 251 | - return 'form-group'; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Add label classes |
|
| 256 | - * |
|
| 257 | - * @return string[] An array of attributes with the label class |
|
| 258 | - */ |
|
| 259 | - public function getLabelClasses() |
|
| 260 | - { |
|
| 261 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 262 | - return array('control-label', $this->labelWidth); |
|
| 263 | - } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 264 | - return array('sr-only'); |
|
| 265 | - } else { |
|
| 266 | - return array('control-label'); |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Add uneditable field classes |
|
| 272 | - * |
|
| 273 | - * @return string An array of attributes with the uneditable class |
|
| 274 | - */ |
|
| 275 | - public function getUneditableClasses() |
|
| 276 | - { |
|
| 277 | - return ''; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Add plain text field classes |
|
| 282 | - * |
|
| 283 | - * @return string An array of attributes with the plain text class |
|
| 284 | - */ |
|
| 285 | - public function getPlainTextClasses() |
|
| 286 | - { |
|
| 287 | - return 'form-control-static'; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Add form class |
|
| 292 | - * |
|
| 293 | - * @param string $type The type of form to add |
|
| 294 | - * |
|
| 295 | - * @return string|null |
|
| 296 | - */ |
|
| 297 | - public function getFormClasses($type) |
|
| 298 | - { |
|
| 299 | - return $type ? 'form-'.$type : null; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Add actions block class |
|
| 304 | - * |
|
| 305 | - * @return string|null |
|
| 306 | - */ |
|
| 307 | - public function getActionClasses() |
|
| 308 | - { |
|
| 309 | - if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 310 | - return 'form-group'; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - return null; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - //////////////////////////////////////////////////////////////////// |
|
| 317 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 318 | - //////////////////////////////////////////////////////////////////// |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Render an help text |
|
| 322 | - * |
|
| 323 | - * @param string $text |
|
| 324 | - * @param array $attributes |
|
| 325 | - * |
|
| 326 | - * @return Element |
|
| 327 | - */ |
|
| 328 | - public function createHelp($text, $attributes = array()) |
|
| 329 | - { |
|
| 330 | - return Element::create('span', $text, $attributes)->addClass('help-block'); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Render an help text |
|
| 335 | - * |
|
| 336 | - * @param string $text |
|
| 337 | - * @param array $attributes |
|
| 338 | - * |
|
| 339 | - * @return Element |
|
| 340 | - */ |
|
| 341 | - public function createBlockHelp($text, $attributes = array()) |
|
| 342 | - { |
|
| 343 | - return Element::create('p', $text, $attributes)->addClass('help-block'); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Render a disabled field |
|
| 348 | - * |
|
| 349 | - * @param Field $field |
|
| 350 | - * |
|
| 351 | - * @return Element |
|
| 352 | - */ |
|
| 353 | - public function createDisabledField(Field $field) |
|
| 354 | - { |
|
| 355 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Render a plain text field |
|
| 360 | - * |
|
| 361 | - * @param Field $field |
|
| 362 | - * |
|
| 363 | - * @return Element |
|
| 364 | - */ |
|
| 365 | - public function createPlainTextField(Field $field) |
|
| 366 | - { |
|
| 367 | - $label = $field->getLabel(); |
|
| 368 | - if ($label) { |
|
| 369 | - $label->for(''); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - //////////////////////////////////////////////////////////////////// |
|
| 376 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 377 | - //////////////////////////////////////////////////////////////////// |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Wrap an item to be prepended or appended to the current field |
|
| 381 | - * |
|
| 382 | - * @return Element A wrapped item |
|
| 383 | - */ |
|
| 384 | - public function placeAround($item) |
|
| 385 | - { |
|
| 386 | - // Render object |
|
| 387 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 388 | - $item = $item->__toString(); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // Get class to use |
|
| 392 | - $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon'; |
|
| 393 | - |
|
| 394 | - return Element::create('span', $item)->addClass('input-group-'.$class); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Wrap a field with prepended and appended items |
|
| 399 | - * |
|
| 400 | - * @param Field $field |
|
| 401 | - * @param array $prepend |
|
| 402 | - * @param array $append |
|
| 403 | - * |
|
| 404 | - * @return string A field concatented with prepended and/or appended items |
|
| 405 | - */ |
|
| 406 | - public function prependAppend($field, $prepend, $append) |
|
| 407 | - { |
|
| 408 | - $return = '<div class="input-group">'; |
|
| 409 | - $return .= join(null, $prepend); |
|
| 410 | - $return .= $field->render(); |
|
| 411 | - $return .= join(null, $append); |
|
| 412 | - $return .= '</div>'; |
|
| 413 | - |
|
| 414 | - return $return; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Wrap a field with potential additional tags |
|
| 419 | - * |
|
| 420 | - * @param Field $field |
|
| 421 | - * |
|
| 422 | - * @return Element A wrapped field |
|
| 423 | - */ |
|
| 424 | - public function wrapField($field) |
|
| 425 | - { |
|
| 426 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 427 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - return $field; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Wrap actions block with potential additional tags |
|
| 435 | - * |
|
| 436 | - * @param Actions $actions |
|
| 437 | - * |
|
| 438 | - * @return string A wrapped actions block |
|
| 439 | - */ |
|
| 440 | - public function wrapActions($actions) |
|
| 441 | - { |
|
| 442 | - // For horizontal forms, we wrap the actions in a div |
|
| 443 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 444 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - return $actions; |
|
| 448 | - } |
|
| 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 | + 'default', |
|
| 35 | + 'primary', |
|
| 36 | + 'warning', |
|
| 37 | + 'danger', |
|
| 38 | + 'success', |
|
| 39 | + 'info', |
|
| 40 | + ); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The field sizes available |
|
| 44 | + * |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + private $fields = array( |
|
| 48 | + 'lg', |
|
| 49 | + 'sm', |
|
| 50 | + // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 51 | + // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 52 | + // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 53 | + // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 54 | + // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 55 | + // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 56 | + // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 57 | + // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The field states available |
|
| 62 | + * |
|
| 63 | + * @var array |
|
| 64 | + */ |
|
| 65 | + protected $states = array( |
|
| 66 | + 'has-warning', |
|
| 67 | + 'has-error', |
|
| 68 | + 'has-success', |
|
| 69 | + ); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The default HTML tag used for icons |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $iconTag = 'span'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * The default set for icon fonts |
|
| 80 | + * By default Bootstrap 3 offers only 'glyphicon' |
|
| 81 | + * See Former docs to use 'social' and 'filetypes' sets for specific icons. |
|
| 82 | + * |
|
| 83 | + * @var string |
|
| 84 | + */ |
|
| 85 | + protected $iconSet = 'glyphicon'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * The default prefix icon names |
|
| 89 | + * "icon" works for Bootstrap 2 and Font-awesome |
|
| 90 | + * |
|
| 91 | + * @var string |
|
| 92 | + */ |
|
| 93 | + protected $iconPrefix = 'glyphicon'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Create a new TwitterBootstrap instance |
|
| 97 | + * |
|
| 98 | + * @param \Illuminate\Container\Container $app |
|
| 99 | + */ |
|
| 100 | + public function __construct(Container $app) |
|
| 101 | + { |
|
| 102 | + $this->app = $app; |
|
| 103 | + $this->setFrameworkDefaults(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + //////////////////////////////////////////////////////////////////// |
|
| 107 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 108 | + //////////////////////////////////////////////////////////////////// |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Filter buttons classes |
|
| 112 | + * |
|
| 113 | + * @param array $classes An array of classes |
|
| 114 | + * |
|
| 115 | + * @return string[] A filtered array |
|
| 116 | + */ |
|
| 117 | + public function filterButtonClasses($classes) |
|
| 118 | + { |
|
| 119 | + // Filter classes |
|
| 120 | + // $classes = array_intersect($classes, $this->buttons); |
|
| 121 | + |
|
| 122 | + // Prepend button type |
|
| 123 | + $classes = $this->prependWith($classes, 'btn-'); |
|
| 124 | + $classes[] = 'btn'; |
|
| 125 | + |
|
| 126 | + return $classes; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Filter field classes |
|
| 131 | + * |
|
| 132 | + * @param array $classes An array of classes |
|
| 133 | + * |
|
| 134 | + * @return array A filtered array |
|
| 135 | + */ |
|
| 136 | + public function filterFieldClasses($classes) |
|
| 137 | + { |
|
| 138 | + // Filter classes |
|
| 139 | + $classes = array_intersect($classes, $this->fields); |
|
| 140 | + |
|
| 141 | + // Prepend field type |
|
| 142 | + $classes = array_map(function ($class) { |
|
| 143 | + return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 144 | + }, $classes); |
|
| 145 | + |
|
| 146 | + return $classes; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + //////////////////////////////////////////////////////////////////// |
|
| 150 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 151 | + //////////////////////////////////////////////////////////////////// |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Framework error state |
|
| 155 | + * |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function errorState() |
|
| 159 | + { |
|
| 160 | + return 'has-error'; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Returns corresponding inline class of a field |
|
| 165 | + * |
|
| 166 | + * @param Field $field |
|
| 167 | + * |
|
| 168 | + * @return string |
|
| 169 | + */ |
|
| 170 | + public function getInlineLabelClass($field) |
|
| 171 | + { |
|
| 172 | + $inlineClass = parent::getInlineLabelClass($field); |
|
| 173 | + if ($field->isOfType('checkbox', 'checkboxes')) { |
|
| 174 | + $inlineClass = 'checkbox-'.$inlineClass; |
|
| 175 | + } elseif ($field->isOfType('radio', 'radios')) { |
|
| 176 | + $inlineClass = 'radio-'.$inlineClass; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $inlineClass; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Set the fields width from a label width |
|
| 184 | + * |
|
| 185 | + * @param array $labelWidths |
|
| 186 | + */ |
|
| 187 | + protected function setFieldWidths($labelWidths) |
|
| 188 | + { |
|
| 189 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 190 | + |
|
| 191 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 192 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 193 | + if ($viewport) { |
|
| 194 | + $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 195 | + $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 196 | + $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns"; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $this->labelWidth = ltrim($labelWidthClass); |
|
| 201 | + $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 202 | + $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + //////////////////////////////////////////////////////////////////// |
|
| 206 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 207 | + //////////////////////////////////////////////////////////////////// |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Add classes to a field |
|
| 211 | + * |
|
| 212 | + * @param Field $field |
|
| 213 | + * @param array $classes The possible classes to add |
|
| 214 | + * |
|
| 215 | + * @return Field |
|
| 216 | + */ |
|
| 217 | + public function getFieldClasses(Field $field, $classes) |
|
| 218 | + { |
|
| 219 | + // Add inline class for checkables |
|
| 220 | + if ($field->isCheckable() and in_array('inline', $classes)) { |
|
| 221 | + $field->inline(); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // Filter classes according to field type |
|
| 225 | + if ($field->isButton()) { |
|
| 226 | + $classes = $this->filterButtonClasses($classes); |
|
| 227 | + } else { |
|
| 228 | + $classes = $this->filterFieldClasses($classes); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // Add form-control class for text-type, textarea and select fields |
|
| 232 | + // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 233 | + if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array( |
|
| 234 | + 'file', |
|
| 235 | + 'plaintext', |
|
| 236 | + )) and !in_array('form-control', $classes) |
|
| 237 | + ) { |
|
| 238 | + $classes[] = 'form-control'; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return $this->addClassesToField($field, $classes); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Add group classes |
|
| 246 | + * |
|
| 247 | + * @return string A list of group classes |
|
| 248 | + */ |
|
| 249 | + public function getGroupClasses() |
|
| 250 | + { |
|
| 251 | + return 'form-group'; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Add label classes |
|
| 256 | + * |
|
| 257 | + * @return string[] An array of attributes with the label class |
|
| 258 | + */ |
|
| 259 | + public function getLabelClasses() |
|
| 260 | + { |
|
| 261 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 262 | + return array('control-label', $this->labelWidth); |
|
| 263 | + } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 264 | + return array('sr-only'); |
|
| 265 | + } else { |
|
| 266 | + return array('control-label'); |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Add uneditable field classes |
|
| 272 | + * |
|
| 273 | + * @return string An array of attributes with the uneditable class |
|
| 274 | + */ |
|
| 275 | + public function getUneditableClasses() |
|
| 276 | + { |
|
| 277 | + return ''; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Add plain text field classes |
|
| 282 | + * |
|
| 283 | + * @return string An array of attributes with the plain text class |
|
| 284 | + */ |
|
| 285 | + public function getPlainTextClasses() |
|
| 286 | + { |
|
| 287 | + return 'form-control-static'; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Add form class |
|
| 292 | + * |
|
| 293 | + * @param string $type The type of form to add |
|
| 294 | + * |
|
| 295 | + * @return string|null |
|
| 296 | + */ |
|
| 297 | + public function getFormClasses($type) |
|
| 298 | + { |
|
| 299 | + return $type ? 'form-'.$type : null; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Add actions block class |
|
| 304 | + * |
|
| 305 | + * @return string|null |
|
| 306 | + */ |
|
| 307 | + public function getActionClasses() |
|
| 308 | + { |
|
| 309 | + if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 310 | + return 'form-group'; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + return null; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + //////////////////////////////////////////////////////////////////// |
|
| 317 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 318 | + //////////////////////////////////////////////////////////////////// |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Render an help text |
|
| 322 | + * |
|
| 323 | + * @param string $text |
|
| 324 | + * @param array $attributes |
|
| 325 | + * |
|
| 326 | + * @return Element |
|
| 327 | + */ |
|
| 328 | + public function createHelp($text, $attributes = array()) |
|
| 329 | + { |
|
| 330 | + return Element::create('span', $text, $attributes)->addClass('help-block'); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Render an help text |
|
| 335 | + * |
|
| 336 | + * @param string $text |
|
| 337 | + * @param array $attributes |
|
| 338 | + * |
|
| 339 | + * @return Element |
|
| 340 | + */ |
|
| 341 | + public function createBlockHelp($text, $attributes = array()) |
|
| 342 | + { |
|
| 343 | + return Element::create('p', $text, $attributes)->addClass('help-block'); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Render a disabled field |
|
| 348 | + * |
|
| 349 | + * @param Field $field |
|
| 350 | + * |
|
| 351 | + * @return Element |
|
| 352 | + */ |
|
| 353 | + public function createDisabledField(Field $field) |
|
| 354 | + { |
|
| 355 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Render a plain text field |
|
| 360 | + * |
|
| 361 | + * @param Field $field |
|
| 362 | + * |
|
| 363 | + * @return Element |
|
| 364 | + */ |
|
| 365 | + public function createPlainTextField(Field $field) |
|
| 366 | + { |
|
| 367 | + $label = $field->getLabel(); |
|
| 368 | + if ($label) { |
|
| 369 | + $label->for(''); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + //////////////////////////////////////////////////////////////////// |
|
| 376 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 377 | + //////////////////////////////////////////////////////////////////// |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Wrap an item to be prepended or appended to the current field |
|
| 381 | + * |
|
| 382 | + * @return Element A wrapped item |
|
| 383 | + */ |
|
| 384 | + public function placeAround($item) |
|
| 385 | + { |
|
| 386 | + // Render object |
|
| 387 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 388 | + $item = $item->__toString(); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // Get class to use |
|
| 392 | + $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon'; |
|
| 393 | + |
|
| 394 | + return Element::create('span', $item)->addClass('input-group-'.$class); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Wrap a field with prepended and appended items |
|
| 399 | + * |
|
| 400 | + * @param Field $field |
|
| 401 | + * @param array $prepend |
|
| 402 | + * @param array $append |
|
| 403 | + * |
|
| 404 | + * @return string A field concatented with prepended and/or appended items |
|
| 405 | + */ |
|
| 406 | + public function prependAppend($field, $prepend, $append) |
|
| 407 | + { |
|
| 408 | + $return = '<div class="input-group">'; |
|
| 409 | + $return .= join(null, $prepend); |
|
| 410 | + $return .= $field->render(); |
|
| 411 | + $return .= join(null, $append); |
|
| 412 | + $return .= '</div>'; |
|
| 413 | + |
|
| 414 | + return $return; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Wrap a field with potential additional tags |
|
| 419 | + * |
|
| 420 | + * @param Field $field |
|
| 421 | + * |
|
| 422 | + * @return Element A wrapped field |
|
| 423 | + */ |
|
| 424 | + public function wrapField($field) |
|
| 425 | + { |
|
| 426 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 427 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + return $field; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Wrap actions block with potential additional tags |
|
| 435 | + * |
|
| 436 | + * @param Actions $actions |
|
| 437 | + * |
|
| 438 | + * @return string A wrapped actions block |
|
| 439 | + */ |
|
| 440 | + public function wrapActions($actions) |
|
| 441 | + { |
|
| 442 | + // For horizontal forms, we wrap the actions in a div |
|
| 443 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 444 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + return $actions; |
|
| 448 | + } |
|
| 449 | 449 | } |
@@ -124,7 +124,7 @@ |
||
| 124 | 124 | $classes = array_intersect($classes, $this->fields); |
| 125 | 125 | |
| 126 | 126 | // Prepend field type |
| 127 | - $classes = array_map(function ($class) { |
|
| 127 | + $classes = array_map(function($class) { |
|
| 128 | 128 | return Str::startsWith($class, 'span') ? $class : 'input-'.$class; |
| 129 | 129 | }, $classes); |
| 130 | 130 | |
@@ -13,374 +13,374 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap 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', 'search'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'large', |
|
| 30 | - 'small', |
|
| 31 | - 'mini', |
|
| 32 | - 'block', |
|
| 33 | - 'danger', |
|
| 34 | - 'info', |
|
| 35 | - 'inverse', |
|
| 36 | - 'link', |
|
| 37 | - 'primary', |
|
| 38 | - 'success', |
|
| 39 | - 'warning', |
|
| 40 | - ); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The field sizes available |
|
| 44 | - * |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - private $fields = array( |
|
| 48 | - 'mini', |
|
| 49 | - 'small', |
|
| 50 | - 'medium', |
|
| 51 | - 'large', |
|
| 52 | - 'xlarge', |
|
| 53 | - 'xxlarge', |
|
| 54 | - 'span1', |
|
| 55 | - 'span2', |
|
| 56 | - 'span3', |
|
| 57 | - 'span4', |
|
| 58 | - 'span5', |
|
| 59 | - 'span6', |
|
| 60 | - 'span7', |
|
| 61 | - 'span8', |
|
| 62 | - 'span9', |
|
| 63 | - 'span10', |
|
| 64 | - 'span11', |
|
| 65 | - 'span12', |
|
| 66 | - ); |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * The field states available |
|
| 70 | - * |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - protected $states = array( |
|
| 74 | - 'success', |
|
| 75 | - 'warning', |
|
| 76 | - 'error', |
|
| 77 | - 'info', |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Create a new TwitterBootstrap instance |
|
| 82 | - * |
|
| 83 | - * @param \Illuminate\Container\Container $app |
|
| 84 | - */ |
|
| 85 | - public function __construct(Container $app) |
|
| 86 | - { |
|
| 87 | - $this->app = $app; |
|
| 88 | - $this->setFrameworkDefaults(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - //////////////////////////////////////////////////////////////////// |
|
| 92 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 93 | - //////////////////////////////////////////////////////////////////// |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Filter buttons classes |
|
| 97 | - * |
|
| 98 | - * @param array $classes An array of classes |
|
| 99 | - * |
|
| 100 | - * @return string[] A filtered array |
|
| 101 | - */ |
|
| 102 | - public function filterButtonClasses($classes) |
|
| 103 | - { |
|
| 104 | - // Filter classes |
|
| 105 | - // $classes = array_intersect($classes, $this->buttons); |
|
| 106 | - |
|
| 107 | - // Prepend button type |
|
| 108 | - $classes = $this->prependWith($classes, 'btn-'); |
|
| 109 | - $classes[] = 'btn'; |
|
| 110 | - |
|
| 111 | - return $classes; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Filter field classes |
|
| 116 | - * |
|
| 117 | - * @param array $classes An array of classes |
|
| 118 | - * |
|
| 119 | - * @return array A filtered array |
|
| 120 | - */ |
|
| 121 | - public function filterFieldClasses($classes) |
|
| 122 | - { |
|
| 123 | - // Filter classes |
|
| 124 | - $classes = array_intersect($classes, $this->fields); |
|
| 125 | - |
|
| 126 | - // Prepend field type |
|
| 127 | - $classes = array_map(function ($class) { |
|
| 128 | - return Str::startsWith($class, 'span') ? $class : 'input-'.$class; |
|
| 129 | - }, $classes); |
|
| 130 | - |
|
| 131 | - return $classes; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - //////////////////////////////////////////////////////////////////// |
|
| 135 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 136 | - //////////////////////////////////////////////////////////////////// |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Add classes to a field |
|
| 140 | - * |
|
| 141 | - * @param Field $field |
|
| 142 | - * @param array $classes The possible classes to add |
|
| 143 | - * |
|
| 144 | - * @return Field |
|
| 145 | - */ |
|
| 146 | - public function getFieldClasses(Field $field, $classes) |
|
| 147 | - { |
|
| 148 | - // Add inline class for checkables |
|
| 149 | - if ($field->isCheckable() and in_array('inline', $classes)) { |
|
| 150 | - $field->inline(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // Filter classes according to field type |
|
| 154 | - if ($field->isButton()) { |
|
| 155 | - $classes = $this->filterButtonClasses($classes); |
|
| 156 | - } else { |
|
| 157 | - $classes = $this->filterFieldClasses($classes); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $this->addClassesToField($field, $classes); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Add group classes |
|
| 165 | - * |
|
| 166 | - * @return string A list of group classes |
|
| 167 | - */ |
|
| 168 | - public function getGroupClasses() |
|
| 169 | - { |
|
| 170 | - return 'control-group'; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Add label classes |
|
| 175 | - * |
|
| 176 | - * @return string An array of attributes with the label class |
|
| 177 | - */ |
|
| 178 | - public function getLabelClasses() |
|
| 179 | - { |
|
| 180 | - return 'control-label'; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Add uneditable field classes |
|
| 185 | - * |
|
| 186 | - * @return string An array of attributes with the uneditable class |
|
| 187 | - */ |
|
| 188 | - public function getUneditableClasses() |
|
| 189 | - { |
|
| 190 | - return 'uneditable-input'; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - public function getPlainTextClasses() |
|
| 194 | - { |
|
| 195 | - return null; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Add form class |
|
| 200 | - * |
|
| 201 | - * @param string $type The type of form to add |
|
| 202 | - * |
|
| 203 | - * @return string|null |
|
| 204 | - */ |
|
| 205 | - public function getFormClasses($type) |
|
| 206 | - { |
|
| 207 | - return $type ? 'form-'.$type : null; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Add actions block class |
|
| 212 | - * |
|
| 213 | - * @return string |
|
| 214 | - */ |
|
| 215 | - public function getActionClasses() |
|
| 216 | - { |
|
| 217 | - return 'form-actions'; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - //////////////////////////////////////////////////////////////////// |
|
| 221 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 222 | - //////////////////////////////////////////////////////////////////// |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Render an help text |
|
| 226 | - * |
|
| 227 | - * @param string $text |
|
| 228 | - * @param array $attributes |
|
| 229 | - * |
|
| 230 | - * @return Element |
|
| 231 | - */ |
|
| 232 | - public function createHelp($text, $attributes = array()) |
|
| 233 | - { |
|
| 234 | - return Element::create('span', $text, $attributes)->addClass('help-inline'); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Render a block help text |
|
| 239 | - * |
|
| 240 | - * @param string $text |
|
| 241 | - * @param array $attributes |
|
| 242 | - * |
|
| 243 | - * @return Element |
|
| 244 | - */ |
|
| 245 | - public function createBlockHelp($text, $attributes = array()) |
|
| 246 | - { |
|
| 247 | - return Element::create('p', $text, $attributes)->addClass('help-block'); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Render a disabled field |
|
| 252 | - * |
|
| 253 | - * @param Field $field |
|
| 254 | - * |
|
| 255 | - * @return Element |
|
| 256 | - */ |
|
| 257 | - public function createDisabledField(Field $field) |
|
| 258 | - { |
|
| 259 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Render a plain text field |
|
| 264 | - * Which fallback to a disabled field |
|
| 265 | - * |
|
| 266 | - * @param Field $field |
|
| 267 | - * |
|
| 268 | - * @return Element |
|
| 269 | - */ |
|
| 270 | - public function createPlainTextField(Field $field) |
|
| 271 | - { |
|
| 272 | - return $this->createDisabledField($field); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Render an icon |
|
| 277 | - * |
|
| 278 | - * @param array $attributes Its general attributes |
|
| 279 | - * |
|
| 280 | - * @return string |
|
| 281 | - */ |
|
| 282 | - public function createIcon($iconType, $attributes = array(), $iconSettings = array()) |
|
| 283 | - { |
|
| 284 | - // Check for empty icons |
|
| 285 | - if (!$iconType) { |
|
| 286 | - return false; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - // Create tag |
|
| 290 | - $tag = array_get($iconSettings, 'tag', $this->iconTag); |
|
| 291 | - $icon = Element::create($tag, null, $attributes); |
|
| 292 | - |
|
| 293 | - // White icons ignore user overrides to use legacy Bootstrap styling |
|
| 294 | - if (Str::contains($iconType, 'white')) { |
|
| 295 | - $iconType = str_replace('white', '', $iconType); |
|
| 296 | - $iconType = trim($iconType, '-'); |
|
| 297 | - $icon->addClass('icon-white'); |
|
| 298 | - $set = null; |
|
| 299 | - $prefix = 'icon'; |
|
| 300 | - } else { |
|
| 301 | - $set = array_get($iconSettings, 'set', $this->iconSet); |
|
| 302 | - $prefix = array_get($iconSettings, 'prefix', $this->iconPrefix); |
|
| 303 | - } |
|
| 304 | - $icon->addClass("$set $prefix-$iconType"); |
|
| 305 | - |
|
| 306 | - return $icon; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - //////////////////////////////////////////////////////////////////// |
|
| 310 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 311 | - //////////////////////////////////////////////////////////////////// |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Wrap an item to be prepended or appended to the current field |
|
| 315 | - * |
|
| 316 | - * @param string $item |
|
| 317 | - * |
|
| 318 | - * @return string A wrapped item |
|
| 319 | - */ |
|
| 320 | - public function placeAround($item) |
|
| 321 | - { |
|
| 322 | - // Render object |
|
| 323 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 324 | - $item = $item->__toString(); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Return unwrapped if button |
|
| 328 | - if (strpos($item, '<button') !== false) { |
|
| 329 | - return $item; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return Element::create('span', $item)->addClass('add-on'); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Wrap a field with prepended and appended items |
|
| 337 | - * |
|
| 338 | - * @param Field $field |
|
| 339 | - * @param array $prepend |
|
| 340 | - * @param array $append |
|
| 341 | - * |
|
| 342 | - * @return string A field concatented with prepended and/or appended items |
|
| 343 | - */ |
|
| 344 | - public function prependAppend($field, $prepend, $append) |
|
| 345 | - { |
|
| 346 | - $class = array(); |
|
| 347 | - if ($prepend) { |
|
| 348 | - $class[] = 'input-prepend'; |
|
| 349 | - } |
|
| 350 | - if ($append) { |
|
| 351 | - $class[] = 'input-append'; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $return = '<div class="'.join(' ', $class).'">'; |
|
| 355 | - $return .= join(null, $prepend); |
|
| 356 | - $return .= $field->render(); |
|
| 357 | - $return .= join(null, $append); |
|
| 358 | - $return .= '</div>'; |
|
| 359 | - |
|
| 360 | - return $return; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Wrap a field with potential additional tags |
|
| 365 | - * |
|
| 366 | - * @param Field $field |
|
| 367 | - * |
|
| 368 | - * @return Element A wrapped field |
|
| 369 | - */ |
|
| 370 | - public function wrapField($field) |
|
| 371 | - { |
|
| 372 | - return Element::create('div', $field)->addClass('controls'); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Wrap actions block with potential additional tags |
|
| 377 | - * |
|
| 378 | - * @param Actions $actions |
|
| 379 | - * |
|
| 380 | - * @return string A wrapped actions block |
|
| 381 | - */ |
|
| 382 | - public function wrapActions($actions) |
|
| 383 | - { |
|
| 384 | - return $actions; |
|
| 385 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical', 'inline', 'search'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'large', |
|
| 30 | + 'small', |
|
| 31 | + 'mini', |
|
| 32 | + 'block', |
|
| 33 | + 'danger', |
|
| 34 | + 'info', |
|
| 35 | + 'inverse', |
|
| 36 | + 'link', |
|
| 37 | + 'primary', |
|
| 38 | + 'success', |
|
| 39 | + 'warning', |
|
| 40 | + ); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The field sizes available |
|
| 44 | + * |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + private $fields = array( |
|
| 48 | + 'mini', |
|
| 49 | + 'small', |
|
| 50 | + 'medium', |
|
| 51 | + 'large', |
|
| 52 | + 'xlarge', |
|
| 53 | + 'xxlarge', |
|
| 54 | + 'span1', |
|
| 55 | + 'span2', |
|
| 56 | + 'span3', |
|
| 57 | + 'span4', |
|
| 58 | + 'span5', |
|
| 59 | + 'span6', |
|
| 60 | + 'span7', |
|
| 61 | + 'span8', |
|
| 62 | + 'span9', |
|
| 63 | + 'span10', |
|
| 64 | + 'span11', |
|
| 65 | + 'span12', |
|
| 66 | + ); |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * The field states available |
|
| 70 | + * |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + protected $states = array( |
|
| 74 | + 'success', |
|
| 75 | + 'warning', |
|
| 76 | + 'error', |
|
| 77 | + 'info', |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Create a new TwitterBootstrap instance |
|
| 82 | + * |
|
| 83 | + * @param \Illuminate\Container\Container $app |
|
| 84 | + */ |
|
| 85 | + public function __construct(Container $app) |
|
| 86 | + { |
|
| 87 | + $this->app = $app; |
|
| 88 | + $this->setFrameworkDefaults(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + //////////////////////////////////////////////////////////////////// |
|
| 92 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 93 | + //////////////////////////////////////////////////////////////////// |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Filter buttons classes |
|
| 97 | + * |
|
| 98 | + * @param array $classes An array of classes |
|
| 99 | + * |
|
| 100 | + * @return string[] A filtered array |
|
| 101 | + */ |
|
| 102 | + public function filterButtonClasses($classes) |
|
| 103 | + { |
|
| 104 | + // Filter classes |
|
| 105 | + // $classes = array_intersect($classes, $this->buttons); |
|
| 106 | + |
|
| 107 | + // Prepend button type |
|
| 108 | + $classes = $this->prependWith($classes, 'btn-'); |
|
| 109 | + $classes[] = 'btn'; |
|
| 110 | + |
|
| 111 | + return $classes; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Filter field classes |
|
| 116 | + * |
|
| 117 | + * @param array $classes An array of classes |
|
| 118 | + * |
|
| 119 | + * @return array A filtered array |
|
| 120 | + */ |
|
| 121 | + public function filterFieldClasses($classes) |
|
| 122 | + { |
|
| 123 | + // Filter classes |
|
| 124 | + $classes = array_intersect($classes, $this->fields); |
|
| 125 | + |
|
| 126 | + // Prepend field type |
|
| 127 | + $classes = array_map(function ($class) { |
|
| 128 | + return Str::startsWith($class, 'span') ? $class : 'input-'.$class; |
|
| 129 | + }, $classes); |
|
| 130 | + |
|
| 131 | + return $classes; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + //////////////////////////////////////////////////////////////////// |
|
| 135 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 136 | + //////////////////////////////////////////////////////////////////// |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Add classes to a field |
|
| 140 | + * |
|
| 141 | + * @param Field $field |
|
| 142 | + * @param array $classes The possible classes to add |
|
| 143 | + * |
|
| 144 | + * @return Field |
|
| 145 | + */ |
|
| 146 | + public function getFieldClasses(Field $field, $classes) |
|
| 147 | + { |
|
| 148 | + // Add inline class for checkables |
|
| 149 | + if ($field->isCheckable() and in_array('inline', $classes)) { |
|
| 150 | + $field->inline(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // Filter classes according to field type |
|
| 154 | + if ($field->isButton()) { |
|
| 155 | + $classes = $this->filterButtonClasses($classes); |
|
| 156 | + } else { |
|
| 157 | + $classes = $this->filterFieldClasses($classes); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $this->addClassesToField($field, $classes); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Add group classes |
|
| 165 | + * |
|
| 166 | + * @return string A list of group classes |
|
| 167 | + */ |
|
| 168 | + public function getGroupClasses() |
|
| 169 | + { |
|
| 170 | + return 'control-group'; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Add label classes |
|
| 175 | + * |
|
| 176 | + * @return string An array of attributes with the label class |
|
| 177 | + */ |
|
| 178 | + public function getLabelClasses() |
|
| 179 | + { |
|
| 180 | + return 'control-label'; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Add uneditable field classes |
|
| 185 | + * |
|
| 186 | + * @return string An array of attributes with the uneditable class |
|
| 187 | + */ |
|
| 188 | + public function getUneditableClasses() |
|
| 189 | + { |
|
| 190 | + return 'uneditable-input'; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + public function getPlainTextClasses() |
|
| 194 | + { |
|
| 195 | + return null; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Add form class |
|
| 200 | + * |
|
| 201 | + * @param string $type The type of form to add |
|
| 202 | + * |
|
| 203 | + * @return string|null |
|
| 204 | + */ |
|
| 205 | + public function getFormClasses($type) |
|
| 206 | + { |
|
| 207 | + return $type ? 'form-'.$type : null; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Add actions block class |
|
| 212 | + * |
|
| 213 | + * @return string |
|
| 214 | + */ |
|
| 215 | + public function getActionClasses() |
|
| 216 | + { |
|
| 217 | + return 'form-actions'; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + //////////////////////////////////////////////////////////////////// |
|
| 221 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 222 | + //////////////////////////////////////////////////////////////////// |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Render an help text |
|
| 226 | + * |
|
| 227 | + * @param string $text |
|
| 228 | + * @param array $attributes |
|
| 229 | + * |
|
| 230 | + * @return Element |
|
| 231 | + */ |
|
| 232 | + public function createHelp($text, $attributes = array()) |
|
| 233 | + { |
|
| 234 | + return Element::create('span', $text, $attributes)->addClass('help-inline'); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Render a block help text |
|
| 239 | + * |
|
| 240 | + * @param string $text |
|
| 241 | + * @param array $attributes |
|
| 242 | + * |
|
| 243 | + * @return Element |
|
| 244 | + */ |
|
| 245 | + public function createBlockHelp($text, $attributes = array()) |
|
| 246 | + { |
|
| 247 | + return Element::create('p', $text, $attributes)->addClass('help-block'); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Render a disabled field |
|
| 252 | + * |
|
| 253 | + * @param Field $field |
|
| 254 | + * |
|
| 255 | + * @return Element |
|
| 256 | + */ |
|
| 257 | + public function createDisabledField(Field $field) |
|
| 258 | + { |
|
| 259 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Render a plain text field |
|
| 264 | + * Which fallback to a disabled field |
|
| 265 | + * |
|
| 266 | + * @param Field $field |
|
| 267 | + * |
|
| 268 | + * @return Element |
|
| 269 | + */ |
|
| 270 | + public function createPlainTextField(Field $field) |
|
| 271 | + { |
|
| 272 | + return $this->createDisabledField($field); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Render an icon |
|
| 277 | + * |
|
| 278 | + * @param array $attributes Its general attributes |
|
| 279 | + * |
|
| 280 | + * @return string |
|
| 281 | + */ |
|
| 282 | + public function createIcon($iconType, $attributes = array(), $iconSettings = array()) |
|
| 283 | + { |
|
| 284 | + // Check for empty icons |
|
| 285 | + if (!$iconType) { |
|
| 286 | + return false; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + // Create tag |
|
| 290 | + $tag = array_get($iconSettings, 'tag', $this->iconTag); |
|
| 291 | + $icon = Element::create($tag, null, $attributes); |
|
| 292 | + |
|
| 293 | + // White icons ignore user overrides to use legacy Bootstrap styling |
|
| 294 | + if (Str::contains($iconType, 'white')) { |
|
| 295 | + $iconType = str_replace('white', '', $iconType); |
|
| 296 | + $iconType = trim($iconType, '-'); |
|
| 297 | + $icon->addClass('icon-white'); |
|
| 298 | + $set = null; |
|
| 299 | + $prefix = 'icon'; |
|
| 300 | + } else { |
|
| 301 | + $set = array_get($iconSettings, 'set', $this->iconSet); |
|
| 302 | + $prefix = array_get($iconSettings, 'prefix', $this->iconPrefix); |
|
| 303 | + } |
|
| 304 | + $icon->addClass("$set $prefix-$iconType"); |
|
| 305 | + |
|
| 306 | + return $icon; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + //////////////////////////////////////////////////////////////////// |
|
| 310 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 311 | + //////////////////////////////////////////////////////////////////// |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Wrap an item to be prepended or appended to the current field |
|
| 315 | + * |
|
| 316 | + * @param string $item |
|
| 317 | + * |
|
| 318 | + * @return string A wrapped item |
|
| 319 | + */ |
|
| 320 | + public function placeAround($item) |
|
| 321 | + { |
|
| 322 | + // Render object |
|
| 323 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 324 | + $item = $item->__toString(); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Return unwrapped if button |
|
| 328 | + if (strpos($item, '<button') !== false) { |
|
| 329 | + return $item; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return Element::create('span', $item)->addClass('add-on'); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Wrap a field with prepended and appended items |
|
| 337 | + * |
|
| 338 | + * @param Field $field |
|
| 339 | + * @param array $prepend |
|
| 340 | + * @param array $append |
|
| 341 | + * |
|
| 342 | + * @return string A field concatented with prepended and/or appended items |
|
| 343 | + */ |
|
| 344 | + public function prependAppend($field, $prepend, $append) |
|
| 345 | + { |
|
| 346 | + $class = array(); |
|
| 347 | + if ($prepend) { |
|
| 348 | + $class[] = 'input-prepend'; |
|
| 349 | + } |
|
| 350 | + if ($append) { |
|
| 351 | + $class[] = 'input-append'; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $return = '<div class="'.join(' ', $class).'">'; |
|
| 355 | + $return .= join(null, $prepend); |
|
| 356 | + $return .= $field->render(); |
|
| 357 | + $return .= join(null, $append); |
|
| 358 | + $return .= '</div>'; |
|
| 359 | + |
|
| 360 | + return $return; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Wrap a field with potential additional tags |
|
| 365 | + * |
|
| 366 | + * @param Field $field |
|
| 367 | + * |
|
| 368 | + * @return Element A wrapped field |
|
| 369 | + */ |
|
| 370 | + public function wrapField($field) |
|
| 371 | + { |
|
| 372 | + return Element::create('div', $field)->addClass('controls'); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Wrap actions block with potential additional tags |
|
| 377 | + * |
|
| 378 | + * @param Actions $actions |
|
| 379 | + * |
|
| 380 | + * @return string A wrapped actions block |
|
| 381 | + */ |
|
| 382 | + public function wrapActions($actions) |
|
| 383 | + { |
|
| 384 | + return $actions; |
|
| 385 | + } |
|
| 386 | 386 | } |
@@ -126,7 +126,7 @@ |
||
| 126 | 126 | * |
| 127 | 127 | * @param Field $field |
| 128 | 128 | * |
| 129 | - * @return Element |
|
| 129 | + * @return Input |
|
| 130 | 130 | */ |
| 131 | 131 | public function createPlainTextField(Field $field) |
| 132 | 132 | { |
@@ -13,274 +13,274 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ZurbFoundation 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'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The field sizes available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $fields = array( |
|
| 29 | - 1 => 'one', |
|
| 30 | - 2 => 'two', |
|
| 31 | - 3 => 'three', |
|
| 32 | - 4 => 'four', |
|
| 33 | - 5 => 'five', |
|
| 34 | - 6 => 'six', |
|
| 35 | - 7 => 'seven', |
|
| 36 | - 8 => 'eight', |
|
| 37 | - 9 => 'nine', |
|
| 38 | - 10 => 'ten', |
|
| 39 | - 11 => 'eleven', |
|
| 40 | - 12 => 'twelve', |
|
| 41 | - ); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The field states available |
|
| 45 | - * |
|
| 46 | - * @var array |
|
| 47 | - */ |
|
| 48 | - protected $states = array( |
|
| 49 | - 'error', |
|
| 50 | - ); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Create a new ZurbFoundation instance |
|
| 54 | - * |
|
| 55 | - * @param Container $app |
|
| 56 | - */ |
|
| 57 | - public function __construct(Container $app) |
|
| 58 | - { |
|
| 59 | - $this->app = $app; |
|
| 60 | - $this->setFrameworkDefaults(); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - //////////////////////////////////////////////////////////////////// |
|
| 64 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 65 | - //////////////////////////////////////////////////////////////////// |
|
| 66 | - |
|
| 67 | - public function filterButtonClasses($classes) |
|
| 68 | - { |
|
| 69 | - return $classes; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function filterFieldClasses($classes) |
|
| 73 | - { |
|
| 74 | - // Filter classes |
|
| 75 | - $classes = array_intersect($classes, $this->fields); |
|
| 76 | - |
|
| 77 | - return $classes; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - //////////////////////////////////////////////////////////////////// |
|
| 81 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - |
|
| 84 | - protected function setFieldWidths($labelWidths) |
|
| 85 | - { |
|
| 86 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 87 | - |
|
| 88 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 89 | - |
|
| 90 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 91 | - if ($viewport) { |
|
| 92 | - $labelWidthClass .= $viewports[$viewport].$this->fields[$columns].' '; |
|
| 93 | - $fieldWidthClass .= $viewports[$viewport].$this->fields[12 - $columns].' '; |
|
| 94 | - $fieldOffsetClass .= $viewports[$viewport].'offset-by-'.$this->fields[$columns].' '; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $this->labelWidth = $labelWidthClass.'columns'; |
|
| 99 | - $this->fieldWidth = $fieldWidthClass.'columns'; |
|
| 100 | - $this->fieldOffset = $fieldOffsetClass.'columns'; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - //////////////////////////////////////////////////////////////////// |
|
| 104 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 105 | - //////////////////////////////////////////////////////////////////// |
|
| 106 | - |
|
| 107 | - public function getFieldClasses(Field $field, $classes = array()) |
|
| 108 | - { |
|
| 109 | - $classes = $this->filterFieldClasses($classes); |
|
| 110 | - |
|
| 111 | - return $this->addClassesToField($field, $classes); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - public function getGroupClasses() |
|
| 115 | - { |
|
| 116 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 117 | - return 'row'; |
|
| 118 | - } else { |
|
| 119 | - return null; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Add label classes |
|
| 125 | - * |
|
| 126 | - * @return string|null An array of attributes with the label class |
|
| 127 | - */ |
|
| 128 | - public function getLabelClasses() |
|
| 129 | - { |
|
| 130 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 131 | - return $this->getFrameworkOption('wrappedLabelClasses'); |
|
| 132 | - } else { |
|
| 133 | - return null; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - public function getUneditableClasses() |
|
| 138 | - { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public function getPlainTextClasses() |
|
| 143 | - { |
|
| 144 | - return null; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - public function getFormClasses($type) |
|
| 148 | - { |
|
| 149 | - return null; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - public function getActionClasses() |
|
| 153 | - { |
|
| 154 | - return null; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - //////////////////////////////////////////////////////////////////// |
|
| 158 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 159 | - //////////////////////////////////////////////////////////////////// |
|
| 160 | - |
|
| 161 | - public function createHelp($text, $attributes = null) |
|
| 162 | - { |
|
| 163 | - if (is_null($attributes) or empty($attributes)) { |
|
| 164 | - $attributes = $this->getFrameworkOption('error_classes'); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return Element::create('span', $text, $attributes); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Render a disabled field |
|
| 172 | - * |
|
| 173 | - * @param Field $field |
|
| 174 | - * |
|
| 175 | - * @return Input |
|
| 176 | - */ |
|
| 177 | - public function createDisabledField(Field $field) |
|
| 178 | - { |
|
| 179 | - $field->disabled(); |
|
| 180 | - |
|
| 181 | - return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Render a plain text field |
|
| 186 | - * Which fallback to a disabled field |
|
| 187 | - * |
|
| 188 | - * @param Field $field |
|
| 189 | - * |
|
| 190 | - * @return Element |
|
| 191 | - */ |
|
| 192 | - public function createPlainTextField(Field $field) |
|
| 193 | - { |
|
| 194 | - return $this->createDisabledField($field); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - //////////////////////////////////////////////////////////////////// |
|
| 198 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 199 | - //////////////////////////////////////////////////////////////////// |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Wrap an item to be prepended or appended to the current field. |
|
| 203 | - * For Zurb we return the item and handle the wrapping in prependAppend |
|
| 204 | - * as wrapping is dependent on whether we're prepending or appending. |
|
| 205 | - * |
|
| 206 | - * @return string A wrapped item |
|
| 207 | - */ |
|
| 208 | - public function placeAround($item) |
|
| 209 | - { |
|
| 210 | - return $item; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Wrap a field with prepended and appended items |
|
| 215 | - * |
|
| 216 | - * @param Field $field |
|
| 217 | - * @param array $prepend |
|
| 218 | - * @param array $append |
|
| 219 | - * |
|
| 220 | - * @return string A field concatented with prepended and/or appended items |
|
| 221 | - */ |
|
| 222 | - public function prependAppend($field, $prepend, $append) |
|
| 223 | - { |
|
| 224 | - $return = ''; |
|
| 225 | - |
|
| 226 | - foreach ($prepend as $item) { |
|
| 227 | - $return .= '<div class="two mobile-one columns"><span class="prefix">'.$item.'</span></div>'; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $return .= '<div class="ten mobile-three columns">'.$field->render().'</div>'; |
|
| 231 | - |
|
| 232 | - foreach ($append as $item) { |
|
| 233 | - $return .= '<div class="two mobile-one columns"><span class="postfix">'.$item.'</span></div>'; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $return; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Wraps all label contents with potential additional tags. |
|
| 241 | - * |
|
| 242 | - * @param string $label |
|
| 243 | - * |
|
| 244 | - * @return string A wrapped label |
|
| 245 | - */ |
|
| 246 | - public function wrapLabel($label) |
|
| 247 | - { |
|
| 248 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 249 | - return Element::create('div', $label)->addClass($this->labelWidth); |
|
| 250 | - } else { |
|
| 251 | - return $label; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Wraps all field contents with potential additional tags. |
|
| 257 | - * |
|
| 258 | - * @param Field $field |
|
| 259 | - * |
|
| 260 | - * @return Element A wrapped field |
|
| 261 | - */ |
|
| 262 | - public function wrapField($field) |
|
| 263 | - { |
|
| 264 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 265 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 266 | - } else { |
|
| 267 | - return $field; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Wrap actions block with potential additional tags |
|
| 273 | - * |
|
| 274 | - * @param Actions $actions |
|
| 275 | - * |
|
| 276 | - * @return string A wrapped actions block |
|
| 277 | - */ |
|
| 278 | - public function wrapActions($actions) |
|
| 279 | - { |
|
| 280 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 281 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 282 | - } else { |
|
| 283 | - return $actions; |
|
| 284 | - } |
|
| 285 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The field sizes available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $fields = array( |
|
| 29 | + 1 => 'one', |
|
| 30 | + 2 => 'two', |
|
| 31 | + 3 => 'three', |
|
| 32 | + 4 => 'four', |
|
| 33 | + 5 => 'five', |
|
| 34 | + 6 => 'six', |
|
| 35 | + 7 => 'seven', |
|
| 36 | + 8 => 'eight', |
|
| 37 | + 9 => 'nine', |
|
| 38 | + 10 => 'ten', |
|
| 39 | + 11 => 'eleven', |
|
| 40 | + 12 => 'twelve', |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The field states available |
|
| 45 | + * |
|
| 46 | + * @var array |
|
| 47 | + */ |
|
| 48 | + protected $states = array( |
|
| 49 | + 'error', |
|
| 50 | + ); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Create a new ZurbFoundation instance |
|
| 54 | + * |
|
| 55 | + * @param Container $app |
|
| 56 | + */ |
|
| 57 | + public function __construct(Container $app) |
|
| 58 | + { |
|
| 59 | + $this->app = $app; |
|
| 60 | + $this->setFrameworkDefaults(); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + //////////////////////////////////////////////////////////////////// |
|
| 64 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 65 | + //////////////////////////////////////////////////////////////////// |
|
| 66 | + |
|
| 67 | + public function filterButtonClasses($classes) |
|
| 68 | + { |
|
| 69 | + return $classes; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function filterFieldClasses($classes) |
|
| 73 | + { |
|
| 74 | + // Filter classes |
|
| 75 | + $classes = array_intersect($classes, $this->fields); |
|
| 76 | + |
|
| 77 | + return $classes; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + //////////////////////////////////////////////////////////////////// |
|
| 81 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + |
|
| 84 | + protected function setFieldWidths($labelWidths) |
|
| 85 | + { |
|
| 86 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 87 | + |
|
| 88 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 89 | + |
|
| 90 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 91 | + if ($viewport) { |
|
| 92 | + $labelWidthClass .= $viewports[$viewport].$this->fields[$columns].' '; |
|
| 93 | + $fieldWidthClass .= $viewports[$viewport].$this->fields[12 - $columns].' '; |
|
| 94 | + $fieldOffsetClass .= $viewports[$viewport].'offset-by-'.$this->fields[$columns].' '; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $this->labelWidth = $labelWidthClass.'columns'; |
|
| 99 | + $this->fieldWidth = $fieldWidthClass.'columns'; |
|
| 100 | + $this->fieldOffset = $fieldOffsetClass.'columns'; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + //////////////////////////////////////////////////////////////////// |
|
| 104 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 105 | + //////////////////////////////////////////////////////////////////// |
|
| 106 | + |
|
| 107 | + public function getFieldClasses(Field $field, $classes = array()) |
|
| 108 | + { |
|
| 109 | + $classes = $this->filterFieldClasses($classes); |
|
| 110 | + |
|
| 111 | + return $this->addClassesToField($field, $classes); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + public function getGroupClasses() |
|
| 115 | + { |
|
| 116 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 117 | + return 'row'; |
|
| 118 | + } else { |
|
| 119 | + return null; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Add label classes |
|
| 125 | + * |
|
| 126 | + * @return string|null An array of attributes with the label class |
|
| 127 | + */ |
|
| 128 | + public function getLabelClasses() |
|
| 129 | + { |
|
| 130 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 131 | + return $this->getFrameworkOption('wrappedLabelClasses'); |
|
| 132 | + } else { |
|
| 133 | + return null; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + public function getUneditableClasses() |
|
| 138 | + { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public function getPlainTextClasses() |
|
| 143 | + { |
|
| 144 | + return null; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + public function getFormClasses($type) |
|
| 148 | + { |
|
| 149 | + return null; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + public function getActionClasses() |
|
| 153 | + { |
|
| 154 | + return null; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + //////////////////////////////////////////////////////////////////// |
|
| 158 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 159 | + //////////////////////////////////////////////////////////////////// |
|
| 160 | + |
|
| 161 | + public function createHelp($text, $attributes = null) |
|
| 162 | + { |
|
| 163 | + if (is_null($attributes) or empty($attributes)) { |
|
| 164 | + $attributes = $this->getFrameworkOption('error_classes'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return Element::create('span', $text, $attributes); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Render a disabled field |
|
| 172 | + * |
|
| 173 | + * @param Field $field |
|
| 174 | + * |
|
| 175 | + * @return Input |
|
| 176 | + */ |
|
| 177 | + public function createDisabledField(Field $field) |
|
| 178 | + { |
|
| 179 | + $field->disabled(); |
|
| 180 | + |
|
| 181 | + return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Render a plain text field |
|
| 186 | + * Which fallback to a disabled field |
|
| 187 | + * |
|
| 188 | + * @param Field $field |
|
| 189 | + * |
|
| 190 | + * @return Element |
|
| 191 | + */ |
|
| 192 | + public function createPlainTextField(Field $field) |
|
| 193 | + { |
|
| 194 | + return $this->createDisabledField($field); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + //////////////////////////////////////////////////////////////////// |
|
| 198 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 199 | + //////////////////////////////////////////////////////////////////// |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Wrap an item to be prepended or appended to the current field. |
|
| 203 | + * For Zurb we return the item and handle the wrapping in prependAppend |
|
| 204 | + * as wrapping is dependent on whether we're prepending or appending. |
|
| 205 | + * |
|
| 206 | + * @return string A wrapped item |
|
| 207 | + */ |
|
| 208 | + public function placeAround($item) |
|
| 209 | + { |
|
| 210 | + return $item; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Wrap a field with prepended and appended items |
|
| 215 | + * |
|
| 216 | + * @param Field $field |
|
| 217 | + * @param array $prepend |
|
| 218 | + * @param array $append |
|
| 219 | + * |
|
| 220 | + * @return string A field concatented with prepended and/or appended items |
|
| 221 | + */ |
|
| 222 | + public function prependAppend($field, $prepend, $append) |
|
| 223 | + { |
|
| 224 | + $return = ''; |
|
| 225 | + |
|
| 226 | + foreach ($prepend as $item) { |
|
| 227 | + $return .= '<div class="two mobile-one columns"><span class="prefix">'.$item.'</span></div>'; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $return .= '<div class="ten mobile-three columns">'.$field->render().'</div>'; |
|
| 231 | + |
|
| 232 | + foreach ($append as $item) { |
|
| 233 | + $return .= '<div class="two mobile-one columns"><span class="postfix">'.$item.'</span></div>'; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return $return; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Wraps all label contents with potential additional tags. |
|
| 241 | + * |
|
| 242 | + * @param string $label |
|
| 243 | + * |
|
| 244 | + * @return string A wrapped label |
|
| 245 | + */ |
|
| 246 | + public function wrapLabel($label) |
|
| 247 | + { |
|
| 248 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 249 | + return Element::create('div', $label)->addClass($this->labelWidth); |
|
| 250 | + } else { |
|
| 251 | + return $label; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Wraps all field contents with potential additional tags. |
|
| 257 | + * |
|
| 258 | + * @param Field $field |
|
| 259 | + * |
|
| 260 | + * @return Element A wrapped field |
|
| 261 | + */ |
|
| 262 | + public function wrapField($field) |
|
| 263 | + { |
|
| 264 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 265 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 266 | + } else { |
|
| 267 | + return $field; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Wrap actions block with potential additional tags |
|
| 273 | + * |
|
| 274 | + * @param Actions $actions |
|
| 275 | + * |
|
| 276 | + * @return string A wrapped actions block |
|
| 277 | + */ |
|
| 278 | + public function wrapActions($actions) |
|
| 279 | + { |
|
| 280 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 281 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 282 | + } else { |
|
| 283 | + return $actions; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | 286 | } |
@@ -126,7 +126,7 @@ |
||
| 126 | 126 | * |
| 127 | 127 | * @param Field $field |
| 128 | 128 | * |
| 129 | - * @return Element |
|
| 129 | + * @return Input |
|
| 130 | 130 | */ |
| 131 | 131 | public function createPlainTextField(Field $field) |
| 132 | 132 | { |
@@ -13,281 +13,281 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ZurbFoundation4 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'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'tiny', |
|
| 30 | - 'small', |
|
| 31 | - 'medium', |
|
| 32 | - 'large', |
|
| 33 | - 'success', |
|
| 34 | - 'radius', |
|
| 35 | - 'round', |
|
| 36 | - 'disabled', |
|
| 37 | - 'prefix', |
|
| 38 | - 'postfix', |
|
| 39 | - ); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The field sizes available |
|
| 43 | - * Zurb Foundation 4 does not apply sizes to the form element, but to the wrapper div |
|
| 44 | - * |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - private $fields = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The field states available |
|
| 51 | - * |
|
| 52 | - * @var array |
|
| 53 | - */ |
|
| 54 | - protected $states = array( |
|
| 55 | - 'error', |
|
| 56 | - ); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Create a new ZurbFoundation instance |
|
| 60 | - * |
|
| 61 | - * @param \Illuminate\Container\Container $app |
|
| 62 | - */ |
|
| 63 | - public function __construct(Container $app) |
|
| 64 | - { |
|
| 65 | - $this->app = $app; |
|
| 66 | - $this->setFrameworkDefaults(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - //////////////////////////////////////////////////////////////////// |
|
| 70 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 71 | - //////////////////////////////////////////////////////////////////// |
|
| 72 | - |
|
| 73 | - public function filterButtonClasses($classes) |
|
| 74 | - { |
|
| 75 | - // Filter classes |
|
| 76 | - $classes = array_intersect($classes, $this->buttons); |
|
| 77 | - $classes[] = 'button'; |
|
| 78 | - |
|
| 79 | - return $classes; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function filterFieldClasses($classes) |
|
| 83 | - { |
|
| 84 | - return null; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - //////////////////////////////////////////////////////////////////// |
|
| 88 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 89 | - //////////////////////////////////////////////////////////////////// |
|
| 90 | - |
|
| 91 | - protected function setFieldWidths($labelWidths) |
|
| 92 | - { |
|
| 93 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 94 | - |
|
| 95 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 96 | - |
|
| 97 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 98 | - if ($viewport) { |
|
| 99 | - $labelWidthClass .= $viewports[$viewport].'-'.$columns.' '; |
|
| 100 | - $fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' '; |
|
| 101 | - $fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' '; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $this->labelWidth = $labelWidthClass.'columns'; |
|
| 106 | - $this->fieldWidth = $fieldWidthClass.'columns'; |
|
| 107 | - $this->fieldOffset = $fieldOffsetClass.'columns'; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - //////////////////////////////////////////////////////////////////// |
|
| 111 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 112 | - //////////////////////////////////////////////////////////////////// |
|
| 113 | - |
|
| 114 | - public function getFieldClasses(Field $field, $classes = array()) |
|
| 115 | - { |
|
| 116 | - if ($field->isButton()) { |
|
| 117 | - $classes = $this->filterButtonClasses($classes); |
|
| 118 | - } else { |
|
| 119 | - $classes = $this->filterFieldClasses($classes); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $this->addClassesToField($field, $classes); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function getGroupClasses() |
|
| 126 | - { |
|
| 127 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 128 | - return 'row'; |
|
| 129 | - } else { |
|
| 130 | - return null; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Add label classes |
|
| 136 | - * |
|
| 137 | - * @return string|null An array of attributes with the label class |
|
| 138 | - */ |
|
| 139 | - public function getLabelClasses() |
|
| 140 | - { |
|
| 141 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 142 | - return $this->getFrameworkOption('wrappedLabelClasses'); |
|
| 143 | - } else { |
|
| 144 | - return null; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function getUneditableClasses() |
|
| 149 | - { |
|
| 150 | - return null; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function getPlainTextClasses() |
|
| 154 | - { |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function getFormClasses($type) |
|
| 159 | - { |
|
| 160 | - return null; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - public function getActionClasses() |
|
| 164 | - { |
|
| 165 | - return null; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - //////////////////////////////////////////////////////////////////// |
|
| 169 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 170 | - //////////////////////////////////////////////////////////////////// |
|
| 171 | - |
|
| 172 | - public function createHelp($text, $attributes = null) |
|
| 173 | - { |
|
| 174 | - if (is_null($attributes) or empty($attributes)) { |
|
| 175 | - $attributes = $this->getFrameworkOption('error_classes'); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return Element::create('span', $text, $attributes); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Render a disabled field |
|
| 183 | - * |
|
| 184 | - * @param Field $field |
|
| 185 | - * |
|
| 186 | - * @return Input |
|
| 187 | - */ |
|
| 188 | - public function createDisabledField(Field $field) |
|
| 189 | - { |
|
| 190 | - $field->disabled(); |
|
| 191 | - |
|
| 192 | - return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Render a plain text field |
|
| 197 | - * Which fallback to a disabled field |
|
| 198 | - * |
|
| 199 | - * @param Field $field |
|
| 200 | - * |
|
| 201 | - * @return Element |
|
| 202 | - */ |
|
| 203 | - public function createPlainTextField(Field $field) |
|
| 204 | - { |
|
| 205 | - return $this->createDisabledField($field); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - //////////////////////////////////////////////////////////////////// |
|
| 209 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 210 | - //////////////////////////////////////////////////////////////////// |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Wrap an item to be prepended or appended to the current field. |
|
| 214 | - * For Zurb we return the item and handle the wrapping in prependAppend |
|
| 215 | - * as wrapping is dependent on whether we're prepending or appending. |
|
| 216 | - * |
|
| 217 | - * @return string A wrapped item |
|
| 218 | - */ |
|
| 219 | - public function placeAround($item) |
|
| 220 | - { |
|
| 221 | - return $item; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Wrap a field with prepended and appended items |
|
| 226 | - * |
|
| 227 | - * @param Field $field |
|
| 228 | - * @param array $prepend |
|
| 229 | - * @param array $append |
|
| 230 | - * |
|
| 231 | - * @return string A field concatented with prepended and/or appended items |
|
| 232 | - */ |
|
| 233 | - public function prependAppend($field, $prepend, $append) |
|
| 234 | - { |
|
| 235 | - $return = ''; |
|
| 236 | - |
|
| 237 | - foreach ($prepend as $item) { |
|
| 238 | - $return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>'; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>'; |
|
| 242 | - |
|
| 243 | - foreach ($append as $item) { |
|
| 244 | - $return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>'; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return $return; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Wraps all label contents with potential additional tags. |
|
| 252 | - * |
|
| 253 | - * @param string $label |
|
| 254 | - * |
|
| 255 | - * @return string A wrapped label |
|
| 256 | - */ |
|
| 257 | - public function wrapLabel($label) |
|
| 258 | - { |
|
| 259 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 260 | - return Element::create('div', $label)->addClass($this->labelWidth); |
|
| 261 | - } else { |
|
| 262 | - return $label; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Wraps all field contents with potential additional tags. |
|
| 268 | - * |
|
| 269 | - * @param Field $field |
|
| 270 | - * |
|
| 271 | - * @return Element A wrapped field |
|
| 272 | - */ |
|
| 273 | - public function wrapField($field) |
|
| 274 | - { |
|
| 275 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 276 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 277 | - } else { |
|
| 278 | - return $field; |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Wrap actions block with potential additional tags |
|
| 284 | - * |
|
| 285 | - * @param Actions $actions |
|
| 286 | - * |
|
| 287 | - * @return string A wrapped actions block |
|
| 288 | - */ |
|
| 289 | - public function wrapActions($actions) |
|
| 290 | - { |
|
| 291 | - return $actions; |
|
| 292 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'tiny', |
|
| 30 | + 'small', |
|
| 31 | + 'medium', |
|
| 32 | + 'large', |
|
| 33 | + 'success', |
|
| 34 | + 'radius', |
|
| 35 | + 'round', |
|
| 36 | + 'disabled', |
|
| 37 | + 'prefix', |
|
| 38 | + 'postfix', |
|
| 39 | + ); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The field sizes available |
|
| 43 | + * Zurb Foundation 4 does not apply sizes to the form element, but to the wrapper div |
|
| 44 | + * |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + private $fields = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The field states available |
|
| 51 | + * |
|
| 52 | + * @var array |
|
| 53 | + */ |
|
| 54 | + protected $states = array( |
|
| 55 | + 'error', |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Create a new ZurbFoundation instance |
|
| 60 | + * |
|
| 61 | + * @param \Illuminate\Container\Container $app |
|
| 62 | + */ |
|
| 63 | + public function __construct(Container $app) |
|
| 64 | + { |
|
| 65 | + $this->app = $app; |
|
| 66 | + $this->setFrameworkDefaults(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + //////////////////////////////////////////////////////////////////// |
|
| 70 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 71 | + //////////////////////////////////////////////////////////////////// |
|
| 72 | + |
|
| 73 | + public function filterButtonClasses($classes) |
|
| 74 | + { |
|
| 75 | + // Filter classes |
|
| 76 | + $classes = array_intersect($classes, $this->buttons); |
|
| 77 | + $classes[] = 'button'; |
|
| 78 | + |
|
| 79 | + return $classes; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function filterFieldClasses($classes) |
|
| 83 | + { |
|
| 84 | + return null; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + //////////////////////////////////////////////////////////////////// |
|
| 88 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 89 | + //////////////////////////////////////////////////////////////////// |
|
| 90 | + |
|
| 91 | + protected function setFieldWidths($labelWidths) |
|
| 92 | + { |
|
| 93 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 94 | + |
|
| 95 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 96 | + |
|
| 97 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 98 | + if ($viewport) { |
|
| 99 | + $labelWidthClass .= $viewports[$viewport].'-'.$columns.' '; |
|
| 100 | + $fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' '; |
|
| 101 | + $fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' '; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $this->labelWidth = $labelWidthClass.'columns'; |
|
| 106 | + $this->fieldWidth = $fieldWidthClass.'columns'; |
|
| 107 | + $this->fieldOffset = $fieldOffsetClass.'columns'; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + //////////////////////////////////////////////////////////////////// |
|
| 111 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 112 | + //////////////////////////////////////////////////////////////////// |
|
| 113 | + |
|
| 114 | + public function getFieldClasses(Field $field, $classes = array()) |
|
| 115 | + { |
|
| 116 | + if ($field->isButton()) { |
|
| 117 | + $classes = $this->filterButtonClasses($classes); |
|
| 118 | + } else { |
|
| 119 | + $classes = $this->filterFieldClasses($classes); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $this->addClassesToField($field, $classes); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function getGroupClasses() |
|
| 126 | + { |
|
| 127 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 128 | + return 'row'; |
|
| 129 | + } else { |
|
| 130 | + return null; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Add label classes |
|
| 136 | + * |
|
| 137 | + * @return string|null An array of attributes with the label class |
|
| 138 | + */ |
|
| 139 | + public function getLabelClasses() |
|
| 140 | + { |
|
| 141 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 142 | + return $this->getFrameworkOption('wrappedLabelClasses'); |
|
| 143 | + } else { |
|
| 144 | + return null; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function getUneditableClasses() |
|
| 149 | + { |
|
| 150 | + return null; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function getPlainTextClasses() |
|
| 154 | + { |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function getFormClasses($type) |
|
| 159 | + { |
|
| 160 | + return null; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + public function getActionClasses() |
|
| 164 | + { |
|
| 165 | + return null; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + //////////////////////////////////////////////////////////////////// |
|
| 169 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 170 | + //////////////////////////////////////////////////////////////////// |
|
| 171 | + |
|
| 172 | + public function createHelp($text, $attributes = null) |
|
| 173 | + { |
|
| 174 | + if (is_null($attributes) or empty($attributes)) { |
|
| 175 | + $attributes = $this->getFrameworkOption('error_classes'); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return Element::create('span', $text, $attributes); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Render a disabled field |
|
| 183 | + * |
|
| 184 | + * @param Field $field |
|
| 185 | + * |
|
| 186 | + * @return Input |
|
| 187 | + */ |
|
| 188 | + public function createDisabledField(Field $field) |
|
| 189 | + { |
|
| 190 | + $field->disabled(); |
|
| 191 | + |
|
| 192 | + return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Render a plain text field |
|
| 197 | + * Which fallback to a disabled field |
|
| 198 | + * |
|
| 199 | + * @param Field $field |
|
| 200 | + * |
|
| 201 | + * @return Element |
|
| 202 | + */ |
|
| 203 | + public function createPlainTextField(Field $field) |
|
| 204 | + { |
|
| 205 | + return $this->createDisabledField($field); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + //////////////////////////////////////////////////////////////////// |
|
| 209 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 210 | + //////////////////////////////////////////////////////////////////// |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Wrap an item to be prepended or appended to the current field. |
|
| 214 | + * For Zurb we return the item and handle the wrapping in prependAppend |
|
| 215 | + * as wrapping is dependent on whether we're prepending or appending. |
|
| 216 | + * |
|
| 217 | + * @return string A wrapped item |
|
| 218 | + */ |
|
| 219 | + public function placeAround($item) |
|
| 220 | + { |
|
| 221 | + return $item; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Wrap a field with prepended and appended items |
|
| 226 | + * |
|
| 227 | + * @param Field $field |
|
| 228 | + * @param array $prepend |
|
| 229 | + * @param array $append |
|
| 230 | + * |
|
| 231 | + * @return string A field concatented with prepended and/or appended items |
|
| 232 | + */ |
|
| 233 | + public function prependAppend($field, $prepend, $append) |
|
| 234 | + { |
|
| 235 | + $return = ''; |
|
| 236 | + |
|
| 237 | + foreach ($prepend as $item) { |
|
| 238 | + $return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>'; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>'; |
|
| 242 | + |
|
| 243 | + foreach ($append as $item) { |
|
| 244 | + $return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>'; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return $return; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Wraps all label contents with potential additional tags. |
|
| 252 | + * |
|
| 253 | + * @param string $label |
|
| 254 | + * |
|
| 255 | + * @return string A wrapped label |
|
| 256 | + */ |
|
| 257 | + public function wrapLabel($label) |
|
| 258 | + { |
|
| 259 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 260 | + return Element::create('div', $label)->addClass($this->labelWidth); |
|
| 261 | + } else { |
|
| 262 | + return $label; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Wraps all field contents with potential additional tags. |
|
| 268 | + * |
|
| 269 | + * @param Field $field |
|
| 270 | + * |
|
| 271 | + * @return Element A wrapped field |
|
| 272 | + */ |
|
| 273 | + public function wrapField($field) |
|
| 274 | + { |
|
| 275 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 276 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 277 | + } else { |
|
| 278 | + return $field; |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Wrap actions block with potential additional tags |
|
| 284 | + * |
|
| 285 | + * @param Actions $actions |
|
| 286 | + * |
|
| 287 | + * @return string A wrapped actions block |
|
| 288 | + */ |
|
| 289 | + public function wrapActions($actions) |
|
| 290 | + { |
|
| 291 | + return $actions; |
|
| 292 | + } |
|
| 293 | 293 | } |
@@ -126,7 +126,7 @@ |
||
| 126 | 126 | * |
| 127 | 127 | * @param Field $field |
| 128 | 128 | * |
| 129 | - * @return Element |
|
| 129 | + * @return Input |
|
| 130 | 130 | */ |
| 131 | 131 | public function createPlainTextField(Field $field) |
| 132 | 132 | { |
@@ -13,283 +13,283 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ZurbFoundation5 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'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'tiny', |
|
| 30 | - 'small', |
|
| 31 | - 'medium', |
|
| 32 | - 'large', |
|
| 33 | - 'success', |
|
| 34 | - 'radius', |
|
| 35 | - 'round', |
|
| 36 | - 'disabled', |
|
| 37 | - 'prefix', |
|
| 38 | - 'postfix', |
|
| 39 | - ); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The field sizes available |
|
| 43 | - * Zurb Foundation 5 does not apply sizes to the form element, but to the wrapper div |
|
| 44 | - * |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - private $fields = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The field states available |
|
| 51 | - * |
|
| 52 | - * @var array |
|
| 53 | - */ |
|
| 54 | - protected $states = array( |
|
| 55 | - 'error', |
|
| 56 | - ); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Create a new ZurbFoundation instance |
|
| 60 | - * |
|
| 61 | - * @param \Illuminate\Container\Container $app |
|
| 62 | - */ |
|
| 63 | - public function __construct(Container $app) |
|
| 64 | - { |
|
| 65 | - $this->app = $app; |
|
| 66 | - $this->setFrameworkDefaults(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - //////////////////////////////////////////////////////////////////// |
|
| 70 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 71 | - //////////////////////////////////////////////////////////////////// |
|
| 72 | - |
|
| 73 | - public function filterButtonClasses($classes) |
|
| 74 | - { |
|
| 75 | - // Filter classes |
|
| 76 | - $classes = array_intersect($classes, $this->buttons); |
|
| 77 | - $classes[] = 'button'; |
|
| 78 | - |
|
| 79 | - return $classes; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function filterFieldClasses($classes) |
|
| 83 | - { |
|
| 84 | - return null; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - //////////////////////////////////////////////////////////////////// |
|
| 88 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 89 | - //////////////////////////////////////////////////////////////////// |
|
| 90 | - |
|
| 91 | - protected function setFieldWidths($labelWidths) |
|
| 92 | - { |
|
| 93 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 94 | - |
|
| 95 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 96 | - |
|
| 97 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 98 | - if ($viewport) { |
|
| 99 | - $labelWidthClass .= $viewports[$viewport].'-'.$columns.' '; |
|
| 100 | - $fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' '; |
|
| 101 | - $fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' '; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $this->labelWidth = $labelWidthClass.'columns'; |
|
| 106 | - $this->fieldWidth = $fieldWidthClass.'columns'; |
|
| 107 | - $this->fieldOffset = $fieldOffsetClass.'columns'; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - //////////////////////////////////////////////////////////////////// |
|
| 111 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 112 | - //////////////////////////////////////////////////////////////////// |
|
| 113 | - |
|
| 114 | - public function getFieldClasses(Field $field, $classes = array()) |
|
| 115 | - { |
|
| 116 | - if ($field->isButton()) { |
|
| 117 | - $classes = $this->filterButtonClasses($classes); |
|
| 118 | - } else { |
|
| 119 | - $classes = $this->filterFieldClasses($classes); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $this->addClassesToField($field, $classes); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function getGroupClasses() |
|
| 126 | - { |
|
| 127 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 128 | - return 'row'; |
|
| 129 | - } else { |
|
| 130 | - return null; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Add label classes |
|
| 136 | - * |
|
| 137 | - * @return string|null An array of attributes with the label class |
|
| 138 | - */ |
|
| 139 | - public function getLabelClasses() |
|
| 140 | - { |
|
| 141 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 142 | - return $this->getFrameworkOption('wrappedLabelClasses'); |
|
| 143 | - } else { |
|
| 144 | - return null; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - public function getUneditableClasses() |
|
| 149 | - { |
|
| 150 | - return null; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function getPlainTextClasses() |
|
| 154 | - { |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function getFormClasses($type) |
|
| 159 | - { |
|
| 160 | - return null; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - public function getActionClasses() |
|
| 164 | - { |
|
| 165 | - return null; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - //////////////////////////////////////////////////////////////////// |
|
| 169 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 170 | - //////////////////////////////////////////////////////////////////// |
|
| 171 | - |
|
| 172 | - public function createHelp($text, $attributes = null) |
|
| 173 | - { |
|
| 174 | - if (is_null($attributes) or empty($attributes)) { |
|
| 175 | - $attributes = $this->getFrameworkOption('error_classes'); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return Element::create('small', $text, $attributes); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Render a disabled field |
|
| 183 | - * |
|
| 184 | - * @param Field $field |
|
| 185 | - * |
|
| 186 | - * @return Input |
|
| 187 | - */ |
|
| 188 | - public function createDisabledField(Field $field) |
|
| 189 | - { |
|
| 190 | - $field->disabled(); |
|
| 191 | - |
|
| 192 | - return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Render a plain text field |
|
| 197 | - * Which fallback to a disabled field |
|
| 198 | - * |
|
| 199 | - * @param Field $field |
|
| 200 | - * |
|
| 201 | - * @return Element |
|
| 202 | - */ |
|
| 203 | - public function createPlainTextField(Field $field) |
|
| 204 | - { |
|
| 205 | - return $this->createDisabledField($field); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - //////////////////////////////////////////////////////////////////// |
|
| 209 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 210 | - //////////////////////////////////////////////////////////////////// |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Wrap an item to be prepended or appended to the current field. |
|
| 214 | - * For Zurb we return the item and handle the wrapping in prependAppend |
|
| 215 | - * as wrapping is dependent on whether we're prepending or appending. |
|
| 216 | - * |
|
| 217 | - * @return string A wrapped item |
|
| 218 | - */ |
|
| 219 | - public function placeAround($item) |
|
| 220 | - { |
|
| 221 | - return $item; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Wrap a field with prepended and appended items |
|
| 226 | - * |
|
| 227 | - * @param Field $field |
|
| 228 | - * @param array $prepend |
|
| 229 | - * @param array $append |
|
| 230 | - * |
|
| 231 | - * @return string A field concatented with prepended and/or appended items |
|
| 232 | - */ |
|
| 233 | - public function prependAppend($field, $prepend, $append) |
|
| 234 | - { |
|
| 235 | - $return = '<div class="row collapse">'; |
|
| 236 | - |
|
| 237 | - foreach ($prepend as $item) { |
|
| 238 | - $return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>'; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>'; |
|
| 242 | - |
|
| 243 | - foreach ($append as $item) { |
|
| 244 | - $return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>'; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $return .= '</div>'; |
|
| 248 | - |
|
| 249 | - return $return; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Wraps all label contents with potential additional tags. |
|
| 254 | - * |
|
| 255 | - * @param string $label |
|
| 256 | - * |
|
| 257 | - * @return string A wrapped label |
|
| 258 | - */ |
|
| 259 | - public function wrapLabel($label) |
|
| 260 | - { |
|
| 261 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 262 | - return Element::create('div', $label)->addClass($this->labelWidth); |
|
| 263 | - } else { |
|
| 264 | - return $label; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Wraps all field contents with potential additional tags. |
|
| 270 | - * |
|
| 271 | - * @param Field $field |
|
| 272 | - * |
|
| 273 | - * @return Element A wrapped field |
|
| 274 | - */ |
|
| 275 | - public function wrapField($field) |
|
| 276 | - { |
|
| 277 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 279 | - } else { |
|
| 280 | - return $field; |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Wrap actions block with potential additional tags |
|
| 286 | - * |
|
| 287 | - * @param Actions $actions |
|
| 288 | - * |
|
| 289 | - * @return string A wrapped actions block |
|
| 290 | - */ |
|
| 291 | - public function wrapActions($actions) |
|
| 292 | - { |
|
| 293 | - return $actions; |
|
| 294 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'tiny', |
|
| 30 | + 'small', |
|
| 31 | + 'medium', |
|
| 32 | + 'large', |
|
| 33 | + 'success', |
|
| 34 | + 'radius', |
|
| 35 | + 'round', |
|
| 36 | + 'disabled', |
|
| 37 | + 'prefix', |
|
| 38 | + 'postfix', |
|
| 39 | + ); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The field sizes available |
|
| 43 | + * Zurb Foundation 5 does not apply sizes to the form element, but to the wrapper div |
|
| 44 | + * |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + private $fields = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The field states available |
|
| 51 | + * |
|
| 52 | + * @var array |
|
| 53 | + */ |
|
| 54 | + protected $states = array( |
|
| 55 | + 'error', |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Create a new ZurbFoundation instance |
|
| 60 | + * |
|
| 61 | + * @param \Illuminate\Container\Container $app |
|
| 62 | + */ |
|
| 63 | + public function __construct(Container $app) |
|
| 64 | + { |
|
| 65 | + $this->app = $app; |
|
| 66 | + $this->setFrameworkDefaults(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + //////////////////////////////////////////////////////////////////// |
|
| 70 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 71 | + //////////////////////////////////////////////////////////////////// |
|
| 72 | + |
|
| 73 | + public function filterButtonClasses($classes) |
|
| 74 | + { |
|
| 75 | + // Filter classes |
|
| 76 | + $classes = array_intersect($classes, $this->buttons); |
|
| 77 | + $classes[] = 'button'; |
|
| 78 | + |
|
| 79 | + return $classes; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function filterFieldClasses($classes) |
|
| 83 | + { |
|
| 84 | + return null; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + //////////////////////////////////////////////////////////////////// |
|
| 88 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 89 | + //////////////////////////////////////////////////////////////////// |
|
| 90 | + |
|
| 91 | + protected function setFieldWidths($labelWidths) |
|
| 92 | + { |
|
| 93 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 94 | + |
|
| 95 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 96 | + |
|
| 97 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 98 | + if ($viewport) { |
|
| 99 | + $labelWidthClass .= $viewports[$viewport].'-'.$columns.' '; |
|
| 100 | + $fieldWidthClass .= $viewports[$viewport].'-'.(12 - $columns).' '; |
|
| 101 | + $fieldOffsetClass .= $viewports[$viewport].'-offset-'.$columns.' '; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $this->labelWidth = $labelWidthClass.'columns'; |
|
| 106 | + $this->fieldWidth = $fieldWidthClass.'columns'; |
|
| 107 | + $this->fieldOffset = $fieldOffsetClass.'columns'; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + //////////////////////////////////////////////////////////////////// |
|
| 111 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 112 | + //////////////////////////////////////////////////////////////////// |
|
| 113 | + |
|
| 114 | + public function getFieldClasses(Field $field, $classes = array()) |
|
| 115 | + { |
|
| 116 | + if ($field->isButton()) { |
|
| 117 | + $classes = $this->filterButtonClasses($classes); |
|
| 118 | + } else { |
|
| 119 | + $classes = $this->filterFieldClasses($classes); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $this->addClassesToField($field, $classes); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function getGroupClasses() |
|
| 126 | + { |
|
| 127 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 128 | + return 'row'; |
|
| 129 | + } else { |
|
| 130 | + return null; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Add label classes |
|
| 136 | + * |
|
| 137 | + * @return string|null An array of attributes with the label class |
|
| 138 | + */ |
|
| 139 | + public function getLabelClasses() |
|
| 140 | + { |
|
| 141 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 142 | + return $this->getFrameworkOption('wrappedLabelClasses'); |
|
| 143 | + } else { |
|
| 144 | + return null; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + public function getUneditableClasses() |
|
| 149 | + { |
|
| 150 | + return null; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function getPlainTextClasses() |
|
| 154 | + { |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function getFormClasses($type) |
|
| 159 | + { |
|
| 160 | + return null; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + public function getActionClasses() |
|
| 164 | + { |
|
| 165 | + return null; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + //////////////////////////////////////////////////////////////////// |
|
| 169 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 170 | + //////////////////////////////////////////////////////////////////// |
|
| 171 | + |
|
| 172 | + public function createHelp($text, $attributes = null) |
|
| 173 | + { |
|
| 174 | + if (is_null($attributes) or empty($attributes)) { |
|
| 175 | + $attributes = $this->getFrameworkOption('error_classes'); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return Element::create('small', $text, $attributes); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Render a disabled field |
|
| 183 | + * |
|
| 184 | + * @param Field $field |
|
| 185 | + * |
|
| 186 | + * @return Input |
|
| 187 | + */ |
|
| 188 | + public function createDisabledField(Field $field) |
|
| 189 | + { |
|
| 190 | + $field->disabled(); |
|
| 191 | + |
|
| 192 | + return Input::create('text', $field->getName(), $field->getValue(), $field->getAttributes()); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Render a plain text field |
|
| 197 | + * Which fallback to a disabled field |
|
| 198 | + * |
|
| 199 | + * @param Field $field |
|
| 200 | + * |
|
| 201 | + * @return Element |
|
| 202 | + */ |
|
| 203 | + public function createPlainTextField(Field $field) |
|
| 204 | + { |
|
| 205 | + return $this->createDisabledField($field); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + //////////////////////////////////////////////////////////////////// |
|
| 209 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 210 | + //////////////////////////////////////////////////////////////////// |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Wrap an item to be prepended or appended to the current field. |
|
| 214 | + * For Zurb we return the item and handle the wrapping in prependAppend |
|
| 215 | + * as wrapping is dependent on whether we're prepending or appending. |
|
| 216 | + * |
|
| 217 | + * @return string A wrapped item |
|
| 218 | + */ |
|
| 219 | + public function placeAround($item) |
|
| 220 | + { |
|
| 221 | + return $item; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Wrap a field with prepended and appended items |
|
| 226 | + * |
|
| 227 | + * @param Field $field |
|
| 228 | + * @param array $prepend |
|
| 229 | + * @param array $append |
|
| 230 | + * |
|
| 231 | + * @return string A field concatented with prepended and/or appended items |
|
| 232 | + */ |
|
| 233 | + public function prependAppend($field, $prepend, $append) |
|
| 234 | + { |
|
| 235 | + $return = '<div class="row collapse">'; |
|
| 236 | + |
|
| 237 | + foreach ($prepend as $item) { |
|
| 238 | + $return .= '<div class="large-2 small-3 columns"><span class="prefix">'.$item.'</span></div>'; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $return .= '<div class="large-10 small-9 columns">'.$field->render().'</div>'; |
|
| 242 | + |
|
| 243 | + foreach ($append as $item) { |
|
| 244 | + $return .= '<div class="large-2 small-3 columns"><span class="postfix">'.$item.'</span></div>'; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $return .= '</div>'; |
|
| 248 | + |
|
| 249 | + return $return; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Wraps all label contents with potential additional tags. |
|
| 254 | + * |
|
| 255 | + * @param string $label |
|
| 256 | + * |
|
| 257 | + * @return string A wrapped label |
|
| 258 | + */ |
|
| 259 | + public function wrapLabel($label) |
|
| 260 | + { |
|
| 261 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 262 | + return Element::create('div', $label)->addClass($this->labelWidth); |
|
| 263 | + } else { |
|
| 264 | + return $label; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Wraps all field contents with potential additional tags. |
|
| 270 | + * |
|
| 271 | + * @param Field $field |
|
| 272 | + * |
|
| 273 | + * @return Element A wrapped field |
|
| 274 | + */ |
|
| 275 | + public function wrapField($field) |
|
| 276 | + { |
|
| 277 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 279 | + } else { |
|
| 280 | + return $field; |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Wrap actions block with potential additional tags |
|
| 286 | + * |
|
| 287 | + * @param Actions $actions |
|
| 288 | + * |
|
| 289 | + * @return string A wrapped actions block |
|
| 290 | + */ |
|
| 291 | + public function wrapActions($actions) |
|
| 292 | + { |
|
| 293 | + return $actions; |
|
| 294 | + } |
|
| 295 | 295 | } |