forked from vitalets/yii-bootstrap-editable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditableDetailView.php
70 lines (55 loc) · 2.42 KB
/
EditableDetailView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* EditableDetailView class file.
*
* This widget makes editable several attributes of single model, shown as name-value table
*
* @author Vitaliy Potapov <noginsk@rambler.ru>
* @link https://github.com/vitalets/yii-bootstrap-editable
* @copyright Copyright © Vitaliy Potapov 2012
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version 1.0.0
*/
Yii::import('ext.editable.EditableField');
Yii::import('zii.widgets.CDetailView');
class EditableDetailView extends CDetailView
{
//common url for all editables
public $url = '';
//set bootstrap css
public $htmlOptions = array('class'=> 'table table-bordered table-striped table-hover table-condensed');
public function init()
{
if (!$this->data instanceof CModel) {
throw new CException('Property "data" should be of CModel class.');
}
parent::init();
}
protected function renderItem($options, $templateData)
{
//if editable set to false --> not editable
$isEditable = array_key_exists('editable', $options) && $options['editable'] !== false;
//if name not defined or it is not safe --> not editable
$isEditable = !empty($options['name']) && $this->data->isAttributeSafe($options['name']);
if ($isEditable) {
//ensure $options['editable'] is array
if(!array_key_exists('editable', $options) || !is_array($options['editable'])) $options['editable'] = array();
//take common url
if (!array_key_exists('url', $options['editable'])) {
$options['editable']['url'] = $this->url;
}
$editableOptions = CMap::mergeArray($options['editable'], array(
'model' => $this->data,
'attribute' => $options['name'],
'emptytext' => ($this->nullDisplay === null) ? Yii::t('zii', 'Not set') : strip_tags($this->nullDisplay),
));
//if value in detailview options provided, set text directly
if(array_key_exists('value', $options) && $options['value'] !== null) {
$editableOptions['text'] = $templateData['{value}'];
$editableOptions['encode'] = false;
}
$templateData['{value}'] = $this->controller->widget('EditableField', $editableOptions, true);
}
parent::renderItem($options, $templateData);
}
}