Advertisement
matthewpoer

SugarCRM Match IDs to Names, relate (Michigan)

May 17th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * get a record by the passed-in GUID. return false if it didn't find it.
  5.  */
  6. function get_contribution_by_id($id){
  7.     $contribution = BeanFactory::getBean('Contr_Contributions',$id);
  8.     if(!empty($contribution->id)){
  9.         return $contribution;
  10.     } else {
  11.         return false;
  12.     }
  13. }
  14.  
  15. /*
  16.  * get a record by the passed-in name. return false if it didn't find it.
  17.  * problematic if we find more than one, so we'll assume (not safe) that
  18.  * 'name' is treated like it's unique. If we did get 1+ then we just return
  19.  * the first that comes back
  20.  */
  21. function get_committee_id_by_name($name){
  22.     $focus = new Campa_CampaignCommittee();
  23.     $where = "( campa_campaigncommittee.name = '{$name}' ) ";
  24.     $ret_array = $focus->create_new_list_query(null, $where,null,null,0,null,true,null,true,false);
  25.     $query = $ret_array['select'] . $ret_array['from'] . $ret_array['where']. $ret_array['order_by'];
  26.     $result = $focus->db->query($query);
  27.     while($row = $focus->db->fetchByAssoc($result)){
  28.         return $row['id'];
  29.     }
  30.     return false;
  31. }
  32.  
  33. /*
  34.  * Copypasta from a spreadsheet, formatted to an array
  35.  * module 1 GUIDs and module 2 names.
  36.  * redacted for customer privacy... imagine this array has like 1000+ records
  37.  */
  38. $a = array(
  39. "101011b7-4acb-3236-54e7-50d0e33c35a0" => "Some Name",
  40. "101536ee-68c1-45ae-63af-50d0e3df6d12" => "Some Other Name",
  41. "fd7903d2-8db0-6438-7ce8-50d0e34f022b" => "A Third Name",
  42. );
  43.  
  44. /*
  45.  * result holders
  46.  */
  47. $sql = array();
  48. $results = array(
  49.     'starttime' => '',
  50.     'success' => array(),
  51.     'errors' => array(),
  52.     'endtime' => '',
  53. );
  54. $key = 0;
  55.  
  56. /*
  57.  * begin. attempt to update the Contr_Contributions (by GUID) to the
  58.  * corresponding Campa_CampaignCommittee (by name) according to the $a array
  59.  * mapping. Log errors and success and at the same time store the SQL that
  60.  * would accomplish this.
  61.  */
  62. $results['starttime'] = date('U');
  63. foreach($a as $contr_contributions_id => $campa_campaigncommittee_name){
  64.     $contribution = get_contribution_by_id($contr_contributions_id);
  65.     $camp_committee = get_committee_id_by_name($campa_campaigncommittee_name);
  66.     if($contribution && $camp_committee){
  67.         $contribution->campa_campaigncommittee_id_c = $camp_committee;
  68.         $contribution->save();
  69.         $results['success'][$key] = $contr_contributions_id;
  70.         $sql[] = " update contr_contributions_cstm set campa_campaigncommittee_id_c = '{$camp_committee}' where contr_contributions_cstm.id_c = '{$contribution->id}' ; ";
  71.     } elseif(!$contribution) {
  72.         $results['errors'][$key] = $contr_contributions_id;
  73.     } elseif(!$camp_committee) {
  74.         $results['errors'][$key] = $campa_campaigncommittee_name;
  75.     }
  76.     $key++;
  77.     // if($key > 5) break;
  78. }
  79. $results['endtime'] = date('U');
  80.  
  81. /*
  82.  * print results and all of the SQL that we generated. We'll send the SQL
  83.  * straight to the production database.
  84.  */
  85. var_dump($results);
  86. foreach($sql as $s){
  87.     echo $s . "<br />";
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement