1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package backend.components
|
8
|
|
|
*
|
9
|
|
|
* Для использования необходимо в шаблоне вызвать метод show().
|
10
|
|
|
* Хлебные крошки автоматически сформируются в зависимости от модуля, контроллера, действия и модели
|
11
|
|
|
*
|
12
|
|
|
* Пример:
|
13
|
|
|
* <pre>
|
14
|
|
|
* Yii::app()->breadcrumbs->show();
|
15
|
|
|
* </pre>
|
16
|
|
|
*/
|
17
|
|
|
class BBreadcrumbsManager
|
18
|
|
|
{
|
19
|
|
|
/**
|
20
|
|
|
* @var string
|
21
|
|
|
*/
|
22
|
|
|
public static $indexName = 'index';
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* @var BModule
|
26
|
|
|
*/
|
27
|
|
|
private $module;
|
|
|
|
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var BController
|
31
|
|
|
*/
|
32
|
|
|
private $controller;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @var BController
|
36
|
|
|
*/
|
37
|
|
|
private $defaultModuleController;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* @var array
|
41
|
|
|
*/
|
42
|
|
|
private $breadcrumbs = array();
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* Есть ли возможность вырезать ссылку на стандартный контроллер,
|
46
|
|
|
* ставится в true только, если появляются похожие части хлебных крошек
|
47
|
|
|
* (одинаковое название DefaultController::Controller)
|
48
|
|
|
*
|
49
|
|
|
* @var boolean
|
50
|
|
|
*/
|
51
|
|
|
private $canCutDefaultControllerLink = false;
|
52
|
|
|
|
53
|
|
|
public function init()
|
54
|
|
|
{
|
55
|
|
|
try
|
56
|
|
|
{
|
57
|
|
|
if( !(Yii::app()->controller->module instanceof BModule) )
|
58
|
|
|
return;
|
59
|
|
|
|
60
|
|
|
$this->controller = Yii::app()->controller;
|
61
|
|
|
|
62
|
|
|
$this->initModel();
|
63
|
|
|
$this->initDefaultController();
|
64
|
|
|
$this->initBreadcrumbs();
|
65
|
|
|
}
|
66
|
|
|
catch( Exception $e )
|
67
|
|
|
{
|
68
|
|
|
Yii::log($e->getMessage(), CLogger::LEVEL_ERROR, 'Breadcrumbs');
|
69
|
|
|
}
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* Присваивание текущему контроллеру приложения сформированных хлебных крошек
|
74
|
|
|
*/
|
75
|
|
|
public function show()
|
76
|
|
|
{
|
77
|
|
|
Yii::app()->controller->breadcrumbs = $this->breadcrumbs;
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
protected function initModel()
|
81
|
|
|
{
|
82
|
|
|
$params = $this->controller->getActionParams();
|
83
|
|
|
|
84
|
|
|
if( $this->controller->action->id === self::$indexName )
|
85
|
|
|
$this->model = null;
|
|
|
|
|
86
|
|
|
elseif( !empty($params['id']) )
|
87
|
|
|
$this->model = $this->controller->loadModel($params['id']);
|
88
|
|
|
else
|
89
|
|
|
{
|
90
|
|
|
$modelName = $this->controller->modelClass;
|
91
|
|
|
$this->model = new $modelName;
|
92
|
|
|
}
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* @throws BBreadcrumbsManagerException
|
97
|
|
|
*/
|
98
|
|
|
protected function initDefaultController()
|
99
|
|
|
{
|
100
|
|
|
$controllerName = Yii::app()->controller->module->defaultController;
|
101
|
|
|
$controllerClass = ucfirst($controllerName).'Controller';
|
102
|
|
|
|
103
|
|
|
$this->defaultModuleController = new $controllerClass($controllerName);
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
protected function initBreadcrumbs()
|
107
|
|
|
{
|
108
|
|
|
if( empty($this->model) )
|
109
|
|
|
$this->initBreadcumbsWithotModel();
|
110
|
|
|
elseif( empty($this->model->id) )
|
111
|
|
|
$this->initBreadcrumbsWithNewModel();
|
112
|
|
|
else
|
113
|
|
|
$this->initBreadcrumbsWithExistingModel();
|
114
|
|
|
|
115
|
|
|
if( !$this->checkControllersNameDiff() && $this->canCutDefaultControllerLink )
|
116
|
|
|
unset($this->breadcrumbs[$this->controller->name]);
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
/**
|
120
|
|
|
* Инициализация хлебных крошек без участия модели (actionIndex)
|
121
|
|
|
*/
|
122
|
|
|
protected function initBreadcumbsWithotModel()
|
123
|
|
|
{
|
124
|
|
|
$this->canCutDefaultControllerLink = true;
|
125
|
|
|
|
126
|
|
|
$this->breadcrumbs[] = $this->controller->name;
|
127
|
|
|
}
|
128
|
|
|
|
129
|
|
|
/**
|
130
|
|
|
* Инициализация хлебных крошек с новой записью
|
131
|
|
|
*/
|
132
|
|
|
protected function initBreadcrumbsWithNewModel()
|
133
|
|
|
{
|
134
|
|
|
$this->breadcrumbs[$this->controller->name] = self::$indexName;
|
135
|
|
|
$this->breadcrumbs[] = 'Создание';
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* Инициализация хлебных крошек с существующей записью
|
140
|
|
|
*/
|
141
|
|
|
protected function initBreadcrumbsWithExistingModel()
|
142
|
|
|
{
|
143
|
|
|
$this->breadcrumbs[$this->controller->name] = self::$indexName;
|
144
|
|
|
|
145
|
|
|
if( $this->model instanceof BProduct && !empty($this->model->parent) )
|
146
|
|
|
{
|
147
|
|
|
$this->breadcrumbs['Родительский продукт'] = Yii::app()->createUrl('/product/product/update', array('id' => $this->model->parent));
|
148
|
|
|
$this->breadcrumbs[] = '[Модификация] '.$this->prepareItemName();
|
149
|
|
|
}
|
150
|
|
|
else
|
151
|
|
|
{
|
152
|
|
|
$this->breadcrumbs[] = $this->prepareItemName();
|
153
|
|
|
}
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* проверка на совпадение имени стандартного и текущего контроллера
|
158
|
|
|
*
|
159
|
|
|
* @return boolean
|
160
|
|
|
*/
|
161
|
|
|
protected function checkControllersNameDiff()
|
162
|
|
|
{
|
163
|
|
|
return $this->controller->id !== $this->defaultModuleController->id;
|
164
|
|
|
}
|
165
|
|
|
|
166
|
|
|
/**
|
167
|
|
|
* Получение названия записи
|
168
|
|
|
* Приоритет по полям:
|
169
|
|
|
* 1) name
|
170
|
|
|
* 2) title
|
171
|
|
|
* 3) id
|
172
|
|
|
*
|
173
|
|
|
* @return string
|
174
|
|
|
*/
|
175
|
|
|
protected function prepareItemName()
|
176
|
|
|
{
|
177
|
|
|
if( !empty($this->model->name) )
|
178
|
|
|
return $this->model->name;
|
179
|
|
|
elseif( !empty($this->model->title) )
|
180
|
|
|
return $this->model->title;
|
181
|
|
|
else
|
182
|
|
|
return $this->model->id;
|
183
|
|
|
}
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
/**
|
187
|
|
|
* @author Nikita Melnikov <[email protected]>
|
188
|
|
|
* @link https://github.com/shogodev/argilla/
|
189
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
190
|
|
|
* @license http://argilla.ru/LICENSE
|
191
|
|
|
* @package backend.components
|
192
|
|
|
*/
|
193
|
|
|
class BBreadcrumbsManagerException extends CException
|
194
|
|
|
{
|
195
|
|
|
|
196
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.