I wanted to post an update on this with some concrete code examples - for the benefit of Fabrice and others that may want this kind of behavior in the future.
As Steve described, I hooked into the after_action_edit() method on the Application Delegate class:
- Code: Select all
// After saving the record, return to the related records list in context with the
// original parent relationship.
function after_action_edit() {
$msg="Record successfully saved.";
$link = $this->getParentRelatedRecordsListURL();
header("Location: $link"."&--msg=".$msg);
exit;
}
I call a new method called getParentRelatedRecordsListURL() because it will also be used in the cancel request - described later. THis is also implemented on the Application Delegate class:
- Code: Select all
// Build link to return to the parent related records list.
function getParentRelatedRecordsListURL() {
$app =& Dataface_Application::getInstance();
$parentrel = $app->getQueryParam('parentrel');
if (isset($parentrel) ) {
$fquery = array('-action'=>'related_records_list');
$record =& $app->getRecord();
$parentrecord =& $record->getParent();
$idarr = explode('?',$parentrecord->getID());
$fquery['-table'] = $idarr[0];
$fquery['-relationship'] = $parentrel;
$link = Dataface_LinkTool::buildLink($fquery, false);
$link = $link.'&'.$idarr[1];
}
else
{
$link = $app->getRecord()->GetURL();
}
return $link;
}
I reference a new query string parameter "parentrel" - which, Fabrice - passes along the parent relationship as Steve describes. To accomplish this I hooked into the getURL() method in the table-specific delegate class, which does nothing more than call a generic addParentRelToUrl() method on the Application Delegate class. addParentRelToUrl() finds the parent relationship needed to pass along in the querytring when editing the child record.
getURL() method in the table-specific delegate class:
- Code: Select all
// add parent relationship to the URL, if present
function getURL(&$record, $params) {
return (Dataface_Application::getInstance()->getDelegate()->addParentRelToURL($record, $params));
}
addParentRelToURL() method in the application delegate class:
- Code: Select all
// Called by delegate classes to tack on the parent relationship to the URL.
function addParentRelToURL(&$record, $params) {
$app =& Dataface_Application::getInstance();
$parentrel = $app->getQueryParam('relationship');
if (isset( $parentrel )) {
$params['-parentrel'] = $parentrel;
}
$link = Dataface_LinkTool::buildLink($params, true);
return $link;
}
Steve describes in his example how to grab a parent record. I did this by hooking into the the getParent() method in the table-specific delegate class. This is because each table will have a different parent-child relationship. Here is the code example for the project_agreements table (which is a child table of my project_master table):
- Code: Select all
// return parent record.
function &getParent() {
$app =& Dataface_Application::getInstance();
$record =& $app->getRecord();
// Grab FK value back to the parent record
$key = $record->getValue( 'pa_project_id' );
return df_get_record('project_mstr', array('pm_project_id'=>$key));
}
To implement the cancel button, I had to go around the framework a little bit. I included the jquery library, by hooking into block__custom_javascripts() in the Application Delegate class:
- Code: Select all
function block__custom_javascripts()
{
echo '<script src="js/jquery-1.4.2.min.js" type="text/javascript" language="javascript"></script>';
echo '<script src="js/dashboard.js" type="text/javascript" language="javascript"></script>';
}
Then in dashboard.js, used the jquery to add the cancel button next to the "Save" button:
- Code: Select all
$(function()
{
// Add a cancel button after every save button.
$("input[value='Save']").parent("td").after("<td><input type='submit' value='Cancel' name='--session:cancel'/></td>");
});
Back in the Application Delegate class, I catch the post before the action handler runs - in beforeHandleRequest(). Here I call the same method to redirect page to the parent related records list:
- Code: Select all
function beforeHandleRequest() {
// Handle the "Cancel" request.
if ($_POST["--session:cancel"] == "Cancel")
{
$link = $this->getParentRelatedRecordsListURL();
header("Location: $link");
exit;
}
}
Yes, I realize a little hacky. Steve, I couldn't find a specific block that references this "Save" button and it was not obvious to me how to extend a template or modify the quickform object to get to the "Save" button.
BTW Steve thank you for publishing and supporting such a fabulous database front-end framework.