1+ <?php
2+ /**
3+ * @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
4+ * @link http://2amigos.us
5+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6+ */
7+ namespace dosamigos \grid ;
8+
9+
10+ use dosamigos \editable \EditableAddressAsset ;
11+ use dosamigos \editable \EditableBootstrapAsset ;
12+ use dosamigos \editable \EditableComboDateAsset ;
13+ use dosamigos \editable \EditableDatePickerAsset ;
14+ use dosamigos \editable \EditableDateTimePickerAsset ;
15+ use dosamigos \editable \EditableSelect2Asset ;
16+ use dosamigos \editable \EditableWysiHtml5Asset ;
17+ use yii \base \InvalidConfigException ;
18+ use yii \grid \DataColumn ;
19+ use yii \helpers \ArrayHelper ;
20+ use yii \helpers \Html ;
21+ use yii \helpers \Url ;
22+
23+ /**
24+ * EditableColumn adds X-Editable capabilities to a column
25+ *
26+ * @author Antonio Ramirez <[email protected] > 27+ * @link http://www.ramirezcobos.com/
28+ * @link http://www.2amigos.us/
29+ * @package common\extensions\grid
30+ */
31+ class EditableColumn extends DataColumn
32+ {
33+ /**
34+ * @var array the options for the X-editable.js plugin.
35+ * Please refer to the X-editable.js plugin web page for possible options.
36+ * @see http://vitalets.github.io/x-editable/docs.html#editable
37+ */
38+ public $ editableOptions = [];
39+ /**
40+ * @var string suffix substituted to a name class of the tag <a>
41+ */
42+ public $ classSuffix ;
43+ /**
44+ * @var string the url to post
45+ */
46+ public $ url ;
47+ /**
48+ * @var string the type of editor
49+ */
50+ public $ type = 'text ' ;
51+
52+ /**
53+ * @inheritdoc
54+ * @throws \yii\base\InvalidConfigException
55+ */
56+ public function init ()
57+ {
58+ if ($ this ->url === null ) {
59+ throw new InvalidConfigException ("'Url' property must be specified. " );
60+ }
61+
62+ parent ::init ();
63+
64+ $ this ->format = 'raw ' ;
65+
66+ $ rel = $ this ->attribute . '_editable ' . $ this ->classSuffix ;
67+ $ this ->options ['pjax ' ] = '0 ' ;
68+ $ this ->options ['rel ' ] = $ rel ;
69+
70+ $ this ->registerClientScript ();
71+ }
72+
73+ /**
74+ * @inheritdoc
75+ */
76+ protected function renderDataCellContent ($ model , $ key , $ index )
77+ {
78+
79+ $ value = parent ::renderDataCellContent ($ model , $ key , $ index );
80+
81+ foreach ($ this ->editableOptions as $ prop => $ v ) {
82+ $ this ->options ['data- ' . $ prop ] = $ v ;
83+ }
84+ $ url = (array )$ this ->url ;
85+ $ this ->options ['data-url ' ] = Url::to ($ url );
86+ $ this ->options ['data-pk ' ] = $ key ;
87+ $ this ->options ['data-name ' ] = $ this ->attribute ;
88+ $ this ->options ['data-type ' ] = $ this ->type ;
89+
90+ return Html::a ($ value , null , $ this ->options );
91+ }
92+
93+ /**
94+ * Registers required script to the columns work
95+ */
96+ protected function registerClientScript ()
97+ {
98+ $ view = $ this ->grid ->getView ();
99+ $ language = ArrayHelper::getValue ($ this ->editableOptions , 'language ' );
100+
101+ switch ($ this ->type ) {
102+ case 'address ' :
103+ EditableAddressAsset::register ($ view );
104+ break ;
105+ case 'combodate ' :
106+ EditableComboDateAsset::register ($ view );
107+ break ;
108+ case 'date ' :
109+ if ($ language ) {
110+ EditableDatePickerAsset::register (
111+ $ view
112+ )->js [] = 'vendor/js/locales/bootstrap-datetimepicker. ' . $ language . '.js ' ;
113+ } else {
114+ EditableDatePickerAsset::register ($ view );
115+ }
116+ break ;
117+ case 'datetime ' :
118+ if ($ language ) {
119+ EditableDateTimePickerAsset::register (
120+ $ view
121+ )->js [] = 'vendor/js/locales/bootstrap-datetimepicker. ' . $ language . '.js ' ;
122+ } else {
123+ EditableDateTimePickerAsset::register ($ view );
124+ }
125+ break ;
126+ case 'select2 ' :
127+ EditableSelect2Asset::register ($ view );
128+ break ;
129+ case 'wysihtml5 ' :
130+ $ language = $ language ? : 'en-US ' ;
131+ EditableWysiHtml5Asset::register (
132+ $ view
133+ )->js [] = 'vendor/locales/bootstrap-wysihtml5. ' . $ language . '.js ' ;
134+ break ;
135+ default :
136+ EditableBootstrapAsset::register ($ view );
137+ }
138+
139+ EditableColumnAsset::register ($ view );
140+ $ rel = $ this ->options ['rel ' ];
141+ $ selector = "a[rel= \"$ rel \"] " ;
142+ $ grid = "# {$ this ->grid ->id }" ;
143+ $ js [] = ";jQuery(' $ selector').editable(); " ;
144+ $ js [] = "yii.editableColumn.registerHandler(' $ grid', ' $ selector'); " ;
145+ $ view ->registerJs (implode ("\n" , $ js ));
146+ }
147+ }
0 commit comments