You are here

function faq_ask_node_insert in FAQ_Ask 7

Same name and namespace in other branches
  1. 8 faq_ask.module \faq_ask_node_insert()

Implementation of hook_node_insert()

Handles the creation of a question node after the node is created. This ensures that the node ID is available, needed for sending e-mail notifications

Parameters

object $node: Node object to handle

File

./faq_ask.module, line 444
This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.

Code

function faq_ask_node_insert($node) {
  global $user;
  if ($node->type == 'faq') {

    // Handle only faq node types
    $terms = _faq_ask_get_terms($node);

    // Update the faq_ask_term_index table if node is unpublished
    if ($node->status == '0') {
      db_delete('faq_ask_term_index')
        ->condition('nid', $node->nid)
        ->execute();
      foreach ($terms as $tid => $term) {
        if ($tid) {

          // If term is available
          db_insert('faq_ask_term_index')
            ->fields(array(
            'nid' => $node->nid,
            'tid' => $tid,
            'sticky' => $node->sticky,
            'created' => $node->created,
          ))
            ->execute();
        }
      }
    }

    // Are we notifying the expert(s)?
    if (variable_get('faq_ask_notify', FALSE)) {

      // Use only the first term entered in the correct vocabulary.
      $term = taxonomy_term_load(array_shift(array_keys($terms)));

      // Find out who the experts are.
      $query = db_select('faq_expert', 'fe')
        ->fields('fe', array(
        'uid',
      ))
        ->condition('fe.tid', array_keys($terms), 'IN');
      $experts = $query
        ->execute()
        ->fetchAll();
      foreach ($experts as $expert) {
        $account = user_load($expert->uid);
        $params = array(
          'category' => is_object($term) ? $term->tid : -1,
          'question' => $node->title,
          'question_details' => $node->detailed_question,
          'nid' => $node->nid,
          'creator' => theme('username', array(
            'account' => user_load($node->uid),
            'plain' => TRUE,
          )),
          'account' => $account,
        );
        $mail_sent = drupal_mail('faq_ask', 'notify_expert', $account->mail, user_preferred_language($account), $params);
        if ($mail_sent) {
          watchdog('FAQ_Ask', 'Expert notification email sent to @to', array(
            '@to' => $account->mail,
          ), WATCHDOG_NOTICE);
        }
        else {
          watchdog('FAQ_Ask', 'Expert notification email to @to failed for the "@cat" category.', array(
            '@to' => $account->mail,
            '@cat' => check_plain($term->name),
          ), WATCHDOG_ERROR);
          drupal_set_message(t('Expert notification email failed for the "@cat" category.', array(
            '@cat' => check_plain($term->name),
          )));
        }
      }
    }

    // Save this is the node to be created
    $asker_email = '';

    // Handle the notification of asker
    if (isset($node->faq_email) && $node->faq_email) {
      $asker_email = $node->faq_email;

      // If this user is not registered as a user before - check if all asking anonymous users should be added to the newsletter list
      if (module_exists('simplenews') && ($tid = variable_get('faq_ask_notify_asker_simplenews_tid', '0'))) {

        // If we have selected a newsletter to add
        if (function_exists('simplenews_subscribe_user')) {
          simplenews_subscribe_user($asker_email, $tid, variable_get('faq_ask_notify_asker_simplenews_confirm', 1), 'FAQ-Ask');
        }
      }
      if (module_exists('mailchimp') && ($lid = variable_get('faq_ask_notify_asker_mailchimp_lid', '0') && function_exists('mailchimp_get_list'))) {

        // If we have selected a newsletter to add
        // dpm($lid, 'list ID');
        $list = mailchimp_get_list($lid);

        // dpm($list, 'list');
        if (function_exists('mailchimp_subscribe_user') && !empty($list)) {

          // Add optional groupings information like terms
          $merge_vars = array(
            'GROUPINGS' => array(
              'groups' => 'FAQ-Ask',
            ),
          );
          mailchimp_subscribe_user($list, $asker_email, $merge_vars, $message = TRUE, $mcapi = NULL);
        }
      }
    }
    elseif (isset($node->faq_notify) && $node->faq_notify) {
      $asker_email = $user->mail;
    }
    else {
      drupal_set_message(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'), 'status');
    }
    if ($asker_email) {
      _faq_ask_set_faq_notification($node->nid, $asker_email);
      drupal_set_message(t('Your question has been submitted. An e-mail will be sent to <i>@mail</i> when answered.', array(
        '@mail' => $asker_email,
      )), 'status');
    }

    // Handle the notification of asker
  }
}