The code is so consistently written, that it was straight forward to add two new callbacks
- callback_can_edit( array($this, '_cbHavePriv'))
- callback_can_delete(array($this, '_cbHavePriv'))
The callback returns true (if User have privilege for that row), otherwise false.
Where _cbHavePriv( $row ) is defined
public function _cbHavePriv( $row ){
$user_id = $this->sess_info['user_id'];
return( $user_id == $row->USER_ID );
} // _cbHavePriv
This required extending libraries/grocery_crud.php (actually, I just added it, . . . for now)
in class grocery_CRUD
/* in the callbacks secton */
protected $callback_before_upload = null;
protected $callback_after_upload = null;
+ protected $callback_can_edit = null;
+ protected $callback_can_delete = null;
/* a little bit farther, after callback_after_upload */
/**
*
* A callback triggered before displaying edit_action (per row)
* @param mixed $callback
* @return grocery_CRUD
*/
public function callback_can_edit($callback = null)
{
$this->callback_can_edit = $callback;
return $this;
}
/**
*
* A callback triggered before displaying delete_action (per row)
* @param mixed $callback
* @return grocery_CRUD
*/
public function callback_can_delete($callback = null)
{
$this->callback_can_delete = $callback;
return $this;
}
Then in class grocery_CRUD_Layout, function showList($ajax = false, $state_info = null)
replace the "foreach($data->list as $num_row => $row){" loop with
if( $this->callback_can_edit != null || $this->callback_can_delete != null ){
foreach( $data->list as $num_row => $row ){
if( $this->callback_can_edit != null ){
$cb_return = call_user_func($this->callback_can_edit, $row);
$data->list[$num_row]->edit_url = ( $cb_return === false ) ? null : $data->edit_url.'/'.$row->{$data->primary_key};
}
if( $this->callback_can_delete != null ){
$cb_return = call_user_func($this->callback_can_delete, $row);
$data->list[$num_row]->delete_url = ( $cb_return === false ) ? null : $data->delete_url.'/'.$row->{$data->primary_key};
}
}
} else {
foreach($data->list as $num_row => $row){
$data->list[$num_row]->edit_url = $data->edit_url.'/'.$row->{$data->primary_key};
$data->list[$num_row]->delete_url = $data->delete_url.'/'.$row->{$data->primary_key};
}
}
finally, in assets/grocery_crud/themes/flexigrid/view/list.php
after the "<div class='tools'>" line, and the != null bits
<div class='tools'>
<?php if(!$unset_delete && $row->delete_url != null ){?>
<a href='<?php echo $row->delete_url?>' title='<?php echo $this->l('list_delete')?> <?php echo $subject?>' class="delete-row" >
<span class='delete-icon'></span>
</a>
<?php }?>
<?php if(!$unset_edit && $row->edit_url != null ){?>
<a href='<?php echo $row->edit_url?>' title='<?php echo $this->l('list_edit')?> <?php echo $subject?>' class="edit_button">
<span class='edit-icon'></span></a>