You are here

function faq_order_settings_form in Frequently Asked Questions 6

Same name and namespace in other branches
  1. 7.2 faq.admin.inc \faq_order_settings_form()
  2. 7 faq.admin.inc \faq_order_settings_form()

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

Parameters

$form_state: Store the submitted form values.

Return value

The form code, before being converted to HTML format.

1 string reference to 'faq_order_settings_form'
faq_menu in ./faq.module
Implements hook_menu().

File

./faq.admin.inc, line 355
Administrative page callbacks for the faq module.

Code

function faq_order_settings_form($form_state, $category = NULL) {
  $order = $date_order = '';
  drupal_add_js(array(
    'faq' => array(
      'faq_hide_qa_accordion' => variable_get('faq_hide_qa_accordion', FALSE),
    ),
  ), 'setting');
  drupal_add_js(array(
    'faq' => array(
      'faq_category_hide_qa_accordion' => variable_get('faq_category_hide_qa_accordion', FALSE),
    ),
  ), 'setting');
  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_state['values']) && empty($category)) {
    $step = "categories";
  }
  else {
    $step = "order";
  }
  $form['step'] = array(
    '#type' => 'value',
    '#value' => $step,
  );

  // 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] = faq_tt("taxonomy:term:{$term->tid}:name", $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'),
          '#submit' => array(
            'faq_order_settings_choose_cat_form_submit',
          ),
        );
      }
    }
  }
  else {
    $default_sorting = variable_get('faq_default_sorting', 'DESC');
    $default_weight = 0;
    if ($default_sorting != 'DESC') {
      $default_weight = 1000000;
    }
    $options = array();
    if (!empty($form_state['values']['faq_category'])) {
      $category = $form_state['values']['faq_category'];
    }

    // Uncategorized ordering.
    if (empty($category)) {
      $category = 0;

      // Descending.
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, COALESCE(w.weight, %d) as effective_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 effective_weight ASC, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, COALESCE(w.weight, %d) as effective_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 effective_weight ASC, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category);
      }
    }
    else {

      // Descending.
      if ($default_sorting == 'DESC') {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, COALESCE(w.weight, %d) as effective_weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON (n.nid = tn.nid AND n.vid = tn.vid) 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 effective_weight ASC, n.sticky DESC, n.created DESC", "n", "nid"), $default_weight, $category, $category);
      }
      else {
        $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, COALESCE(w.weight, %d) as effective_weight, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON (n.nid = tn.nid AND n.vid = tn.vid) 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 effective_weight ASC, n.sticky DESC, n.created ASC", "n", "nid"), $default_weight, $category, $category);
      }
    }
    while ($node = db_fetch_object($result)) {
      $options[$node->nid] = check_plain($node->title);
    }
    $form['weight']['faq_category'] = array(
      '#type' => 'value',
      '#value' => $category,
    );

    // Show table ordering form.
    $form['order_no_cats']['#tree'] = TRUE;
    $form['order_no_cats']['#theme'] = 'faq_draggable_question_order_table';
    $i = 0;
    foreach ($options as $nid => $title) {
      $form['order_no_cats'][$i]['nid'] = array(
        '#type' => 'hidden',
        '#value' => $nid,
      );
      $form['order_no_cats'][$i]['title'] = array(
        '#value' => $title,
      );
      $form['order_no_cats'][$i]['sort'] = array(
        '#type' => 'weight',
        '#delta' => count($options),
        '#default_value' => $i,
      );
      $i++;
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save order'),
      '#weight' => 3,
      '#submit' => array(
        'faq_order_settings_reorder_form_submit',
      ),
    );
  }
  return $form;
}