1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\slider; |
4
|
|
|
|
5
|
|
|
use app; |
6
|
|
|
use app\models\Slider; |
7
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
8
|
|
|
use Yii; |
9
|
|
|
use yii\base\InvalidConfigException; |
10
|
|
|
use yii\base\Widget; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
use yii\helpers\Json; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Slider widget |
16
|
|
|
* @package app\slider\sliders |
17
|
|
|
*/ |
18
|
|
|
class SliderWidget extends Widget |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string Slider name |
22
|
|
|
*/ |
23
|
|
|
public $slider_name = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var integer Slider ID |
27
|
|
|
*/ |
28
|
|
|
public $slider_id = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Slider|ActiveRecordHelper slider model instance |
32
|
|
|
*/ |
33
|
|
|
public $slider = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string View file to render slider widget with - can be redefined by Slider.custom_slider_view_file |
37
|
|
|
*/ |
38
|
|
|
public $view_file = "slider-widget"; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string View file for each slide - can be redefined by Slider.custom_slide_view_file |
42
|
|
|
*/ |
43
|
|
|
public $slide_view_file = "slide"; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array Params for the JS slider - will be merged with Slider.params |
47
|
|
|
*/ |
48
|
|
|
public $params = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string Advanced css classes to apply to this slider - will be appended by Slider.css_class |
52
|
|
|
*/ |
53
|
|
|
public $css_class = ""; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function init() |
59
|
|
|
{ |
60
|
|
|
parent::init(); |
61
|
|
|
if ($this->slider_id === null && $this->slider_name === null && $this->slider === null) { |
62
|
|
|
throw new InvalidConfigException("Either slider, slider_id or slider_name should be set."); |
63
|
|
|
} |
64
|
|
|
if ($this->slider === null) { |
65
|
|
|
if ($this->slider_name !== null) { |
66
|
|
|
$this->slider = Yii::$app->cache->get("Slider:name:".$this->slider_name); |
67
|
|
|
if ($this->slider === false) { |
68
|
|
|
$this->slider = Slider::find() |
|
|
|
|
69
|
|
|
->where(['name' => $this->slider_name]) |
70
|
|
|
->one(); |
71
|
|
|
if ($this->slider !== null) { |
72
|
|
|
Yii::$app->cache->set( |
73
|
|
|
"Slider:name:" . $this->slider_name, |
74
|
|
|
$this->slider, |
75
|
|
|
86400, |
76
|
|
|
new \yii\caching\TagDependency([ |
77
|
|
|
'tags' => [ |
78
|
|
|
$this->slider->objectTag(), |
79
|
|
|
] |
80
|
|
|
]) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$this->slider = Slider::findById($this->slider_id); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function run() |
94
|
|
|
{ |
95
|
|
|
if ($this->slider === null) { |
96
|
|
|
return '<!-- slider model not found -->'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$viewFile = !empty($this->slider->custom_slider_view_file) ? |
100
|
|
|
$this->slider->custom_slider_view_file : |
101
|
|
|
$this->view_file; |
102
|
|
|
|
103
|
|
|
$slide_viewFile = !empty($this->slider->custom_slide_view_file) ? |
104
|
|
|
$this->slider->custom_slide_view_file : |
105
|
|
|
$this->slide_view_file; |
106
|
|
|
|
107
|
|
|
$slider_params = Json::decode($this->slider->params); |
108
|
|
|
if (!is_array($slider_params)) { |
109
|
|
|
$slider_params = []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$slider_params = ArrayHelper::merge($this->params, $slider_params); |
113
|
|
|
|
114
|
|
|
$css_class = $this->css_class . ' ' . $this->slider->css_class; |
115
|
|
|
|
116
|
|
|
/** @var \app\slider\AbstractSliderWidget $class_name */ |
117
|
|
|
$class_name = $this->slider->handler()->slider_widget; |
|
|
|
|
118
|
|
|
|
119
|
|
|
return $class_name::widget([ |
120
|
|
|
'viewFile' => $viewFile, |
121
|
|
|
'slide_viewFile' => $slide_viewFile, |
122
|
|
|
'slider_params' => $slider_params, |
123
|
|
|
'css_class' => $css_class, |
124
|
|
|
'slider' => $this->slider, |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.