You are here

function rate_expiration_node_insert in Rate 7

Implements hook_node_insert().

File

expiration/rate_expiration.module, line 189

Code

function rate_expiration_node_insert($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) {
    $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} ? (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_insert('rate_expiration')
        ->fields(array(
        'nid' => $node->nid,
        'widget_name' => $widget->name,
        'start' => $begin,
        'end' => $end,
      ))
        ->execute();
    }
  }
}