Admin View Edit/Delete buttons

Hello, my goal is to create the same style list that is on the admin view on the index page, which I have done. Basically, I got rid of the admin (manage) page and am just using an index page. However, what I want to do is have a version that has the edit/delete buttons in the right hand column and one that doesn’t. If you are not logged in, you can see the list of items and view each one in full by clicking the magnifying glass. If you are logged in, you can also delete or edit each item. How would I go about doing this? Here is the code that creates the list:


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

	'id'=>'stories-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'attr1',

		'attr2',

		'attr3',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

I can of course get rid of the array(‘class’=>’…’), but that also gets rid of the magnifying glass. I also don’t really know how to check if a user is logged in. Sorry, I’m a Yii noob.

You should use the ‘visible’ property of CGridColumn. You can use Yii::app()->user->isGuest to check that your user is logged in. So:


array(

   'class'   => 'CButtonColumn',

   'visible' => Yii::app()->user->isGuest,

),

The visible property is evaluated for every row so it’s good to use if you need to display/hide “data” from row to row…

Check this post it could give you a good idea - http://www.yiiframework.com/forum/index.php?/topic/9377-help-how-to-hide-some-data-value-in-cgridview/page__view__findpost__p__46410

Thanks guys, this helps, but I’m still not sure how to exclude certain buttons from the cbuttonview in the case that a user is logged in vs. the user is an admin.

So here is there some option to do this?


'columns'=>array(

		'x','y,'z',

		array(

			'class'=>'CButtonColumn', 

			'visible' => !Yii::app()->user->isGuest, //with this option only a logged in user sees the buttons

		),

So 1.) Is there a visibility option (or some other isXXXX method) that checks if a user is an admin as opposed to just logged in? and 2.) is there an option I can use in the CButtonColumn array that excludes certain buttons (e.g. delete)? THANKS!

How do you give a user the ADMIN status… tha you should check…

As for the buttons there is a "visible" property for every button - http://www.yiiframew#buttons-detail

but as I wrote above it would be better to do something like




if admin

   show all buttons

else

   show only view button






array(

			'class'=>'myButtonColumn',

                        'deleteButtonVisible'=>'false',

		)



@Thirumalai

Note that deleteButtonVisible does not exist in Yii… myButtonColumn is an extension that was needed before "visible" was implemented in Yii…

Instead now in Yii this can be used




'buttons'=>array(

   'delete'=>array(

   	'visible'=>false

   )

)



Thanks but I’m getting the error [color="#8B0000"]call_user_func_array() [<a href=‘function.call-user-func-array’>function.call-user-func-array</a>]: First argument is expected to be a valid callback, ‘’ was given[/color] with the following:


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

	'id'=>'stories-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'x','y','z',

		array(

			'class'=>'CButtonColumn',

			'buttons' => array(

				'delete' => array(

					'visible' => false

				)

			)

		)

	),

)); 

Just checked… ‘visible’ should be an expression that is evaluated… so you can use ‘false’… or any expression that returns true or false…

THANKS it worked!