You are here

function rate_expiration_node_update in Rate 7

Implements hook_node_update().

File

expiration/rate_expiration.module, line 225

Code

function rate_expiration_node_update($node) {
  global $user;
  $timezone = is_null($user->timezone) ? variable_get('date_default_timezone', 0) : $user->timezone;
  if (!is_numeric($timezone)) {

    // Convert textual timezone to numeric offset.
    $timezone = new DateTimeZone($timezone);
    $timezone = $timezone
      ->getOffset(new DateTime('now', $timezone));
  }

  // Get active rate widgets and filter widgets without the "allow override" option..
  $widgets = rate_get_active_widgets('node', $node->type);
  $widgets = array_filter($widgets, '_rate_expiration_filter_widgets');
  foreach ($widgets as $widget) {
    $container = 'rate_expiration_' . $widget->name;
    $begin = "{$container}_begin";
    $end = "{$container}_end";
    $begin = $node->{$begin} ? (int) strtotime($node->{$begin} . 'Z') - $timezone : 0;
    $end = $node->{$end} ? (int) strtotime($node->{$end} . 'Z') - $timezone : 0;

    // Correct empty dates which are not 0 cause of timezone handling.
    $begin = $begin <= 86400 ? 0 : $begin;
    $end = $end <= 86400 ? 0 : $end;
    if (empty($begin) && empty($end)) {
      db_delete('rate_expiration')
        ->condition('nid', $node->nid)
        ->condition('widget_name', $widget->name)
        ->execute();
    }
    else {
      $exists = db_select('rate_expiration', 're')
        ->fields('re', array(
        'start',
        'end',
      ))
        ->condition('re.nid', $node->nid)
        ->condition('re.widget_name', $widget->name)
        ->countQuery()
        ->execute()
        ->fetchField();
      if ($exists) {
        db_update('rate_expiration')
          ->fields(array(
          'start' => $begin,
          'end' => $end,
        ))
          ->condition('nid', $node->nid)
          ->condition('widget_name', $widget->name)
          ->execute();
      }
      else {
        db_insert('rate_expiration')
          ->fields(array(
          'nid' => $node->nid,
          'widget_name' => $widget->name,
          'start' => $begin,
          'end' => $end,
        ))
          ->execute();
      }
    }
  }
}