Inline Function In Widget Declaration

I’m not an old hand when it comes to PHP, and certainly not one when it comes to Yii, so this could go either way.

In declaring a CGridView I’m trying to use an inline function with RBAC to control the ‘template’ property. I’ve got this view code that works:


<?php


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $dataProvider,

    'selectableRows' => '0',

    'columns' => array(

        array(

            'name' => 'user_email',

            'header' => 'User Email',

            'value' => 'CHtml::encode($data->user_email)',

            'type' => 'raw'

        ),

        array( // display a column with "view", "update" and "delete" buttons

            'class' => 'CButtonColumn',

            'template' => temp()

        )

    )

));


function temp()

{

    if (Yii::app()->user->checkAccess(User::ROLE_ADMIN))

        return '{view}{update}{delete}';

    else

        return '{view}';

}

?>

but I’d rather say this for ‘template’:


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $dataProvider,

    'selectableRows' => '0',

    'columns' => array(

        array(

            'name' => 'user_email',

            'header' => 'User Email',

            'value' => 'CHtml::encode($data->user_email)',

            'type' => 'raw'

        ),

        array( // display a column with "view", "update" and "remove" buttons

            'class' => 'CButtonColumn',

            'template' => function ()

            {

                if (Yii::app()->user->checkAccess(User::ROLE_ADMIN))

                    return '{view}{update}{delete}';

                else

                    return '{view}';

            }

        )

    )

));

Unfortunately this throws an exception:


CBaseController->widget("zii.widgets.grid.CGridView", array("dataProvider" => CActiveDataProvider, "selectableRows" => "0", "columns" => array(array("name" => "user_email", "header" => "User Email", "value" => "CHtml::encode($data->user_email)", "type" => "raw"), array("class" => "CButtonColumn", "template" => Closure)))) 

I don’t care about whether or not this is the right place to make the decision about what the template should be, I just want to know what it is about the inline declaration (“Closure” class?) that’s a problem for PHP/Yii (PHP, probably).

TIA.

the property ‘template’ type is string .not Closure .if as what you did every property can give a Closure and the internal implement should check every property to see if it should be “evaled” . :o

Thank you yiqing95. I understand now that is a Closure object that is “seen”, and not the return from the function. I have moved the declaration of ‘template’ to a simple IF ELSE before the widget:


if (Yii::app()->user->checkAccess(User::ROLE_ADMIN))

    $template = '{view}{update}{delete}';

else

    $template = '{view}';


$this->widget('zii.widgets.grid.CGridView', array(

[...]

    'template' => $template

[...]



:)

You can just use the ternary operator:


'template' => Yii::app()->user->checkAccess(User::ROLE_ADMIN) ? '{view}{update}{delete}' : '{view}'

Hi

you can use something look like this


'template' => $this->temp()

and create the temp function in componets/Controller




public function temp(){

 if (Yii::app()->user->checkAccess(User::ROLE_ADMIN))

        return '{view}{update}{delete}';

    else

        return '{view}';


}

Hope it will be helpful.

Tsunami, it probably wasn’t a terribly good example; the ternary will work there, but I’m expecting (possibly) more than two possible outcomes, so a switch would have been better (inline). Thanks for the input, though.