1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Damian Dlugosz <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Braincrafted\Bundle\BootstrapBundle\Twig; |
8
|
|
|
|
9
|
|
|
use Twig_Extension; |
10
|
|
|
use Twig_SimpleFunction; |
11
|
|
|
|
12
|
|
|
class BootstrapButtonExtension extends Twig_Extension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var BootstrapIconExtension |
16
|
|
|
*/ |
17
|
|
|
private $iconExtension; |
18
|
|
|
|
19
|
|
|
private $defaults = array( |
20
|
|
|
'label' => '', |
21
|
|
|
'icon' => false, |
22
|
|
|
'type' => 'default', |
23
|
|
|
'size' => 'md', |
24
|
|
|
'attr' => array(), |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param BootstrapIconExtension $iconExtension |
29
|
|
|
*/ |
30
|
|
|
public function __construct(BootstrapIconExtension $iconExtension) |
31
|
|
|
{ |
32
|
|
|
$this->iconExtension = $iconExtension; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function getFunctions() |
39
|
|
|
{ |
40
|
|
|
return array( |
41
|
|
|
new Twig_SimpleFunction('button', array($this, 'buttonFunction'), array('is_safe' => array('html'))), |
42
|
|
|
new Twig_SimpleFunction('button_link', array($this, 'buttonLinkFunction'), array('is_safe' => array('html'))), |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $options |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function buttonFunction(array $options = array()) |
51
|
|
|
{ |
52
|
|
|
$options = array_merge($this->defaults, $options); |
53
|
|
|
|
54
|
|
|
$options['attr']['class'] = "btn btn-{$options['type']} btn-{$options['size']}" . (isset($options['attr']['class']) ? ' '.$options['attr']['class'] : ''); |
55
|
|
|
$options['attr']['type'] = isset($options['submit']) && $options['submit'] ? 'submit' : 'button'; |
56
|
|
|
|
57
|
|
|
$icon = $options['icon'] ? $this->iconExtension->iconFunction($options['icon']).' ' : ''; |
58
|
|
|
$attr = $options['attr'] ? $this->attributes($options['attr']) : ''; |
59
|
|
|
|
60
|
|
|
$button = "<button{$attr}>{$icon}{$options['label']}</button>"; |
61
|
|
|
|
62
|
|
|
return $button; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $options |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function buttonLinkFunction(array $options = array()) |
70
|
|
|
{ |
71
|
|
|
$options = array_merge($this->defaults, $options); |
72
|
|
|
|
73
|
|
|
$options['attr']['class'] = "btn btn-{$options['type']} btn-{$options['size']}" . (isset($options['attr']['class']) ? ' '.$options['attr']['class'] : ''); |
74
|
|
|
$options['attr']['href'] = (isset($options['url']) ? $options['url'] : '#'); |
75
|
|
|
|
76
|
|
|
$icon = $options['icon'] ? $this->iconExtension->iconFunction($options['icon']).' ' : ''; |
77
|
|
|
$attr = $options['attr'] ? $this->attributes($options['attr']) : ''; |
78
|
|
|
|
79
|
|
|
$button = "<a{$attr}>{$icon}{$options['label']}</a>"; |
80
|
|
|
|
81
|
|
|
return $button; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function attributes(array $attributes) |
85
|
|
|
{ |
86
|
|
|
$result = ''; |
87
|
|
|
array_walk($attributes, function($value, $attr) use (&$result) { |
|
|
|
|
88
|
|
|
$result .= " $attr=\"$value\""; |
89
|
|
|
}); |
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
public function getName() |
97
|
|
|
{ |
98
|
|
|
return 'braincrafted_bootstrap_button'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|