1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Nikita Melnikov <[email protected]>
|
4
|
|
|
* @author Vladimir Utenkov <[email protected]>
|
5
|
|
|
* @link https://github.com/shogodev/argilla/
|
6
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
7
|
|
|
* @license http://argilla.ru/LICENSE
|
8
|
|
|
*/
|
9
|
|
|
/**
|
10
|
|
|
* Класс для работы с полями из контроллера необходимо добавить в метод CController::actions()
|
11
|
|
|
*/
|
12
|
|
|
class OnFlyEditAction extends CAction
|
13
|
|
|
{
|
14
|
|
|
/**
|
15
|
|
|
* Объект модели
|
16
|
|
|
*
|
17
|
|
|
* @var BActiveRecord
|
18
|
|
|
*/
|
19
|
|
|
protected $model;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* PK записи в базе
|
23
|
|
|
*
|
24
|
|
|
* @var int
|
25
|
|
|
*/
|
26
|
|
|
protected $id;
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* Название поля в базе / свойства модели
|
30
|
|
|
*
|
31
|
|
|
* @var string
|
32
|
|
|
*/
|
33
|
|
|
protected $field;
|
34
|
|
|
|
35
|
|
|
/**
|
36
|
|
|
* Новое значение для свойства модели
|
37
|
|
|
*
|
38
|
|
|
* @var string
|
39
|
|
|
*/
|
40
|
|
|
protected $value;
|
41
|
|
|
|
42
|
|
|
public function run()
|
43
|
|
|
{
|
44
|
|
|
$request = Yii::app()->request;
|
45
|
|
|
$id = $request->getPost('id');
|
46
|
|
|
$field = $request->getPost('field');
|
47
|
|
|
$value = $request->getPost('value');
|
48
|
|
|
$gridId = Yii::app()->request->getPost('gridId');
|
49
|
|
|
|
50
|
|
|
if( !empty($id) && !empty($field) && isset($value) )
|
51
|
|
|
{
|
52
|
|
|
$this->init($id, $field, $value, $gridId)->process();
|
53
|
|
|
}
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* Присваивание нового значения для выбранной модели, с заданным полем и ID
|
58
|
|
|
*
|
59
|
|
|
* @throws CHttpException Бросается в случае ошибки при сохранении модели.
|
60
|
|
|
*/
|
61
|
|
|
protected function process()
|
62
|
|
|
{
|
63
|
|
|
$field = $this->field;
|
64
|
|
|
|
65
|
|
|
$this->model->setAttributes(array($field => $this->value));
|
66
|
|
|
|
67
|
|
|
if( $this->model->save() )
|
68
|
|
|
{
|
69
|
|
|
if( Yii::app()->request->isAjaxRequest )
|
70
|
|
|
{
|
71
|
|
|
echo $this->model->$field;
|
72
|
|
|
}
|
73
|
|
|
}
|
74
|
|
|
else
|
75
|
|
|
{
|
76
|
|
|
throw new CHttpException(500, implode("; ", $this->model->getErrors($field)));
|
77
|
|
|
}
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* Инициализация свойств
|
82
|
|
|
*
|
83
|
|
|
* @param $id
|
84
|
|
|
* @param $field
|
85
|
|
|
* @param $value
|
86
|
|
|
* @param $gridId
|
87
|
|
|
*
|
88
|
|
|
* @return OnFlyEditAction
|
89
|
|
|
*/
|
90
|
|
|
protected function init($id, $field, $value, $gridId)
|
91
|
|
|
{
|
92
|
|
|
$this->id = $id;
|
93
|
|
|
$this->field = $field;
|
94
|
|
|
$this->value = $value;
|
95
|
|
|
|
96
|
|
|
if( !empty($gridId) )
|
97
|
|
|
{
|
98
|
|
|
$this->model = $this->parseGridId($gridId)->findByPk($this->id);
|
99
|
|
|
}
|
100
|
|
|
else
|
101
|
|
|
$this->model = $this->controller->loadModel($this->id);
|
102
|
|
|
|
103
|
|
|
return $this;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
/**
|
107
|
|
|
* @param $gridId
|
108
|
|
|
*
|
109
|
|
|
* @return BActiveRecord
|
110
|
|
|
*/
|
111
|
1 |
|
protected function parseGridId($gridId)
|
112
|
|
|
{
|
113
|
1 |
|
$model = null;
|
|
|
|
|
114
|
|
|
|
115
|
1 |
|
if( preg_match("/(\w+)_(\w+)-(\w+)$/U", $gridId, $matches) )
|
116
|
1 |
|
{
|
117
|
1 |
|
$model = $matches[1];
|
|
|
|
|
118
|
1 |
|
$table = $matches[2];
|
119
|
1 |
|
$type = $matches[3];
|
120
|
|
|
|
121
|
1 |
|
if( $type === 'files' && isset(Yii::app()->db->schema->tables[Yii::app()->db->tablePrefix.$table]) )
|
122
|
1 |
|
{
|
123
|
1 |
|
$model = new UploadModel($table);
|
124
|
1 |
|
}
|
125
|
1 |
|
}
|
126
|
|
|
else
|
127
|
|
|
{
|
128
|
1 |
|
$model = preg_replace("/-.*/", "", $gridId);
|
129
|
1 |
|
$model = new $model;
|
130
|
|
|
}
|
131
|
|
|
|
132
|
1 |
|
return $model;
|
133
|
|
|
}
|
134
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.