You are here

function rate_expiration_nodeapi in Rate 6.2

Implements hook_nodeapi().

File

expiration/rate_expiration.module, line 176

Code

function rate_expiration_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  switch ($op) {
    case 'insert':
    case 'update':

      // 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) {
        $timezone = is_null($user->timezone) ? variable_get('date_default_timezone', 0) : $user->timezone;
        $container = 'rate_expiration_' . $widget->name;
        $begin = "{$container}_begin";
        $end = "{$container}_end";
        $begin = $node->{$begin} ? strtotime($node->{$begin} . 'Z') - $timezone : NULL;
        $end = $node->{$end} ? strtotime($node->{$end} . 'Z') - $timezone : NULL;
        if (empty($begin) && empty($end)) {
          $sql = 'DELETE FROM {rate_expiration} WHERE nid = %d';
          db_query($sql, $node->nid);
        }
        else {
          $sql = 'UPDATE {rate_expiration}
          SET start = %d, end = %d
          WHERE nid = %d';
          db_query($sql, $begin, $end, $node->nid);
          if (!db_affected_rows()) {
            $sql = 'INSERT INTO {rate_expiration}
            (nid, widget_name, start, end)
            VALUES
            (%d, \'%s\', %d, %d)';
            db_query($sql, $node->nid, $widget->name, $begin, $end);
          }
        }
      }
      break;
    case 'delete':
      $sql = 'DELETE FROM {rate_expiration} WHERE nid = %d';
      db_query($sql, $node->nid);
      break;
  }
}