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
|
1 |
|
Yii::import('zii.widgets.grid.CGridColumn');
|
10
|
|
|
|
11
|
|
|
/**
|
12
|
|
|
* Класс для получение возможности редактировать поле в CGridView с помощью ajax
|
13
|
|
|
*
|
14
|
|
|
* Класс должен инициализироваться вместе CGridView
|
15
|
|
|
* @example
|
16
|
|
|
* <code>
|
17
|
|
|
*
|
18
|
|
|
* $onFlyEdit = array(
|
19
|
|
|
* 'name' => 'notice',
|
20
|
|
|
* 'htmlOptions' => array(),
|
21
|
|
|
* 'class' => 'OnFlyEditField',
|
22
|
|
|
* 'header' => 'FieldHeader',
|
23
|
|
|
* );
|
24
|
|
|
*
|
25
|
|
|
* $this->widget('bootstrap.widgets.TbGridView', array(
|
26
|
|
|
* 'columns' => array($onFlyEdit,))
|
27
|
|
|
* );
|
28
|
|
|
* </code>
|
29
|
|
|
*/
|
30
|
|
|
class OnFlyEditField extends BDataColumn
|
31
|
|
|
{
|
32
|
|
|
/**
|
33
|
|
|
* Имя свойства / поля в базе, используемое для вывода
|
34
|
|
|
*
|
35
|
|
|
* @var string
|
36
|
|
|
*/
|
37
|
|
|
public $name;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* Значение свойства
|
41
|
|
|
*
|
42
|
|
|
* @var string
|
43
|
|
|
*/
|
44
|
|
|
public $value;
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* id грида
|
48
|
|
|
*
|
49
|
|
|
* @var string
|
50
|
|
|
*/
|
51
|
|
|
public $gridId;
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* Массив значений для вывода поля в виде выпадающего списка
|
55
|
|
|
*
|
56
|
|
|
* @var array|null
|
57
|
|
|
*/
|
58
|
|
|
public $dropDown = null;
|
59
|
|
|
|
60
|
|
|
public $htmlOptions = array('class' => 'span1');
|
61
|
|
|
|
62
|
|
|
public $elementOptions = array();
|
63
|
|
|
|
64
|
|
|
public $action = 'onflyedit';
|
65
|
|
|
|
66
|
|
|
public $gridUpdate = false;
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* URL для AJAX запроса.
|
70
|
|
|
*
|
71
|
|
|
* @var string
|
72
|
|
|
*/
|
73
|
|
|
public $ajaxUrl;
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* Инициализация вывода свойства в столбец
|
77
|
|
|
*
|
78
|
|
|
* @override
|
79
|
|
|
*
|
80
|
|
|
* @return void
|
81
|
|
|
*/
|
82
|
1 |
|
public function init()
|
83
|
|
|
{
|
84
|
1 |
|
parent::init();
|
85
|
|
|
|
86
|
1 |
|
if( empty($this->ajaxUrl) )
|
87
|
1 |
|
$this->ajaxUrl = Yii::app()->controller->createUrl(Yii::app()->controller->id."/".$this->action);
|
88
|
|
|
|
89
|
1 |
|
OnFlyWidget::registerOnFlyScripts();
|
90
|
|
|
|
91
|
1 |
|
Yii::app()->clientScript->registerScript(
|
92
|
1 |
|
'initOnFly'.$this->grid->id,
|
93
|
|
|
'$(function() {
|
94
|
|
|
Backend("onFly", function(box) {
|
95
|
|
|
box.init(jQuery);
|
96
|
1 |
|
jQuery.fn.yiiGridView.addObserver("'.$this->grid->id.'", function(id) { box.reinstall(jQuery); });
|
97
|
|
|
});
|
98
|
1 |
|
});',
|
99
|
|
|
CClientScript::POS_END
|
100
|
1 |
|
);
|
101
|
1 |
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* Вывод значения свойтва в клетку
|
105
|
|
|
*
|
106
|
|
|
* @override
|
107
|
|
|
*
|
108
|
|
|
* @param int $row
|
109
|
|
|
* @param BActiveRecord|array $data
|
110
|
|
|
*
|
111
|
|
|
* @return void
|
112
|
|
|
*/
|
113
|
|
|
protected function renderDataCellContent($row, $data)
|
114
|
|
|
{
|
115
|
|
|
$htmlOptions = CMap::mergeArray(
|
116
|
|
|
array(
|
117
|
|
|
'data-grid-id' => $this->gridId,
|
118
|
|
|
'data-grid-update' => $this->gridUpdate
|
119
|
|
|
),
|
120
|
|
|
$this->elementOptions
|
121
|
|
|
);
|
122
|
|
|
|
123
|
|
|
Yii::app()->controller->widget('OnFlyWidget', array(
|
124
|
|
|
'type' => empty($this->dropDown) ? OnFlyWidget::TYPE_INPUT : OnFlyWidget::TYPE_DROPDOWN,
|
125
|
|
|
'ajaxUrl' => $this->ajaxUrl,
|
126
|
|
|
'attribute' => $this->name,
|
127
|
|
|
'primaryKey' => $this->getPrimaryKey($data),
|
128
|
|
|
'value' => $data[$this->name],
|
129
|
|
|
'items' => $this->dropDown,
|
130
|
|
|
'htmlOptions' => $htmlOptions
|
131
|
|
|
));
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
/**
|
135
|
|
|
* @param BActiveRecord|array $data
|
136
|
|
|
*
|
137
|
|
|
* @return mixed
|
138
|
|
|
*/
|
139
|
|
|
protected function getPrimaryKey($data)
|
140
|
|
|
{
|
141
|
|
|
return $data instanceof BActiveRecord ? $data->getPrimaryKey() : $data['id'];
|
142
|
|
|
}
|
143
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.