Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * get a record by the passed-in GUID. return false if it didn't find it.
- */
- function get_contribution_by_id($id){
- $contribution = BeanFactory::getBean('Contr_Contributions',$id);
- if(!empty($contribution->id)){
- return $contribution;
- } else {
- return false;
- }
- }
- /*
- * get a record by the passed-in name. return false if it didn't find it.
- * problematic if we find more than one, so we'll assume (not safe) that
- * 'name' is treated like it's unique. If we did get 1+ then we just return
- * the first that comes back
- */
- function get_committee_id_by_name($name){
- $focus = new Campa_CampaignCommittee();
- $where = "( campa_campaigncommittee.name = '{$name}' ) ";
- $ret_array = $focus->create_new_list_query(null, $where,null,null,0,null,true,null,true,false);
- $query = $ret_array['select'] . $ret_array['from'] . $ret_array['where']. $ret_array['order_by'];
- $result = $focus->db->query($query);
- while($row = $focus->db->fetchByAssoc($result)){
- return $row['id'];
- }
- return false;
- }
- /*
- * Copypasta from a spreadsheet, formatted to an array
- * module 1 GUIDs and module 2 names.
- * redacted for customer privacy... imagine this array has like 1000+ records
- */
- $a = array(
- "101011b7-4acb-3236-54e7-50d0e33c35a0" => "Some Name",
- "101536ee-68c1-45ae-63af-50d0e3df6d12" => "Some Other Name",
- "fd7903d2-8db0-6438-7ce8-50d0e34f022b" => "A Third Name",
- );
- /*
- * result holders
- */
- $sql = array();
- $results = array(
- 'starttime' => '',
- 'success' => array(),
- 'errors' => array(),
- 'endtime' => '',
- );
- $key = 0;
- /*
- * begin. attempt to update the Contr_Contributions (by GUID) to the
- * corresponding Campa_CampaignCommittee (by name) according to the $a array
- * mapping. Log errors and success and at the same time store the SQL that
- * would accomplish this.
- */
- $results['starttime'] = date('U');
- foreach($a as $contr_contributions_id => $campa_campaigncommittee_name){
- $contribution = get_contribution_by_id($contr_contributions_id);
- $camp_committee = get_committee_id_by_name($campa_campaigncommittee_name);
- if($contribution && $camp_committee){
- $contribution->campa_campaigncommittee_id_c = $camp_committee;
- $contribution->save();
- $results['success'][$key] = $contr_contributions_id;
- $sql[] = " update contr_contributions_cstm set campa_campaigncommittee_id_c = '{$camp_committee}' where contr_contributions_cstm.id_c = '{$contribution->id}' ; ";
- } elseif(!$contribution) {
- $results['errors'][$key] = $contr_contributions_id;
- } elseif(!$camp_committee) {
- $results['errors'][$key] = $campa_campaigncommittee_name;
- }
- $key++;
- // if($key > 5) break;
- }
- $results['endtime'] = date('U');
- /*
- * print results and all of the SQL that we generated. We'll send the SQL
- * straight to the production database.
- */
- var_dump($results);
- foreach($sql as $s){
- echo $s . "<br />";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement