HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbEditableDetailView.php
Go to the documentation of this file.
1 <?php
11 Yii::import('booster.widgets.TbEditableField');
12 Yii::import('zii.widgets.CDetailView');
13 
19 class TbEditableDetailView extends CDetailView
20 {
24  /*
25  commented due to using magic methods and setting any of default TbEditableField param
26  from top level config of TbEditableDetailView
27  */
28  //public $url = null;
29 
33  /*
34  commented due to using magic methods and setting any of default TbEditableField param
35  from top level config of TbEditableDetailView
36  */
37  //public $params = null;
38 
39  public function init()
40  {
41  if (!$this->data instanceof CModel) {
42  throw new CException('Property "data" should be of CModel class.');
43  }
44 
45  //set bootstrap css
46  /* TODO if(yii::app()->editable->form === 'bootstrap') { */
47  $this->htmlOptions = array('class'=> 'table table-bordered table-striped table-hover');
48  //disable loading Yii's css for bootstrap
49  $this->cssFile = false;
50  // }
51 
52  parent::init();
53  }
54 
55  protected function renderItem($options, $templateData)
56  {
57  //apply editable if not set 'editable' params or set and not false
58  $apply = !empty($options['name']) && (!isset($options['editable']) || $options['editable'] !== false);
59 
60  if ($apply) {
61  //ensure $options['editable'] is array
62  if(!isset($options['editable'])) $options['editable'] = array();
63 
64  //merge options with defaults: url, params, etc.
65  $options['editable'] = CMap::mergeArray($this->_data, $options['editable']);
66 
67  //options to be passed into TbEditableField (constructed from $options['editable'])
68  $widgetOptions = array(
69  'model' => $this->data,
70  'attribute' => $options['name']
71  );
72 
73  //if value in detailview options provided, set text directly (as value here means text)
74  if(isset($options['value']) && $options['value'] !== null) {
75  $widgetOptions['text'] = $templateData['{value}'];
76  $widgetOptions['encode'] = false;
77  }
78 
79  $widgetOptions = CMap::mergeArray($widgetOptions, $options['editable']);
80 
81  $widget = $this->controller->createWidget('TbEditableField', $widgetOptions);
82 
83  //'apply' maybe changed during init of widget (e.g. if related model has unsafe attribute)
84  if($widget->apply) {
85  ob_start();
86  $widget->run();
87  $templateData['{value}'] = ob_get_clean();
88  }
89  }
90 
91  parent::renderItem($options, $templateData);
92  }
93 
94  //***************************************************************************************
95  // Generic getter/setter implementation to accept default configuration for TbEditableField
96  //***************************************************************************************
97 
99  private $_data = array();
102 
108  private function getEditableProperties() {
109  if(!isset($this->_editableProperties)) {
110  $reflection = new ReflectionClass('TbEditableField');
111  $this->_editableProperties = array_map(function($d){return $d->getName();},$reflection->getProperties());
112  }
114  }
115 
120  public function __get($key) {
121  return (array_key_exists($key,$this->_data) ? $this->_data[$key] : parent::__get($key));
122  }
123 
128  public function __set($key, $value) {
129  if(in_array($key,$this->getEditableProperties())) {
130  $this->_data[$key] = $value;
131  } else {
132  parent::__set($key,$value);
133  }
134  }
135 
140  public function __isset($name) {
141  return array_key_exists($name,$this->_data)||parent::__isset($name);
142  }
143 }