How to access the $data variable in the visible property of a column?

Hi!

I have a CGridView with records that only the original uploader or the admin can update/delete, otherwise no update/delete buttons should be displayed in the column (even though the empty column is there). I tried doing this in the visible property of the column, but realized that it is impossible for a column to be visible and invisible at the same time, so I suppose I should create the delete and update buttons, and then set the visible properties on them to be evaluated based on $data. I have a function on Yii::app()->user called adminOrOwner that takes an instance of a model and returns true if the user is an admin or is the original uploader, false if neither. This is my widget code so far:

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'dataProvider'=&gt;&#036;dataProvider,


'summaryText' =&gt; 'Showing {start} to {end} of {count} total movies submitted',


'columns' =&gt; array(


	array(


		'class' =&gt; 'CButtonColumn',


		'template' =&gt; '{delete} {update}',


		'visible' =&gt; 'Yii::app()-&gt;user-&gt;isAdminOrOwner(&#036;data)',


		'header' =&gt; 'Actions'


	),


	'name',


	array(


		'name' =&gt; 'Link',


		'type' =&gt; 'html',


		'value' =&gt; 'CHtml::link(&#036;data-&gt;link, &#036;data-&gt;link)'


	),


	'description:ntext',


	array(


		'name' =&gt; 'creation_time',


		'value' =&gt; 'date(&quot;l, F jS Y (H:i:s&quot;, &#036;data-&gt;creation_time) . &quot; GMT)&quot;', //Monday, January 1st 2011 (18:24:20 GMT)


		'header' =&gt; 'Creation Time'


	),


	'uploader.username:text:Uploaded By'


),

)); ?>

I have found that the visible property is not evaluated at runtime like the value property. So, is there another way I can get access to the model of the current row?

Thanks!

I have thought some more on this, and figured that the best way to go about this (which is what I want exactly) is to have the conditional statement to alter the template property, rather than the visible property. However, the template property does not get evaluated, and I need an expression which is evaluated for every row that is created (I.E. something like the value property of a CDataColumn, which is evaluated every time a row is created, based on $data). Any ideas?

Thanks!

I finally got it to work! Reading a how-to about the CButtonColumn on the wiki and seeing the ‘visible’ property in the ‘buttons’ array mentioned there, an idea struck me. What if I wouldn’t care about the column itself or don’t mind the template, but hide the buttons using their ‘visible’ property that indeed gets evaluated for the each row, rather than just once! Well, that proved to be true. I’m pasting the solution here in case someone needs it:

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'dataProvider'=&gt;&#036;dataProvider,


'summaryText' =&gt; 'Showing {start} to {end} of {count} total movies submitted',


'columns' =&gt; array(


	array(


		'class' =&gt; 'CButtonColumn',


		'buttons' =&gt; array(


			'delete' =&gt; array(


				'visible' =&gt; 'Yii::app()-&gt;user-&gt;AdminOrUploader(&#036;data)',


			),


			'update' =&gt; array(


				'visible' =&gt; 'Yii::app()-&gt;user-&gt;AdminOrUploader(&#036;data)',


			),


		),


		'template' =&gt; '{delete} {update}',

// ‘visible’ => ‘Yii::app()->user->isAdminOrOwner($data)’,

		'header' =&gt; 'Actions'


	),


	'name',


	array(


		'name' =&gt; 'Link',


		'type' =&gt; 'html',


		'value' =&gt; 'CHtml::link(&#036;data-&gt;link, &#036;data-&gt;link)'


	),


	'description:ntext',


	array(


		'name' =&gt; 'creation_time',


		'value' =&gt; 'date(&quot;l, F jS Y (H:i:s&quot;, &#036;data-&gt;creation_time) . &quot; GMT)&quot;', //Monday, January 1st 2011 (18:24:20 GMT)


		'header' =&gt; 'Creation Time'


	),


	'uploader.username:text:Uploaded By'


),

)); ?>

can i use the visible property with CHtml::link, i want to show the link when admin login, when user login link not display. how can i do…

i m using this …but this is not workin??????????????? help me…

<div class=“hlink”><?=CHtml::link(“View Users”,array(‘user/all_users’),‘visible’=>Yii::app()->user->checkAccess(‘admin’))?></div>

Did you find solution how to do this ? I’m also trying to figure it out… Alternative solution would work also… put chtml::link inside cmenu widget so I could use ‘visible’ parameter, but I dont know how to do that either ;)

I’m new to yii…

Hi zeeshan and Havunen,

Parham Doustdar was talking about CButtonColumn’s ‘visible’ property …

@zeeshan

I would write like this:




<?php if (Yii::app()->user->checkAccess('admin')): ?>

<div class="hlink">

<?php echo CHtml::link("View Users", array('user/all_users')); ?>

</div>

<?php endif; ?>



There’s no ‘visible’ property for CHtml::link.

http://www.yiiframework.com/doc/api/1.1/CHtml#link-detail

@Havunen

There’s a code sample in the reference of CMenu.

http://www.yiiframework.com/doc/api/1.1/CMenu




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

    'items'=>array(

        array('label'=>'Home', 'url'=>array('site/index')),

        array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(

            array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),

            array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),

        )),

        array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),

    ),

));