You are here

function faq_ask_form_submit in FAQ_Ask 6

Implementation of hook_form_submit(). This function gets the entered question and category and creates an unpublished FAQ node.

File

./faq_ask.module, line 301
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_form_submit($form, &$form_state) {
  global $user;
  $form_values = $form_state['values'];
  $category = $form_state['values']['category'];
  if ($category == 0 && empty($form_state['values']['new_cat'])) {

    // Can only be 0 if allowing category suggestion.
  }
  else {
    if ($category == 0) {

      // We have a new category to process.
      $new_cat = $form_state['values']['new_cat'];
      $term = array(
        'name' => $new_cat,
        'description' => t('Suggested by @name (!uid) on @date', array(
          '@name' => $user->name,
          '!uid' => $user->uid,
          '@date' => date('F j, Y'),
        )),
        'vid' => $form_state['values']['faq_vid'],
        'weight' => 0,
        'parent' => 0,
        'tid' => NULL,
      );
      taxonomy_save_term($term);
      $term = (object) $term;

      // Setting up the expert will be done in hook_taxonomy.
    }
    else {
      $term = taxonomy_get_term($category);
    }
    $node = array(
      'type' => 'faq',
      'body' => '',
      /* Empty string rather than NULL. */
      'title' => $form_state['values']['title'],
      'taxonomy' => $category >= 0 ? array(
        $category => $term,
      ) : NULL,
      'created' => time(),
      'uid' => $user->uid,
      'name' => $user->uid ? $user->name : variable_get('anonymous', t('Anonymous')),
      'status' => 0,
      /* Unpublished. */
      'format' => 1,
      /* Default filter (filtered HTML) */
      'comment' => variable_get('comment_faq', 0),
      'detailed_question' => isset($form_state['values']['detailed_question']) ? $form_state['values']['detailed_question'] : '',
    );
    $node_options = variable_get('node_options_' . $node['type'], array(
      'status',
      'promote',
    ));
    foreach (array(
      'promote',
      'sticky',
      'revision',
    ) as $key) {
      $node[$key] = in_array($key, $node_options) ? 1 : 0;
    }
    if ($form_state['values']['nid']) {
      $node['nid'] = $form_state['values']['nid'];
    }

    // Okay, let's get it done. Node_submit will prepare it and make it an object.
    $node = node_submit($node);
    node_save($node);

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

      // $from = variable_get('site_name', 'Drupal') .'<'. variable_get('site_mail', ini_get('sendmail_from')) .'>';
      $params = array(
        'category' => $category >= 0 ? check_plain($term->name) : -1,
        'question' => $node->title,
        'nid' => $node->nid,
        'creator' => theme('username', $node, array(
          'plain' => TRUE,
        )),
      );

      // Find out who the experts are.
      $result = db_query('SELECT uid FROM {faq_expert} WHERE tid=%d', $category);
      while ($expert = db_fetch_array($result)) {
        $account = user_load(array(
          'uid' => $expert['uid'],
        ));
        $params['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),
          )));
        }
      }
    }
    drupal_set_message(t('Your question has been submitted. It will appear in the FAQ listing as soon as it has been answered.'), 'status');
    drupal_goto('faq' . ($category > 0 ? '/' . $category : NULL));
  }
}