You are here

function faq_weight_settings_form in Frequently Asked Questions 5

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

Define a form to edit the q/a weights

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

File

./faq.module, line 427

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' => 'hidden',
    '#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) {
        $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'];
    if (empty($category)) {
      $category = 0;
      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), 0, w.weight) as weight 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"), $category);
      $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), 0, w.weight) as weight 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"), $category);
    }
    else {
      $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), 0, w.weight) as weight FROM {node} n LEFT JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.vid 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"), $category, $category);
      $date_result = db_query(db_rewrite_sql("SELECT n.nid, n.title, if((w.weight IS NULL), 0, w.weight) as weight FROM {node} n LEFT JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = n.vid 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"), $category, $category);
    }
    while ($node = db_fetch_object($result)) {
      $options[$node->nid] = $node->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' => 'hidden',
      '#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('Update'),
      '#weight' => 3,
      '#attributes' => array(
        'onclick' => 'faq_update_order();',
      ),
    );
    $form['#redirect'] = "admin/settings/faq/weight";
  }
  return $form;
}