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:
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.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);
Start with editting soap/SoapSugarUsers.php
1) add 'relationship_name' => 'xsd:string' to line 1000:
2) Change the function ‘get_relationships’ on line 1016 to process the new criterium.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);
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_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());
}
This will enable you to get a specific relationshipPHP 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;
}
And after quite a lot of puzzling: Setting a specific relation.
The new call will be
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.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);
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.
3) go to soap/SoapTypes.php and add our new relationship_name to the stack at line 341PHP 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();
}
That should do the trick.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'),
)
);
Cheerz Parsec


LinkBack URL
About LinkBacks



Reply With Quote

Bookmarks