Results 1 to 3 of 3

Thread: SOAP enhancement: Get a specific relationship (with solution)

  1. #1
    parsec is offline Senior Member
    Join Date
    Sep 2009
    Posts
    26

    Smile SOAP enhancement: Get a specific relationship (with solution)

    Purpose is to make it possible to be able to pinpoint the exact relationship you wat to read/modify. I ran into this problem because I have 2 relationships between the same modules. Offcourse it must be possible to process the old call aswell, so you don’t have to change all your current code…

    You can find the exact relationship name in the ‘relationships’ table in your database.

    New call:

    PHP Code:
    $params = array(
                           
    'session' => $session_id,
                           
    'module_name' => 'Contacts',
                           
    'module_id' => '50059edb-41a5-7ded-3836-48a3c3653945'// id of the Account
                           
    'related_module' => 'Leads',
                           
    'related_module_query' => '',
                           
    'relationship_name' => 'my_relationship',
                           
    'deleted' => 0
                           
                       
    );
     
    $result $client->call('get_relationships',$params); 
    Note the 'relationship_name' => 'my_relationship' line. Here you can specify the exact relationship. When you leave this blank, it will be processed as usual.

    Start with editting soap/SoapSugarUsers.php

    1) add 'relationship_name' => 'xsd:string' to line 1000:

    PHP Code:
    $server->register(
        
    'get_relationships',
        array(
    'session'=>'xsd:string''module_name'=>'xsd:string''module_id'=>'xsd:string''related_module'=>'xsd:string''related_module_query'=>'xsd:string''deleted'=>'xsd:int''relationship_name' => 'xsd:string' ),
        array(
    'return'=>'tns:get_relationships_result'),
        
    $NAMESPACE); 
    2) Change the function ‘get_relationships’ on line 1016 to process the new criterium.

    PHP Code:
    function get_relationships($session$module_name$module_id$related_module$related_module_query$deleted$relationship_name){
            
    $error = new SoapError();
        
    $ids = array();
        if(!
    validate_authenticated($session)){
            
    $error->set_error('invalid_login');
            return array(
    'ids'=>$ids,'error'=> $error->get_soap_array());
        }
        global  
    $beanList$beanFiles;
        
    $error = new SoapError();

        if(empty(
    $beanList[$module_name]) || empty($beanList[$related_module])){
            
    $error->set_error('no_module');
            return array(
    'ids'=>$ids'error'=>$error->get_soap_array());
        }
        
    $class_name $beanList[$module_name];
        require_once(
    $beanFiles[$class_name]);
        
    $mod = new $class_name();
        
    $mod->retrieve($module_id);
        if(!
    $mod->ACLAccess('DetailView')){
            
    $error->set_error('no_access');
            return array(
    'ids'=>$ids'error'=>$error->get_soap_array());
        }

        
    $id_list get_linked_records($related_module$module_name$module_id$relationship_name);

        if (
    $id_list === FALSE) {
            
    $error->set_error('no_relationship_support');
            return array(
    'ids'=>$ids'error'=>$error->get_soap_array());
        }
        elseif (
    count($id_list) == 0) {
            return array(
    'ids'=>$ids'error'=>$error->get_soap_array());
        }

        
    $list = array();

        
    $id_list_quoted array_map("add_squotes"$id_list);
        
    $in implode(", "$id_list_quoted);

        
    $related_class_name $beanList[$related_module];
        require_once(
    $beanFiles[$related_class_name]);
        
    $related_mod = new $related_class_name();

        
    $sql "SELECT {$related_mod->table_name}.id FROM {$related_mod->table_name} ";





        
    $sql .= " WHERE {$related_mod->table_name}.id IN ({$in}) ";

        if (!empty(
    $related_module_query)) {
            
    $sql .= " AND ( {$related_module_query} )";
        }

        
    $result $related_mod->db->query($sql);
        while (
    $row $related_mod->db->fetchByAssoc($result)) {
            
    $list[] = $row['id'];
        }

        
    $return_list = array();

        foreach(
    $list as $id) {
            
    $related_class_name $beanList[$related_module];
            
    $related_mod = new $related_class_name();
            
    $related_mod->retrieve($id);

            
    $return_list[] = array(
                
    'id' => $id,
                
    'date_modified' => $related_mod->date_modified,
                
    'deleted' => $related_mod->deleted
            
    );
        }

        return array(
    'ids' => $return_list'error' => $error->get_soap_array());

    3) open soap/SoapRelationshipHelper.php and change the function ‘get_linked_records’ on line 496. Bypass the ‘get_module_link_field’ call when a relationship_name is specified.

    PHP Code:
    function get_linked_records($get_module$from_module$get_id$relationship_name) {
        global 
    $beanList$beanFiles;

        
    // instantiate and retrieve $from_module
        
    $from_class $beanList[$from_module];
        require_once(
    $beanFiles[$from_class]);
        
    $from_mod = new $from_class();
        
    $from_mod->retrieve($get_id);
        if (
    $relationship_name <> ""){
        
    $field $relationship_name;
        }
        else {
        
    $field get_module_link_field($from_module$get_module);
        }
        if (
    $field === FALSE) {
            return 
    FALSE;
        }

        
    $from_mod->load_relationship($field);
        
    $id_arr $from_mod->$field->get();

        return 
    $id_arr;

    This will enable you to get a specific relationship

    And after quite a lot of puzzling: Setting a specific relation.

    The new call will be

    PHP Code:
    $relationship = array(
                
    'module1' =>$module1,
                
    'module1_id' => $module1id,
                
    'module2' => $module2,
                
    'module2_id' => $module2id,
                
    'relationship_name' => ‘my_relationship’
            
    );
            
    //print_r($relationship);
             
    $set_entry_params = array(
                     
    'session' => $this->sessionid,
                     
    'set_relationship_value'=> $relationship);
            
    // call set_relationship()
            
    $result $this->soapclient2->call('set_relationship'$set_entry_params); 
    Notice the line 'relationship_name' => ‘my_relationship’. This will enable you to specify the relationship. When it is empty, it will be processed as normal.

    Needed modifications:

    1) open SoapSugarUsers
    2) Edit the function “handle_set_relationship” at line 1169. Add the line $relationship_name = $set_relationship_value['relationship_name']; and bypass the $key lookup when $relationship_name isn’t empty. Also notice that the processing of the relation was taken out of the if statement and made dependent by the “process_key” var.

    PHP Code:

    function handle_set_relationship($set_relationship_value)
    {
        global  
    $beanList$beanFiles;
        
    $error = new SoapError();

        
    $module1 $set_relationship_value['module1'];
        
    $module1_id $set_relationship_value['module1_id'];
        
    $module2 $set_relationship_value['module2'];
        
    $module2_id $set_relationship_value['module2_id'];
        
    $relationship_name $set_relationship_value['relationship_name'];
        
    $process_key "0";


        if(empty(
    $beanList[$module1]) || empty($beanList[$module2]) )
        {
            
    $error->set_error('no_module');
            return 
    $error->get_soap_array();
        }
        
    $class_name $beanList[$module1];
        require_once(
    $beanFiles[$class_name]);
        
    $mod = new $class_name();
        
    $mod->retrieve($module1_id);
        if(!
    $mod->ACLAccess('DetailView')){
            
    $error->set_error('no_access');
            return 
    $error->get_soap_array();
        }
        if(
    $module1 == "Contacts" && $module2 == "Users"){
            
    $key 'contacts_users_id';
        }
        else if (
    $relationship_name <> "")
        {
            
    $key $relationship_name;
            
    $process_key "1";
        }
        else
        {
            
    $key array_search(strtolower($module2),$mod->relationship_fields);
            if (!
    $key) {
                require_once(
    'modules/Relationships/Relationship.php');
                
    $key Relationship::retrieve_by_modules($module1$module2$GLOBALS['db']);
                if (!empty(
    $key)) {
                   
    $process_key "1";
                } 
    // if
            
    // if
        
    }
        
    //$relationship_name_error = "Key is ".$key."";
        //$error->set_error($relationship_name_error);
        //return $error->get_soap_array();
        
    if ($process_key == "1")
        {
                    
    $mod->load_relationship($key);
                    
    $mod->$key->add($module2_id);
                    return 
    $error->get_soap_array();
        }
        if(!
    $key)
        {
            
    $error->set_error('no_module');
            return 
    $error->get_soap_array();
        }

        if((
    $module1 == 'Meetings' || $module1 == 'Calls') && ($module2 == 'Contacts' || $module2 == 'Users')){
            
    $key strtolower($module2);
            
    $mod->load_relationship($key);
            
    $mod->$key->add($module2_id);
        }else{
            
    $mod->$key $module2_id;
            
    $mod->save_relationship_changes(false);
        }

        return 
    $error->get_soap_array();

    3) go to soap/SoapTypes.php and add our new relationship_name to the stack at line 341

    PHP Code:
    $server->wsdl->addComplexType(
        
    'set_relationship_value',
        
    'complexType',
            
    'struct',
            
    'all',
            
    '',
            array(
                
    'module1'=>array('name'=>'module1''type'=>'xsd:string'),
                
    'module1_id'=>array('name'=>'module1_id''type'=>'xsd:string'),
                
    'module2'=>array('name'=>'module2''type'=>'xsd:string'),
                
    'module2_id'=>array('name'=>'module_2_id''type'=>'xsd:string'),
                           
    'relationship_name'=>array('name'=>'relationship_name''type'=>'xsd:string'),
            )
    ); 
    That should do the trick.

    Cheerz Parsec

  2. #2
    gunni is offline Sugar Community Member
    Join Date
    Aug 2006
    Location
    Cologne, Germany
    Posts
    364

    Default Re: SOAP enhancement: Get a specific relationship (with solution)

    Hmm, maybe that will affect (in positive) my bug http://www.sugarcrm.com/crm/support/...f-4acb6a6f5e40

    Or the other way round, if my bug gets fixed, maybe your problem will be gone, too.
    If sugar will use the relationship name instead of the related modules name, would do the trick.

  3. #3
    parsec is offline Senior Member
    Join Date
    Sep 2009
    Posts
    26

    Default Re: SOAP enhancement: Get a specific relationship (with solution)

    Yes exactly. And what I described above fixes this problem. You just need a few minor adjustments. This method will enable you to choose to use or the connected modules method, or the relationship name method.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Setup Error: KINAMU SOAP Enhancement not properly installed
    By netriver in forum Installation and Upgrade Help
    Replies: 1
    Last Post: 2009-12-03, 06:30 PM
  2. Replies: 16
    Last Post: 2009-02-13, 09:41 AM
  3. SOAP Set Relationship - how to delete a relationship?
    By darcy.rippon in forum Developer Help
    Replies: 0
    Last Post: 2009-02-02, 07:22 PM
  4. relationship-specific custom fields (contact<->account)
    By dbischoff in forum Developer Help
    Replies: 1
    Last Post: 2007-09-26, 10:57 PM
  5. SOAP Enhancement
    By Angel in forum Developer Help
    Replies: 0
    Last Post: 2007-08-16, 06:21 AM

Tags for this Thread

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
  •