|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hipanel\widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Widget; |
|
7
|
|
|
use yii\helpers\Html; |
|
8
|
|
|
|
|
9
|
|
|
class BackButton extends Widget |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Html options |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
public $options = [ |
|
16
|
|
|
'class' => 'btn btn-default', |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
public $selectorClass = 'btn-cancel'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $label; |
|
28
|
|
|
|
|
29
|
|
|
public function run() |
|
30
|
|
|
{ |
|
31
|
|
|
$curr = $this->getCurrentUrl(); |
|
32
|
|
|
$ref = $this->getReferrerUrl(); |
|
33
|
|
|
if ($curr === $ref) { |
|
34
|
|
|
Yii::$app->session[$this->getIdentifier()] = Yii::$app->session[$this->getIdentifier()] + 1; |
|
35
|
|
|
} else { |
|
36
|
|
|
Yii::$app->session[$this->getIdentifier()] = 1; |
|
37
|
|
|
} |
|
38
|
|
|
$this->registerClientScript(); |
|
39
|
|
|
Html::addCssClass($this->options, $this->selectorClass); |
|
40
|
|
|
|
|
41
|
|
|
return Html::button($this->getLabel(), $this->options); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getLabel() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->label !== null ? $this->label : Yii::t('hipanel', 'Cancel'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function registerClientScript() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->getView()->registerJs(";$('.{$this->selectorClass}').on('click', function () {window.history.go({$this->getStep()});});"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getStep() |
|
61
|
|
|
{ |
|
62
|
|
|
return -Yii::$app->session[$this->getIdentifier()]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getIdentifier() |
|
66
|
|
|
{ |
|
67
|
|
|
return self::class; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function getCurrentUrl() |
|
71
|
|
|
{ |
|
72
|
|
|
return Yii::$app->request->absoluteUrl; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function getReferrerUrl() |
|
76
|
|
|
{ |
|
77
|
|
|
$out = null; |
|
|
|
|
|
|
78
|
|
|
$referrer = Yii::$app->request->referrer; |
|
79
|
|
|
$position = strpos($referrer, '?'); |
|
80
|
|
|
if ($position !== false) { |
|
81
|
|
|
$out = substr($referrer, 0, $position); |
|
82
|
|
|
} else { |
|
83
|
|
|
$out = $referrer; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $out; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.