1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>, Sergey Glagolev <[email protected]>, 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 frontend.components.form
|
8
|
|
|
* Компонент для работы с формами
|
9
|
|
|
* @property string returnUrl
|
10
|
|
|
* @property string successMessage
|
11
|
|
|
*/
|
12
|
|
|
class FForm extends CForm
|
13
|
|
|
{
|
14
|
|
|
public static $DEFAULT_FORMS_PATH = 'frontend.forms.';
|
15
|
|
|
|
16
|
|
|
public $formName;
|
17
|
|
|
|
18
|
|
|
public $layout = FormLayouts::FORM_LAYOUT;
|
19
|
|
|
|
20
|
|
|
public $elementsLayout;
|
21
|
|
|
|
22
|
|
|
public $inputElementClass = 'FFormInputElement';
|
23
|
|
|
|
24
|
|
|
public $activeForm = array('class' => 'CActiveForm',
|
25
|
|
|
'enableAjaxValidation' => true);
|
26
|
|
|
|
27
|
|
|
public $ajaxSubmit = true;
|
28
|
|
|
|
29
|
|
|
public $validateOnSubmit = true;
|
30
|
|
|
|
31
|
|
|
public $validateOnChange = true;
|
32
|
|
|
|
33
|
|
|
public $autocomplete = false;
|
34
|
|
|
|
35
|
|
|
public $loadFromSession = false;
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @var bool $setUserData флаг подстановки пользовательски данных в форму
|
39
|
|
|
*/
|
40
|
|
|
public $setUserData = true;
|
41
|
|
|
|
42
|
|
|
public $clearAfterSubmit = false;
|
43
|
|
|
|
44
|
|
|
protected $layoutViewParams = array();
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @var int
|
48
|
|
|
*/
|
49
|
|
|
protected $redirectDelay;
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* @var string
|
53
|
|
|
*/
|
54
|
|
|
protected $redirectUrl;
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* @var bool
|
58
|
|
|
*/
|
59
|
|
|
protected $status = false;
|
60
|
|
|
|
61
|
16 |
|
public function __construct($config, $model = null, $parent = null)
|
62
|
|
|
{
|
63
|
16 |
|
if( is_string($config) )
|
64
|
16 |
|
{
|
65
|
15 |
|
$config = strpos($config, '.') !== false ? $config : static::$DEFAULT_FORMS_PATH.$config;
|
66
|
15 |
|
$this->formName = get_class($model);
|
67
|
15 |
|
}
|
68
|
6 |
|
else if( is_array($config) )
|
69
|
6 |
|
{
|
70
|
6 |
|
$this->formName = Arr::get($config, 'name');
|
71
|
6 |
|
}
|
72
|
|
|
|
73
|
16 |
|
parent::__construct($config, $model, $parent);
|
74
|
16 |
|
}
|
75
|
|
|
|
76
|
7 |
|
public function __toString()
|
77
|
|
|
{
|
78
|
7 |
|
return $this->render();
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* @param bool $status
|
83
|
|
|
*
|
84
|
|
|
* @return void
|
85
|
|
|
*/
|
86
|
3 |
|
public function setStatus($status)
|
87
|
|
|
{
|
88
|
3 |
|
$this->status = $status;
|
89
|
3 |
|
}
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* @return bool
|
93
|
|
|
*/
|
94
|
1 |
|
public function getStatus()
|
95
|
|
|
{
|
96
|
1 |
|
return $this->status;
|
97
|
|
|
}
|
98
|
|
|
|
99
|
|
|
/**
|
100
|
|
|
* Добавление всплывающего окна после отправки формы
|
101
|
|
|
* @HINT сообщение добавляется только при отправки формы НЕ через ajax
|
102
|
|
|
*
|
103
|
|
|
* @param string $message
|
104
|
|
|
* @param string $type
|
105
|
|
|
*/
|
106
|
|
|
public function setFlashMessage($message, $type = 'success')
|
107
|
|
|
{
|
108
|
|
|
if( $this->ajaxSubmit == false )
|
|
|
|
|
109
|
|
|
Yii::app()->user->setFlash($type, $message);
|
110
|
|
|
}
|
111
|
|
|
|
112
|
|
|
/**
|
113
|
|
|
* @param int $delay
|
114
|
|
|
* @param string $url
|
115
|
|
|
*
|
116
|
|
|
* @return void
|
117
|
|
|
*/
|
118
|
|
|
public function setRedirectDelay($delay, $url = null)
|
119
|
|
|
{
|
120
|
|
|
$this->redirectDelay = $delay;
|
121
|
|
|
$this->redirectUrl = !empty($url) ? $url : Yii::app()->request->requestUri;
|
122
|
|
|
$this->registerRedirectScript();
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
/**
|
126
|
|
|
* @param null $defaultUrl
|
127
|
|
|
*
|
128
|
|
|
* @return mixed
|
129
|
|
|
*/
|
130
|
|
|
public function getReturnUrl($defaultUrl = null)
|
131
|
|
|
{
|
132
|
|
|
return Yii::app()->user->getState('__'.$this->formName.'ReturnUrl', $defaultUrl === null ? Yii::app()->getRequest()->getScriptUrl() : CHtml::normalizeUrl($defaultUrl));
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
/**
|
136
|
|
|
* @param string $value
|
137
|
|
|
*/
|
138
|
8 |
|
public function setReturnUrl($value)
|
139
|
|
|
{
|
140
|
8 |
|
Yii::app()->user->setState('__'.$this->formName.'ReturnUrl', $value);
|
141
|
8 |
|
}
|
142
|
|
|
|
143
|
|
|
/**
|
144
|
|
|
* @param $message
|
145
|
|
|
*/
|
146
|
|
|
public function setSuccessMessage($message)
|
147
|
|
|
{
|
148
|
|
|
Yii::app()->user->setFlash('__'.$this->formName.'Success', $message);
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
/**
|
152
|
|
|
* @return string
|
153
|
|
|
*/
|
154
|
8 |
|
public function getSuccessMessage()
|
155
|
|
|
{
|
156
|
8 |
|
return Yii::app()->user->getFlash('__'.$this->formName.'Success');
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* @param $message
|
161
|
|
|
*/
|
162
|
1 |
|
public function setErrorMessage(array $message)
|
163
|
|
|
{
|
164
|
1 |
|
Yii::app()->user->setFlash('__'.$this->formName.'Error', $message);
|
165
|
1 |
|
}
|
166
|
|
|
|
167
|
|
|
/**
|
168
|
|
|
* @return mixed
|
169
|
|
|
*/
|
170
|
8 |
|
public function getErrorMessage()
|
171
|
|
|
{
|
172
|
8 |
|
return Yii::app()->user->getFlash('__'.$this->formName.'Error');
|
173
|
|
|
}
|
174
|
|
|
|
175
|
8 |
|
public function renderBegin()
|
176
|
|
|
{
|
177
|
8 |
|
$this->beforeRender();
|
178
|
|
|
|
179
|
|
|
$options = array(
|
180
|
8 |
|
'validateOnSubmit' => $this->validateOnSubmit,
|
181
|
8 |
|
'validateOnChange' => $this->validateOnChange
|
182
|
8 |
|
);
|
183
|
|
|
|
184
|
8 |
|
if( $this->autocomplete === false )
|
185
|
8 |
|
$this->activeForm['htmlOptions']['autocomplete'] = 'off';
|
186
|
|
|
|
187
|
8 |
|
$this->activeForm['clientOptions'] = CMap::mergeArray($options, Arr::get($this->activeForm, 'clientOptions', array()));
|
188
|
|
|
|
189
|
8 |
|
return parent::renderBegin();
|
190
|
|
|
}
|
191
|
|
|
|
192
|
8 |
|
public function renderBody()
|
193
|
|
|
{
|
194
|
8 |
|
$output = array('{title}' => $this->renderTitle(),
|
195
|
8 |
|
'{elements}' => $this->renderElements(),
|
196
|
8 |
|
'{errors}' => $this->getActiveFormWidget()->errorSummary($this->getModel()),
|
197
|
8 |
|
'{description}' => $this->description,
|
198
|
8 |
|
'{buttons}' => $this->renderButtons());
|
199
|
|
|
|
200
|
|
|
// Рендерим представления динамически
|
201
|
8 |
|
$output = CMap::mergeArray($output, $this->renderLayoutViews($this->layout));
|
202
|
|
|
|
203
|
8 |
|
return strtr($this->layout, $output);
|
204
|
|
|
}
|
205
|
|
|
|
206
|
|
|
/**
|
207
|
|
|
* @param string $name
|
208
|
|
|
* @param CFormElement $element
|
209
|
|
|
* @param bool $forButtons
|
210
|
|
|
*/
|
211
|
16 |
|
public function addedElement($name, $element, $forButtons)
|
212
|
|
|
{
|
213
|
16 |
|
if( isset($element->type) && in_array($element->type, array('text', 'password', 'tel', 'textarea')) )
|
214
|
16 |
|
if( empty($element->attributes['class']) )
|
215
|
16 |
|
$element->attributes['class'] = 'inp';
|
216
|
16 |
|
}
|
217
|
|
|
|
218
|
8 |
|
public function renderTitle()
|
219
|
|
|
{
|
220
|
8 |
|
$output = '';
|
221
|
|
|
|
222
|
8 |
|
if( $this->title !== null )
|
223
|
8 |
|
{
|
224
|
|
|
if( $this->getParent() instanceof self )
|
225
|
|
|
{
|
226
|
|
|
$attributes = $this->attributes;
|
227
|
|
|
unset($attributes['name'], $attributes['type']);
|
228
|
|
|
$output = $this->title;
|
229
|
|
|
}
|
230
|
|
|
else
|
231
|
|
|
$output = $this->title;
|
232
|
|
|
}
|
233
|
|
|
|
234
|
8 |
|
return $output;
|
235
|
|
|
}
|
236
|
|
|
|
237
|
8 |
|
public function renderButtons()
|
238
|
|
|
{
|
239
|
8 |
|
$output = '';
|
240
|
|
|
|
241
|
8 |
|
foreach($this->getButtons() as $button)
|
242
|
|
|
{
|
243
|
7 |
|
$output .= $this->renderElement($this->modifySubmitButton($button));
|
244
|
8 |
|
}
|
245
|
|
|
|
246
|
8 |
|
return $output !== '' ? $output : '';
|
247
|
|
|
}
|
248
|
|
|
|
249
|
|
|
/**
|
250
|
|
|
* Renders a single element which could be an input element, a sub-form, a string, or a button.
|
251
|
|
|
*
|
252
|
|
|
* @param mixed $element the form element to be rendered. This can be either a {@link CFormElement} instance
|
253
|
|
|
* or a string representing the name of the form element.
|
254
|
|
|
*
|
255
|
|
|
* @return string the rendering result
|
256
|
|
|
*/
|
257
|
8 |
|
public function renderElement($element)
|
258
|
|
|
{
|
259
|
8 |
View Code Duplication |
if( is_string($element) )
|
|
|
|
|
260
|
8 |
|
{
|
261
|
|
|
if( ($e = $this[$element]) === null && ($e = $this->getButtons()->itemAt($element)) === null )
|
262
|
|
|
return $element;
|
263
|
|
|
else
|
264
|
|
|
$element = $e;
|
265
|
|
|
}
|
266
|
8 |
|
if( $element->getVisible() )
|
267
|
8 |
|
{
|
268
|
8 |
|
if( $element instanceof CFormInputElement )
|
|
|
|
|
269
|
8 |
|
{
|
270
|
8 |
|
if( $element->type === 'hidden' )
|
271
|
8 |
|
return "<div style=\"visibility:hidden\">\n".$element->render()."</div>\n";
|
272
|
|
|
else
|
273
|
|
|
{
|
274
|
8 |
|
return $element->render()."\n";
|
275
|
|
|
}
|
276
|
|
|
}
|
277
|
7 |
|
else if( $element instanceof CFormButtonElement )
|
|
|
|
|
278
|
7 |
|
return $element->render()."\n";
|
279
|
|
|
else
|
280
|
7 |
|
return $element->render();
|
281
|
|
|
}
|
282
|
|
|
|
283
|
1 |
|
return '';
|
284
|
|
|
}
|
285
|
|
|
|
286
|
|
|
/**
|
287
|
|
|
* Обработка формы: получение данных и валидация
|
288
|
|
|
* @return bool
|
289
|
|
|
*/
|
290
|
10 |
|
public function process()
|
291
|
|
|
{
|
292
|
10 |
|
if( $this->loadFromSession )
|
293
|
10 |
|
$this->loadFromSession();
|
294
|
|
|
|
295
|
10 |
|
if( !Yii::app()->request->isPostRequest )
|
296
|
10 |
|
return false;
|
297
|
|
|
|
298
|
9 |
|
$this->loadData();
|
299
|
9 |
|
$errors = json_decode(CActiveForm::validate($this->getModels(), null, false), true);
|
300
|
|
|
|
301
|
9 |
|
if( !$errors )
|
302
|
9 |
|
return true;
|
303
|
|
|
|
304
|
4 |
|
if( Yii::app()->request->isAjaxRequest )
|
305
|
4 |
|
{
|
306
|
3 |
|
echo json_encode(array('status' => 'ok', 'validateErrors' => json_encode($errors)));
|
307
|
3 |
|
Yii::app()->end();
|
|
|
|
|
308
|
|
|
}
|
309
|
|
|
else
|
310
|
1 |
|
$this->setErrorMessage($errors);
|
311
|
|
|
|
312
|
1 |
|
return false;
|
313
|
|
|
}
|
314
|
|
|
|
315
|
|
|
/**
|
316
|
|
|
* Посылает сообщение о успешной обработке данных
|
317
|
|
|
*
|
318
|
|
|
* @param string $message - сообщение
|
319
|
|
|
* @param bool $scrollOnMessage - скролить страницу на сообщение
|
320
|
|
|
* @param array $responseData
|
321
|
|
|
* @param bool $end - завершить работу скрипта
|
322
|
|
|
*/
|
323
|
12 |
|
public function responseSuccess($message = '', $scrollOnMessage = false, $responseData = array(), $end = true)
|
324
|
|
|
{
|
325
|
3 |
|
echo json_encode(CMap::mergeArray($responseData, array(
|
326
|
3 |
|
'status' => 'ok',
|
327
|
3 |
|
'messageForm' => $message,
|
328
|
|
|
'scrollOnMessage' => $scrollOnMessage
|
329
|
3 |
|
)));
|
330
|
|
|
|
331
|
|
|
if( $end )
|
332
|
3 |
|
Yii::app()->end();
|
|
|
|
|
333
|
12 |
|
}
|
334
|
|
|
|
335
|
|
|
/**
|
336
|
|
|
* Валидация и сохранение данных в модель
|
337
|
|
|
* @return bool
|
338
|
|
|
* @throws CHttpException
|
339
|
|
|
*/
|
340
|
5 |
|
public function save()
|
341
|
|
|
{
|
342
|
5 |
|
if( $this->process() )
|
343
|
3 |
|
{
|
344
|
3 |
|
Yii::app()->db->beginTransaction();
|
345
|
|
|
|
346
|
|
|
/**
|
347
|
|
|
* @var CActiveRecord $model
|
348
|
|
|
*/
|
349
|
3 |
|
foreach($this->getModels() as $model)
|
350
|
|
|
{
|
351
|
3 |
|
if( !($model instanceof CActiveRecord) )
|
352
|
3 |
|
continue;
|
353
|
|
|
|
354
|
3 |
|
if( get_class($model) !== get_class($this->model) )
|
355
|
3 |
|
{
|
356
|
2 |
|
$foreignKey = null;
|
357
|
|
|
|
358
|
2 |
|
foreach($this->model->getMetaData()->relations as $relation)
|
359
|
2 |
|
if( $relation->className == get_class($model) )
|
360
|
2 |
|
$foreignKey = $relation->foreignKey;
|
361
|
|
|
|
362
|
2 |
|
if( !$foreignKey )
|
363
|
2 |
|
throw new CHttpException(500, 'Can`t get foreign key for '.get_class($model));
|
364
|
|
|
|
365
|
2 |
|
$model->$foreignKey = $this->model->getPrimaryKey();
|
366
|
2 |
|
}
|
367
|
|
|
|
368
|
3 |
View Code Duplication |
if( !$model->save(false) )
|
|
|
|
|
369
|
3 |
|
{
|
370
|
|
|
Yii::app()->db->currentTransaction->rollback();
|
371
|
|
|
throw new CHttpException(500, 'Can`t save '.get_class($this->model).' model');
|
372
|
|
|
}
|
373
|
|
|
|
374
|
3 |
|
$this->saveFiles($model);
|
375
|
3 |
|
}
|
376
|
|
|
|
377
|
3 |
|
Yii::app()->db->currentTransaction->commit();
|
378
|
|
|
|
379
|
3 |
|
if( $this->clearAfterSubmit )
|
380
|
3 |
|
$this->clearSession();
|
381
|
|
|
|
382
|
3 |
|
$this->setStatus(true);
|
383
|
|
|
|
384
|
3 |
|
return true;
|
385
|
|
|
}
|
386
|
|
|
|
387
|
|
|
return false;
|
388
|
|
|
}
|
389
|
|
|
|
390
|
|
|
public function saveToSession()
|
391
|
|
|
{
|
392
|
|
|
// todo: сделать проверку на password и не сохранять его
|
393
|
|
|
if( $this->getModel() !== null && Yii::app()->request->isPostRequest )
|
394
|
|
|
{
|
395
|
|
|
$sessionParams = Yii::app()->request->getPost(get_class($this->getModel()), array());
|
396
|
|
|
$sessionKey = $this->getSessionKey($this->getModel());
|
397
|
|
|
|
398
|
|
|
if( !isset(Yii::app()->session[$sessionKey]) )
|
399
|
|
|
Yii::app()->session[$sessionKey] = array();
|
400
|
|
|
|
401
|
|
|
$sessionParams = CMap::mergeArray(Yii::app()->session[$sessionKey], $sessionParams);
|
402
|
|
|
Yii::app()->session[$sessionKey] = $sessionParams;
|
403
|
|
|
}
|
404
|
|
|
|
405
|
|
|
foreach($this->getElements() as $element)
|
406
|
|
|
if( $element instanceof self )
|
407
|
|
|
$element->saveToSession();
|
408
|
|
|
}
|
409
|
|
|
|
410
|
1 |
|
public function clearSession()
|
411
|
|
|
{
|
412
|
1 |
|
foreach($this->getModels() as $model)
|
413
|
1 |
|
unset(Yii::app()->session[$this->getSessionKey($model)]);
|
414
|
1 |
|
}
|
415
|
|
|
|
416
|
2 |
|
public function loadFromSession()
|
417
|
|
|
{
|
418
|
2 |
|
if( $this->getModel() !== null )
|
419
|
2 |
|
{
|
420
|
2 |
|
$sessionKey = $this->getSessionKey($this->getModel());
|
421
|
2 |
|
$sessionParams = Yii::app()->session[$sessionKey];
|
422
|
|
|
|
423
|
|
|
if( $sessionParams )
|
424
|
2 |
|
$this->getModel()->setAttributes($sessionParams);
|
425
|
2 |
|
}
|
426
|
|
|
|
427
|
2 |
|
foreach($this->getElements() as $element)
|
428
|
2 |
|
if( $element instanceof self )
|
429
|
2 |
|
$element->loadFromSession();
|
430
|
2 |
|
}
|
431
|
|
|
|
432
|
10 |
|
public function ajaxValidation()
|
433
|
|
|
{
|
434
|
10 |
|
if( Yii::app()->request->isAjaxRequest && isset($_POST['ajax']) )
|
435
|
10 |
|
{
|
436
|
|
|
if( $this->loadFromSession )
|
437
|
|
|
$this->saveToSession();
|
438
|
|
|
|
439
|
|
|
$this->loadData();
|
440
|
|
|
|
441
|
|
|
$result = $this->performAjaxValidation();
|
442
|
|
|
echo json_encode($result);
|
443
|
|
|
Yii::app()->end();
|
|
|
|
|
444
|
|
|
}
|
445
|
10 |
|
}
|
446
|
|
|
|
447
|
|
|
protected function performAjaxValidation()
|
448
|
|
|
{
|
449
|
|
|
$result = array();
|
450
|
|
|
|
451
|
|
|
if( $this->getModel() !== null )
|
452
|
|
|
{
|
453
|
|
|
$this->getModel()->validate();
|
454
|
|
|
|
455
|
|
|
foreach($this->getModel()->getErrors() as $attribute => $errors)
|
456
|
|
|
$result[CHtml::activeId($this->getModel(), $attribute)] = $errors;
|
457
|
|
|
|
458
|
|
|
foreach($this->getElements() as $element)
|
459
|
|
|
if( $element instanceof self )
|
460
|
|
|
$result = CMap::mergeArray($result, $element->performAjaxValidation());
|
461
|
|
|
}
|
462
|
|
|
|
463
|
|
|
return $result;
|
464
|
|
|
}
|
465
|
|
|
|
466
|
|
|
public function isTabular()
|
467
|
|
|
{
|
468
|
|
|
return false;
|
469
|
|
|
}
|
470
|
|
|
|
471
|
|
|
public function sendNotification($email, $vars = array())
|
472
|
|
|
{
|
473
|
|
|
Yii::app()->notification->send($this->model, $vars, $email);
|
474
|
|
|
}
|
475
|
|
|
|
476
|
|
|
public function sendNotificationBackend($vars = array())
|
477
|
|
|
{
|
478
|
|
|
$vars = CMap::mergeArray($vars, array('model' => $this->model));
|
479
|
|
|
Yii::app()->notification->send(get_class($this->model).'Backend', $vars, null, 'backend');
|
480
|
|
|
}
|
481
|
|
|
|
482
|
|
|
public function addLayoutViewParams($data)
|
483
|
|
|
{
|
484
|
|
|
$this->layoutViewParams[] = $data;
|
485
|
|
|
}
|
486
|
|
|
|
487
|
|
|
public function insertAfter($after, $element, $key)
|
488
|
|
|
{
|
489
|
|
|
$elements = $this->getElements()->toArray();
|
490
|
|
|
Arr::insertAfter($elements, $key, $element, $after);
|
491
|
|
|
|
492
|
|
|
$this->getElements()->clear();
|
493
|
|
|
$this->getElements()->copyFrom($elements);
|
494
|
|
|
}
|
495
|
|
|
|
496
|
|
|
/**
|
497
|
|
|
* Подставляет пользовательские данных в форму
|
498
|
|
|
*/
|
499
|
8 |
|
public function setUserData()
|
500
|
|
|
{
|
501
|
8 |
|
if( Yii::app()->user->isGuest )
|
502
|
8 |
|
return;
|
503
|
|
|
|
504
|
|
|
/**
|
505
|
|
|
* @var FActiveRecord $model
|
506
|
|
|
*/
|
507
|
3 |
|
foreach($this->getModels() as $model)
|
508
|
|
|
{
|
509
|
3 |
|
if( $this->loadFromSession && Yii::app()->session[$this->getSessionKey($model)] )
|
510
|
3 |
|
continue;
|
511
|
|
|
|
512
|
3 |
|
$attributes = array('email' => Yii::app()->user->getEmail());
|
513
|
3 |
|
$attributes = CMap::mergeArray($attributes, Yii::app()->user->profile->getAttributes());
|
514
|
|
|
|
515
|
3 |
|
foreach($model->getAttributes() as $attribute => $value)
|
516
|
|
|
{
|
517
|
3 |
|
if( empty($value) && !empty($attributes[$attribute]) )
|
518
|
3 |
|
$model->setAttribute($attribute, $attributes[$attribute]);
|
519
|
3 |
|
}
|
520
|
3 |
|
}
|
521
|
3 |
|
}
|
522
|
|
|
|
523
|
|
|
/**
|
524
|
|
|
* @param $model
|
525
|
|
|
*
|
526
|
|
|
* @throws CHttpException
|
527
|
|
|
*/
|
528
|
8 |
|
protected function saveFiles($model)
|
529
|
|
|
{
|
530
|
3 |
|
if( Yii::app()->request->isPostRequest && $model instanceof FActiveFileRecord )
|
531
|
3 |
|
{
|
532
|
|
|
$files = CUploadedFile::getInstances($model, $model->formAttribute);
|
533
|
|
|
|
534
|
|
|
foreach($files as $file)
|
535
|
|
|
{
|
536
|
|
|
$name = UploadHelper::prepareFileName($model->uploadPath, $file->name);
|
537
|
|
|
$path = $model->uploadPath.$name;
|
538
|
|
|
|
539
|
|
|
if( $file->saveAs($path) )
|
540
|
|
|
{
|
541
|
|
|
chmod($path, 0664);
|
542
|
|
|
|
543
|
|
|
if( $model->fileModel === get_class($model) )
|
544
|
|
|
{
|
545
|
|
|
$fileModel = $model;
|
546
|
|
|
$fileModel->{$model->formAttribute} = $name;
|
547
|
|
|
}
|
548
|
|
|
else
|
549
|
|
|
{
|
550
|
|
|
$fileModel = new $model->fileModel;
|
551
|
|
|
$fileModel->name = $name;
|
552
|
|
|
$fileModel->parent = $model->id;
|
553
|
8 |
|
$fileModel->size = Yii::app()->format->formatSize($file->size);
|
554
|
|
|
}
|
555
|
|
|
|
556
|
|
|
if( !$fileModel->save() )
|
557
|
|
|
throw new CHttpException(500, 'Can`t save uploaded file');
|
558
|
|
|
}
|
559
|
|
|
else
|
560
|
|
|
throw new CHttpException(500, 'Can`t upload file');
|
561
|
|
|
}
|
562
|
|
|
}
|
563
|
3 |
|
}
|
564
|
|
|
|
565
|
|
|
/**
|
566
|
|
|
* Создание скрипта для редиректа после сабмита формы
|
567
|
|
|
* @return void
|
568
|
|
|
*/
|
569
|
|
|
protected function registerRedirectScript()
|
570
|
|
|
{
|
571
|
|
|
$script = 'setTimeout(function(){
|
572
|
|
|
location.href = "'.$this->redirectUrl.'";
|
573
|
|
|
}, '.$this->redirectDelay.');';
|
574
|
|
|
|
575
|
|
|
Yii::app()->getClientScript()->registerScript("redirectAfterSubmit", $script, CClientScript::POS_LOAD);
|
576
|
|
|
}
|
577
|
|
|
|
578
|
8 |
|
protected function renderLayoutViews($layout)
|
579
|
|
|
{
|
580
|
8 |
|
$replaceArray = array();
|
581
|
|
|
|
582
|
8 |
|
if( preg_match_all('/{view:(.+)}/', $layout, $matches) )
|
583
|
8 |
|
{
|
584
|
|
|
foreach($matches[0] as $key => $value)
|
585
|
|
|
$replaceArray[$value] = Yii::app()->controller->renderPartial($matches[1][$key], isset($this->layoutViewParams[$key]) ? $this->layoutViewParams[$key] : null, true);
|
586
|
|
|
}
|
587
|
|
|
|
588
|
8 |
|
return $replaceArray;
|
589
|
|
|
}
|
590
|
|
|
|
591
|
7 |
|
protected function modifySubmitButton($button)
|
592
|
|
|
{
|
593
|
7 |
|
if( !$this->ajaxSubmit || !isset($button->name) )
|
594
|
7 |
|
return $button;
|
595
|
|
|
|
596
|
7 |
|
if( !isset($button->attributes['ajax']) )
|
597
|
7 |
|
$button->attributes['ajax'] = array();
|
598
|
|
|
|
599
|
7 |
|
$button->attributes['ajax'] = CMap::mergeArray(array(
|
600
|
7 |
|
'type' => 'POST',
|
601
|
7 |
|
'dataType' => 'json',
|
602
|
|
|
'beforeSend' => 'function(){
|
603
|
7 |
|
//$("#'.$this->getActiveFormWidget()->id.'").data("settings").submitting = true;
|
604
|
|
|
$.mouseLoader(true);
|
605
|
7 |
|
}',
|
606
|
7 |
|
'url' => $this->action,
|
607
|
7 |
|
'success' => 'function(resp){checkResponse(resp, $("#'.$this->getActiveFormWidget()->id.'"))}',
|
608
|
7 |
|
'error' => 'function(resp){alert(resp.responseText)}',
|
609
|
7 |
|
), $button->attributes['ajax']);
|
610
|
|
|
|
611
|
7 |
|
$button->attributes['id'] = $this->getActiveFormWidget()->id.'_'.$button->name;
|
612
|
|
|
|
613
|
7 |
|
return $button;
|
614
|
|
|
}
|
615
|
|
|
|
616
|
8 |
|
protected function beforeRender()
|
617
|
|
|
{
|
618
|
8 |
|
$this->returnUrl = Yii::app()->request->getUrl();
|
619
|
8 |
|
$message = $this->getSuccessMessage();
|
620
|
|
|
|
621
|
8 |
|
if( !empty($message) )
|
622
|
8 |
|
return $message;
|
623
|
|
|
else
|
624
|
|
|
{
|
625
|
8 |
|
$errors = $this->getErrorMessage();
|
626
|
8 |
|
if( !empty($errors) )
|
627
|
8 |
|
{
|
628
|
1 |
|
$this->model->clearErrors();
|
629
|
1 |
|
$this->model->addErrors($errors);
|
630
|
1 |
|
}
|
631
|
|
|
}
|
632
|
|
|
|
633
|
8 |
|
if( $this->loadFromSession )
|
634
|
8 |
|
{
|
635
|
1 |
|
$this->loadFromSession();
|
636
|
1 |
|
}
|
637
|
|
|
|
638
|
8 |
|
if( $this->setUserData )
|
639
|
8 |
|
{
|
640
|
8 |
|
$this->setUserData();
|
641
|
8 |
|
}
|
642
|
8 |
|
}
|
643
|
|
|
|
644
|
|
|
/**
|
645
|
|
|
* @param FActiveRecord $model
|
646
|
|
|
*
|
647
|
|
|
* @return string
|
648
|
|
|
*/
|
649
|
2 |
|
private function getSessionKey($model)
|
650
|
|
|
{
|
651
|
2 |
|
return 'form_'.$this->formName.get_class($model);
|
652
|
|
|
}
|
653
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.