You are here

function simplenews_scheduler_nodeapi in Simplenews Scheduler 6

Same name and namespace in other branches
  1. 5 simplenews_scheduler.module \simplenews_scheduler_nodeapi()
  2. 6.2 simplenews_scheduler.module \simplenews_scheduler_nodeapi()

Implementation of hook_nodeapi().

File

./simplenews_scheduler.module, line 183
Simplenews Scheduler module allows a schedule to be set for sending (and resending) a Simplenews item.

Code

function simplenews_scheduler_nodeapi(&$node, $op) {
  if ($node->nid && in_array($node->type, variable_get('simplenews_content_types', array(
    'simplenews',
  )))) {

    // Get Scheduler values from Simplenews.
    $send = $node->simplenews['send'];
    $activated = $node->simplenews['activated'];
    $interval = $node->simplenews['interval'];
    $start_date = $node->simplenews['start_date'];
    $stop = $node->simplenews['stop'];
    $stop_date = $node->simplenews['stop_date'];
    $stop_edition = $node->simplenews['stop_edition'];

    // Change activation status if necessary.
    switch ($send) {
      case 0:
      case 1:
        $activated = 0;
        break;
      case 3:
        $activated = 1;
        break;
    }

    // Convert the user time back to GMT time and use that as our record.
    $start_date = date_convert($start_date, DATE_DATETIME, DATE_UNIX, date_default_timezone_name());
    $stop_date = date_convert($stop_date, DATE_DATETIME, DATE_UNIX, date_default_timezone_name());
    switch ($op) {
      case 'insert':
      case 'update':
        if (!$node->is_edition) {
          $result = db_query("INSERT INTO {simplenews_scheduler} (nid, activated, interval, start_date, stop, stop_date, stop_edition) VALUES (%d, %d, '%s', %d, %d, %d, %d) \n                            ON DUPLICATE KEY UPDATE last_run = 0, activated = %d, interval = '%s', start_date = %d, stop = %d, stop_date = %d, stop_edition = %d", $node->nid, $activated, $interval, $start_date, $stop, $stop_date, $stop_edition, $activated, $interval, $start_date, $stop, $stop_date, $stop_edition, $node->nid);
          if (!$result) {
            drupal_set_message(t('Saving or updating schedule settings for <em>@title</em> has been unsuccessful.', array(
              '@title' => $node->title,
            )), 'error');
          }
        }
        break;
      case 'load':
        $result = db_query("SELECT * FROM {simplenews_scheduler} WHERE nid = %d", $node->nid);
        $row = db_fetch_array($result);
        if ($row) {
          $node->simplenews_scheduler = $row;
        }
        else {

          // Maybe this was an edition that has been sent?
          $result = db_query("SELECT * FROM {simplenews_scheduler_editions} WHERE eid = %d", $node->nid);
          $row = db_fetch_array($result);
          if ($row) {
            $node->simplenews_scheduler_edition = $row;
          }
        }
        break;
    }
  }
}