1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author 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.components
|
8
|
|
|
*
|
9
|
|
|
* @mixin CommonBehavior
|
10
|
|
|
* @mixin SeoBehavior
|
11
|
|
|
* @mixin TextBlockBehavior
|
12
|
|
|
* @mixin FControllerBehavior
|
13
|
|
|
*
|
14
|
|
|
* @property Counter[] $counters
|
15
|
|
|
* @property array $copyrights
|
16
|
|
|
* @property array $contacts
|
17
|
|
|
* @property array|string $settings
|
18
|
|
|
*
|
19
|
|
|
* @property FBasket|FCollectionElement[] $basket
|
20
|
|
|
* @property FFavorite|FCollectionElement[] $favorite
|
21
|
|
|
* @property FCompare|FCollectionElement[] $compare
|
22
|
|
|
* @property FForm $fastOrderForm
|
23
|
|
|
* @property FForm $callbackForm
|
24
|
|
|
* @property FForm $loginPopupForm
|
25
|
|
|
*/
|
26
|
|
|
class FController extends CController
|
27
|
|
|
{
|
28
|
|
|
public $breadcrumbs = array();
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* @var array $activeUrl
|
32
|
|
|
*/
|
33
|
|
|
public $activeUrl = array();
|
34
|
|
|
|
35
|
|
|
/**
|
36
|
|
|
* @var bool
|
37
|
|
|
*/
|
38
|
|
|
protected $rememberThisPage;
|
39
|
|
|
|
40
|
|
|
protected $canonicalUrl;
|
41
|
|
|
|
42
|
|
|
public function behaviors()
|
43
|
|
|
{
|
44
|
|
|
return array(
|
45
|
|
|
'seo' => array('class' => 'SeoBehavior'),
|
46
|
|
|
'controller' => array('class' => 'FControllerBehavior'),
|
47
|
|
|
'textBlock' => array('class' => 'TextBlockBehavior'),
|
48
|
|
|
'common' => array('class' => 'CommonBehavior'),
|
49
|
|
|
);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
public function actions()
|
53
|
|
|
{
|
54
|
|
|
return array(
|
55
|
|
|
'captcha' => array(
|
56
|
|
|
'class' => 'FCaptchaAction',
|
57
|
|
|
),
|
58
|
|
|
);
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
public function init()
|
62
|
|
|
{
|
63
|
|
|
Yii::app()->setHomeUrl($this->createAbsoluteUrl('index/index'));
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
public function renderOverride($view, $data = null, $return = false, $processOutput = false)
|
67
|
|
|
{
|
68
|
|
|
if( !empty($this->action) && file_exists($this->viewPath.'/'.$this->action->id.'/'.$view.'.php') )
|
69
|
|
|
{
|
70
|
|
|
$this->renderPartial($this->action->id.'/'.$view, $data, $return, $processOutput);
|
71
|
|
|
}
|
72
|
|
|
else if( file_exists($this->viewPath.'/'.$view.'.php') )
|
73
|
|
|
{
|
74
|
|
|
$this->renderPartial($view, $data, $return, $processOutput);
|
75
|
|
|
}
|
76
|
|
|
else
|
77
|
|
|
{
|
78
|
|
|
$this->renderPartial('//'.$view, $data, $return, $processOutput);
|
79
|
|
|
}
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* @param string $modelClass
|
84
|
|
|
* @param int $id
|
85
|
|
|
*
|
86
|
|
|
* @return CActiveRecord
|
87
|
|
|
* @throws CHttpException
|
88
|
|
|
*/
|
89
|
|
|
public function loadModel($modelClass, $id)
|
90
|
|
|
{
|
91
|
|
|
$model = $modelClass::model()->findByPk($id);
|
92
|
|
|
|
93
|
|
|
if( $model === null )
|
94
|
|
|
throw new CHttpException(404, "The requested page of {$modelClass} does not exist.");
|
95
|
|
|
|
96
|
|
|
return $model;
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
/**
|
100
|
|
|
* @param string $view
|
101
|
|
|
* @param null $data
|
102
|
|
|
* @param bool $return
|
103
|
|
|
*
|
104
|
|
|
* @return string|null
|
105
|
|
|
* @throws CException
|
106
|
|
|
*/
|
107
|
|
|
public function render($view, $data = null, $return = false)
|
108
|
|
|
{
|
109
|
|
|
$this->onBeforeRender(new CEvent($this, array('data' => $data, 'view' => $view)));
|
110
|
|
|
|
111
|
|
|
if( $this->beforeRender($view) )
|
112
|
|
|
{
|
113
|
|
|
$output = $this->renderPartial($view, $data, true);
|
114
|
|
|
|
115
|
|
|
if( ($layoutFile = $this->getLayoutFile($this->layout)) !== false )
|
116
|
|
|
{
|
117
|
|
|
$this->onBeforeRenderLayout(new CEvent($this, array('content' => $output)));
|
118
|
|
|
$output = $this->renderFile($layoutFile, array('content' => $output), true);
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
$this->afterRender($view, $output);
|
122
|
|
|
$output = $this->processOutput($output);
|
123
|
|
|
|
124
|
|
|
if( $return )
|
125
|
|
|
return $output;
|
126
|
|
|
else
|
127
|
|
|
echo $output;
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
return null;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
public function onBeforeRender(CEvent $event)
|
134
|
|
|
{
|
135
|
|
|
$this->raiseEvent('onBeforeRender', $event);
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
public function onBeforeRenderLayout(CEvent $event)
|
139
|
|
|
{
|
140
|
|
|
$this->raiseEvent('onBeforeRenderLayout', $event);
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
/**
|
144
|
|
|
* http://help.yandex.ru/webmaster/?id=1111858#canonical
|
145
|
|
|
*
|
146
|
|
|
* @return string
|
147
|
|
|
*/
|
148
|
|
|
public function getCanonicalUrl()
|
149
|
|
|
{
|
150
|
|
|
if( is_null($this->canonicalUrl) )
|
151
|
|
|
{
|
152
|
|
|
$request = Yii::app()->request;
|
153
|
|
|
$path = Utils::normalizeUrl('/'.CHtml::encode($request->getPathInfo()));
|
154
|
|
|
|
155
|
|
|
if( !Yii::app()->errorHandler->error && $path )
|
156
|
|
|
$path = Yii::app()->urlManager->createPath($path);
|
157
|
|
|
|
158
|
|
|
$url = array(
|
159
|
|
|
'host' => $request->getHostInfo(),
|
160
|
|
|
'path' => $path,
|
161
|
|
|
'query' => array(),
|
162
|
|
|
);
|
163
|
|
|
|
164
|
|
|
if( Yii::app()->urlManager->rule )
|
165
|
|
|
foreach(Yii::app()->urlManager->rule->canonicalParams as $param)
|
166
|
|
|
if( $value = $request->getParam($param) )
|
167
|
|
|
$url['query'][$param] = $value;
|
168
|
|
|
|
169
|
|
|
$this->canonicalUrl = Utils::buildUrl($url);
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
return $this->canonicalUrl;
|
173
|
|
|
}
|
174
|
|
|
|
175
|
|
|
public function setCanonicalUrl($canonicalUrl)
|
176
|
|
|
{
|
177
|
|
|
$this->canonicalUrl = $canonicalUrl;
|
178
|
|
|
}
|
179
|
|
|
|
180
|
|
|
/**
|
181
|
|
|
* @param bool $cutDefaultParams
|
182
|
|
|
*
|
183
|
|
|
* @return string
|
184
|
|
|
*/
|
185
|
|
|
public function getCurrentUrl($cutDefaultParams = true)
|
186
|
|
|
{
|
187
|
|
|
return $this->createUrl($this->getCurrentRoute(), $this->getActionParams($cutDefaultParams));
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
/**
|
191
|
|
|
* @param bool $cutDefaultParams
|
192
|
|
|
*
|
193
|
|
|
* @return string
|
194
|
|
|
*/
|
195
|
|
|
public function getCurrentAbsoluteUrl($cutDefaultParams = true)
|
196
|
|
|
{
|
197
|
|
|
return $this->createAbsoluteUrl($this->getCurrentRoute(), $this->getActionParams($cutDefaultParams));
|
198
|
|
|
}
|
199
|
|
|
|
200
|
|
|
/**
|
201
|
|
|
* @return string
|
202
|
|
|
*/
|
203
|
|
|
public function getCurrentRoute()
|
204
|
|
|
{
|
205
|
|
|
$route = $this->id.'/'.$this->action->id;
|
206
|
|
|
|
207
|
|
|
if( $module = $this->getModule() )
|
208
|
|
|
$route = '/'.$module->id.'/'.$route;
|
209
|
|
|
|
210
|
|
|
return $route;
|
211
|
|
|
}
|
212
|
|
|
|
213
|
|
|
/**
|
214
|
|
|
* @param bool $cutDefaultParams
|
215
|
|
|
*
|
216
|
|
|
* @return string
|
217
|
|
|
*/
|
218
|
|
|
public function getActionUrl($cutDefaultParams = true)
|
219
|
|
|
{
|
220
|
|
|
return preg_replace('/\?.*/', '', $this->getCurrentUrl($cutDefaultParams));
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/**
|
224
|
|
|
* @param bool $cutDefaultParams
|
225
|
|
|
*
|
226
|
|
|
* @return array
|
227
|
|
|
*/
|
228
|
|
|
public function getActionParams($cutDefaultParams = false)
|
229
|
|
|
{
|
230
|
|
|
$params = $_GET;
|
231
|
|
|
|
232
|
|
|
if( $cutDefaultParams )
|
233
|
|
|
{
|
234
|
|
|
$rule = Arr::get(Yii::app()->urlManager->rules, Yii::app()->urlManager->ruleIndex);
|
235
|
|
|
foreach(Arr::get($rule, 'defaultParams', array()) as $key => $value)
|
236
|
|
|
unset($params[$key]);
|
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
return $params;
|
240
|
|
|
}
|
241
|
|
|
|
242
|
|
|
/**
|
243
|
|
|
* @param string $route
|
244
|
|
|
* @param array $params
|
245
|
|
|
* @param string $ampersand
|
246
|
|
|
*
|
247
|
|
|
* @return string
|
248
|
|
|
*/
|
249
|
|
|
public function createUrl($route, $params = array(), $ampersand = '&')
|
250
|
|
|
{
|
251
|
|
|
// Исправляем относительные роуты в модуле на абсолютные
|
252
|
|
|
if( !empty($route) && $route[0] !== '/' && ($module = $this->getModule()) !== null )
|
253
|
|
|
$route = '/'.$route;
|
254
|
|
|
|
255
|
|
|
return parent::createUrl($route, $params, $ampersand);
|
256
|
|
|
}
|
257
|
|
|
|
258
|
|
|
/**
|
259
|
|
|
* @param CAction $action
|
260
|
|
|
*
|
261
|
|
|
* @return void
|
262
|
|
|
*/
|
263
|
|
|
protected function afterAction($action)
|
264
|
|
|
{
|
265
|
|
|
if( Yii::app()->urlManager->shouldRememberReturnUrl() )
|
266
|
|
|
{
|
267
|
|
|
Yii::app()->user->setReturnUrl($this->getCurrentUrl());
|
268
|
|
|
}
|
269
|
|
|
|
270
|
|
|
parent::afterAction($action);
|
271
|
|
|
}
|
272
|
|
|
} |