You are here

function faq_weight_settings_form in Frequently Asked Questions 5.2

Same name and namespace in other branches
  1. 5 faq.module \faq_weight_settings_form()

Define the elements for the FAQ Settings page - weight tab.

Parameters

$form_values: The submitted form values.

Return value

The form code, before being converted to HTML format.

1 string reference to 'faq_weight_settings_form'
faq_menu in ./faq.module
Implementation of hook_menu().

File

./faq.module, line 587
The FAQ module allows users to create a FAQ page, with questions and answers displayed in different styles, according to the settings.

Code

function faq_weight_settings_form($form_values = NULL) {
  drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js', 'module');
  drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  $use_categories = variable_get('faq_use_categories', FALSE);
  if (!$use_categories) {
    $step = "order";
  }
  elseif (!isset($form_values)) {
    $step = "categories";
  }
  else {
    $step = "order";
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $step,
  );
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;

  // Categorized q/a.
  if ($step == "categories") {

    // Get list of categories.
    $vocabularies = taxonomy_get_vocabularies('faq');
    $options = array();
    foreach ($vocabularies as $vid => $vobj) {
      $tree = taxonomy_get_tree($vid);
      foreach ($tree as $term) {
        if (!taxonomy_term_count_nodes($term->tid, 'faq')) {
          continue;
        }
        $options[$term->tid] = $term->name;
        $form['choose_cat']['faq_category'] = array(
          '#type' => 'select',
          '#title' => t('Choose a category'),
          '#description' => t('Choose a category that you wish to order the questions for.'),
          '#options' => $options,
          '#multiple' => FALSE,
        );
        $form['choose_cat']['search'] = array(
          '#type' => 'submit',
          '#value' => t('Search'),
        );
      }
    }
  }
  else {
    $options = array();
    $category = $form_values['faq_category'];
    $default_sorting = variable_get('faq_default_sorting', 'DESC');
    $default_weight = 0;
    if ($default_sorting != 'DESC') {
      $default_weight = 1000000;
    }
    if (empty($category)) {
      $category = 0;
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY weight ASC, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY weight ASC, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 ORDER BY n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category);
      }
    }
    else {
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY weight ASC, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category, $category);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY weight ASC, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category, $category);
        $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), %d, w.weight) as weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid LEFT JOIN {faq_weights} w ON n.nid = w.nid AND w.tid = %d WHERE n.type='faq' AND n.status = 1 AND tn.tid = %d ORDER BY n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category, $category);
      }
    }
    while ($node = db_fetch_object($result)) {
      $title = drupal_strlen($node->title) <= 64 ? $node->title : drupal_substr($node->title, 0, 63) . '...';
      $options[$node->nid] = $title;
      $order .= "{$node->nid},";
    }
    $order = rtrim($order, ",");
    while ($node = db_fetch_object($date_result)) {
      $date_options[$node->nid] = $node->title;
      $date_order .= "{$node->nid},";
    }
    $date_order = rtrim($date_order, ",");
    $form['weight']['faq_node_order'] = array(
      '#type' => 'hidden',
      '#default_value' => $order,
    );
    $form['weight']['faq_node_date_order'] = array(
      '#type' => 'hidden',
      '#default_value' => $date_order,
    );
    $form['weight']['faq_category'] = array(
      '#type' => 'value',
      '#value' => $category,
    );
    $asc = '<a href="#" onclick="faq_order_by_date(\'ASC\');">ascending</a>';
    $desc = '<a href="#" onclick="faq_order_by_date(\'DESC\');">descending</a>';
    $form['weight']['order_no_cats'] = array(
      '#type' => 'select',
      '#title' => t('Question Order'),
      '#description' => t("This determines the order of the questions and answers on the FAQ page.  Just select one or more questions and use the arrows to change their position in the list.  You can also order the list by the question creation date !desc or !asc.", array(
        '!desc' => $desc,
        '!asc' => $asc,
      )),
      '#options' => $options,
      '#multiple' => TRUE,
      '#size' => min(20, count($options)),
    );
    $form['weight']['move_up'] = array(
      '#type' => 'markup',
      '#value' => '<input type="button" onclick="faq_move_selected_item_up();"
      value="&uarr;" class="faq-arrow"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
    );
    $form['weight']['move_down'] = array(
      '#type' => 'markup',
      '#value' => '<input type="button" onclick="faq_move_selected_item_down();"
      value="&darr;" class="faq-arrow"/><br />',
    );
    $form['update']['attach'] = array(
      '#type' => 'submit',
      '#value' => t('Save order'),
      '#weight' => 3,
      '#attributes' => array(
        'onclick' => 'faq_update_order();',
      ),
    );
    $form['#redirect'] = "admin/settings/faq/weight";
  }
  return $form;
}