1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace frontend\widgets; |
9
|
|
|
|
10
|
|
|
use common\models\Widget; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Exception; |
13
|
|
|
use yii\helpers\ArrayHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Render active widget to frontend. |
17
|
|
|
* The following example shows how to use RenderWidget: |
18
|
|
|
* ~~~ |
19
|
|
|
* RenderWidget::widget([ |
20
|
|
|
* 'location' => 'sidebar', |
21
|
|
|
* 'config' => [ |
22
|
|
|
* 'beforeWidget' => '<div class="widget">', |
23
|
|
|
* 'afterWidget' => '</div>', |
24
|
|
|
* 'beforeTitle' => '<h4 class="widget-title">', |
25
|
|
|
* 'afterTitle' => '</h4>', |
26
|
|
|
* ] |
27
|
|
|
* ]) |
28
|
|
|
* ~~~ |
29
|
|
|
* |
30
|
|
|
* @author Agiel K. Saputra <[email protected]> |
31
|
|
|
* @since 0.2.0 |
32
|
|
|
*/ |
33
|
|
|
class RenderWidget extends \yii\base\Widget |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var array Configuration of widget. |
37
|
|
|
*/ |
38
|
|
|
public $config; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string Location of active widget. |
42
|
|
|
*/ |
43
|
|
|
public $location; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array Loaded activated widget. |
47
|
|
|
*/ |
48
|
|
|
private $_widget = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array Default configuration of widget. |
52
|
|
|
*/ |
53
|
|
|
private $_defaultConfig = [ |
54
|
|
|
'beforeTitle' => '', |
55
|
|
|
'afterTitle' => '', |
56
|
|
|
'beforeWidget' => '', |
57
|
|
|
'afterWidget' => '', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function init() |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* @var $activeWidgets \common\models\Widget |
67
|
|
|
*/ |
68
|
|
|
$activeWidgets = Widget::find() |
69
|
|
|
->where(['location' => $this->location]) |
70
|
|
|
->orderBy(['order' => SORT_ASC]) |
71
|
|
|
->all(); |
72
|
|
|
|
73
|
|
|
if ($activeWidgets) { |
74
|
|
|
foreach ($activeWidgets as $activeWidget) { |
75
|
|
|
$this->_widget[] = ArrayHelper::merge( |
76
|
|
|
$this->_defaultConfig, |
77
|
|
|
$this->config, |
78
|
|
|
$activeWidget->getConfig(), |
79
|
|
|
['id' => $activeWidget->id] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function run() |
89
|
|
|
{ |
90
|
|
|
foreach ($this->_widget as $widget) { |
91
|
|
|
try { |
92
|
|
|
Yii::createObject($widget); |
93
|
|
|
} catch (Exception $e) { |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|