Yii Framework Forum: Inline Function In Widget Declaration - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Inline Function In Widget Declaration Might be a general PHP issue Rate Topic: -----

#1 User is offline   ants_pants 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 16-April 13
  • Location:UK

Posted 29 June 2013 - 05:57 PM

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.
0

#2 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 585
  • Joined: 27-December 10
  • Location:china

Posted 29 June 2013 - 11:44 PM

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
0

#3 User is offline   ants_pants 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 16-April 13
  • Location:UK

Posted 30 June 2013 - 04:01 AM

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
[...]

:)
0

#4 User is offline   Tsunami 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 16-February 12

Posted 30 June 2013 - 08:16 AM

You can just use the ternary operator:

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

0

#5 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 840
  • Joined: 19-February 13

Posted 30 June 2013 - 09:17 AM

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.
Thanks,
Ankit Modi
0

#6 User is offline   ants_pants 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 16-April 13
  • Location:UK

Posted 01 July 2013 - 03:32 PM

View PostTsunami, on 30 June 2013 - 08:16 AM, said:

You can just use the ternary operator:

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


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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users