Show Related Record Lists after Edit Record?

A place to discuss development of the Xataface core.

Show Related Record Lists after Edit Record?

Postby rugcutter » Wed Feb 24, 2010 3:20 pm

I have two tables in my application. The project_mstr table is a parent table, and project_agreements is a child table that has a foreign key column "pa_project_id" that links back to the project_mstr table.

I set up the relationships.ini for project_mstr as follows:

Code: Select all
[Agreements]
project_agreements.pa_project_id = "$pm_project_id"


So far so good. I see an "agreements" tab for a given project_mstr record. I can add new agreements records, and once I edit the new agreements record and save I return to the list of related agreements records.

My question is on editing an existing agreement record. It is not quite the same as adding a new agreement record. The user is taken out of the project / agreement relationship. Once the record is edited and saved, the page redirects back to the editing the same record.

What I would like to accomplish is as follows:

1) Hook into the event under the "Save" button, so that after the record is updated, the page redirects back to the list of related agreements records, that are related from the project_mstr record. e.g. same behavior as "add new"

2) Add a "Cancel" button that performs no database updates, but redirects back to the list of related agreements records.


For 1) my thought was to implement the "after_action_edit()" function in the Application Delegate class (since I would like this behavior consistent across all relationships). My hunch is to set up a link with the action=related_records_list, based on the code from new_related_record.php. However when I tried this I get errors since the relationship is lost when editing the existing record.

For 2) I was also thinking of implementing a block__blockname() function in the Application Delegate class for the cancel button. Is it available on that class? The button would use the related_records_list action as well, to return to the list.

Am I on the right track?
rugcutter
 
Posts: 11
Joined: Thu Apr 23, 2009 9:43 pm

Re: Show Related Record Lists after Edit Record?

Postby shannah » Thu Feb 25, 2010 5:51 pm

Yes. You are on the right track in both cases. For the after_action_edit action it can take a parameter as follows:

Code: Select all
function after_action_edit($params=array()){
    $record = $params['record'];
    // Can you find the related record from here?
    $parent = df_get_record('parent_table', array('id'=>'='.$record->val('parent_id')));
    if ( $parent ){
        header('Location: '.$parent->getURL('-action=related_records_list&-relationship=relationship_name'));
        exit;
    }
}



For the block, you can see what blocks are available to be filled by setting debug=1 at the beginning of your conf.ini file.


Another thing you might want to consider for navigation purposes is to implement the getBreadCrumbs() method on the child table so that the breadcrumbs (i.e. You are here) will show the path to the record including the parent record and links.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Show Related Record Lists after Edit Record?

Postby fabiofromfrance » Fri Feb 26, 2010 7:12 am

Steve,
I am using your script templates as I found it very good but can't figure out how to get relationship_name value programatically ?
For the parent_table and parent_id parameter I am fine since I have only one parent table but for the relationship_name I have a pb as I got three relationship tabs ... I can't figure out where I could get the names of the relationship I am coming from.
Thanks again for your help
Fabrice,
fabiofromfrance
 
Posts: 14
Joined: Mon Feb 15, 2010 3:03 pm

Re: Show Related Record Lists after Edit Record?

Postby shannah » Mon Mar 01, 2010 10:32 am

This is a bit tricky, as once you're in the edit action it's a new context with no reference to the parent relationship. You would need to either store the most recent relationship in a cookie or session variable -- or you would have to pass it as a GET parameter when you click on the record from the relationship tab. For the session/cookie solution you could define the session or cookie in any hook. For the GET parameter solution you would want to override the getURL method of the delegate class to append some parameter (e.g. -parent-relationship=foo), the fetch this later in your after_edit trigger.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Show Related Record Lists after Edit Record?

Postby rugcutter » Mon Mar 01, 2010 3:24 pm

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.
rugcutter
 
Posts: 11
Joined: Thu Apr 23, 2009 9:43 pm

Re: Show Related Record Lists after Edit Record?

Postby shannah » Mon Mar 01, 2010 3:39 pm

Wow.. That's great, rugcutter. It is a real treat to see the api being leveraged so fully by someone else.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Developers

Who is online

Users browsing this forum: No registered users and 13 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved