Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By cptmac

Thread: Cancel an Edit (save) from a Logic Hook v6.1.0

  1. #1
    cptmac is offline Member
    Join Date
    Apr 2011
    Posts
    9

    Default Cancel an Edit (save) from a Logic Hook v6.1.0 (Solved)

    Being a veteran of MVC and PHP but a noob on sugarCRM I have been frustrated trying
    to figure out how to tie logic hook business errors to the front end Edit Screen.
    I have seen posts state this was not the intent of logic_hooks functions and I will
    heartily disagree. There are times when we need to write complex edits that
    don't make sense to write in JavaScript or using AJAX. Most of the posts that I have seen
    on this topic seem to be from Sugar 5.x. I am running 6.1.0 and based on what
    I have seen I believe there was a pattern change from v6 to v5 (or at least
    a bunch of variable names have changed). Anyway, here is a soup to nuts
    guide on how to code a complex edit in a logic hook and get back to the edit
    screen without saving, display the user typed data, and display an error
    message. Maybe this is common knowledge to everyone but me and if it is then
    I have wasted a few bytes of the infinite internet.

    Once you have your logic_hook firing (not discussed here as there are plenty
    of tutorials on getting a hook to fire), your logic in the hook should look
    something like this:

    PHP Code:
    function <MyModule>BeforeSave(&$bean$event$arguments) {

        
    $result=performComplexEdit(); //do your complex logic here return boolean true if OK false if error
        
    if($result) {
            return 
    true//let save proceed as normal
        
    }
        
        
    $_SESSION['myError']="Whatever I want to display on the screen";
        
    //build the URL to return to
        
    $module=$_REQUEST['module'];
        
    $action="&action=EditView"//we need to have EditView b/c that is the action we are taking
        
    $returnModule="&return_module=".$_REQUEST['return_module'];
        
        
    $offset=$_REQUEST['offset'];
        if(
    $offset=="") {
        } else {
            
    $offset="&offset=$offset";
        }
        
        
    $stamp=$_REQUEST['stamp'];
        if(
    $stamp=="") {
        } else {
            
    $stamp="&stamp=$stamp";
        }
            
        if(
    $recordId=="") {
            
    $returnAction="&return_action=DetailView";
        } else {
            
    $recordId="&record=".$recordId;
        }
        
        
    $url="index.php?module=".$module.$offset.$stamp.$action.$returnModule.$returnAction.$recordId;
        
    header("Location: $url");
        
    $_SESSION['MyBean']=$bean;  //store the bean in session so we can retrieve the values later
        
    exit;   //goto the location contained in header

    OK now for the important part. You need to edit (create it if it doesn't exist) the
    following file: custom/modules/<MyModule>/views/view.edit.php

    It needs to look something like this:

    PHP Code:
    require_once('include/MVC/View/views/view.edit.php');
    class <
    MyModule>ViewEdit extends ViewEdit {

          public function 
    preDisplay() {  
                
    //this displays errors among other things
              
    $this->errors[]=$_SESSION['myError'];
              unset(
    $_SESSION['myError']);
              
    parent::preDisplay();
          }    
      
      public function 
    display() {
        if(isset(
    $_SESSION['MyBean'])) { //an error must have occurred bean is in session
            
    $sessionBean=$_SESSION['MyBean'];                
            
    $fieldKeys=array_keys($_SESSION['MyBean']->field_defs);
            foreach(
    $fieldKeys as $key) {  //get each field the user typed and set them into the current bean
                
    if($sessionBean->field_defs[$key]['type']=="date") {
            
    //dates get formated into YYYY-MM-DD and on next submit they throw an error for me
                    
    $this->bean->{$key} = 
                                         
    date_format(date_create($_SESSION['PromoGroupBean']->{$key}),"m/d/Y");
                } else {
                    
    $this->bean->{$key} = $_SESSION['PromoGroupBean']->{$key};
                }
            }
            unset(
    $_SESSION['MyBean']);
        }
        
    parent::display();
      }

    IMPORTANT NOTES: It is imperative that you name the Class properly. I thought the
    override occurred b/c of the location of the file but it also must be named starting
    with your module name otherwise it will not work. It is also imperative
    that you call
    PHP Code:
    parent::preDisplay() and parent::display() 
    from your override
    method unless you are going to handle everything those methods do (which I
    didn't want to do). I set each field into the ViewEdit
    bean by hand b/c when I set the whole bean it didn't quite work properly. I
    did not debug it further.

    In any event if you did everything correct when your complexEdit() returns false,
    you will return to your edit screen, see the user data, and an error message
    in red will be at the top of the screen.

    Happy Coding!
    Last edited by cptmac; 2011-04-28 at 11:28 PM. Reason: updated title

  2. #2
    phaniapsr is offline Sugar Community Member
    Join Date
    Jun 2011
    Location
    Hyderabad
    Posts
    12

    Default Re: Cancel an Edit (save) from a Logic Hook v6.1.0

    Hi,

    I just have the exact requirement which you have described. I did every thing as you specified. But i am not able to get the previous data and even the error message. I have the module with the name mango_Patients. In this i would like to check the email and some other fields.
    The logichook code is:
    PHP Code:
    class ValidateEmail{
           function 
    validate(&$bean$event$arguments){
                if(
    $this->emailValidate($bean->doctoremergemail)){
                        
    $_SESSION['myError']="Emergency Email is invalid";
                        
    //build the URL to return to
                        
    $module=$_REQUEST['module'];
                        
    $action="&action=EditView"//we need to have EditView b/c that is the action we are taking
                        
    $returnModule="&return_module=".$_REQUEST['return_module'];
                        
                        
    $offset=$_REQUEST['offset'];
                        if(
    $offset=="") {
                        } else {
                            
    $offset="&offset=$offset";
                        }
                        
                        
    $stamp=$_REQUEST['stamp'];
                        if(
    $stamp=="") {
                        } else {
                            
    $stamp="&stamp=$stamp";
                        }
                            
                        if(
    $recordId=="") {
                            
    $returnAction="&return_action=DetailView";
                        } else {
                            
    $recordId="&record=".$recordId;
                        }
                        
                        
    $url="index.php?module=".$module.$offset.$stamp.$action.$returnModule.$returnAction.$recordId;
                        
    header("Location: $url");
                        
    $_SESSION['MyBean']=$bean;  //store the bean in session so we can retrieve the values later
                        
    exit;
                       
                    }
                if(
    $this->emailValidate($bean->doctorprimaryemailaddress)){
                      
    //echo "Primary Email worng";
                    
    }    
              }
           function 
    emailValidate($email){
                 if (!
    eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)){ 
                   return 
    1;
                 }   
               }      

    Below is the code i have implemented in custom/modules/mango_Patients/views/view.edit.php (I dont have the view directory i created and proceded)

    PHP Code:
    <?php 
    require_once('include/MVC/View/views/view.edit.php');
    class 
    mango_PatientsViewEdit extends ViewEdit {

          public function 
    preDisplay() {  
                
    //this displays errors among other things
              
    $this->errors[]=$_SESSION['myError'];
              unset(
    $_SESSION['myError']);
              
    parent::preDisplay();
          }    
      
      public function 
    display() {
        if(isset(
    $_SESSION['MyBean'])) { //an error must have occurred bean is in session
            
    $sessionBean=$_SESSION['MyBean'];                
            
    $fieldKeys=array_keys($_SESSION['MyBean']->field_defs);
            foreach(
    $fieldKeys as $key) {  //get each field the user typed and set them into the current bean
                
    if($sessionBean->field_defs[$key]['type']=="date") {
            
    //dates get formated into YYYY-MM-DD and on next submit they throw an error for me
                    
    $this->bean->{$key} = 
                                         
    date_format(date_create($_SESSION['PromoGroupBean']->{$key}),"m/d/Y");
                } else {
                    
    $this->bean->{$key} = $_SESSION['PromoGroupBean']->{$key};
                }
            }
            unset(
    $_SESSION['MyBean']);
        }
        
    parent::display();
      }
    }  
    ?>

    I called these
    PHP Code:
    parent::preDisplay() and parent::display() 
    methods from modules/mango_Patients/mango_Patients.php file.

    PHP Code:
    require_once('modules/mango_Patients/mango_Patients_sugar.php');
    class 
    mango_Patients extends mango_Patients_sugar {
        
        function 
    mango_Patients(){    
            
    parent::mango_Patients_sugar();
            
    parent::preDisplay();
            
    parent::display();  
        }
        


    I am new to Sugar CRM. Please point me where i am doing wrong. I am egearly waiting for you reply.

    Thanks in advance.

  3. #3
    stenebenau is offline Junior Member
    Join Date
    Jul 2011
    Posts
    1

    Default Re: Cancel an Edit (save) from a Logic Hook v6.1.0

    Hi,

    I have the save problem, couldn't get it to work. Could please tell me if you find your solution?

    Thanks,

  4. #4
    cptmac is offline Member
    Join Date
    Apr 2011
    Posts
    9

    Default Re: Cancel an Edit (save) from a Logic Hook v6.1.0

    phaniapsr,

    Sorry for the delay, I never received notification of the thread being updated.

    The first thing I see is in the view.edit you need to update these references to be the name of your bean:

    $this->bean->{$key} =
    date_format(date_create($_SESSION['PromoGroupBean']->{$key}),"m/d/Y");
    } else {
    $this->bean->{$key} = $_SESSION['PromoGroupBean']->{$key};

    in your case the lines should read:
    $this->bean->{$key} = date_format(date_create($_SESSION['MyBean']->{$key}),"m/d/Y");
    } else {
    $this->bean->{$key} = $_SESSION['MyBean']->{$key};

    If you are still working this issue, reply back and I will try to help you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Cancel an edit from a logic hook
    By pauloalexandrino in forum Developer Help
    Replies: 21
    Last Post: 2012-04-21, 03:05 AM
  2. Cancel save in logic hook
    By eggsurplus in forum Developer Help
    Replies: 4
    Last Post: 2012-02-08, 09:37 AM
  3. After Save logic hook?
    By Krissy-D in forum Developer Help
    Replies: 2
    Last Post: 2011-02-06, 08:42 PM
  4. After Save Logic Hook
    By thamim in forum Developer Help
    Replies: 0
    Last Post: 2010-08-12, 09:49 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •