1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
|
9
|
|
|
/**
|
10
|
|
|
* Class RowGridWidget
|
11
|
|
|
*/
|
12
|
|
|
abstract class RowGridWidget extends CWidget
|
13
|
|
|
{
|
14
|
|
|
public $modelName;
|
15
|
|
|
|
16
|
|
|
public $modelId;
|
17
|
|
|
|
18
|
|
|
public $modelAttribute;
|
19
|
|
|
|
20
|
|
|
public $urlRoute;
|
21
|
|
|
|
22
|
|
|
public $header = 'Заголовок';
|
23
|
|
|
|
24
|
|
|
public $templateButtonColumn = '{update} {delete}';
|
25
|
|
|
|
26
|
|
|
public $urlCreate;
|
27
|
|
|
|
28
|
|
|
public $urlEdit;
|
29
|
|
|
|
30
|
|
|
public $urlDelete;
|
31
|
|
|
|
32
|
|
|
private $updateSelectors = array();
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @return array
|
36
|
|
|
*/
|
37
|
|
|
abstract protected function getColumns();
|
38
|
|
|
|
39
|
|
|
public function init()
|
40
|
|
|
{
|
41
|
|
|
if( is_null($this->modelName) )
|
42
|
|
|
throw new CHttpException(500, 'Укажите свойство modelName для виджета '.__CLASS__);
|
43
|
|
|
|
44
|
|
|
if( is_null($this->modelAttribute) )
|
45
|
|
|
throw new CHttpException(500, 'Укажите свойство modelAttribute для виджета '.__CLASS__);
|
46
|
|
|
|
47
|
|
|
if( is_null($this->urlRoute) && (is_null($this->urlCreate) || is_null($this->urlEdit) || is_null($this->urlDelete)) )
|
48
|
|
|
throw new CHttpException(500, 'Укажите свойство urlRoute для виджета '.__CLASS__);
|
49
|
|
|
else
|
50
|
|
|
{
|
51
|
|
|
if( is_null($this->urlCreate) )
|
52
|
|
|
$this->urlCreate = $this->controller->createUrl($this->urlRoute.'/create', array('model_id' => $this->modelId, 'popup' => true));
|
53
|
|
|
|
54
|
|
|
if( is_null($this->urlEdit) )
|
55
|
|
|
$this->urlEdit = 'Yii::app()->controller->createUrl("'.$this->urlRoute.'/update", array("id" => $data->primaryKey, "popup" => true))';
|
56
|
|
|
|
57
|
|
|
if( is_null($this->urlDelete) )
|
58
|
|
|
$this->urlDelete = 'Yii::app()->controller->createUrl("'.$this->urlRoute.'/delete", array("id" => $data->primaryKey))';
|
59
|
|
|
}
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
View Code Duplication |
public function run()
|
|
|
|
|
63
|
|
|
{
|
64
|
|
|
if( !$this->isAvailable() )
|
65
|
|
|
return;
|
66
|
|
|
|
67
|
|
|
echo '<tr><th><label>'.$this->header.'</label></th><td>';
|
68
|
|
|
$this->renderButton();
|
69
|
|
|
$this->renderGrid();
|
70
|
|
|
echo '</td></tr>';
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* @return BActiveRecord $model
|
75
|
|
|
*/
|
76
|
|
|
protected function getModel()
|
77
|
|
|
{
|
78
|
|
|
/**
|
79
|
|
|
* @var BActiveRecord $model
|
80
|
|
|
*/
|
81
|
|
|
$modelName = $this->modelName;
|
82
|
|
|
$model = $modelName::model();
|
83
|
|
|
$model->setAttribute($this->modelAttribute, $this->modelId);
|
84
|
|
|
|
85
|
|
|
return $model;
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
private function renderButton()
|
89
|
|
|
{
|
90
|
|
|
$buttonId = $this->getId().'_add_button';
|
91
|
|
|
|
92
|
|
|
echo CHtml::openTag('div', array('class' => 's-buttons s-buttons-top'));
|
93
|
|
|
$this->widget('BButton', array(
|
94
|
|
|
'htmlOptions' => array('id' => $buttonId),
|
95
|
|
|
'label' => 'Добавить',
|
96
|
|
|
'url' => $this->urlCreate,
|
97
|
|
|
'type' => 'info',
|
98
|
|
|
'popupDepended' => true,
|
99
|
|
|
));
|
100
|
|
|
echo CHtml::closeTag('div');
|
101
|
|
|
|
102
|
|
|
$this->addUpdateSelector('#'.$buttonId);
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
private function renderGrid()
|
106
|
|
|
{
|
107
|
|
|
$this->controller->widget('BGridView', array(
|
108
|
|
|
'id' => $this->getId(),
|
109
|
|
|
'dataProvider' => $this->getModel()->search(),
|
110
|
|
|
'template' => "{items}\n{pager}\n{scripts}",
|
111
|
|
|
'filter' => $this->getModel(),
|
112
|
|
|
'columns' => CMap::mergeArray($this->getColumns(), $this->getColumnButtons())
|
113
|
|
|
));
|
114
|
|
|
|
115
|
|
|
$this->registerUpdateScript();
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
/**
|
119
|
|
|
* @return array
|
120
|
|
|
*/
|
121
|
|
|
protected function getColumnButtons()
|
122
|
|
|
{
|
123
|
|
|
$updateButtonClass = $this->getId().'_update_button';
|
124
|
|
|
$this->addUpdateSelector('.'.$updateButtonClass);
|
125
|
|
|
|
126
|
|
|
return array(
|
127
|
|
|
array(
|
128
|
|
|
'class' => 'BButtonColumn',
|
129
|
|
|
'template' => $this->templateButtonColumn,
|
130
|
|
|
'updateButtonUrl' => $this->urlEdit,
|
131
|
|
|
'updateButtonOptions' => array('class' => 'update '.$updateButtonClass),
|
132
|
|
|
'deleteButtonUrl' => $this->urlDelete
|
133
|
|
|
)
|
134
|
|
|
);
|
135
|
|
|
}
|
136
|
|
|
|
137
|
|
|
private function registerUpdateScript()
|
138
|
|
|
{
|
139
|
|
|
Yii::app()->clientScript->registerScript($this->getId().'_grid_update_script', "
|
140
|
|
|
jQuery(document).on('click', '".implode(', ', $this->updateSelectors)."', function(e) {
|
141
|
|
|
e.preventDefault();
|
142
|
|
|
assigner.open(this.href, {'updateGridId' : '{$this->getId()}'});
|
143
|
|
|
});
|
144
|
|
|
");
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
private function addUpdateSelector($selector)
|
148
|
|
|
{
|
149
|
|
|
$this->updateSelectors[$selector] = $selector;
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
private function isAvailable()
|
153
|
|
|
{
|
154
|
|
|
return !$this->controller->popup && $this->controller->isUpdate();
|
155
|
|
|
}
|
156
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.