|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Brett O'Donnell <[email protected]> |
|
4
|
|
|
* @copyright 2016 Mr PHP |
|
5
|
|
|
* @link https://github.com/cornernote/yii2-link-behavior |
|
6
|
|
|
* @license BSD-3-Clause https://raw.github.com/cornernote/yii2-link-behavior/master/LICENSE.md |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace cornernote\linkbehavior; |
|
10
|
|
|
|
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\Behavior; |
|
13
|
|
|
use yii\base\Event; |
|
14
|
|
|
use yii\db\ActiveRecord; |
|
15
|
|
|
use yii\db\BaseActiveRecord; |
|
16
|
|
|
use yii\helpers\ArrayHelper; |
|
17
|
|
|
use yii\helpers\Html; |
|
18
|
|
|
use yii\helpers\Inflector; |
|
19
|
|
|
use yii\helpers\StringHelper; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* LinkBehavior |
|
23
|
|
|
* |
|
24
|
|
|
* @usage: |
|
25
|
|
|
* ``` |
|
26
|
|
|
* public function behaviors() { |
|
27
|
|
|
* return [ |
|
28
|
|
|
* [ |
|
29
|
|
|
* 'class' => 'cornernote\linkbehavior\LinkBehavior', |
|
30
|
|
|
* ], |
|
31
|
|
|
* ]; |
|
32
|
|
|
* } |
|
33
|
|
|
* ``` |
|
34
|
|
|
* |
|
35
|
|
|
* @property string|array $url |
|
36
|
|
|
* @property string $link |
|
37
|
|
|
* @property string $linkText |
|
38
|
|
|
* @property string $controllerName |
|
39
|
|
|
* @property string $primaryKeyString |
|
40
|
|
|
* |
|
41
|
|
|
* @property ActiveRecord|LinkBehavior $owner |
|
42
|
|
|
*/ |
|
43
|
|
|
class LinkBehavior extends Behavior |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string The name of default action for the model, usually view |
|
48
|
|
|
*/ |
|
49
|
|
|
public $defaultAction = 'view'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string The name of the controller to be used in links |
|
53
|
|
|
*/ |
|
54
|
|
|
public $moduleName; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string The name of the controller to be used in links |
|
58
|
|
|
*/ |
|
59
|
|
|
private $_controllerName; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gets the name of the controller to be used in links |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getControllerName() |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->_controllerName) |
|
69
|
|
|
return $this->_controllerName; |
|
70
|
|
|
return $this->_controllerName = Inflector::slug(Inflector::camel2words(StringHelper::basename($this->owner->className()))); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sets the name of the controller to be used in links |
|
75
|
|
|
* |
|
76
|
|
|
* @param $controllerName |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setControllerName($controllerName) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->_controllerName = $controllerName; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The name of this model to be used in titles |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getLinkText() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->owner->className() . '-' . $this->owner->getPrimaryKeyString(); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns a URL Array to the model |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $action |
|
97
|
|
|
* @param array $params |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getUrl($action = null, $params = []) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$action) |
|
|
|
|
|
|
103
|
|
|
$action = $this->defaultAction; |
|
104
|
|
|
return ArrayHelper::merge([ |
|
105
|
|
|
'/' . ($this->owner->moduleName ? $this->owner->moduleName . '/' : '') . $this->owner->getControllerName() . '/' . $action, |
|
|
|
|
|
|
106
|
|
|
'id' => $this->owner->getPrimaryKeyString(), |
|
|
|
|
|
|
107
|
|
|
], $params); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns a Link to the model |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $title |
|
114
|
|
|
* @param string $action |
|
115
|
|
|
* @param array $params |
|
116
|
|
|
* @param array $options |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getLink($title = null, $action = null, $params = [], $options = []) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($title === null) |
|
122
|
|
|
$title = $this->owner->linkText; |
|
123
|
|
|
return Html::a($title, $this->owner->getUrl($action, $params), $options); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns Primary Key as a string |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getPrimaryKeyString() |
|
132
|
|
|
{ |
|
133
|
|
|
if (is_array($this->owner->primaryKey)) |
|
134
|
|
|
return implode('-', $this->owner->primaryKey); |
|
135
|
|
|
return $this->owner->primaryKey; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: