1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devgroup\JsTreeWidget\helpers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\Json; |
7
|
|
|
use yii\helpers\Url; |
8
|
|
|
use yii\web\JsExpression; |
9
|
|
|
|
10
|
|
|
class ContextMenuHelper |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns JavaScript expression(\yii\web\JsExpression) for context menu item. |
15
|
|
|
* Used for redirecting browser to node-specific action(ie. edit, delete). |
16
|
|
|
* Automatically appends needed $.data() attributes(can be specified in HTML5 data attribute format too). |
17
|
|
|
* |
18
|
|
|
* @param array $route Action route |
19
|
|
|
* @param bool|array $dataAttributes Array of attributes to append, true to append all attributes, false if you don't want to append any data-attributes. |
20
|
|
|
* @return JsExpression |
21
|
|
|
*/ |
22
|
|
|
public static function actionUrl(array $route, $dataAttributes = true) |
23
|
|
|
{ |
24
|
|
|
$baseUrl = Json::encode(Url::to($route)); |
25
|
|
|
$union = strpos($baseUrl, '?') > 0 ? '&' : '?'; |
26
|
|
|
|
27
|
|
|
$dataExpression = "var data = \$object.data(), dataVariables = [];"; |
28
|
|
|
|
29
|
|
|
if (is_array($dataAttributes) === true) { |
30
|
|
|
// only selected set of attributes |
31
|
|
|
foreach ($dataAttributes as $attribute => $match) { |
32
|
|
|
if (is_numeric($attribute) === true) { |
33
|
|
|
$attribute = $match; |
34
|
|
|
} |
35
|
|
|
$jsonAttribute = Json::encode($attribute); |
|
|
|
|
36
|
|
|
$matchAttribute = Json::encode($match); |
37
|
|
|
|
38
|
|
|
$dataExpression .= " |
39
|
|
|
if (typeof(data[$matchAttribute]) !== 'undefined') { |
40
|
|
|
dataVariables.push( '$attribute=' + encodeURIComponent(data[$matchAttribute]) ); |
41
|
|
|
}\n"; |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
} elseif ($dataAttributes === true) { |
45
|
|
|
// all attributes |
46
|
|
|
$dataExpression .= " |
47
|
|
|
for (var attributeName in data) { |
48
|
|
|
dataVariables.push(encodeURIComponent(attributeName) + '=' + encodeURIComponent(data[attributeName])); |
49
|
|
|
}\n"; |
50
|
|
|
} else { |
51
|
|
|
$dataExpression = "var dataVariables = '';"; |
52
|
|
|
} |
53
|
|
|
$dataExpression .= "dataVariables=dataVariables.join('&'); "; |
54
|
|
|
return new JsExpression(" |
55
|
|
|
function(node) { |
56
|
|
|
var \$object = node.reference ? \$(node.reference[0]) : node; |
57
|
|
|
$dataExpression |
58
|
|
|
document.location = $baseUrl + '$union' + dataVariables; |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.