1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Form\Former\Fields; |
13
|
|
|
|
14
|
|
|
use Former\Form\Fields\Checkbox; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* CheckboxButton is a Former field class for converting checkboxes to buttons with custom colors for each. |
18
|
|
|
* |
19
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class CheckboxButton extends Checkbox |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Render radio buttons. |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
3 |
|
public function render() |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
3 |
|
return '<div class="btn-toolbar checkbox-btn" data-toggle="buttons">' . parent::render() . '</div>'; |
32
|
|
|
} catch (\Exception $e) { |
33
|
|
|
echo $e; |
34
|
|
|
die; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Render checkable radio button. |
40
|
|
|
* |
41
|
|
|
* @param array|string $item |
42
|
|
|
* @param int $fallbackValue |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
3 |
|
protected function createCheckable($item, $fallbackValue = 1) |
47
|
|
|
{ |
48
|
|
|
// Make sure the parent class will create inline radios |
49
|
3 |
|
$this->inline = true; |
50
|
3 |
|
$this->grouped(); |
51
|
|
|
// $this= |
52
|
|
|
// Extract the color for this button/radio & unset it to prevent creating attribute |
53
|
3 |
|
$color = $item['attributes']['color']; |
54
|
3 |
|
unset($item['attributes']['color']); |
55
|
|
|
|
56
|
|
|
// Color for selected button/radio |
57
|
3 |
|
$selectedColor = ';color:' . $color . ';'; |
58
|
|
|
|
59
|
|
|
// Data attribute for the button color |
60
|
3 |
|
$item['attributes']['data-color'] = $color; |
61
|
|
|
|
62
|
|
|
// Generate the radio button |
63
|
3 |
|
$item = parent::createCheckable($item, $fallbackValue); |
64
|
|
|
|
65
|
|
|
// If property checked found, then make the button selected with styles |
66
|
3 |
|
if (strpos($item, 'checked') !== false) { |
67
|
|
|
$selectedColor = ';background:' . $color . ';color:white;'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Add bootstrap classes for styling the buttons |
71
|
3 |
|
$item = str_replace('inline', 'btn btn-default', $item); |
72
|
|
|
|
73
|
|
|
// Add styles to the wrapper label tag |
74
|
3 |
|
$style = 'border-color: ' . $color . $selectedColor; |
75
|
3 |
|
$item = str_replace('<label', '<label style="' . $style . '"', $item); |
76
|
|
|
|
77
|
3 |
|
return $item; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.