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 one column in CGridView.
  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  * @author Vitaliy Potapov <noginsk@rambler.ru>
 13  * @link https://github.com/vitalets/x-editable-yii
 14  * @copyright Copyright &copy; Vitaliy Potapov 2012
 15  * @version 1.3.1
 16  */
 17 
 18 Yii::import('yiiwheels.widgets.editable.WhEditableField');
 19 Yii::import('zii.widgets.grid.CDataColumn');
 20 
 21 class WhEditableColumn extends CDataColumn
 22 {
 23     /**
 24      * @var array editable config options.
 25      * @see EditableField config
 26      */
 27     public $editable = array();
 28 
 29     public function init()
 30     {
 31         if (!$this->name) {
 32             throw new CException('You should provide name for EditableColumn');
 33         }
 34 
 35         parent::init();
 36 
 37         //need to attach ajaxUpdate handler to refresh editables on pagination and sort
 38         WhEditable::attachAjaxUpdateEvent($this->grid);
 39     }
 40 
 41     /**
 42      * Renders data cell content
 43      * @param int $row
 44      * @param mixed $data
 45      */
 46     protected function renderDataCellContent($row, $data)
 47     {
 48         $isModel = $data instanceOf CModel;
 49 
 50         if ($isModel) {
 51             $widgetClass = 'EditableField';
 52             $options = array(
 53                 'model' => $data,
 54                 'attribute' => empty($this->editable['attribute']) ? $this->name : $this->editable['attribute'],
 55             );
 56 
 57             //if value defined in column config --> we should evaluate it
 58             //and pass to widget via `text` option: set flag `passText` = true
 59             $passText = !empty($this->value);
 60         } else {
 61             $widgetClass = 'WhEditable';
 62             $options = array(
 63                 'pk' => $data[$this->grid->dataProvider->keyField],
 64                 'name' => empty($this->editable['name']) ? $this->name : $this->editable['name'],
 65             );
 66 
 67             $passText = true;
 68             //if autotext will be applied, do not pass `text` option (pass `value` instead)
 69             if (empty($this->value) && WhEditable::isAutotext($this->editable, isset($this->editable['type']) ? $this->editable['type'] : '')) {
 70                 $options['value'] = $data[$this->name];
 71                 $passText = false;
 72             }
 73         }
 74 
 75         //for live update
 76         $options['liveTarget'] = $this->grid->id;
 77 
 78         $options = CMap::mergeArray($this->editable, $options);
 79 
 80         //if value defined for column --> use it as element text
 81         if ($passText) {
 82             ob_start();
 83             parent::renderDataCellContent($row, $data);
 84             $text = ob_get_clean();
 85             $options['text'] = $text;
 86             $options['encode'] = false;
 87         }
 88 
 89         //apply may be a string expression, see https://github.com/vitalets/x-editable-yii/issues/33
 90         if (isset($options['apply']) && is_string($options['apply'])) {
 91             $options['apply'] = $this->evaluateExpression($options['apply'], array('data' => $data, 'row' => $row));
 92         }
 93 
 94         //evaluate htmlOptions inside editable config as they can depend on $data
 95         //see https://github.com/vitalets/x-editable-yii/issues/40
 96         if (isset($options['htmlOptions']) && is_array($options['htmlOptions'])) {
 97             foreach ($options['htmlOptions'] as $k => $v) {
 98                 if (is_string($v) && (strpos($v, '$data') !== false || strpos($v, '$row') !== false)) {
 99                     $options['htmlOptions'][$k] = $this->evaluateExpression($v, array('data' => $data, 'row' => $row));
100                 }
101             }
102         }
103 
104         $this->grid->controller->widget($widgetClass, $options);
105     }
106 
107     /**
108      * Require this overwrite to show bootstrap sort icons
109      */
110     protected function renderHeaderCellContent()
111     {
112         if (yii::app()->editable->form != EditableConfig::FORM_BOOTSTRAP) {
113             parent::renderHeaderCellContent();
114             return;
115         }
116 
117         if ($this->grid->enableSorting && $this->sortable && $this->name !== null) {
118             $sort = $this->grid->dataProvider->getSort();
119             $label = isset($this->header) ? $this->header : $sort->resolveLabel($this->name);
120 
121             if ($sort->resolveAttribute($this->name) !== false)
122                 $label .= '<span class="caret"></span>';
123 
124             echo $sort->link($this->name, $label, array('class' => 'sort-link'));
125         } else {
126             if ($this->name !== null && $this->header === null) {
127                 if ($this->grid->dataProvider instanceof CActiveDataProvider)
128                     echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
129                 else
130                     echo CHtml::encode($this->name);
131             } else
132                 parent::renderHeaderCellContent();
133         }
134     }
135 
136     /**
137      * Require this overwrite to show bootstrap filter field
138      */
139     public function renderFilterCell()
140     {
141         if (yii::app()->editable->form != EditableConfig::FORM_BOOTSTRAP) {
142             parent::renderFilterCell();
143             return;
144         }
145 
146         echo '<td><div class="filter-container">';
147         $this->renderFilterCellContent();
148         echo '</div></td>';
149     }
150 }
151 
YiiWheels API documentation generated by ApiGen 2.8.0