Page 1 of 1

Screen following BeforeDelete

PostPosted: Fri Mar 26, 2010 7:12 pm
by sheriff_deadeye
i sure appreciate all the great info on this forum...much appreciated.

i could use some help with a beforedelete() function. the following function is working as needed. it checks for existing rows in a child table, and if any are found, an error is returned to prevent deletion of the parent. After the error is returned, though, the user is dropped into the 'show all' screen instead of remaining on the 'edit' screen. so currently the flow is:
1. list screen
2. edit screen
3. click the delete button
4. check for child records. child records found
5. error message is displayed "cannot delete record because child records exist"
6. show all screen is displayed.

so...how do make sure that after the delete button is clicked and the error message is displayed, the user remains on the edit screen ?

Thanks in advance for the help.


if ( isset($record) ){
$app =& Dataface_Application::getInstance();
$query =& $app->getQuery();

$keyid = $record->val('ps_project_stage_id');
$key_field = "pd_project_stage_id";
$table = "project_deliverables";
$msg = "This STAGE cannot be deleted. It is being used in 1 or more DELIVERABLES.";


$sql = "select count(*) as row_count from ".$table;
$sql = $sql." where ".$key_field;
$sql= $sql." = ".$keyid;

$res = mysql_query($sql);
$num_rows = mysql_result($res,0);

if ($num_rows > 0 ){
return Dataface_Error::permissionDenied($msg);
}

}

Re: Screen following BeforeDelete

PostPosted: Mon Mar 29, 2010 10:21 am
by shannah
I think you're on the right track. The problem here is just that the delete action always sends users back to list view after the delete is "successful". Most of the time this is the logical choice because a successful delete generally results in the details view being irrelevant (as the record would have been deleted).

If you use the after_action_delete method in addition to your beforeDelete method, you'll be able to force the workflow that you describe.

e.g.

[code]
class tables_mytable {
var $customActionAfterDelete = null;

function beforeDelete(&$record){
...
...
if ( $failed ){ // i.e. if the delete fails for any reason
$this->customActionAfterDelete = 'edit';
}
}


function after_action_delete($params=array()){
$record = $params['record'];
if ( isset($this->customActionAfterDelete) ){
header('Location: '.$record->getURL('-action='.$this->customActionAfterDelete).'&--msg='.urlencode('Permission denied because of this and that'));
exit;
}
}
}

Things to notice:

1. I create a variable in the delegate class to track if the delete fails and specify which action to forward after failed delete.
2. The after_action_delete method checks for this variable and forwards to the edit page instead.

Re: Screen following BeforeDelete

PostPosted: Mon Mar 29, 2010 11:53 am
by sheriff_deadeye
thanks for the quick response. I will give this a try later today and let you know how it works.

Re: Screen following BeforeDelete

PostPosted: Mon Mar 29, 2010 7:55 pm
by sheriff_deadeye
thanks stave...got it working.

i did have to make a small change, though. i was probably missing something easy...anyway...

i could not retrieve the url in the after_action_delete function, so i included it in the beforeDelete function.

final result

function beforeDelete(&$record){
if (there is an error in the delete ){
$this->customActionAfterDelete = $record->getURL('-action=edit');
}
}

function after_action_delete($params=array()){
if ( isset($this->customActionAfterDelete) ){
header('Location: '.$this->customActionAfterDelete.'&--msg='.urlencode('This STAGE cannot be deleted. It is being used by 1 or more DELIVERABLES.'));
exit;
}
}