YiiWheels
  • Package
  • Class
  • Tree

Packages

  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • editable
      • fileupload
      • fileuploader
      • formhelpers
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhEditable
  • WhEditableColumn
  • WhEditableDetailView
  • WhEditableField
  • WhEditableSaver
  1 <?php
  2 /**
  3  * WhEditableColumn class
  4  *
  5  * Makes editable CDetailView (several attributes of single model shown as name-value table).
  6  *
  7  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  8  * @copyright Copyright &copy; 2amigos.us 2013-
  9  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 10  * @package YiiWheels.widgets.editable
 11  *
 12  *
 13  * @author Vitaliy Potapov <noginsk@rambler.ru>
 14  * @link https://github.com/vitalets/x-editable-yii
 15  * @copyright Copyright &copy; Vitaliy Potapov 2012
 16  * @version 1.3.1
 17  */
 18 
 19 Yii::import('yiiwheels.widgets.editable.WhEditableField');
 20 Yii::import('zii.widgets.CDetailView');
 21 
 22 class WhEditableDetailView extends CDetailView
 23 {
 24 
 25     /**
 26      * @var array  Data for default fields of WhEditableField
 27      */
 28     private $_data = array();
 29 
 30     /**
 31      * @var Valid attributes for WhEditableField (singleton)
 32      */
 33     private $_editableProperties;
 34 
 35 
 36     /**
 37      * Initializes the widget
 38      * @throws CException
 39      */
 40     public function init()
 41     {
 42         if (!$this->data instanceof CModel) {
 43             throw new CException('Property "data" should be of CModel class.');
 44         }
 45 
 46 
 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     /**
 56      * Renders an item
 57      * @param array $options
 58      * @param string $templateData
 59      */
 60     protected function renderItem($options, $templateData)
 61     {
 62         //apply editable if not set 'editable' params or set and not false
 63         $apply = !empty($options['name']) && (!isset($options['editable']) || $options['editable'] !== false);
 64 
 65         if ($apply) {
 66             //ensure $options['editable'] is array
 67             if (!isset($options['editable'])) $options['editable'] = array();
 68 
 69             //merge options with defaults: url, params, etc.
 70             $options['editable'] = CMap::mergeArray($this->_data, $options['editable']);
 71 
 72             //options to be passed into EditableField (constructed from $options['editable'])
 73             $widgetOptions = array(
 74                 'model' => $this->data,
 75                 'attribute' => $options['name']
 76             );
 77 
 78             //if value in detailview options provided, set text directly (as value here means text)
 79             if (isset($options['value']) && $options['value'] !== null) {
 80                 $widgetOptions['text'] = $templateData['{value}'];
 81                 $widgetOptions['encode'] = false;
 82             }
 83 
 84             $widgetOptions = CMap::mergeArray($widgetOptions, $options['editable']);
 85 
 86             $widget = $this->controller->createWidget('WhEditableField', $widgetOptions);
 87 
 88             //'apply' maybe changed during init of widget (e.g. if related model has unsafe attribute)
 89             if ($widget->apply) {
 90                 ob_start();
 91                 $widget->run();
 92                 $templateData['{value}'] = ob_get_clean();
 93             }
 94         }
 95 
 96         parent::renderItem($options, $templateData);
 97     }
 98 
 99     /**
100      * Get the properties available for {@link EditableField}.
101      * These properties can also be set for the {@link WEditableDetailView} as default values.
102      */
103     private function getEditableProperties()
104     {
105         if (!isset($this->_editableProperties)) {
106             $reflection = new ReflectionClass('WhEditableField');
107             $this->_editableProperties = array_map(function ($d) {
108                 return $d->getName();
109             }, $reflection->getProperties());
110         }
111         return $this->_editableProperties;
112     }
113 
114     /**
115      * (non-PHPdoc)
116      * @see CComponent::__get()
117      */
118     public function __get($key)
119     {
120         return (array_key_exists($key, $this->_data) ? $this->_data[$key] : parent::__get($key));
121     }
122 
123     /**
124      * (non-PHPdoc)
125      * @see CComponent::__set()
126      */
127     public function __set($key, $value)
128     {
129         if (in_array($key, $this->getEditableProperties())) {
130             $this->_data[$key] = $value;
131         } else {
132             parent::__set($key, $value);
133         }
134     }
135 
136     /**
137      * (non-PHPdoc)
138      * @see CComponent::__isset()
139      */
140     public function __isset($name)
141     {
142         return array_key_exists($name, $this->_data) || parent::__isset($name);
143     }
144 }
145 
YiiWheels API documentation generated by ApiGen 2.8.0