Download Session Docs

cga_import.admin.php
/**                                                                                                                                               
 * Downloads a session document from given api url                                                                                                
 */                                                                                                                                               
function cga_import_download_session_doc($api_url, $doc_type) {                                                                                   
  $json = file_get_contents($api_url);                                                                                                            
  $decoded = json_decode($json);                                                                                                                  
  if(isset($decoded[0]->data)) {                                                                                                                  
    $url = $decoded[0]->data[3]->url;                                                                                                             
    // Update the sessions docs bean with new documents                                                                                           
    $cal_bean = bean_load(71);                                                                                                                    
    //$cal_bean = bean_load(67);                                                                                                                  
    $bean_wrapper = entity_metadata_wrapper('bean', $cal_bean);                                                                                   
    $file_info = system_retrieve_file($url, 'public://', TRUE, FILE_EXISTS_REPLACE);                                                              
    if (!empty($file_info)) {                                                                                                                     
      switch($doc_type) {                                                                                                                         
        case "senate_status_sheet":                                                                                                               
          $bean_wrapper->field_senate_status_sheet->file->set($file_info);                                                                        
          break;                                                                                                                                  
        case "house_status_sheet":                                                                                                                
          $bean_wrapper->field_house_status_sheet->file->set($file_info);                                                                         
          break;                                                                                                                                  
        case "senate_journal":                                                                                                                    
          $bean_wrapper->field_senate_journal->file->set($file_info);                                                                             
          break;                                                                                                                                  
        case "house_journal":                                                                                                                     
          $bean_wrapper->field_house_journal->file->set($file_info);                                                                              
          break;                                                                                                                                  
      }                                                                                                                                           
      $bean_wrapper->save();                                                                                                                      
    }                                                                                                                                             
    watchdog("cga_import", "success downloading json: " . $json);                                                                                 
  }                                                                                                                                               
  else {                                                                                                                                          
    watchdog("cga_import", "trouble downloading json: " . $json);                                                                                 
  }                                                                                                                                               
}