You are here

function faq_page in Frequently Asked Questions 7

Same name and namespace in other branches
  1. 5.2 faq.module \faq_page()
  2. 5 faq.module \faq_page()
  3. 6 faq.module \faq_page()

Function to display the faq page.

Parameters

int $tid: Default is 0, determines if the questions and answers on the page will be shown according to a category or non-categorized.

string $faq_display: Optional parameter to override default question layout setting.

string $category_display: Optional parameter to override default category layout setting.

Return value

string|NULL The output variable which contains an HTML formatted page with FAQ questions and answers.

1 call to faq_page()
_faq_faq_page_filter_replacer in ./faq.module
Helper function for faq input filter.
1 string reference to 'faq_page'
faq_menu in ./faq.module
Implements hook_menu().

File

./faq.module, line 414
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_page($tid = 0, $faq_display = '', $category_display = '') {
  if (module_exists('pathauto')) {
    module_load_include('inc', 'pathauto');
  }
  $faqpath = _faq_path();

  // Things to provide translations for.
  $default_values = array(
    t('Frequently Asked Questions'),
    t('Back to Top'),
    t('Q:'),
    t('A:'),
  );
  $output = $output_answers = '';
  drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  if (arg(0) == $faqpath) {
    drupal_set_title(t(variable_get('faq_title', 'Frequently Asked Questions')));
  }
  if (!module_exists("taxonomy")) {
    $tid = 0;
  }

  // Configure the breadcrumb trail.
  if (!empty($tid) && ($current_term = taxonomy_term_load($tid))) {
    if (variable_get('faq_auto_generate_alias', TRUE) && !drupal_lookup_path('alias', arg(0) . '/' . $tid) && module_exists('pathauto')) {
      $alias = pathauto_create_alias('faq', 'insert', arg(0) . '/' . arg(1), array(
        'term' => $current_term,
      ));
      if ($alias) {
        drupal_goto($alias['alias']);
      }
    }
    if (drupal_match_path($_GET['q'], $faqpath . '/*')) {
      faq_set_breadcrumb($current_term);
    }
  }
  if (empty($faq_display)) {
    $faq_display = variable_get('faq_display', 'questions_top');
  }
  $use_categories = variable_get('faq_use_categories', FALSE);
  if (!empty($category_display)) {
    $use_categories = TRUE;
  }
  else {
    $category_display = variable_get('faq_category_display', 'categories_inline');
  }
  if (!module_exists("taxonomy")) {
    $use_categories = FALSE;
  }
  $faq_path = drupal_get_path('module', 'faq') . '/includes';
  if ($use_categories && $category_display == 'hide_qa' || $faq_display == 'hide_answer') {
    drupal_add_js(array(
      'faq' => array(
        'faq_hide_qa_accordion' => variable_get('faq_hide_qa_accordion', FALSE),
      ),
    ), array(
      'type' => 'setting',
      'scope' => JS_DEFAULT,
    ));
    drupal_add_js(array(
      'faq' => array(
        'faq_category_hide_qa_accordion' => variable_get('faq_category_hide_qa_accordion', FALSE),
      ),
    ), array(
      'type' => 'setting',
      'scope' => JS_DEFAULT,
    ));
    drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js');
  }

  // Non-categorized questions and answers.
  if (!$use_categories || $category_display == 'none' && empty($tid)) {
    if (!empty($tid)) {
      drupal_not_found();
      return;
    }
    $default_sorting = variable_get('faq_default_sorting', 'DESC');
    $query = db_select('node', 'n');
    $weight_alias = $query
      ->leftJoin('faq_weights', 'w', '%alias.nid=n.nid');
    $query
      ->addTag('node_access')
      ->fields('n', array(
      'nid',
    ))
      ->condition('n.type', 'faq')
      ->condition('n.status', 1)
      ->condition(db_or()
      ->condition("{$weight_alias}.tid", 0)
      ->isNull("{$weight_alias}.tid"));
    $default_weight = 0;
    if ($default_sorting == 'ASC') {
      $default_weight = 1000000;
    }

    // Works, but involves variable concatenation - safe though, since
    // $default_weight is an integer.
    $query
      ->addExpression("COALESCE(w.weight, {$default_weight})", 'effective_weight');

    // @codingStandardsIgnoreStart
    // @todo Doesn't work in Postgres.

    //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));

    // @codingStandardsIgnoreEnd
    $query
      ->orderBy('effective_weight', 'ASC')
      ->orderBy('n.sticky', 'DESC');
    if ($default_sorting == 'ASC') {
      $query
        ->orderBy('n.created', 'ASC');
    }
    else {
      $query
        ->orderBy('n.created', 'DESC');
    }
    if (module_exists('i18n_select')) {
      $query
        ->condition('n.language', i18n_select_langcodes());
    }

    // Only need the nid column.
    $nids = $query
      ->execute()
      ->fetchCol();
    $data = node_load_multiple($nids);
    switch ($faq_display) {
      case 'questions_top':
        include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.questions_top.inc';
        $output = theme('faq_questions_top', array(
          'data' => $data,
        ));
        break;
      case 'hide_answer':
        include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.hide_answer.inc';
        $output = theme('faq_hide_answer', array(
          'data' => $data,
        ));
        break;
      case 'questions_inline':
        include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.questions_inline.inc';
        $output = theme('faq_questions_inline', array(
          'data' => $data,
        ));
        break;
      case 'new_page':
        include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.new_page.inc';
        $output = theme('faq_new_page', array(
          'data' => $data,
        ));
        break;
    }
  }
  else {
    $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);

    // If we're viewing a specific category/term.
    if (!empty($tid)) {
      if ($term = taxonomy_term_load($tid)) {
        $title = t(variable_get('faq_title', 'Frequently Asked Questions'));
        if (arg(0) == 'faq-page' && is_numeric(arg(1))) {
          drupal_set_title($title . ($title ? ' - ' : '') . faq_tt("taxonomy:term:{$term->tid}:name", $term->name));
        }
        _display_faq_by_category($faq_display, $category_display, $term, 0, $output, $output_answers);
        return theme('faq_page', array(
          'content' => $output,
          'answers' => $output_answers,
        ));
      }
      else {
        drupal_not_found();
        return;
      }
    }
    $list_style = variable_get('faq_category_listing', 'ul');
    $vocabularies = taxonomy_get_vocabularies('faq');
    $vocab_omit = variable_get('faq_omit_vocabulary', array());
    $items = array();
    $vocab_items = array();
    $valid_vocab = FALSE;
    foreach ($vocabularies as $vid => $vobj) {
      if (isset($vocab_omit[$vid]) && $vocab_omit[$vid] != 0) {
        continue;
      }
      $valid_vocab = TRUE;
      if ($category_display == 'new_page') {
        $vocab_items = _get_indented_faq_terms($vid, 0);
        $items = array_merge($items, $vocab_items);
      }
      else {
        if ($hide_child_terms && $category_display == 'hide_qa') {
          $tree = taxonomy_get_tree($vid, 0, 1, TRUE);
        }
        else {
          $tree = taxonomy_get_tree($vid, 0, NULL, TRUE);
        }
        if (function_exists('i18n_taxonomy_localize_terms')) {
          $tree = i18n_taxonomy_localize_terms($tree);
        }
        foreach ($tree as $term) {
          switch ($category_display) {
            case 'hide_qa':
            case 'categories_inline':
              if (faq_taxonomy_term_count_nodes($term->tid)) {
                _display_faq_by_category($faq_display, $category_display, $term, 1, $output, $output_answers);
              }
              break;
          }
        }
      }
    }
    if ($category_display == "new_page") {
      $output = theme('item_list', array(
        'items' => $items,
        'title' => NULL,
        'type' => $list_style,
      ));
    }
    if (!$valid_vocab) {
      drupal_set_message(t('Categories are enabled but no vocabulary is associated with the FAQ content type. Either create a vocabulary or disable categorization in order for questions to appear.'), 'error');
    }
  }
  $faq_description_default = array(
    'value' => '',
    'format' => filter_fallback_format(),
  );
  $faq_description = variable_get('faq_description', $faq_description_default);
  $format = $faq_description['format'];
  if ($format) {
    $description = check_markup($faq_description['value'], $format);
  }
  else {
    $description = check_plain($faq_description['value']);
  }
  return theme('faq_page', array(
    'content' => $output,
    'answers' => $output_answers,
    'description' => $description,
  ));
}