forked from vitalets/x-editable-yii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditableDetailView.php
90 lines (73 loc) · 3.03 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* EditableDetailView class file.
*
* @author Vitaliy Potapov <[email protected]>
* @link https://github.com/vitalets/x-editable-yii
* @copyright Copyright © Vitaliy Potapov 2012
* @version 1.0.0
*/
Yii::import('editable.EditableField');
Yii::import('zii.widgets.CDetailView');
/**
* EditableDetailView widget makes editable CDetailView (several attributes of single model shown as name-value table).
*
* @package widgets
*/
class EditableDetailView extends CDetailView
{
/**
* @var string submit url for all editables in detailview
*/
public $url = null;
/**
* @var array additional params to send on server
*/
public $params = null;
public function init()
{
if (!$this->data instanceof CModel) {
throw new CException('Property "data" should be of CModel class.');
}
//set bootstrap css
if(yii::app()->editable->form === EditableConfig::FORM_BOOTSTRAP) {
$this->htmlOptions = array('class'=> 'table table-bordered table-striped table-hover');
}
parent::init();
}
protected function renderItem($options, $templateData)
{
//apply editable if not set 'editable' params or set and not false
$apply = !empty($options['name']) && (!isset($options['editable']) || $options['editable'] !== false);
if ($apply) {
//ensure $options['editable'] is array
if(!isset($options['editable'])) $options['editable'] = array();
//take common url if not defined for particular item and not related model
if (!isset($options['editable']['url']) && strpos($options['name'], '.') === false) {
$options['editable']['url'] = $this->url;
}
//take common params if not defined for particular item
if (!isset($options['editable']['params'])) {
$options['editable']['params'] = $this->params;
}
$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 (as value means text)
if(isset($options['value']) && $options['value'] !== null) {
$editableOptions['text'] = $templateData['{value}'];
$editableOptions['encode'] = false;
}
$widget = $this->controller->createWidget('EditableField', $editableOptions);
//'apply' can be changed during init of widget (e.g. if related model and unsafe attribute)
if($widget->apply) {
ob_start();
$widget->run();
$templateData['{value}'] = ob_get_clean();
}
}
parent::renderItem($options, $templateData);
}
}