GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TbEditableSaver   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 256
ccs 0
cts 94
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
C update() 0 72 7
A error() 0 4 1
A setAttribute() 0 7 2
A onBeforeUpdate() 0 4 1
A onAfterUpdate() 0 4 1
A beforeUpdate() 0 4 1
A afterUpdate() 0 4 1
A checkErrors() 0 12 3
1
<?php
2
/**
3
 *## EditableSaver class file.
4
 *
5
 * @author Vitaliy Potapov <[email protected]>
6
 * @link https://github.com/vitalets/x-editable-yii
7
 * @copyright Copyright &copy; Vitaliy Potapov 2012
8
 * @version 1.1.0
9
 */
10
11
/**
12
 * EditableSaver helps to update model by editable widget submit request.
13
 *
14
 * @package booster.widgets.supplementary
15
*/
16
class TbEditableSaver extends CComponent
17
{
18
	/**
19
	 * scenarion used in model for update
20
	 *
21
	 * @var mixed
22
	 */
23
	public $scenario = 'editable';
24
25
	/**
26
	 * name of model
27
	 *
28
	 * @var mixed
29
	 */
30
	public $modelClass;
31
	/**
32
	 * primaryKey value
33
	 *
34
	 * @var mixed
35
	 */
36
	public $primaryKey;
37
	/**
38
	 * name of attribute to be updated
39
	 *
40
	 * @var mixed
41
	 */
42
	public $attribute;
43
	/**
44
	 * model instance
45
	 *
46
	 * @var CActiveRecord
47
	 */
48
	public $model;
49
50
	/**
51
	 * @var mixed new value of attribute
52
	 */
53
	public $value;
54
55
	/**
56
	 * http status code returned in case of error
57
	 */
58
	public $errorHttpCode = 400;
59
60
	/**
61
	 * name of changed attributes. Used when saving model
62
	 *
63
	 * @var mixed
64
	 */
65
	protected $changedAttributes = array();
66
67
	/**
68
	 *### ._construct()
69
	 *
70
	 * Constructor
71
	 *
72
	 * @param $modelClass
73
	 *
74
	 * @throws CException
75
	 * @internal param mixed $modelName
76
	 * @return \TbEditableSaver
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
77
	 */
78
	public function __construct($modelClass)
79
	{
80
		if (empty($modelClass)) {
81
			throw new CException(Yii::t(
82
				'TbEditableSaver.editable',
83
				'You should provide modelClass in constructor of EditableSaver.'
84
			));
85
		}
86
87
		$this->modelClass = $modelClass;
88
89
		//for non-namespaced models do ucfirst (for backwards compability)
90
		//see https://github.com/vitalets/x-editable-yii/issues/9
91
		if (strpos($this->modelClass, '\\') === false) {
92
			$this->modelClass = ucfirst($this->modelClass);
93
		}
94
	}
95
96
	/**
97
	 *### .update()
98
	 *
99
	 * main function called to update column in database
100
	 *
101
	 */
102
	public function update()
103
	{
104
		//get params from request
105
		$this->primaryKey = yii::app()->request->getParam('pk');
106
		$this->attribute = yii::app()->request->getParam('name');
107
		$this->value = yii::app()->request->getParam('value');
108
109
		//checking params
110
		if (empty($this->attribute)) {
111
			throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
112
		}
113
		if (empty($this->primaryKey)) {
114
			throw new CException(Yii::t('TbEditableSaver.editable', 'Property "primaryKey" should be defined.'));
115
		}
116
117
		//loading model
118
		$this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
119
		if (!$this->model) {
120
			throw new CException(Yii::t(
121
				'TbEditableSaver.editable',
122
				'Model {class} not found by primary key "{pk}"',
123
				array(
124
					'{class}' => get_class($this->model),
125
					'{pk}' => is_array($this->primaryKey) ? CJSON::encode($this->primaryKey) : $this->primaryKey
126
				)
127
			));
128
		}
129
130
		//set scenario
131
		$this->model->setScenario($this->scenario);
132
133
		//commented to be able to work with virtual attributes
134
		//see https://github.com/vitalets/yii-bootstrap-editable/issues/15
135
		/*
136
		//is attribute exists
137
		if (!$this->model->hasAttribute($this->attribute)) {
138
			throw new CException(Yii::t('EditableSaver.editable', 'Model {class} does not have attribute "{attr}"', array(
139
			  '{class}'=>get_class($this->model), '{attr}'=>$this->attribute)));
140
		}
141
		*/
142
143
		//is attribute safe
144
		if (!$this->model->isAttributeSafe($this->attribute)) {
145
			throw new CException(Yii::t(
146
				'editable',
147
				'Model {class} rules do not allow to update attribute "{attr}"',
148
				array(
149
					'{class}' => get_class($this->model),
150
					'{attr}' => $this->attribute
151
				)
152
			));
153
		}
154
155
		//setting new value
156
		$this->setAttribute($this->attribute, $this->value);
157
158
		//validate attribute
159
		$this->model->validate(array($this->attribute));
160
		$this->checkErrors();
161
162
		//trigger beforeUpdate event
163
		$this->beforeUpdate();
164
		$this->checkErrors();
165
166
		//saving (no validation, only changed attributes)
167
		if ($this->model->save(false, $this->changedAttributes)) {
168
			//trigger afterUpdate event
169
			$this->afterUpdate();
170
		} else {
171
			$this->error(Yii::t('TbEditableSaver.editable', 'Error while saving record!'));
172
		}
173
	}
174
175
	/**
176
	 *### .checkErros()
177
	 *
178
	 * errors as CHttpException
179
	 * @internal param $msg
180
	 * @throws CHttpException
181
	 */
182
	public function checkErrors()
183
	{
184
		if (!$this->model->hasErrors())
185
			return;
186
187
		$msg = array();
188
		foreach ($this->model->getErrors() as $attribute => $errors) {
189
			// TODO: make use of $attribute elements
190
			$msg = array_merge($msg, $errors);
191
		}
192
		$this->error($msg[0]);
193
	}
194
195
	/**
196
	 *### .error()
197
	 *
198
	 * errors as CHttpException
199
	 *
200
	 * @param $msg
201
	 *
202
	 * @throws CHttpException
203
	 */
204
	public function error($msg)
205
	{
206
		throw new CHttpException($this->errorHttpCode, $msg);
207
	}
208
209
	/**
210
	 *### .setAttribute()
211
	 *
212
	 * setting new value of attribute.
213
	 * Attrubute name also stored in array to save only changed attributes
214
	 *
215
	 * @param mixed $name
216
	 * @param mixed $value
217
	 */
218
	public function setAttribute($name, $value)
219
	{
220
		$this->model->$name = $value;
221
		if (!in_array($name, $this->changedAttributes)) {
222
			$this->changedAttributes[] = $name;
223
		}
224
	}
225
226
	/**
227
	 *### .onBeforeUpdate()
228
	 *
229
	 * This event is raised before the update is performed.
230
	 *
231
	 * @param CModelEvent $event the event parameter
232
	 */
233
	public function onBeforeUpdate($event)
234
	{
235
		$this->raiseEvent('onBeforeUpdate', $event);
236
	}
237
238
	/**
239
	 *### .onAfterUpdate()
240
	 *
241
	 * This event is raised after the update is performed.
242
	 *
243
	 * @param CModelEvent $event the event parameter
244
	 */
245
	public function onAfterUpdate($event)
246
	{
247
		$this->raiseEvent('onAfterUpdate', $event);
248
	}
249
250
	/**
251
	 *### .beforeUpdate()
252
	 *
253
	 * beforeUpdate
254
	 *
255
	 */
256
	protected function beforeUpdate()
257
	{
258
		$this->onBeforeUpdate(new CModelEvent($this));
259
	}
260
261
	/**
262
	 *### .afterUpdate()
263
	 *
264
	 * afterUpdate
265
	 *
266
	 */
267
	protected function afterUpdate()
268
	{
269
		$this->onAfterUpdate(new CModelEvent($this));
270
	}
271
}
272