Tagged: drupal

add task to cron api (Elysia Cron)

cron.php
/**
 * Use Elysia Cron to run jobs
 */
function cga_import_cronapi($op, $job = NULL) {

  $items['download_house_status_sheet'] = array(
    'description' => 'Download House Status Sheet',
    'rule' => '0 0,2,18,20,22 * * *', // 12am, 2am, 6pm, 8pm, 10pm
    'callback' => 'cga_import_download_house_status_sheet',
  );
  $items['download_senate_status_sheet'] = array(
    'description' => 'Download Senate Status Sheet',
    'rule' => '5 0,2,18,20,22 * * *', // 12am, 2am, 6pm, 8pm, 10pm
    'callback' => 'cga_import_download_senate_status_sheet',
  );
  $items['download_senate_journal'] = array(
    'description' => 'Download Senate Journal',
    'rule' => '10 0,2,18,20,22 * * *', // 12am, 2am, 6pm, 8pm, 10pm
    'callback' => 'cga_import_download_senate_journal',
  );
  $items['download_house_journal'] = array(
    'description' => 'Download House Journal',
    'rule' => '15 0,2,18,20,22 * * *', // 12am, 2am, 6pm, 8pm, 10pm
    'callback' => 'cga_import_download_house_journal',
  );
  return $items;
}
function cga_import_download_house_status_sheet() {
  $url = 'http://192.70.175.128/Public/cgaAPI.nsf/api.xsp/schedules/house/ss';
  cga_import_cron_get_session_doc($url, "house_status_sheet");
}
function cga_import_cron_get_senate_status_sheet() {
  $url = 'http://192.70.175.128/Public/cgaAPI.nsf/api.xsp/schedules/senate/ss';
  cga_import_cron_get_session_doc($url, "senate_status_sheet");
}
function cga_import_cron_get_senate_journal() {
  $url = 'http://192.70.175.128/Public/cgaAPI.nsf/api.xsp/schedules/senate/journal';
  cga_import_cron_get_session_doc($url, "senate_journal");
}
function cga_import_cron_get_house_journal() {
  $url = 'http://192.70.175.128/Public/cgaAPI.nsf/api.xsp/schedules/house/journal';
  cga_import_cron_get_session_doc($url, "house_journal");
}

Session Docs template

bean--chamber-publications--default.tpl.php
<div id="status_sheets" class="document-container">
   <h4 class="document-section-title" >Status of Bills:</h4>
      <a href="<?php print $content['field_house_status_sheet'][0]['#markup'] . "?t=$last_changed"; ?>" class="chamber-document cta-button <?php print cga_entity_queries_in_session() ? '' : 'disabled'; ?>" >House Status Sheet</a>
      <a href="<?php print $content['field_senate_status_sheet'][0]['#markup'] . "?t=$last_changed"; ?>" class="chamber-document cta-button <?php print cga_entity_queries_in_session() ? '' : 'disabled'; ?>" >Senate Status Sheet</a>
</div>
<div id="journals" class="document-container">
    <h4 class="document-section-title">Cumulative Journals:</h4>
      <a href="<?php print $content['field_house_status_sheet'][0]['#markup'] . "?t=$last_changed"; ?>" class="chamber-document cta-button <?php print cga_entity_queries_in_session() ? '' : 'disabled'; ?>" >House Status Sheet</a>
      <a href="<?php print $content['field_senate_status_sheet'][0]['#markup'] . "?t=$last_changed"; ?>" class="chamber-document cta-button <?php print cga_entity_queries_in_session() ? '' : 'disabled'; ?>" >Senate Status Sheet</a>
</div>

htaccess prevent pdf caching

.htaccess
<FilesMatch ".(pdf)$">
  FileETag None
  <ifModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</FilesMatch>

Clear Node's Cache in hook_node_update

clear_cache_node_update.php
function cga_import_node_update($node) {
  $nodeurl = url('node/'. $node->nid);
  cache_clear_all($nodeurl, 'cache_page');
}

// The above doesnay work, below does but it is overkill.
$core = array('cache', 'cache_path', 'cache_filter', 'cache_bootstrap', 'cache_page');
$cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
foreach ($cache_tables as $table) {
  cache_clear_all('*', $table, TRUE);
}

// This works and is more narrow
$cache_tables = array('cache_field', 'cache_block');
foreach ($cache_tables as $table) {
  cache_clear_all('*', $table, TRUE);
}

// field:node:[nid]
// 
cache_clear_all('*', 'cache_field', TRUE);

Get Block ID from Block Type

cga_utilities.php
<?php
/**
 * Utility function to get Block ID for a certain block type
 */
function cga_import_get_id_of_block_type($block_type) {
  $bids = array();

  $query = new EntityFieldQuery();
  $result = $query
   ->entityCondition('entity_type', 'bean')
   ->entityCondition('bundle', $block_type)
   ->execute();
  //get bean ids
  if (isset($result['bean'])) {
   $bids = array_keys($result['bean']);
  }
  return array_shift($bids);
}

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);                                                                                 
  }                                                                                                                                               
}