iTx Technologies offre gratuitement
cet espace pour SugarCRM !

title

Body

[fermer]

/soap/ -> SoapRelationshipHelper.php (source)

   1  <?php
   2  
   3  
   4  if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
   5  /*********************************************************************************
   6   * SugarCRM is a customer relationship management program developed by
   7   * SugarCRM, Inc. Copyright (C) 2004 - 2009 SugarCRM Inc.
   8   * 
   9   * This program is free software; you can redistribute it and/or modify it under
  10   * the terms of the GNU General Public License version 3 as published by the
  11   * Free Software Foundation with the addition of the following permission added
  12   * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  13   * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  14   * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  15   * 
  16   * This program is distributed in the hope that it will be useful, but WITHOUT
  17   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  19   * details.
  20   * 
  21   * You should have received a copy of the GNU General Public License along with
  22   * this program; if not, see http://www.gnu.org/licenses or write to the Free
  23   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24   * 02110-1301 USA.
  25   * 
  26   * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  27   * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  28   * 
  29   * The interactive user interfaces in modified source and object code versions
  30   * of this program must display Appropriate Legal Notices, as required under
  31   * Section 5 of the GNU General Public License version 3.
  32   * 
  33   * In accordance with Section 7(b) of the GNU General Public License version 3,
  34   * these Appropriate Legal Notices must retain the display of the "Powered by
  35   * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  36   * technical reasons, the Appropriate Legal Notices must display the words
  37   * "Powered by SugarCRM".
  38   ********************************************************************************/
  39  require_once ('soap/SoapError.php');
  40  
  41  function check_for_relationship($relationships, $module){
  42      foreach($relationships as $table=>$rel){
  43          if( $rel['rhs_key'] == $module){
  44              return $table;
  45  
  46          }
  47      }
  48      return false;
  49  }
  50  
  51  /*
  52   * takes in two modules and returns the relationship information about them
  53   *
  54   */
  55  
  56  function retrieve_relationships_properties($module_1, $module_2, $relationship_name = ""){
  57      
  58      $rs = new Relationship();
  59      $query =  "SELECT * FROM $rs->table_name WHERE ((lhs_module = '$module_1' AND rhs_module='$module_2') OR (lhs_module = '$module_2' AND rhs_module='$module_1'))";
  60      if(!empty($relationship_name) && isset($relationship_name)){
  61          $query .= " AND relationship_name = '$relationship_name'";
  62      }
  63      $result = $rs->db->query($query);
  64  
  65      return $rs->db->fetchByAssoc($result);
  66  }
  67  
  68  
  69  
  70  
  71  /*
  72   * retireves relationships between two modules
  73   * This will return all viewable relationships between two modules
  74   * module_query is a filter on the first module
  75   * related_module_query is a filter on the second module
  76   * relationship_query is a filter on the relationship between them
  77   * show_deleted is if deleted items should be shown or not
  78   *
  79   */
  80  function retrieve_relationships($module_name,  $related_module, $relationship_query, $show_deleted, $offset, $max_results){
  81      global  $beanList, $beanFiles, $dictionary, $current_user;
  82      $error = new SoapError();
  83      $result_list = array();
  84      if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
  85  
  86          $error->set_error('no_module');
  87          return array('result'=>$result_list, 'error'=>$error->get_soap_array());
  88      }
  89  
  90      $result = retrieve_relationship_query($module_name,  $related_module, $relationship_query, $show_deleted, $offset, $max_results);
  91  
  92      if(empty($result['module_1'])){
  93  
  94          $error->set_error('no_relationship_support');
  95          return array('result'=>$result_list, 'error'=>$error->get_soap_array());
  96      }
  97      $query = $result['query'];
  98      $module_1 = $result['module_1'];
  99      $table = $result['join_table'];
 100  
 101      $class_name = $beanList[$module_1];
 102      require_once($beanFiles[$class_name]);
 103      $mod = new $class_name();
 104  
 105      $count_query = str_replace('rt.*', 'count(*)', $query);
 106      $result = $mod->db->query($count_query);
 107      $row = $mod->db->fetchByAssoc($result);
 108      $total_count = $row['count(*)'];
 109  
 110      if($max_results != '-99'){
 111          $result = $mod->db->limitQuery($query, $offset, $max_results);
 112      }else{
 113          $result = $mod->db->query($query);
 114      }
 115      while($row = $mod->db->fetchByAssoc($result)){
 116  
 117          $result_list[] = $row;
 118      }
 119  
 120      return array('table_name'=>$table, 'result'=>$result_list, 'total_count'=>$total_count, 'error'=>$error->get_soap_array());
 121  }
 122  
 123  /*
 124   * retireves modified relationships between two modules
 125   * This will return all viewable relationships between two modules
 126   * module_query is a filter on the first module
 127   * related_module_query is a filter on the second module
 128   * relationship_query is a filter on the relationship between them
 129   * show_deleted is if deleted items should be shown or not
 130   *
 131   */
 132  function retrieve_modified_relationships($module_name, $related_module, $relationship_query, $show_deleted, $offset, $max_results, $select_fields = array(), $relationship_name = ''){
 133      global  $beanList, $beanFiles, $dictionary, $current_user;
 134      $error = new SoapError();
 135      $result_list = array();
 136      if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
 137  
 138          $error->set_error('no_module');
 139          return array('result'=>$result_list, 'error'=>$error->get_soap_array());
 140      }
 141  
 142      $row = retrieve_relationships_properties($module_name, $related_module, $relationship_name);
 143  
 144      if(empty($row)){
 145  
 146          $error->set_error('no_relationship_support');
 147          return array('result'=>$result_list, 'error'=>$error->get_soap_array());
 148      }
 149  
 150      $table = $row['join_table'];
 151      $has_join = true;
 152      if(empty($table)){
 153          //return array('table_name'=>$table, 'result'=>$result_list, 'error'=>$error->get_soap_array());
 154          $table = $row['rhs_table'];
 155          $module_1 = $row['lhs_module'];
 156          $mod_key = $row['lhs_key'];
 157          $module_2 = $row['rhs_module'];
 158          $mod2_key = $row['rhs_key'];
 159          $has_join = false;
 160      }
 161      else{
 162          $module_1 = $row['lhs_module'];
 163          $mod_key = $row['join_key_lhs'];
 164          $module_2 = $row['rhs_module'];
 165          $mod2_key = $row['join_key_rhs'];
 166      }
 167  
 168  
 169  
 170      $class_name = $beanList[$module_1];
 171      require_once($beanFiles[$class_name]);
 172      $mod = new $class_name();
 173  
 174      $mod2_name = $beanList[$module_2];
 175      require_once($beanFiles[$mod2_name]);
 176      $mod2 = new $mod2_name();
 177      $table_alias = 'rt';
 178      if($has_join == false){
 179          $table_alias = 'm1';
 180      }
 181  
 182      if(isset($select_fields) && !empty($select_fields)){
 183          $index = 0;
 184          $field_select ='';
 185          foreach($select_fields as $field){
 186              if($field == "id"){
 187                  $field_select .= "DISTINCT m1.".$field;
 188              }
 189              else{
 190                  if(strpos($field, ".") == false){
 191                      $field_select .= "m1.".$field;
 192                  }
 193                  else{
 194                      $field_select .= $field;
 195                  }
 196              }
 197              if($index < (count($select_fields) - 1))
 198              {
 199                  $field_select .= ",";
 200                  $index++;
 201              }
 202          }//end foreach
 203          $query = "SELECT $field_select FROM $table $table_alias ";
 204      }
 205      else{
 206          $query = "SELECT rt.* FROM  $table $table_alias ";
 207      }
 208  
 209      if($has_join == false){
 210          $query .= " inner join $mod->table_name m2 on $table_alias.$mod2_key = m2.id ";
 211  
 212  
 213  
 214  
 215      }
 216      else{
 217          $query .= " inner join $mod->table_name m1 on rt.$mod_key = m1.id ";
 218          $query .= " inner join $mod2->table_name m2 on rt.$mod2_key = m2.id AND m2.id = '$current_user->id'";
 219  
 220  
 221  
 222  
 223  
 224  
 225      }
 226  
 227      if(!empty($relationship_query)){
 228          $query .= ' WHERE ' . string_format($relationship_query, array($table_alias));
 229      }
 230  
 231      if($max_results != '-99'){
 232          $result = $mod->db->limitQuery($query, $offset, $max_results);
 233      }else{
 234          $result = $mod->db->query($query);
 235      }
 236      while($row = $mod->db->fetchByAssoc($result)){
 237          $result_list[] = $row;
 238      }
 239  
 240      return array('table_name'=>$table, 'result'=>$result_list, 'total_count'=>$total_count, 'error'=>$error->get_soap_array());
 241  }
 242  
 243  /*
 244   * retireves relationships between two modules
 245   * This will return all viewable relationships between two modules
 246   * module_query is a filter on the first module
 247   * related_module_query is a filter on the second module
 248   * relationship_query is a filter on the relationship between them
 249   * show_deleted is if deleted items should be shown or not
 250   *
 251   */
 252  function clear_relationships($module_name,  $related_module){
 253      global  $beanList, $beanFiles, $dictionary;
 254      $result_list = array();
 255      if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
 256  
 257  
 258          return false;
 259      }
 260  
 261      $row = retrieve_relationships_properties($module_name, $related_module);
 262      if(empty($row)){
 263  
 264          return false;
 265      }
 266  
 267      if($module_name == $row['lhs_module']){
 268          $module_1 = $module_name;
 269          $module_2 = $related_module;
 270      }else{
 271          $module_2 = $module_name;
 272          $module_1 = $related_module;
 273      }
 274      $table = $row['join_table'];
 275      $class_name = $beanList[$module_1];
 276      require_once($beanFiles[$class_name]);
 277      $mod = new $class_name();
 278      $clear_query = "DELTE * FROM  $table WHERE 1=1";
 279      $result = $mod->db->query($clear_query);
 280      return true;
 281  }
 282  
 283  function server_save_relationships($list, $from_date, $to_date){
 284      require_once ('include/utils/db_utils.php');
 285      global  $beanList, $beanFiles;
 286      $from_date = db_convert("'".$from_date."'", 'datetime');
 287      $to_date = db_convert("'".$to_date."'", 'datetime');
 288      global $sugar_config;
 289      $db = DBManagerFactory::getInstance();
 290  
 291      $ids = array();
 292      $add = 0;
 293      $modify = 0;
 294      $deleted = 0;
 295  
 296      foreach($list as $record)
 297      {
 298          $insert = '';
 299          $insert_values = '';
 300          $update = '';
 301          $select_values    = '';
 302          $args = array();
 303  
 304          $id = $record['id'];
 305  
 306          $table_name = $record['module_name'];
 307          $resolve = 1;
 308  
 309          foreach($record['name_value_list'] as $name_value){
 310              $name = $name_value['name'];
 311  
 312              if($name == 'date_modified'){
 313                      $value = $to_date;
 314              }else{
 315                      $value = db_convert("'".$name_value['value'] . "'", 'varchar');
 316              }
 317              if($name != 'resolve'){
 318              if(empty($insert)){
 319                  $insert = '('    .$name;
 320                  $insert_values = '('    .$value;
 321                  if($name != 'date_modified' && $name != 'id' ){
 322                      $select_values = $name ."=$value";
 323                  }
 324                  if($name != 'id'){
 325                      $update = $name ."=$value";
 326                  }
 327              }else{
 328                  $insert .= ', '    .$name;
 329                  $insert_values .= ', '    .$value;
 330                  if(empty($update)){
 331                      $update .= $name."=$value";
 332                  }else{
 333                      $update .= ','.$name."=$value";
 334                  }
 335  
 336                  if($name != 'date_modified' && $name != 'id' ){
 337                      if(empty($select_values)){
 338                          $select_values = $name ."=$value";
 339                      }else{
 340                          $select_values .= ' AND '.$name ."=$value";
 341                      }
 342                  }
 343              }
 344              }else{
 345                  $resolve = $value;
 346              }
 347  
 348  
 349  
 350  
 351          }
 352          //ignore resolve for now server always wins
 353          $resolve = 1;
 354          $insert = "INSERT INTO $table_name $insert) VALUES $insert_values)";
 355          $update = "UPDATE $table_name SET $update WHERE id=";
 356          $delete = "DELETE FROM $table_name WHERE id=";
 357          $select_by_id_date = "SELECT id FROM $table_name WHERE id ='$id' AND date_modified > $from_date AND date_modified<= $to_date";
 358          $select_by_id = "SELECT id FROM $table_name WHERE id ='$id'";
 359          $select_by_values = "SELECT id FROM $table_name WHERE $select_values";
 360          $updated = false;
 361  
 362  
 363          $result = $db->query($select_by_id_date);
 364          //see if we have a matching id in the date_range
 365          if(!($row = $db->fetchByAssoc($result))){
 366              //if not lets check if we have one that matches the values
 367  
 368              $result = $db->query($select_by_values);
 369              if(!($row = $db->fetchByAssoc($result))){
 370  
 371                  $result = $db->query($select_by_id);
 372                  if($row = $db->fetchByAssoc($result)){
 373  
 374                      $db->query($update ."'".$row['id']."'" );
 375                      $ids[] = $row['id'];
 376                      $modify++;
 377                  }else{
 378                      $db->query($insert);
 379                      $add++;
 380                      $ids[] = $row['id'];
 381                  }
 382              }
 383      }
 384  
 385      }
 386      return array('add'=>$add, 'modify'=>$modify, 'ids'=>$ids);
 387  }
 388  
 389  /*
 390   *
 391   * gets the from statement from a query without the order by and without the select
 392   *
 393   */
 394  function get_from_statement($query){
 395      $query = explode('FROM', $query);
 396      if(sizeof($query) == 1){
 397          $query = explode('from', $query[0]);
 398      }
 399      $query = explode( 'ORDER BY',$query[1]);
 400  
 401      return ' FROM ' . $query[0];
 402  
 403  }
 404  
 405  function retrieve_relationship_query($module_name,  $related_module, $relationship_query, $show_deleted, $offset, $max_results){
 406      global  $beanList, $beanFiles, $dictionary, $current_user;
 407      $error = new SoapError();
 408      $result_list = array();
 409      if(empty($beanList[$module_name]) || empty($beanList[$related_module])){
 410  
 411          $error->set_error('no_module');
 412          return array('query' =>"", 'module_1'=>"", 'join_table' =>"", 'error'=>$error->get_soap_array());
 413      }
 414  
 415      $row = retrieve_relationships_properties($module_name, $related_module);
 416      if(empty($row)){
 417  
 418          $error->set_error('no_relationship_support');
 419          return array('query' =>"", 'module_1'=>"", 'join_table' =>"", 'error'=>$error->get_soap_array());
 420      }
 421  
 422      $module_1 = $row['lhs_module'];
 423      $mod_key = $row['join_key_lhs'];
 424      $module_2 = $row['rhs_module'];
 425      $mod2_key = $row['join_key_rhs'];
 426  
 427      $table = $row['join_table'];
 428      if(empty($table)){
 429          return array('query' =>"", 'module_1'=>"", 'join_table' =>"", 'error'=>$error->get_soap_array());
 430      }
 431      $class_name = $beanList[$module_1];
 432      require_once($beanFiles[$class_name]);
 433      $mod = new $class_name();
 434  
 435      $mod2_name = $beanList[$module_2];
 436      require_once($beanFiles[$mod2_name]);
 437      $mod2 = new $mod2_name();
 438      $query = "SELECT rt.* FROM  $table rt ";
 439      $query .= " inner join $mod->table_name m1 on rt.$mod_key = m1.id ";
 440      $query .= " inner join $mod2->table_name m2 on rt.$mod2_key = m2.id  ";
 441  
 442  
 443  
 444  
 445  
 446  
 447  
 448  
 449  
 450  
 451  
 452  
 453  
 454  
 455  
 456  
 457  
 458  
 459  
 460  
 461  
 462  
 463  
 464  
 465  
 466  
 467  
 468      if(!empty($relationship_query)){
 469          $query .= ' WHERE ' . $relationship_query;
 470      }
 471  
 472      return array('query' =>$query, 'module_1'=>$module_1, 'join_table' => $table, 'error'=>$error->get_soap_array());
 473  }
 474  
 475  // Returns name of 'link' field between two given modules
 476  function get_module_link_field($module_1, $module_2) {
 477      global $beanList, $beanFiles;
 478  
 479      // check to make sure both modules exist
 480      if (empty($beanList[$module_1]) || empty($beanList[$module_2])) {
 481          return FALSE;
 482      }
 483  
 484      $class_1 = $beanList[$module_1];
 485      require_once($beanFiles[$class_1]);
 486  
 487      $obj_1 = new $class_1();
 488  
 489      // loop through link fields of $module_1, checking for a link to $module_2
 490      foreach ($obj_1->get_linked_fields() as $linked_field) {
 491          $obj_1->load_relationship($linked_field['name']);
 492          $field = $linked_field['name'];
 493  
 494          if (empty($obj_1->$field)) {
 495              continue;
 496          }
 497  
 498          if ($obj_1->$field->getRelatedModuleName() == $module_2) {
 499              return $field;
 500          }
 501      }
 502  
 503      return FALSE;
 504  }
 505  
 506  // Retrieves array of ids for records of $get_module linked to $from_module by $get_id
 507  // Example: to retrieve list of Contacts associated to Account X: $return = get_linked_records("Contacts", "Accounts", "X");
 508  function get_linked_records($get_module, $from_module, $get_id) {
 509      global $beanList, $beanFiles;
 510  
 511      // instantiate and retrieve $from_module
 512      $from_class = $beanList[$from_module];
 513      require_once($beanFiles[$from_class]);
 514      $from_mod = new $from_class();
 515      $from_mod->retrieve($get_id);
 516  
 517      $field = get_module_link_field($from_module, $get_module);
 518      if ($field === FALSE) {
 519          return FALSE;
 520      }
 521  
 522      $from_mod->load_relationship($field);
 523      $id_arr = $from_mod->$field->get();
 524  
 525      return $id_arr;
 526  }
 527  
 528  ?>


Generé en: Thu Mar 4 09:44:50 2010 | Cross-referenced par PHPXref 0.7