-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDCategoryBehavior.php
271 lines (234 loc) · 7.83 KB
/
DCategoryBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* @author ElisDN <[email protected]>
* @link http://www.elisdn.ru
* @version 1.4
*
* @property string $urlAttribute
* @property string $titleAttribute
* @property string $aliasAttribute
* @property string $linkActiveAttribute
* @property string $requestPathAttribute
*
* @property integer[] $array
* @property mixed $assocList
* @property mixed $aliasList
* @property mixed $menuList
*/
class DCategoryBehavior extends CActiveRecordBehavior
{
/**
* @var string model attribute used for showing title
*/
public $titleAttribute = 'title';
/**
* @var string model attribute, which defined alias
*/
public $aliasAttribute = 'alias';
/**
* @var string model property, which contains url.
* Optionally your model can have 'url' attribute or getUrl() method,
* which construct correct url for using our getMenuList().
*/
public $urlAttribute = 'url';
/**
* @var string model property, which contains icon.
* Optionally for 'image' value your model can have 'image' attribute or getImage() method,
* which construct correct url for using our getMenuList().
*/
public $iconAttribute;
/**
* @var string model property, which return true for active menu item.
* Optionally declare own getLinkActive() method in your model.
*/
public $linkActiveAttribute = 'linkActive';
/**
* @var string set this request property if you can use default getLinkActive() method
*/
public $requestPathAttribute = 'path';
/**
* @var array default criteria for all queries
*/
public $defaultCriteria = array();
protected $_primaryKey;
protected $_tableSchema;
protected $_tableName;
protected $_criteria;
/**
* Return primary keys of all items
* @return array
*/
public function getArray()
{
$criteria = $this->getOwnerCriteria();
$criteria->select = $this->primaryKeyAttribute;
$command = $this->createFindCommand($criteria);
$result = $command->queryColumn();
$this->clearOwnerCriteria();
return $result;
}
/**
* Returns associated array ($id=>$title, $id=>$title, ...)
* @return array
*/
public function getAssocList()
{
$this->cached();
$items = $this->getFullAssocData(array(
$this->primaryKeyAttribute,
$this->titleAttribute,
));
$result = array();
foreach($items as $item){
$result[$item[$this->primaryKeyAttribute]] = $item[$this->titleAttribute];
}
return $result;
}
/**
* Returns associated array ($alias=>$title, $alias=>$title, ...)
* @return array
*/
public function getAliasList()
{
$this->cached();
$items = $this->getFullAssocData(array(
$this->aliasAttribute,
$this->titleAttribute,
));
$result = array();
foreach($items as $item){
$result[$item[$this->aliasAttribute]] = $item[$this->titleAttribute];
}
return $result;
}
/**
* Returns associated array ($url=>$title, $url=>$title, ...)
* @return array
*/
public function getUrlList()
{
$criteria = $this->getOwnerCriteria();
$items = $this->cached($this->getOwner())->findAll($criteria);
$result = array();
foreach ($items as $item){
$result = $result + array($item->{$this->urlAttribute}=>$item->{$this->titleAttribute});
}
return $result;
}
/**
* Returns items for zii.widgets.CMenu widget
* @return array
*/
public function getMenuList()
{
$criteria = $this->getOwnerCriteria();
$items = $this->cached($this->getOwner())->findAll($criteria);
$result = array();
foreach ($items as $item){
$active = $item->{$this->linkActiveAttribute};
$result[$item->getPrimaryKey()] = array(
'id'=>$item->getPrimaryKey(),
'label'=>$item->{$this->titleAttribute},
'url'=>$item->{$this->urlAttribute},
'icon'=>$this->iconAttribute !== null ? $item->{$this->iconAttribute} : '',
'active'=>$active,
'itemOptions'=>array('class'=>'item_' . $item->getPrimaryKey()),
'linkOptions'=>$active ? array('rel'=>'nofollow') : array(),
);
}
return $result;
}
/**
* Finds model by alias attribute
* @param $alias
* @return CActiveRecord model
*/
public function findByAlias($alias)
{
$model = $this->cached($this->getOwner())->find(array(
'condition'=>'t.' . $this->aliasAttribute . '=:alias',
'params'=>array(':alias'=>$alias),
));
return $model;
}
/**
* Optional redeclare this method in your model for use (@link getMenuList())
* or define in (@link requestPathAttribute) your $_GET attribute for url matching
* @return bool true if current request url matches with category alias
*/
public function getLinkActive()
{
return mb_strpos(Yii::app()->request->getParam($this->requestPathAttribute), $this->getOwner()->{$this->aliasAttribute}, null, 'UTF-8') === 0;
}
/**
* Redeclare this method in your model for use of (@link getMenuList()) method
* @return string
*/
public function getUrl()
{
return '#';
}
protected function getFullAssocData($attributes)
{
$criteria = $this->getOwnerCriteria();
$attributes = $this->aliasAttributes(array_unique(array_merge($attributes, array($this->primaryKeyAttribute))));
$criteria->select = implode(', ', $attributes);
$command = $this->createFindCommand($criteria);
$this->clearOwnerCriteria();
return $command->queryAll();
}
protected function createFindCommand($criteria)
{
$builder = new CDbCommandBuilder(Yii::app()->db->getSchema());
$command = $builder->createFindCommand($this->tableName, $criteria);
return $command;
}
protected function cached($model=null)
{
if ($model === null)
$model = $this->getOwner();
$connection = $model->getDbConnection();
return $model->cache($connection->queryCachingDuration);
}
protected function aliasAttributes($attributes)
{
$aliasesAttributes = array();
foreach ($attributes as $attribute) {
$aliasesAttributes[] = 't.' . $attribute;
}
return $aliasesAttributes;
}
protected function getPrimaryKeyAttribute()
{
if ($this->_primaryKey === null)
$this->_primaryKey = $this->tableSchema->primaryKey;
return $this->_primaryKey;
}
protected function getTableSchema()
{
if ($this->_tableSchema === null)
$this->_tableSchema = $this->getOwner()->getMetaData()->tableSchema;
return $this->_tableSchema;
}
protected function getTableName()
{
if ($this->_tableName === null)
$this->_tableName = $this->getOwner()->tableName();
return $this->_tableName;
}
protected function getOwnerCriteria()
{
$criteria = clone $this->getOwner()->getDbCriteria();
$criteria->mergeWith($this->defaultCriteria);
$this->_criteria = clone $criteria;
return $criteria;
}
protected function clearOwnerCriteria()
{
$this->getOwner()->setDbCriteria(new CDbCriteria());
}
protected function getOriginalCriteria()
{
return clone $this->_criteria;
}
}