|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>, Sergey Glagolev <[email protected]>
|
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
|
7
|
|
|
* @package frontend.models.menu
|
|
8
|
|
|
*
|
|
9
|
|
|
* @method static Menu model(string $class = __CLASS__)
|
|
10
|
|
|
*
|
|
11
|
|
|
* @property int $id
|
|
12
|
|
|
* @property string $name
|
|
13
|
|
|
* @property string $sysname
|
|
14
|
|
|
* @property string $url
|
|
15
|
|
|
* @property int $visible
|
|
16
|
|
|
*
|
|
17
|
|
|
* @property MenuItem[] $items
|
|
18
|
|
|
*/
|
|
19
|
|
|
class Menu extends FActiveRecord implements IMenuItem
|
|
20
|
|
|
{
|
|
21
|
|
|
protected $depth;
|
|
22
|
|
|
|
|
23
|
|
|
/**
|
|
24
|
|
|
* @var FActiveRecord[]|IMenuItem[]
|
|
25
|
|
|
*/
|
|
26
|
|
|
protected $models;
|
|
27
|
|
|
|
|
28
|
9 |
|
public function defaultScope()
|
|
29
|
|
|
{
|
|
30
|
|
|
return array(
|
|
31
|
|
|
'with' => 'items'
|
|
32
|
9 |
|
);
|
|
33
|
|
|
}
|
|
34
|
|
|
|
|
35
|
1 |
|
public function relations()
|
|
36
|
|
|
{
|
|
37
|
|
|
return array(
|
|
38
|
1 |
|
'items' => array(self::HAS_MANY, 'MenuItem', 'menu_id', 'order' => 'items.position, items.id'),
|
|
39
|
1 |
|
);
|
|
40
|
|
|
}
|
|
41
|
|
|
|
|
42
|
|
|
/**
|
|
43
|
|
|
* Получение массива элементов меню по системному имени
|
|
44
|
|
|
*
|
|
45
|
|
|
* @param string $sysname
|
|
46
|
|
|
* @param integer $depth
|
|
47
|
|
|
*
|
|
48
|
|
|
* @return array
|
|
49
|
|
|
*/
|
|
50
|
9 |
|
public function getMenu($sysname, $depth = null)
|
|
51
|
|
|
{
|
|
52
|
9 |
|
$data = array();
|
|
53
|
|
|
|
|
54
|
|
|
/**
|
|
55
|
|
|
* @var Menu $menu
|
|
56
|
|
|
*/
|
|
57
|
|
|
|
|
58
|
9 |
|
$menu = $this->findByAttributes(array('sysname' => $sysname));
|
|
59
|
|
|
|
|
60
|
|
|
if( $menu )
|
|
61
|
9 |
|
{
|
|
62
|
9 |
|
$menu->setDepth($depth);
|
|
63
|
9 |
|
$data = $menu->build();
|
|
64
|
9 |
|
}
|
|
65
|
|
|
|
|
66
|
9 |
|
return $data;
|
|
67
|
|
|
}
|
|
68
|
|
|
|
|
69
|
|
|
/**
|
|
70
|
|
|
* @return array
|
|
71
|
|
|
*/
|
|
72
|
9 |
|
public function build()
|
|
73
|
|
|
{
|
|
74
|
9 |
|
$data = array();
|
|
75
|
9 |
|
$this->loadModels();
|
|
76
|
|
|
|
|
77
|
9 |
|
foreach($this->items as $item)
|
|
78
|
|
|
{
|
|
79
|
9 |
|
if( $this->depth === 0 )
|
|
80
|
9 |
|
continue;
|
|
81
|
|
|
|
|
82
|
9 |
|
$item->setDepth($this->depth - 1);
|
|
83
|
9 |
|
$data[] = $this->buildItem($item);
|
|
84
|
9 |
|
}
|
|
85
|
|
|
|
|
86
|
9 |
|
return $data;
|
|
87
|
|
|
}
|
|
88
|
|
|
|
|
89
|
|
|
/**
|
|
90
|
|
|
* @param int $depth
|
|
91
|
|
|
*/
|
|
92
|
9 |
|
public function setDepth($depth = null)
|
|
93
|
|
|
{
|
|
94
|
9 |
|
if( isset($depth) )
|
|
95
|
9 |
|
{
|
|
96
|
1 |
|
$this->depth = $depth;
|
|
97
|
1 |
|
}
|
|
98
|
9 |
|
}
|
|
99
|
|
|
|
|
100
|
|
|
/**
|
|
101
|
|
|
* @return array
|
|
102
|
|
|
*/
|
|
103
|
9 |
|
public function getMenuUrl()
|
|
104
|
|
|
{
|
|
105
|
9 |
|
if( empty($this->url) )
|
|
106
|
9 |
|
return '';
|
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
if( substr($this->url, 0, 1) === '/' && substr($this->url, -1, 1) === '/' )
|
|
|
|
|
|
|
109
|
|
|
return $this->url;
|
|
|
|
|
|
|
110
|
|
|
else
|
|
111
|
|
|
return array($this->url);
|
|
112
|
|
|
}
|
|
113
|
|
|
|
|
114
|
|
|
/**
|
|
115
|
|
|
* @return array
|
|
116
|
|
|
*/
|
|
117
|
9 |
|
public function getChildren()
|
|
118
|
|
|
{
|
|
119
|
9 |
|
return $this->build();
|
|
120
|
|
|
}
|
|
121
|
|
|
|
|
122
|
|
|
/**
|
|
123
|
|
|
* @return string
|
|
124
|
|
|
*/
|
|
125
|
9 |
|
public function getName()
|
|
126
|
|
|
{
|
|
127
|
9 |
|
return $this->name;
|
|
128
|
|
|
}
|
|
129
|
|
|
|
|
130
|
9 |
|
protected function buildItem(MenuItem $item)
|
|
131
|
|
|
{
|
|
132
|
9 |
|
$model = preg_replace("/([A-Z][a-z]+)\w*/", "$1", $item->frontend_model);
|
|
133
|
|
|
|
|
134
|
|
|
return array(
|
|
135
|
9 |
|
'label' => $item->getName(),
|
|
|
|
|
|
|
136
|
9 |
|
'url' => $item->getMenuUrl(),
|
|
137
|
9 |
|
'items' => $item->getChildren(),
|
|
138
|
|
|
'itemOptions' => array(
|
|
139
|
9 |
|
'class' => 'icn-menu-'.strtolower($model).$item->item_id
|
|
140
|
9 |
|
),
|
|
141
|
9 |
|
);
|
|
142
|
|
|
}
|
|
143
|
|
|
|
|
144
|
9 |
|
protected function loadModels()
|
|
145
|
|
|
{
|
|
146
|
9 |
|
if( $this->models === null )
|
|
147
|
9 |
|
{
|
|
148
|
9 |
|
$this->models = array();
|
|
149
|
9 |
|
$loadedModels = CHtml::listData($this->items, 'item_id', 'item_id', 'frontend_model');
|
|
150
|
|
|
|
|
151
|
9 |
|
foreach($loadedModels as $modelClass => $modelPk)
|
|
152
|
|
|
{
|
|
153
|
|
|
/**
|
|
154
|
|
|
* @var FActiveRecord $model
|
|
155
|
|
|
*/
|
|
156
|
9 |
|
$model = $modelClass::model();
|
|
157
|
9 |
|
$this->models = CMap::mergeArray($this->models, $model->findAllByPk($modelPk));
|
|
158
|
9 |
|
}
|
|
159
|
|
|
|
|
160
|
9 |
|
$this->setModels();
|
|
161
|
9 |
|
}
|
|
162
|
9 |
|
}
|
|
163
|
|
|
|
|
164
|
9 |
|
protected function setModels()
|
|
165
|
|
|
{
|
|
166
|
9 |
|
foreach($this->models as $model)
|
|
167
|
|
|
{
|
|
168
|
9 |
|
foreach($this->items as $menuItem)
|
|
169
|
|
|
{
|
|
170
|
9 |
|
if( $menuItem->item_id === $model->getPrimaryKey() && $menuItem->frontend_model === get_class($model) )
|
|
171
|
9 |
|
{
|
|
172
|
9 |
|
$menuItem->setModel($model);
|
|
173
|
9 |
|
$model->setDepth(isset($this->depth) ? $this->depth - 1 : null);
|
|
174
|
9 |
|
break;
|
|
175
|
|
|
}
|
|
176
|
9 |
|
}
|
|
177
|
9 |
|
}
|
|
178
|
|
|
}
|
|
179
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.