You are here

function simplenews_nodeapi in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_nodeapi()
  2. 6 simplenews.module \simplenews_nodeapi()

Implementation of hook_nodeapi().

File

./simplenews.module, line 605
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_nodeapi(&$node, $op, $teaser, $page) {
  global $user;

  // Operate only on node types set in 'simplenews_content_types' variable.
  if (!in_array($node->type, variable_get('simplenews_content_types', array(
    'simplenews',
  )))) {
    return;
  }
  switch ($op) {
    case 'alter':

      // Don't replace the tokens when node alter is called by simplenews_mail.
      if (!isset($node->simplenews_mail)) {
        $tid = db_result(db_query("\n          SELECT s.tid\n          FROM {simplenews_newsletters} s\n          INNER JOIN {node} n\n            ON s.nid = n.nid\n          WHERE n.nid = %d", $node->nid));
        global $language, $user;
        $context = array(
          'node' => $node,
          // note the context represents the current user account for presentation.
          'account' => $user,
          'newsletter' => taxonomy_get_term($tid),
        );

        // Check for function_exists to avoid fatal errors in 6--1 to 6--2 upgrade paths.
        if (isset($node->body) && function_exists('token_replace')) {
          $node->body = token_replace($node->body, 'simplenews', $context);
        }
        if (isset($node->teaser) && function_exists('token_replace')) {
          $node->teaser = token_replace($node->teaser, 'simplenews', $context);
        }
      }
      break;
    case 'validate':
      $vid = variable_get('simplenews_vid', '');
      if (!isset($node->taxonomy[$vid]) || empty($node->taxonomy[$vid]) || simplenews_validate_taxonomy($node->taxonomy) == FALSE) {
        form_set_error('taxonomy', t('No newsletter term is selected, the newsletter taxonomy term is probably not configured correctly.<br /> Check and <strong>save</strong> the <a href="@settings">Simplenews general settings</a>.', array(
          '%name' => taxonomy_vocabulary_load($vid)->name,
          '@settings' => url('admin/settings/simplenews/general'),
        )));
      }
      break;
    case 'insert':
    case 'update':

      // Initialize $node->simplenews in case it does not exist
      // (which should be the default case)
      if (empty($node->simplenews)) {
        $node->simplenews = _simplenews_get_newsletter_settings($node);
      }
      if (empty($node->simplenews)) {
        $node->simplenews = _simplenews_get_node_defaults();
      }

      // Get Simplenews vid
      $simplenews_vid = variable_get('simplenews_vid', '');

      // Drupal changed its taxonomy format on the insert and update operations
      // in version 6.24.
      $first_value = reset($node->taxonomy);

      // Drupal <= 6.23: $node->taxonomy[vid] = tid
      if (is_int($first_value)) {
        $node->simplenews['tid'] = $node->taxonomy[$simplenews_vid];
      }
      elseif (is_object($first_value)) {
        foreach ($node->taxonomy as $tid => $term) {
          if ($term->vid == $simplenews_vid) {
            $node->simplenews['tid'] = $term->tid;
          }
        }
      }

      // Call the same update function as tab submit function
      simplenews_newsletter_update($node, $node->simplenews);
      break;
    case 'delete':
      $result = db_query('
        DELETE FROM {simplenews_newsletters}
        WHERE nid = %d', $node->nid);
      if ($result) {
        drupal_set_message(t('Newsletter %title was deleted.', array(
          '%title' => $node->title,
        )));
      }

      // Check if pending emails of this newsletter issue exist and delete these too.
      module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
      $count = simplenews_clear_spool_node($node);
      if ($count) {
        drupal_set_message(t('Newsletter %title was deleted but had @count pending emails. They can no longer be sent.', array(
          '%title' => $node->title,
          '@count' => $count,
        )), 'warning');
        watchdog('simplenews', 'Newsletter %title deleted with @count pending emails. Emails can no longer be sent.', array(
          '%title' => $node->title,
          '@count' => $count,
        ), WATCHDOG_ALERT);
      }
      break;
    case 'load':

      // @todo: {simplenews_newsletters} data might be missing if an uninstall happened at some time with simplenews nodes created. provide reasonable defaults.
      $newsletter = db_fetch_array(db_query('
        SELECT *
        FROM {simplenews_newsletters}
        WHERE nid = %d', $node->nid));
      return array(
        'simplenews' => $newsletter,
      );
      break;
  }
}