You are here

function sharedblocks_cron in Shared Blocks 7

Same name and namespace in other branches
  1. 6 sharedblocks.module \sharedblocks_cron()

Implements hook_cron().

File

./sharedblocks.module, line 127

Code

function sharedblocks_cron() {

  // Check to see if any shared blocks have expired
  $blocks = db_select('sharedblocks', 'sb')
    ->fields('sb', array(
    'id',
    'description',
    'url',
    'block_data',
    'update_interval',
  ))
    ->condition('expiration', REQUEST_TIME, '<')
    ->execute();
  while ($row = $blocks
    ->fetchAssoc()) {

    // If so, grab a new copy of the block data from the url.
    $block_data = sharedblocks_fetch_block($row['url']);
    if ($block_data) {
      $record['block_data'] = serialize($block_data);
      watchdog('sharedblocks', t('Block @description was successfully fetched.', array(
        '@description' => $row['description'],
      )));
    }
    else {
      watchdog('sharedblocks', t('Could not retrieve block from @url', array(
        '@url' => $row['url'],
      )), WATCHDOG_WARNING);
    }

    // Save record to the database.
    $record['id'] = $row['id'];
    $record['expiration'] = REQUEST_TIME + $row['update_interval'];
    drupal_write_record('sharedblocks', $record, 'id');
  }
}