Search code examples
phpyiicgridview

determine if a model attribute is empty in yii


            'buttons'=>array(
            'catalog'=>array(
                'label'=>'Catalog tehnic',
                'imageUrl' => strlen($data->catalog) > 1 ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'url'=>'$data->catalog',
            ),

i am trying to set different icons for a button in cgridview buttoncollumn if a value is set or not. i have tried different things as following all whit same result.

                'imageUrl' => strlen($data->catalog) > 1 ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => isset($data->catalog)  ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => !empty($data->catalog) ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => $data->catalog == '' ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",
                'imageUrl' => !$data->catalog ?  Yii::app()->baseUrl . "/images/pdf.jpg" : Yii::app()->baseUrl . "/images/bw.png",

it is geting very frustrating so pls help me out guys. thx in advance


Solution

  • CButtonColumn class render buttons on default generated zii.widgets.grid.CGridView.

    Reading class code, only url and visible properties can be evaluated as PHP expression.

    imageUrl property in this class is processed as plain string (not PHP expression).

    I made this experiment. Suggestions are totally welcome. I hope it helps you:

    Create in protected/components a new class: for example ExtCButtonColumn.php. This class should inherit class CButtonColumn.

    class ExtCButtonColumn extends CButtonColumn
    {
    ...
    }
    

    In class code, rewrite rendering method, keeping original concept and avoiding break existing or legacy implementations on other CGridViews:

    protected function renderButton($id,$button,$row,$data)
    {
        if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
                return;
            $label=isset($button['label']) ? $button['label'] : $id;
            $url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
            $options=isset($button['options']) ? $button['options'] : array();
            if(!isset($options['title']))
               $options['title']=$label;
    
                $imageUrl = isset($button['imageUrl']) ? $this->evaluateExpression($button['imageUrl'],array('data'=>$data,'row'=>$row)) : '';
                if ( $imageUrl == '')
                   $imageUrl = isset($button['imageUrl']) ? $button['imageUrl'] : '#';
            if( $imageUrl != '#' )
               echo CHtml::link(CHtml::image($imageUrl,$label),$url,$options);
            else
               echo CHtml::link($label,$url,$options);
        }
    

    Finally in your view code, in widget definition ( $this->widget array ), in columns array , add a similar buttons definition (this is a example with a custom button). Now is possible in all imageUrl attributes to use PHP expressions like url and visible properties:

    array(
        'class'    => 'ExtCButtonColumn',
        'template' => '{process}{view}{update}{close}{back}',
        'buttons'  => array
         (
            'process' => array
            (
               'label'    => 'Procesar',
               'imageUrl' => 'strlen($data->catalog) > 1  ? Yii::app()->request->baseUrl . "/images/icons/myicon1.png" : Yii::app()->request->baseUrl . "/images/icons/myicon2.png"',   
         ...