|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @company MTE Telecom, Ltd. |
|
4
|
|
|
* @author Ushkov Nikolai <[email protected]> |
|
5
|
|
|
* Date: 19.04.16 |
|
6
|
|
|
* Time: 14:45 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Nnx\DataGrid\View\Helper\JqGrid; |
|
10
|
|
|
|
|
11
|
|
|
use Zend\View\Helper\AbstractHelper; |
|
12
|
|
|
use Nnx\DataGrid\Button\ButtonInterface; |
|
13
|
|
|
use Zend\View\Helper\EscapeHtml; |
|
14
|
|
|
use Zend\View\Renderer\PhpRenderer; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class GridButton |
|
18
|
|
|
* @package Nnx\DataGrid\View\Helper\JqGrid |
|
19
|
|
|
*/ |
|
20
|
|
|
class GridButton extends AbstractHelper |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $variables = []; |
|
26
|
|
|
|
|
27
|
|
|
public function __invoke(ButtonInterface $button, $variables) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->variables = $variables; |
|
30
|
|
|
/** @var PhpRenderer $view */ |
|
31
|
|
|
$view = $this->getView(); |
|
32
|
|
|
/** @var EscapeHtml $escape */ |
|
33
|
|
|
$escape = $view->plugin('escapeHtml'); |
|
34
|
|
|
|
|
35
|
|
|
$attributeString = $this->getAttributesString($button->getAttributes()); |
|
36
|
|
|
$url = $button->getUrl(); |
|
|
|
|
|
|
37
|
|
|
if (is_array($url)) { |
|
38
|
|
|
$routeName = ''; |
|
|
|
|
|
|
39
|
|
|
$routeParams = []; |
|
|
|
|
|
|
40
|
|
|
$routeOptions = []; |
|
41
|
|
|
if (array_key_exists('routeName', $url)) { |
|
42
|
|
|
$routeName = $url['routeName']; |
|
43
|
|
|
} |
|
44
|
|
|
if (array_key_exists('routeParams', $url)) { |
|
45
|
|
|
$routeParams = $url['routeParams']; |
|
46
|
|
|
} |
|
47
|
|
|
if (array_key_exists('routeOptions', $url)) { |
|
48
|
|
|
$routeOptions = $url['routeOptions']; |
|
49
|
|
|
} |
|
50
|
|
|
$url = $view->url($routeName, $routeParams, $routeOptions); |
|
51
|
|
|
} |
|
52
|
|
|
if ($button->getLibJs()) { |
|
53
|
|
|
$view->mteAsseticAppend([ |
|
54
|
|
|
'headScript' => $button->getLibJs() |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
$url = $this->getUrl($url); |
|
|
|
|
|
|
58
|
|
|
$title = $escape($button->getTitle()); |
|
59
|
|
|
$html = "<a href='$url' $attributeString>$title</a> "; |
|
|
|
|
|
|
60
|
|
|
$js = $button->getJs(); |
|
|
|
|
|
|
61
|
|
|
return ['html' => $html,'js' => $js]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $attributes |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getAttributesString($attributes) |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var PhpRenderer $view */ |
|
71
|
|
|
$view = $this->getView(); |
|
72
|
|
|
/** @var EscapeHtml $escape */ |
|
73
|
|
|
$escape = $view->plugin('escapeHtml'); |
|
74
|
|
|
$res = ''; |
|
|
|
|
|
|
75
|
|
|
foreach ($attributes as $name=>$value) { |
|
76
|
|
|
$res .= $escape($name) . '="'.$escape($value).'" '; |
|
77
|
|
|
} |
|
78
|
|
|
return $res; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $urlTemplate |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getUrl($urlTemplate) |
|
86
|
|
|
{ |
|
87
|
|
|
return preg_replace_callback('/:([a-zA-Z_]+)/', [$this, 'replaceCallback'], $urlTemplate); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $matches |
|
92
|
|
|
* @return mixed|string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function replaceCallback($matches) |
|
95
|
|
|
{ |
|
96
|
|
|
$varName = $matches[1]; |
|
97
|
|
|
$value = ''; |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if (array_key_exists($varName, $this->variables)) { |
|
100
|
|
|
if ($varName !== 'backurl') { |
|
101
|
|
|
$value = urlencode($this->variables[$varName]); |
|
102
|
|
|
} else { |
|
103
|
|
|
$value = $this->variables[$varName]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $value; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
|
|
|
|
|
110
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.