faq.module 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. <?php
  2. /**
  3. * @file
  4. * The FAQ module allows users to create a FAQ page, with questions and answers
  5. * displayed in different styles, according to the settings.
  6. */
  7. /**
  8. * Implements hook_help().
  9. */
  10. function faq_help($path, $arg) {
  11. $output = '';
  12. switch ($path) {
  13. case 'admin/help#faq':
  14. $output .= '<p>' . t("This module allows users with the 'administer faq' permission to create question and answer pairs which will be displayed on the faq page. The faq page is automatically generated from the FAQ nodes configured and the layout of this page can be modified on the settings page. Users will need the 'view faq page' permission in order to view the faq page.") . '</p>' .
  15. '<p>' . t("To create a question and answer, the user must create a 'FAQ' node (Create content >> FAQ). This screen allows the user to edit the question and answer text. If the 'Taxonomy' module is enabled and there are some terms configured for the FAQ node type, it will also be possible to put the questions into different categories when editing.") . '</p>' .
  16. '<p>' . t("The 'Frequently Asked Questions' settings configuration screen will allow users with 'administer faq' permissions to specify different layouts of the questions and answers.") . '</p>' .
  17. '<p>' . t("All users with 'view faq page' permissions will be able to view the generated FAQ page.") . '</p>';
  18. return $output;
  19. case 'admin/modules#description':
  20. return t('Allows the user to configure the layout of questions and answers on a FAQ page.');
  21. }
  22. }
  23. /**
  24. * Implements hook_permission().
  25. */
  26. function faq_permission() {
  27. return array(
  28. 'administer faq' => array(
  29. 'title' => t('Administer FAQ module'),
  30. ),
  31. 'administer faq order' => array(
  32. 'title' => t('Administer FAQ order'),
  33. ),
  34. 'view faq page' => array(
  35. 'title' => t('View FAQ pages'),
  36. ),
  37. );
  38. }
  39. /**
  40. * Implements hook_node_access().
  41. */
  42. function faq_node_access($node, $op, $account = NULL) {
  43. global $user;
  44. if (empty($account)) {
  45. $account = $user;
  46. }
  47. // Ignore non-FAQ node.
  48. if ((is_object($node) ? $node->type : $node) !== 'faq') {
  49. return NODE_ACCESS_IGNORE;
  50. }
  51. if ($op != 'create') {
  52. $node = (object) $node;
  53. }
  54. if ($op == 'view') {
  55. return NODE_ACCESS_IGNORE;
  56. }
  57. elseif ($op == 'create' || $op == 'update' || $op == 'delete') {
  58. if (user_access('administer faq')) {
  59. return NODE_ACCESS_ALLOW;
  60. }
  61. }
  62. return NODE_ACCESS_IGNORE;
  63. }
  64. /**
  65. * Implements hook_menu().
  66. */
  67. function faq_menu() {
  68. $items = array();
  69. $items['faq-page'] = array(
  70. 'title' => 'Frequently Asked Questions',
  71. 'page callback' => 'faq_page',
  72. 'access callback' => 'user_access',
  73. 'access arguments' => array('view faq page'),
  74. 'weight' => 1,
  75. 'type' => MENU_SUGGESTED_ITEM,
  76. );
  77. $items['faq-page/list'] = array(
  78. 'title' => 'List',
  79. 'page callback' => 'faq_page',
  80. 'access callback' => 'user_access',
  81. 'access arguments' => array('view faq page'),
  82. 'weight' => -10,
  83. 'type' => MENU_DEFAULT_LOCAL_TASK,
  84. );
  85. $items['faq-page/order'] = array(
  86. 'title' => 'Order',
  87. 'description' => 'Allows the user to configure the order of questions and answers on a FAQ page.',
  88. 'file' => 'faq.admin.inc',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('faq_order_settings_form'),
  91. 'access callback' => 'user_access',
  92. 'access arguments' => array('administer faq order'),
  93. 'type' => MENU_LOCAL_TASK,
  94. 'weight' => -8,
  95. );
  96. $items['faq-page/%'] = array(
  97. 'title' => 'Frequently Asked Questions',
  98. 'page callback' => 'faq_page',
  99. 'page arguments' => array(1),
  100. 'access callback' => 'user_access',
  101. 'access arguments' => array('view faq page'),
  102. 'type' => MENU_CALLBACK,
  103. );
  104. $items['faq-page/%/list'] = array(
  105. 'title' => 'List',
  106. 'page callback' => 'faq_page',
  107. 'page arguments' => array(1),
  108. 'access callback' => 'user_access',
  109. 'access arguments' => array('view faq page'),
  110. 'type' => MENU_DEFAULT_LOCAL_TASK,
  111. 'weight' => -10,
  112. );
  113. $items['faq-page/%/order'] = array(
  114. 'title' => 'Order',
  115. 'description' => 'Allows the user to configure the order of questions and answers on a FAQ page.',
  116. 'file' => 'faq.admin.inc',
  117. 'page callback' => 'drupal_get_form',
  118. 'page arguments' => array('faq_order_settings_form', 1),
  119. 'access callback' => 'user_access',
  120. 'access arguments' => array('administer faq order'),
  121. 'type' => MENU_LOCAL_TASK,
  122. 'weight' => -8,
  123. );
  124. $items['admin/config/content/faq'] = array(
  125. 'title' => 'Frequently Asked Questions',
  126. 'description' => 'Allows the user to configure the layout of questions and answers on a FAQ page.',
  127. 'file' => 'faq.admin.inc',
  128. 'page callback' => 'faq_settings_page',
  129. 'access callback' => 'user_access',
  130. 'access arguments' => array('administer faq'),
  131. );
  132. $items['admin/config/content/faq/general'] = array(
  133. 'title' => 'General',
  134. 'description' => 'Allows the user to configure the header and descriptive text for the FAQ page.',
  135. 'file' => 'faq.admin.inc',
  136. 'page callback' => 'drupal_get_form',
  137. 'page arguments' => array('faq_general_settings_form'),
  138. 'access callback' => 'user_access',
  139. 'access arguments' => array('administer faq'),
  140. 'type' => MENU_DEFAULT_LOCAL_TASK,
  141. 'weight' => -10,
  142. );
  143. $items['admin/config/content/faq/questions'] = array(
  144. 'title' => 'Questions',
  145. 'description' => 'Allows the user to configure the layout of questions and answers on a FAQ page.',
  146. 'file' => 'faq.admin.inc',
  147. 'page callback' => 'drupal_get_form',
  148. 'page arguments' => array('faq_questions_settings_form'),
  149. 'access callback' => 'user_access',
  150. 'access arguments' => array('administer faq'),
  151. 'type' => MENU_LOCAL_TASK,
  152. 'weight' => -9,
  153. );
  154. $items['admin/config/content/faq/categories'] = array(
  155. 'title' => 'Categories',
  156. 'description' => 'Allows the user to configure the layout of questions and answers using categories on a FAQ page.',
  157. 'file' => 'faq.admin.inc',
  158. 'page callback' => 'drupal_get_form',
  159. 'page arguments' => array('faq_categories_settings_form'),
  160. 'access callback' => 'user_access',
  161. 'access arguments' => array('administer faq'),
  162. 'type' => MENU_LOCAL_TASK,
  163. 'weight' => -8,
  164. );
  165. return $items;
  166. }
  167. /**
  168. * Implements hook_node_info().
  169. *
  170. * Defines the FAQ node/content type.
  171. * @return
  172. * An array, containing the title, module name and the description.
  173. */
  174. function faq_node_info() {
  175. return array(
  176. 'faq' => array(
  177. 'name' => t('FAQ'),
  178. 'base' => 'faq',
  179. 'description' => t('A frequently asked question and its answer.'),
  180. 'title_label' => t('Question'),
  181. ),
  182. );
  183. }
  184. /**
  185. * Implements hook_form().
  186. *
  187. * @param &$node
  188. * The node being added or edited.
  189. * @param &$param
  190. * The hook can set this variable to an associative array of attributes to add
  191. * to the enclosing <form> tag.
  192. * @return
  193. * The form elements in the $form array.
  194. */
  195. function faq_form($node, $form_state) {
  196. $type = node_type_get_type($node);
  197. // Set defaults.
  198. if (empty($node->detailed_question)) {
  199. $node->detailed_question = '';
  200. }
  201. $form = node_content_form($node, $form_state);
  202. $form['title']['#description'] = t('Question to be answered. This will appear in all question listings, such as the FAQ blocks.');
  203. // Detailed question.
  204. if (variable_get('faq_question_long_form', 1) || variable_get('faq_question_length', 'short') == 'long') {
  205. $form['detailed_question'] = array(
  206. '#type' => 'textarea',
  207. '#title' => t('Question details'),
  208. '#default_value' => $node->detailed_question,
  209. '#rows' => 3,
  210. '#description' => t('Longer question text. This will be displayed in all layouts where the answer appears, in addition to the shorter question text.'),
  211. );
  212. }
  213. // Answer.
  214. if (!empty($type->body_label)) {
  215. field_attach_form('node', $node, $form, $form_state, $node->language);
  216. }
  217. // dpm($form);
  218. return $form;
  219. }
  220. /**
  221. * Implements hook_field_extra_fields().
  222. */
  223. function faq_field_extra_fields() {
  224. $extra['node']['faq'] = array(
  225. 'form' => array(
  226. 'detailed_question' => array(
  227. 'label' => t('Question details'),
  228. 'description' => t('Longer question text'),
  229. 'weight' => -5,
  230. ),
  231. ),
  232. );
  233. return $extra;
  234. }
  235. /**
  236. * Implements hook_insert().
  237. *
  238. * Inserts the faq node question text into the 'faq_questions' table.
  239. *
  240. * @param $node
  241. * The node object.
  242. */
  243. function faq_insert($node) {
  244. db_insert('faq_questions')
  245. ->fields(array(
  246. 'nid' => $node->nid,
  247. 'vid' => $node->vid,
  248. 'question' => $node->title,
  249. 'detailed_question' => $node->detailed_question,
  250. ))
  251. ->execute();
  252. }
  253. /**
  254. * Implements hook_update().
  255. *
  256. * Updates the faq node question text in the 'faq_questions' table.
  257. *
  258. * @param $node
  259. * The node object.
  260. */
  261. function faq_update($node) {
  262. if (isset($node->revision) && $node->revision) {
  263. faq_insert($node);
  264. }
  265. else {
  266. // Just to be safe, we do a merge query instead of an update query.
  267. db_merge('faq_questions')
  268. ->fields(array(
  269. 'question' => $node->title,
  270. 'detailed_question' => $node->detailed_question,
  271. ))
  272. ->key(array(
  273. 'nid' => $node->nid,
  274. 'vid' => $node->vid,
  275. ))
  276. ->execute();
  277. }
  278. }
  279. /**
  280. * Implements hook_delete().
  281. *
  282. * Deletes an FAQ node from the database.
  283. *
  284. * @param &$node
  285. * Which node to delete.
  286. */
  287. function faq_delete($node) {
  288. db_delete('faq_weights')
  289. ->condition('nid', $node->nid)
  290. ->execute();
  291. db_delete('faq_questions')
  292. ->condition('nid', $node->nid)
  293. ->execute();
  294. }
  295. /**
  296. * Implements hook_load().
  297. *
  298. * Initialises $node->question using the value in the 'faq_questions' table.
  299. *
  300. * @param $node
  301. * The node object.
  302. */
  303. function faq_load($nodes) {
  304. foreach ($nodes as $nid => &$node) {
  305. $result = db_query('SELECT question, detailed_question FROM {faq_questions} WHERE nid = :nid AND vid = :vid', array(
  306. ':nid' => $node->nid,
  307. ':vid' => $node->vid,
  308. ))->fetchObject();
  309. if ($result && !drupal_match_path($_GET['q'], 'node/' . $node->nid . '/edit')) {
  310. $question_length = variable_get('faq_question_length', 'short');
  311. if ($question_length == 'long' && !empty($result->detailed_question)) {
  312. $result->title = $result->detailed_question;
  313. }
  314. else {
  315. $result->title = $result->question;
  316. }
  317. }
  318. foreach ($result as $property => &$value) {
  319. $node->$property = $value;
  320. }
  321. }
  322. }
  323. /**
  324. * Implements hook_node_revision_delete().
  325. */
  326. function faq_node_revision_delete($node) {
  327. db_delete('faq_questions')
  328. ->condition('nid', $node->nid)
  329. ->condition('vid', $node->vid)
  330. ->execute();
  331. }
  332. /**
  333. * Implements hook_view().
  334. */
  335. function faq_view($node, $view_mode) {
  336. drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  337. $language = $node->language;
  338. if (isset($node->body[$language]) && $node->body[$language]) {
  339. $node->detailed_question = check_markup($node->detailed_question, $node->body[$language][0]['format'], $language);
  340. if (!isset($node->body[$language][0])) drupal_set_message('<pre>'.print_r($node->body, TRUE).'</pre>');
  341. }
  342. else {
  343. $node->detailed_question = check_markup($node->detailed_question, NULL, $language);
  344. }
  345. if ( !empty($node->detailed_question)
  346. && variable_get('faq_question_length', 'short') == 'both'
  347. && ( variable_get('faq_display', 'questions_top') == 'hide_answer'
  348. || drupal_match_path($_GET['q'], 'node/' . $node->nid)
  349. )
  350. ) {
  351. $node->content['detailed_question'] = array(
  352. '#markup' => '<div class="faq-detailed-question">' . $node->detailed_question . '</div>',
  353. '#weight' => '-1',
  354. );
  355. }
  356. return $node;
  357. }
  358. /**
  359. * Implements hook_views_api().
  360. */
  361. function faq_views_api() {
  362. return array(
  363. 'api' => 3,
  364. 'path' => drupal_get_path('module', 'faq') . '/views',
  365. );
  366. }
  367. /**
  368. * Function to display the faq page.
  369. *
  370. * @param $tid
  371. * Default is 0, determines if the questions and answers on the page
  372. * will be shown according to a category or non-categorized.
  373. * @param $faq_display
  374. * Optional parameter to override default question layout setting.
  375. * @param $category_display
  376. * Optional parameter to override default category layout setting.
  377. * @return
  378. * The output variable which contains an HTML formatted page with FAQ
  379. * questions and answers.
  380. */
  381. function faq_page($tid = 0, $faq_display = '', $category_display = '') {
  382. if (module_exists('pathauto')) {
  383. module_load_include('inc', 'pathauto');
  384. }
  385. // Things to provide translations for.
  386. $default_values = array(t('Frequently Asked Questions'), t('Back to Top'), t('Q:'), t('A:'));
  387. $output = $output_answers = '';
  388. drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  389. if (arg(0) == 'faq-page') {
  390. drupal_set_title(variable_get('faq_title', 'Frequently Asked Questions'));
  391. }
  392. if (!module_exists("taxonomy")) {
  393. $tid = 0;
  394. }
  395. // Configure the breadcrumb trail.
  396. if (!empty($tid) && $current_term = taxonomy_term_load($tid)) {
  397. if (!drupal_lookup_path('alias', arg(0) . '/' . $tid) && module_exists('pathauto')) {
  398. $alias = pathauto_create_alias('faq', 'insert', arg(0) . '/' . arg(1), array('term' => $current_term));
  399. if ($alias) {
  400. drupal_goto($alias['alias']);
  401. }
  402. }
  403. if (drupal_match_path($_GET['q'], 'faq-page/*')) {
  404. faq_set_breadcrumb($current_term);
  405. }
  406. }
  407. if (empty($faq_display)) {
  408. $faq_display = variable_get('faq_display', 'questions_top');
  409. }
  410. $use_categories = variable_get('faq_use_categories', FALSE);
  411. if (!empty($category_display)) {
  412. $use_categories = TRUE;
  413. }
  414. else {
  415. $category_display = variable_get('faq_category_display', 'categories_inline');
  416. }
  417. if (!module_exists("taxonomy")) {
  418. $use_categories = FALSE;
  419. }
  420. $faq_path = drupal_get_path('module', 'faq') . '/includes';
  421. if (($use_categories && $category_display == 'hide_qa') || $faq_display == 'hide_answer') {
  422. drupal_add_js(array('faq' => array('faq_hide_qa_accordion' => variable_get('faq_hide_qa_accordion', FALSE))), array('type' => 'setting', 'scope' => JS_DEFAULT));
  423. 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));
  424. drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js');
  425. }
  426. // Non-categorized questions and answers.
  427. if (!$use_categories || ($category_display == 'none' && empty($tid))) {
  428. if (!empty($tid)) {
  429. drupal_not_found();
  430. return;
  431. }
  432. $default_sorting = variable_get('faq_default_sorting', 'DESC');
  433. $query = db_select('node', 'n');
  434. $weight_alias = $query->leftJoin('faq_weights', 'w', '%alias.nid=n.nid');
  435. $query
  436. ->addTag('node_access')
  437. ->fields('n', array('nid'))
  438. ->condition('n.type', 'faq')
  439. ->condition('n.status', 1)
  440. ->condition(db_or()->condition("$weight_alias.tid", 0)->isNull("$weight_alias.tid"));
  441. $default_weight = 0;
  442. if ($default_sorting == 'ASC') {
  443. $default_weight = 1000000;
  444. }
  445. // Works, but involves variable concatenation - safe though, since
  446. // $default_weight is an integer.
  447. $query->addExpression("COALESCE(w.weight, $default_weight)", 'effective_weight');
  448. // Doesn't work in Postgres.
  449. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));
  450. $query->orderBy('effective_weight', 'ASC')
  451. ->orderBy('n.sticky', 'DESC');
  452. if ($default_sorting == 'ASC') {
  453. $query->orderBy('n.created', 'ASC');
  454. }
  455. else {
  456. $query->orderBy('n.created', 'DESC');
  457. }
  458. if (module_exists('i18n_select')) {
  459. $query->condition('n.language', i18n_select_langcodes());
  460. }
  461. // Only need the nid column.
  462. $nids = $query->execute()->fetchCol();
  463. $data = node_load_multiple($nids);
  464. switch ($faq_display) {
  465. case 'questions_top':
  466. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.questions_top.inc';
  467. $output = theme('faq_questions_top', array('data' => $data));
  468. break;
  469. case 'hide_answer':
  470. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.hide_answer.inc';
  471. $output = theme('faq_hide_answer', array('data' => $data));
  472. break;
  473. case 'questions_inline':
  474. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.questions_inline.inc';
  475. $output = theme('faq_questions_inline', array('data' => $data));
  476. break;
  477. case 'new_page':
  478. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.new_page.inc';
  479. $output = theme('faq_new_page', array('data' => $data));
  480. break;
  481. } // End of switch.
  482. }
  483. // Categorize questions.
  484. else {
  485. $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);
  486. // If we're viewing a specific category/term.
  487. if (!empty($tid)) {
  488. if ($term = taxonomy_term_load($tid)) {
  489. $title = variable_get('faq_title', 'Frequently Asked Questions');
  490. if (arg(0) == 'faq-page' && is_numeric(arg(1))) {
  491. drupal_set_title($title . ($title ? ' - ' : '') . faq_tt("taxonomy:term:$term->tid:name", $term->name));
  492. }
  493. _display_faq_by_category($faq_display, $category_display, $term, 0, $output, $output_answers);
  494. return theme('faq_page', array('content' => $output, 'answers' => $output_answers));
  495. }
  496. else {
  497. drupal_not_found();
  498. return;
  499. }
  500. }
  501. $list_style = variable_get('faq_category_listing', 'ul');
  502. $vocabularies = taxonomy_get_vocabularies('faq');
  503. $vocab_omit = variable_get('faq_omit_vocabulary', array());
  504. $items = array();
  505. $vocab_items = array();
  506. $valid_vocab = FALSE;
  507. foreach ($vocabularies as $vid => $vobj) {
  508. if (isset($vocab_omit[$vid]) && $vocab_omit[$vid] != 0) {
  509. continue;
  510. }
  511. $valid_vocab = TRUE;
  512. if ($category_display == "new_page") {
  513. $vocab_items = _get_indented_faq_terms($vid, 0);
  514. $items = array_merge($items, $vocab_items);
  515. }
  516. // Not a new page.
  517. else {
  518. if ($hide_child_terms && $category_display == 'hide_qa') {
  519. $tree = taxonomy_get_tree($vid, 0, 1, TRUE);
  520. }
  521. else {
  522. $tree = taxonomy_get_tree($vid, 0, NULL, TRUE);
  523. }
  524. foreach ($tree as $term) {
  525. switch ($category_display) {
  526. case 'hide_qa':
  527. case 'categories_inline':
  528. if (faq_taxonomy_term_count_nodes($term->tid)) {
  529. _display_faq_by_category($faq_display, $category_display, $term, 1, $output, $output_answers);
  530. }
  531. break;
  532. } // End of switch (category_display).
  533. } // End of foreach term.
  534. } // End of if $category_display != new_page.
  535. } // End of foreach vocab.
  536. if ($category_display == "new_page") {
  537. $output = theme('item_list', array('items' => $items, 'title' => NULL, 'type' => $list_style));
  538. }
  539. if (!$valid_vocab) {
  540. 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');
  541. }
  542. }
  543. $faq_description = variable_get('faq_description', '');
  544. $format = variable_get('faq_description_format', 0);
  545. if ($format) {
  546. $faq_description = check_markup($faq_description, $format);
  547. }
  548. return theme('faq_page', array('content' => $output, 'answers' => $output_answers, 'description' => $faq_description));
  549. }
  550. /**
  551. * Display FAQ questions and answers filtered by category.
  552. *
  553. * @param $faq_display
  554. * Define the way the FAQ is being shown; can have the values:
  555. * 'questions top',hide answers','questions inline','new page'.
  556. * @param $category_display
  557. * The layout of categories which should be used.
  558. * @param $term
  559. * The category / term to display FAQs for.
  560. * @param $display_header
  561. * Set if the header will be shown or not.
  562. * @param &$output
  563. * Reference which holds the content of the page, HTML formatted.
  564. * @param &$output_answer
  565. * Reference which holds the answers from the FAQ, when showing questions
  566. * on top.
  567. */
  568. function _display_faq_by_category($faq_display, $category_display, $term, $display_header, &$output, &$output_answers) {
  569. $default_sorting = variable_get('faq_default_sorting', 'DESC');
  570. $query = db_select('node', 'n');
  571. $ti_alias = $query->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)');
  572. $td_alias = $query->innerJoin('taxonomy_term_data', 'td', "({$ti_alias}.tid = %alias.tid)");
  573. $w_alias = $query->leftJoin('faq_weights', 'w', "%alias.tid = {$ti_alias}.tid AND n.nid = %alias.nid");
  574. $query
  575. ->fields('n', array('nid'))
  576. ->condition('n.type', 'faq')
  577. ->condition('n.status', 1)
  578. ->condition("{$ti_alias}.tid", $term->tid)
  579. ->addTag('node_access');
  580. $default_weight = 0;
  581. if ($default_sorting == 'ASC') {
  582. $default_weight = 1000000;
  583. }
  584. // Works, but involves variable concatenation - safe though, since
  585. // $default_weight is an integer.
  586. $query->addExpression("COALESCE(w.weight, $default_weight)", 'effective_weight');
  587. // Doesn't work in Postgres.
  588. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));
  589. $query->orderBy('effective_weight', 'ASC')
  590. ->orderBy('n.sticky', 'DESC');
  591. if ($default_sorting == 'ASC') {
  592. $query->orderBy('n.created', 'ASC');
  593. }
  594. else {
  595. $query->orderBy('n.created', 'DESC');
  596. }
  597. if (module_exists('i18n_select')) {
  598. $query->condition('n.language', i18n_select_langcodes());
  599. $query->condition("{$td_alias}.language", i18n_select_langcodes());
  600. }
  601. // We only want the first column, which is nid, so that we can load all
  602. // related nodes.
  603. $nids = $query->execute()->fetchCol();
  604. $data = node_load_multiple($nids);
  605. // Handle indenting of categories.
  606. $depth = 0;
  607. if (!isset($term->depth)) {
  608. $term->depth = 0;
  609. }
  610. while ($depth < $term->depth) {
  611. $display_header = 1;
  612. $indent = '<div class="faq-category-indent">';
  613. $output .= $indent;
  614. $depth++;
  615. }
  616. // Set up the class name for hiding the q/a for a category if required.
  617. $faq_class = "faq-qa";
  618. if ($category_display == "hide_qa") {
  619. $faq_class = "faq-qa-hide";
  620. }
  621. $faq_path = drupal_get_path('module', 'faq') . '/includes';
  622. switch ($faq_display) {
  623. case 'questions_top':
  624. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.questions_top.inc';
  625. // @todo fix workaround: have to share result.
  626. $output .= theme('faq_category_questions_top', array('data' => $data, 'display_header' => $display_header, 'category_display' => $category_display, 'term' => $term, 'class' => $faq_class, 'parent_term' => $term));
  627. $output_answers .= theme('faq_category_questions_top_answers', array('data' => $data, 'display_header' => $display_header, 'category_display' => $category_display, 'term' => $term, 'class' => $faq_class, 'parent_term' => $term));
  628. break;
  629. case 'hide_answer':
  630. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.hide_answer.inc';
  631. $output .= theme('faq_category_hide_answer', array('data' => $data, 'display_header' => $display_header, 'category_display' => $category_display, 'term' => $term, 'class' => $faq_class, 'parent_term' => $term));
  632. break;
  633. case 'questions_inline':
  634. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.questions_inline.inc';
  635. $output .= theme('faq_category_questions_inline', array('data' => $data, 'display_header' => $display_header, 'category_display' => $category_display, 'term' => $term, 'class' => $faq_class, 'parent_term' => $term));
  636. break;
  637. case 'new_page':
  638. include_once DRUPAL_ROOT . '/' . $faq_path . '/faq.new_page.inc';
  639. $output .= theme('faq_category_new_page', array('data' => $data, 'display_header' => $display_header, 'category_display' => $category_display, 'term' => $term, 'class' => $faq_class, 'parent_term' => $term));
  640. break;
  641. } // End of switch (faq_display).
  642. // Handle indenting of categories.
  643. while ($depth > 0) {
  644. $output .= '</div>';
  645. $depth--;
  646. }
  647. }
  648. /**
  649. * Implements hook_theme().
  650. */
  651. function faq_theme() {
  652. $path = drupal_get_path('module', 'faq') . '/includes';
  653. return array(
  654. 'faq_draggable_question_order_table' => array(
  655. 'render element' => 'form',
  656. ),
  657. 'faq_questions_top' => array(
  658. 'path' => $path,
  659. 'file' => 'faq.questions_top.inc',
  660. 'template' => 'faq-questions-top',
  661. 'variables' => array('data' => NULL),
  662. ),
  663. 'faq_category_questions_top' => array(
  664. 'path' => $path,
  665. 'file' => 'faq.questions_top.inc',
  666. 'template' => 'faq-category-questions-top',
  667. 'variables' => array('data' => NULL, 'display_header' => 0, 'category_display' => NULL, 'term' => NULL, 'class' => NULL, 'parent_term' => NULL),
  668. ),
  669. 'faq_category_questions_top_answers' => array(
  670. 'path' => $path,
  671. 'file' => 'faq.questions_top.inc',
  672. 'template' => 'faq-category-questions-top-answers',
  673. 'variables' => array('data' => NULL, 'display_header' => 0, 'category_display' => NULL, 'term' => NULL, 'class' => NULL, 'parent_term' => NULL),
  674. ),
  675. 'faq_hide_answer' => array(
  676. 'path' => $path,
  677. 'file' => 'faq.hide_answer.inc',
  678. 'template' => 'faq-hide-answer',
  679. 'variables' => array('data' => NULL),
  680. ),
  681. 'faq_category_hide_answer' => array(
  682. 'path' => $path,
  683. 'file' => 'faq.hide_answer.inc',
  684. 'template' => 'faq-category-hide-answer',
  685. 'variables' => array('data' => NULL, 'display_header' => 0, 'category_display' => NULL, 'term' => NULL, 'class' => NULL, 'parent_term' => NULL),
  686. ),
  687. 'faq_questions_inline' => array(
  688. 'path' => $path,
  689. 'file' => 'faq.questions_inline.inc',
  690. 'template' => 'faq-questions-inline',
  691. 'variables' => array('data' => NULL),
  692. ),
  693. 'faq_category_questions_inline' => array(
  694. 'path' => $path,
  695. 'file' => 'faq.questions_inline.inc',
  696. 'template' => 'faq-category-questions-inline',
  697. 'variables' => array('data' => NULL, 'display_header' => 0, 'category_display' => NULL, 'term' => NULL, 'class' => NULL, 'parent_term' => NULL),
  698. ),
  699. 'faq_new_page' => array(
  700. 'path' => $path,
  701. 'file' => 'faq.new_page.inc',
  702. 'template' => 'faq-new-page',
  703. 'variables' => array('data' => NULL),
  704. ),
  705. 'faq_category_new_page' => array(
  706. 'path' => $path,
  707. 'file' => 'faq.new_page.inc',
  708. 'template' => 'faq-category-new-page',
  709. 'variables' => array('data' => NULL, 'display_header' => 0, 'category_display' => NULL, 'term' => NULL, 'class' => NULL, 'parent_term' => NULL),
  710. ),
  711. 'faq_page' => array(
  712. 'variables' => array('content' => '', 'answers' => '', 'description' => NULL),
  713. ),
  714. );
  715. }
  716. /**
  717. * Implements hook_block_info().
  718. */
  719. function faq_block_info() {
  720. $blocks['faq_categories']['info'] = t('FAQ Categories');
  721. return $blocks;
  722. }
  723. /**
  724. * Implements hook_block_view().
  725. */
  726. function faq_block_view($delta) {
  727. static $vocabularies, $terms;
  728. $block = array();
  729. switch ($delta) {
  730. case 'faq_categories':
  731. if (module_exists('taxonomy')) {
  732. if (!isset($terms)) {
  733. $terms = array();
  734. $vocabularies = taxonomy_get_vocabularies('faq');
  735. $vocab_omit = array_flip(variable_get('faq_omit_vocabulary', array()));
  736. $vocabularies = array_diff_key($vocabularies, $vocab_omit);
  737. foreach ($vocabularies as $vocab) {
  738. foreach (taxonomy_get_tree($vocab->vid) as $term) {
  739. if (faq_taxonomy_term_count_nodes($term->tid)) {
  740. $terms[$term->name] = $term->tid;
  741. }
  742. }
  743. }
  744. }
  745. if (count($terms) > 0) {
  746. $block['subject'] = t('FAQ Categories');
  747. $items = array();
  748. foreach ($terms as $name => $tid) {
  749. $items[] = l(faq_tt("taxonomy:term:$tid:name", $name), 'faq-page/' . $tid);
  750. }
  751. $list_style = variable_get('faq_category_listing', 'ul');
  752. $block['content'] = theme('item_list', array('items' => $items, 'title' => NULL, 'type' => $list_style));
  753. }
  754. }
  755. break;
  756. } // End switch($delta).
  757. return $block;
  758. }
  759. /**
  760. * Return a HTML formatted list of terms indented according to the term depth.
  761. *
  762. * @param $vid
  763. * Vocabulary id.
  764. * @param $tid
  765. * Term id.
  766. * @return
  767. * Return a HTML formatted list of terms indented according to the term depth.
  768. */
  769. function _get_indented_faq_terms($vid, $tid) {
  770. if (module_exists('pathauto')) {
  771. module_load_include('inc', 'pathauto');
  772. }
  773. $display_faq_count = variable_get('faq_count', FALSE);
  774. $hide_child_terms = variable_get('faq_hide_child_terms', FALSE);
  775. $items = array();
  776. $tree = taxonomy_get_tree($vid, $tid, 1, TRUE);
  777. foreach ($tree as $term) {
  778. $tree_count = faq_taxonomy_term_count_nodes($term->tid);
  779. if ($tree_count) {
  780. // Get taxonomy image.
  781. $term_image = '';
  782. if (module_exists('taxonomy_image')) {
  783. $term_image = taxonomy_image_display($term->tid, array('class' => 'faq-tax-image'));
  784. }
  785. // Get term description.
  786. $desc = '';
  787. if (!empty($term->description)) {
  788. $desc = '<div class="faq-qa-description">';
  789. $desc .= check_markup(faq_tt("taxonomy:term:$term->tid:description", $term->description)) . "</div>";
  790. }
  791. // See if this term has any nodes itself, should it be a link?
  792. $query = db_select('node', 'n');
  793. $ti_alias = $query->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)');
  794. $term_node_count = $query
  795. ->condition('n.status', 1)
  796. ->condition('n.type', 'faq')
  797. ->condition("{$ti_alias}.tid", $term->tid)
  798. ->addTag('node_access')
  799. ->countQuery()
  800. ->execute()
  801. ->fetchField();
  802. if ($term_node_count > 0) {
  803. $path = "faq-page/$term->tid";
  804. if (!drupal_lookup_path('alias', arg(0) . '/' . $term->tid) && module_exists('pathauto')) {
  805. $alias = pathauto_create_alias('faq', 'insert', arg(0) . '/' . $term->tid, array('term' => $term));
  806. if ($alias) {
  807. $path = $alias['alias'];
  808. }
  809. }
  810. if ($display_faq_count) {
  811. $count = $term_node_count;
  812. if ($hide_child_terms) {
  813. $count = $tree_count;
  814. }
  815. $cur_item = $term_image . l(faq_tt("taxonomy:term:$term->tid:name", $term->name), $path) . " ($count) " . $desc;
  816. }
  817. else {
  818. $cur_item = $term_image . l(faq_tt("taxonomy:term:$term->tid:name", $term->name), $path) . $desc;
  819. }
  820. }
  821. else {
  822. $cur_item = $term_image . check_plain(faq_tt("taxonomy:term:$term->tid:name", $term->name)) . $desc;
  823. }
  824. if (!empty($term_image)) {
  825. $cur_item .= '<div class="clear-block"></div>';
  826. }
  827. $term_items = array();
  828. if (!$hide_child_terms) {
  829. $term_items = _get_indented_faq_terms($vid, $term->tid);
  830. }
  831. $items[] = array(
  832. "data" => $cur_item,
  833. "children" => $term_items,
  834. );
  835. }
  836. }
  837. return $items;
  838. }
  839. /**
  840. * Get a list of terms associated with the FAQ nodes.
  841. *
  842. * @return
  843. * Return the HTML-formatted content.
  844. */
  845. function faq_get_terms() {
  846. $items = array();
  847. $vocabularies = taxonomy_get_vocabularies('faq');
  848. $vocab_omit = array_flip(variable_get('faq_omit_vocabulary', array()));
  849. $vocabularies = array_diff_key($vocabularies, $vocab_omit);
  850. foreach ($vocabularies as $vid => $vobj) {
  851. $vocab_items = _get_indented_faq_terms($vid, 0);
  852. $items = array_merge($items, $vocab_items);
  853. }
  854. return theme('item_list', array('items' => $items));
  855. }
  856. /**
  857. * Format the output for the faq_site_map() function.
  858. *
  859. * @return
  860. * Return a list of FAQ categories if categorization is enabled, otherwise
  861. * return a list of faq nodes.
  862. */
  863. function faq_get_faq_list() {
  864. // Return list of vocab terms if categories are configured.
  865. $use_categories = variable_get('faq_use_categories', FALSE);
  866. if ($use_categories) {
  867. return faq_get_terms();
  868. }
  869. // Otherwise return list of weighted FAQ nodes.
  870. $items = array();
  871. $default_sorting = variable_get('faq_default_sorting', 'DESC');
  872. $query = db_select('node', 'n');
  873. $w_alias = $query->leftJoin('faq_weights', 'w', "%alias.nid = n.nid");
  874. $query
  875. ->fields('n', array('nid'))
  876. ->condition('n.type', 'faq')
  877. ->condition('n.status', 1)
  878. ->condition(db_or()->condition("{$w_alias}.tid", 0)->isNull("{$w_alias}.tid"))
  879. ->addTag('node_access');
  880. $default_weight = 0;
  881. if ($default_sorting == 'ASC') {
  882. $default_weight = 1000000;
  883. }
  884. // Works, but involves variable concatenation - safe though, since
  885. // $default_weight is an integer.
  886. $query->addExpression("COALESCE(w.weight, $default_weight)", 'effective_weight');
  887. // Doesn't work in Postgres.
  888. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));
  889. $query->orderBy('effective_weight', 'ASC')
  890. ->orderBy('n.sticky', 'DESC');
  891. if ($default_sorting == 'ASC') {
  892. $query->orderBy('n.created', 'ASC');
  893. }
  894. else {
  895. $query->orderBy('n.created', 'DESC');
  896. }
  897. if (module_exists('i18n_select')) {
  898. $query->condition('n.language', i18n_select_langcodes());
  899. }
  900. // We only want the first column, which is nid, so that we can load all
  901. // related nodes.
  902. $nids = $query->execute()->fetchCol();
  903. $nodes = node_load_multiple($nids);
  904. foreach ($nodes as $node) {
  905. if (node_access('view', $node)) {
  906. $items[] = l($node->question, "node/$node->nid");
  907. }
  908. }
  909. return theme('item_list', array('items' => $items));
  910. }
  911. if (!function_exists('array_diff_key')) {
  912. function array_diff_key() {
  913. $arrs = func_get_args();
  914. $result = array_shift($arrs);
  915. foreach ($arrs as $array) {
  916. foreach ($result as $key => $v) {
  917. if (array_key_exists($key, $array)) {
  918. unset($result[$key]);
  919. }
  920. }
  921. }
  922. return $result;
  923. }
  924. }
  925. /**
  926. * Helper function to setup the faq question.
  927. *
  928. * @param &$data
  929. * Array reference to store display data in.
  930. * @param $node
  931. * The node object.
  932. * @param $path
  933. * The path/url which the question should link to if links are disabled.
  934. * @param $anchor
  935. * Link anchor to use in question links.
  936. */
  937. function faq_view_question(&$data, $node, $path = NULL, $anchor = NULL) {
  938. $disable_node_links = variable_get('faq_disable_node_links', FALSE);
  939. $question = '';
  940. // Don't link to faq node, instead provide no link, or link to current page.
  941. if ($disable_node_links) {
  942. if (empty($path) && empty($anchor)) {
  943. $question = check_plain($node->title);
  944. }
  945. elseif (empty($path)) {
  946. // Can't seem to use l() function with empty string as screen-readers
  947. // don't like it, so create anchor name manually.
  948. $question = '<a id="' . $anchor . '"></a>' . check_plain($node->title);
  949. }
  950. else {
  951. $options = array();
  952. if ($anchor) {
  953. $options['attributes'] = array('id' => $anchor);
  954. }
  955. $question = l($node->title, $path, $options);
  956. }
  957. }
  958. // Link to faq node.
  959. else {
  960. if (empty($anchor)) {
  961. $question = l($node->title, "node/$node->nid");
  962. }
  963. else {
  964. $question = l($node->title, "node/$node->nid", array("attributes" => array("id" => "$anchor")));
  965. }
  966. }
  967. $question = '<span datatype="" property="dc:title">' . $question . '</span>';
  968. if (variable_get('faq_display', 'questions_top') != 'hide_answer' && !empty($node->detailed_question) && variable_get('faq_question_length', 'short') == 'both') {
  969. $node->detailed_question = check_markup($node->detailed_question, 'filtered_html', '', FALSE);
  970. $question .= '<div class="faq-detailed-question">' . $node->detailed_question . '</div>';
  971. }
  972. $data['question'] = $question;
  973. }
  974. /**
  975. * Helper function to setup the faq answer.
  976. *
  977. * @param &$data
  978. * Array reference to store display data in.
  979. * @param $node
  980. * The node object.
  981. * @param $back_to_top
  982. * An array containing the "back to top" link.
  983. * @param $teaser
  984. * Whether or not to use teasers.
  985. * @param $links
  986. * Whether or not to show node links.
  987. */
  988. function faq_view_answer(&$data, $node, $back_to_top, $teaser, $links) {
  989. $view_mode = $teaser ? 'teaser' : 'full';
  990. $langcode = $GLOBALS['language_content']->language;
  991. // Build the faq node content and invoke other modules' links, etc, functions.
  992. $node = (object) $node;
  993. node_build_content($node, $view_mode, $langcode);
  994. // Add "edit answer" link if they have the correct permissions.
  995. if (node_access('update', $node)) {
  996. $node->content['links']['node']['#links']['faq_edit_link'] = array(
  997. 'title' => t('Edit answer'),
  998. 'href' => "node/$node->nid/edit",
  999. 'query' => drupal_get_destination(),
  1000. 'attributes' => array('title' => t('Edit answer')),
  1001. );
  1002. }
  1003. // Add "back to top" link.
  1004. if (!empty($back_to_top)) {
  1005. $node->content['links']['node']['#links']['faq_back_to_top'] = $back_to_top;
  1006. }
  1007. $build = $node->content;
  1008. // We don't need duplicate rendering info in node->content.
  1009. unset($node->content);
  1010. $build += array(
  1011. '#theme' => 'node',
  1012. '#node' => $node,
  1013. '#view_mode' => $view_mode,
  1014. '#language' => $langcode,
  1015. );
  1016. // Add contextual links for this node.
  1017. if (!empty($node->nid) && !($view_mode == 'full' && node_is_page($node))) {
  1018. $build['#contextual_links']['node'] = array('node', array($node->nid));
  1019. }
  1020. // Allow modules to modify the structured node.
  1021. $type = 'node';
  1022. drupal_alter(array('node_view', 'entity_view'), $build, $type);
  1023. $node_links = ($links ? $build['links']['node']['#links'] : (!empty($back_to_top) ? array($build['links']['node']['#links']['faq_back_to_top']) : NULL));
  1024. unset($build['links']);
  1025. unset($build['#theme']); // We don't want node title displayed.
  1026. $content = drupal_render($build);
  1027. // Unset unused $node text so that a bad theme can not open a security hole.
  1028. // $node->body = NULL;
  1029. // $node->teaser = NULL;
  1030. $data['body'] = $content;
  1031. $data['links'] = !empty($node_links) ? theme('links', array('links' => $node_links, 'attributes' => array('class' => 'links inline'))) : '';
  1032. }
  1033. /**
  1034. * Helper function to setup the "back to top" link.
  1035. *
  1036. * @param $path
  1037. * The path/url where the "back to top" link should bring the user too. This
  1038. * could be the 'faq-page' page or one of the categorized faq pages, e.g 'faq-page/123'
  1039. * where 123 is the tid.
  1040. * @return
  1041. * An array containing the "back to top" link.
  1042. */
  1043. function faq_init_back_to_top($path) {
  1044. $back_to_top = array();
  1045. $back_to_top_text = trim(variable_get('faq_back_to_top', 'Back to Top'));
  1046. if (!empty($back_to_top_text)) {
  1047. $back_to_top = array(
  1048. 'title' => check_plain($back_to_top_text),
  1049. 'href' => $path,
  1050. 'attributes' => array('title' => t('Go back to the top of the page.')),
  1051. 'fragment' => 'faq-top',
  1052. 'html' => TRUE,
  1053. );
  1054. }
  1055. return $back_to_top;
  1056. }
  1057. /**
  1058. * Helper function for retrieving the sub-categories faqs.
  1059. *
  1060. * @param $term
  1061. * The category / term to display FAQs for.
  1062. * @param $theme_function
  1063. * Theme function to use to format the Q/A layout for sub-categories.
  1064. * @param $default_weight
  1065. * Is 0 for $default_sorting = DESC; is 1000000 for $default_sorting = ASC.
  1066. * @param $default_sorting
  1067. * If 'DESC', nodes are sorted by creation date descending; if 'ASC', nodes
  1068. * are sorted by creation date ascending.
  1069. * @param $category_display
  1070. * The layout of categories which should be used.
  1071. * @param $class
  1072. * CSS class which the HTML div will be using. A special class name is
  1073. * required in order to hide and questions / answers.
  1074. * @param $parent_term
  1075. * The original, top-level, term we're displaying FAQs for.
  1076. */
  1077. function faq_get_child_categories_faqs($term, $theme_function, $default_weight, $default_sorting, $category_display, $class, $parent_term = NULL) {
  1078. $output = array();
  1079. $list = taxonomy_get_children($term->tid);
  1080. if (!is_array($list)) {
  1081. return '';
  1082. }
  1083. foreach ($list as $tid => $child_term) {
  1084. $child_term->depth = $term->depth + 1;
  1085. if (faq_taxonomy_term_count_nodes($child_term->tid)) {
  1086. $query = db_select('node', 'n');
  1087. $ti_alias = $query->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)');
  1088. $td_alias = $query->innerJoin('taxonomy_term_data', 'td', "({$ti_alias}.tid = %alias.tid)");
  1089. $w_alias = $query->leftJoin('faq_weights', 'w', "%alias.tid = {$ti_alias}.tid AND n.nid = %alias.nid");
  1090. $query
  1091. ->fields('n', array('nid'))
  1092. ->condition('n.type', 'faq')
  1093. ->condition('n.status', 1)
  1094. ->condition("{$ti_alias}.tid", $child_term->tid)
  1095. ->addTag('node_access');
  1096. $default_weight = 0;
  1097. if ($default_sorting == 'ASC') {
  1098. $default_weight = 1000000;
  1099. }
  1100. // Works, but involves variable concatenation - safe though, since
  1101. // $default_weight is an integer.
  1102. $query->addExpression("COALESCE(w.weight, $default_weight)", 'effective_weight');
  1103. // Doesn't work in Postgres.
  1104. //$query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));
  1105. $query->orderBy('effective_weight', 'ASC')
  1106. ->orderBy('n.sticky', 'DESC');
  1107. if ($default_sorting == 'ASC') {
  1108. $query->orderBy('n.created', 'ASC');
  1109. }
  1110. else {
  1111. $query->orderBy('n.created', 'DESC');
  1112. }
  1113. if (module_exists('i18n_select')) {
  1114. $query->condition('n.language', i18n_select_langcodes());
  1115. $query->condition("{$td_alias}.language", i18n_select_langcodes());
  1116. }
  1117. // We only want the first column, which is nid, so that we can load all
  1118. // related nodes.
  1119. $nids = $query->execute()->fetchCol();
  1120. $data = node_load_multiple($nids);
  1121. $output[] = theme($theme_function, array('data' => $data, 'display_header' => 1, 'category_display' => $category_display, 'term' => $child_term, 'class' => $class, 'parent_term' => $parent_term));
  1122. }
  1123. }
  1124. return $output;
  1125. }
  1126. /**
  1127. * Helper function to setup the list of sub-categories for the header.
  1128. *
  1129. * @param $term
  1130. * The term to setup the list of child terms for.
  1131. * @return
  1132. * An array of sub-categories.
  1133. */
  1134. function faq_view_child_category_headers($term) {
  1135. $child_categories = array();
  1136. $list = taxonomy_get_children($term->tid);
  1137. foreach ($list as $tid => $child_term) {
  1138. $term_node_count = faq_taxonomy_term_count_nodes($child_term->tid);
  1139. if ($term_node_count) {
  1140. // Get taxonomy image.
  1141. $term_image = '';
  1142. if (module_exists('taxonomy_image')) {
  1143. $term_image = taxonomy_image_display($child_term->tid, array('class' => 'faq-tax-image'));
  1144. }
  1145. $term_vars['link'] = l(faq_tt("taxonomy:term:$child_term->tid:name", $child_term->name), "faq-page/$child_term->tid");
  1146. $term_vars['description'] = check_markup(faq_tt("taxonomy:term:$child_term->tid:description", $child_term->description));
  1147. $term_vars['count'] = $term_node_count;
  1148. $term_vars['term_image'] = $term_image;
  1149. $child_categories[] = $term_vars;
  1150. }
  1151. }
  1152. return $child_categories;
  1153. }
  1154. /**
  1155. * Implements hook_pathauto().
  1156. */
  1157. function faq_pathauto($op) {
  1158. switch ($op) {
  1159. case 'settings':
  1160. $settings = array();
  1161. $settings['module'] = 'faq';
  1162. $settings['groupheader'] = t('FAQ category page settings');
  1163. $settings['patterndescr'] = t('Default path pattern (applies to all FAQ categories with blank patterns below)');
  1164. $settings['patterndefault'] = t('faq-page/[term:tid]');
  1165. $settings['batch_update_callback'] = 'faq_pathauto_bulkupdate';
  1166. $settings['token_type'] = 'term';
  1167. return (object) $settings;
  1168. default:
  1169. break;
  1170. }
  1171. }
  1172. /**
  1173. * Implements hook_path_alias_types().
  1174. */
  1175. function faq_path_alias_types() {
  1176. $objects['faq-page/'] = t('FAQ pages');
  1177. return $objects;
  1178. }
  1179. /**
  1180. * Batch processing callback; Generate aliases for faq pages.
  1181. */
  1182. function faq_pathauto_bulkupdate() {
  1183. module_load_include('inc', 'pathauto');
  1184. if (!isset($context['sandbox']['current'])) {
  1185. $context['sandbox']['count'] = 0;
  1186. $context['sandbox']['current'] = 0;
  1187. }
  1188. // Get the allowed vocabs.
  1189. $vocabularies = taxonomy_get_vocabularies('faq');
  1190. $vocab_omit = variable_get('faq_omit_vocabulary', array());
  1191. $faq_vocabs = array();
  1192. foreach ($vocabularies as $vid => $vobj) {
  1193. if (isset($vocab_omit[$vid]) && $vocab_omit[$vid] != 0) {
  1194. continue;
  1195. }
  1196. $faq_vocabs[$vid] = $vid;
  1197. }
  1198. // Get tids that need aliasing.
  1199. $query = db_select('taxonomy_term_data', 'td');
  1200. $query->leftJoin('url_alias', 'ua', "CONCAT('faq-page/', td.tid) = ua.source");
  1201. $query->addField('td', 'tid');
  1202. $query->isNull('ua.source');
  1203. $query->condition('td.tid', $context['sandbox']['current'], '>');
  1204. $query->condition('td.vid', $faq_vocabs, 'IN');
  1205. $query->orderBy('td.tid');
  1206. $query->addTag('pathauto_bulk_update');
  1207. $query->addMetaData('entity', 'taxonomy_term');
  1208. // Get the total amount of items to process.
  1209. if (!isset($context['sandbox']['total'])) {
  1210. $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
  1211. // If there are no nodes to update, the stop immediately.
  1212. if (!$context['sandbox']['total']) {
  1213. $context['finished'] = 1;
  1214. return;
  1215. }
  1216. }
  1217. $query->range(0, 25);
  1218. $tids = $query->execute()->fetchCol();
  1219. $terms = taxonomy_term_load_multiple($tids);
  1220. foreach ($terms as $term) {
  1221. pathauto_create_alias('faq', 'bulkupdate', 'faq-page/' . $term->tid, array('term' => $term));
  1222. }
  1223. $context['sandbox']['count'] += count($tids);
  1224. $context['sandbox']['current'] = max($tids);
  1225. $context['message'] = t('Updated alias for faq page @tid.', array('@tid' => end($tids)));
  1226. if ($context['sandbox']['count'] != $context['sandbox']['total']) {
  1227. $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
  1228. }
  1229. }
  1230. /**
  1231. * Implements hook_taxonomy_term_insert().
  1232. */
  1233. function faq_taxonomy_term_insert($term) {
  1234. if (module_exists('pathauto')) {
  1235. module_load_include('inc', 'pathauto');
  1236. $vocabularies = taxonomy_get_vocabularies('faq');
  1237. $vocab_omit = variable_get('faq_omit_vocabulary', array());
  1238. foreach ($vocabularies as $vid => $vobj) {
  1239. if ((isset($vocab_omit[$vid]) && $vocab_omit[$vid] != 0) || ($term->vid != $vid)) {
  1240. continue;
  1241. }
  1242. $alias = pathauto_create_alias('faq', 'insert', 'faq-page/' . $term->tid, array('term' => $term));
  1243. }
  1244. }
  1245. }
  1246. /**
  1247. * Implements hook_taxonomy_term_update().
  1248. */
  1249. function faq_taxonomy_term_update($term) {
  1250. if (module_exists('pathauto')) {
  1251. module_load_include('inc', 'pathauto');
  1252. $vocabularies = taxonomy_get_vocabularies('faq');
  1253. $vocab_omit = variable_get('faq_omit_vocabulary', array());
  1254. foreach ($vocabularies as $vid => $vobj) {
  1255. if ((isset($vocab_omit[$vid]) && $vocab_omit[$vid] != 0) || ($term->vid != $vid)) {
  1256. continue;
  1257. }
  1258. $alias = pathauto_create_alias('faq', 'update', 'faq-page/' . $term->tid, array('term' => $term));
  1259. }
  1260. }
  1261. }
  1262. /**
  1263. * Implements hook_taxonomy_term_delete().
  1264. */
  1265. function faq_taxonomy_term_delete($term) {
  1266. if (module_exists('pathauto')) {
  1267. module_load_include('inc', 'pathauto');
  1268. pathauto_path_delete_all("faq-page/{$term->tid}");
  1269. }
  1270. }
  1271. /**
  1272. * Function to set up the FAQ breadcrumbs for a given taxonomy term.
  1273. *
  1274. * @param $term
  1275. * The taxonomy term object.
  1276. */
  1277. function faq_set_breadcrumb($term = NULL) {
  1278. $breadcrumb = array();
  1279. if (variable_get('faq_custom_breadcrumbs', TRUE)) {
  1280. if (module_exists("taxonomy") && $term) {
  1281. $breadcrumb[] = l(faq_tt("taxonomy:term:$term->tid:name", $term->name), 'faq-page/' . $term->tid);
  1282. while ($parents = taxonomy_get_parents($term->tid)) {
  1283. $term = array_shift($parents);
  1284. $breadcrumb[] = l(faq_tt("taxonomy:term:$term->tid:name", $term->name), 'faq-page/' . $term->tid);
  1285. }
  1286. }
  1287. $breadcrumb[] = l(variable_get('faq_title', 'Frequently Asked Questions'), 'faq-page');
  1288. $breadcrumb[] = l(t('Home'), NULL, array('attributes' => array('title' => variable_get('site_name', ''))));
  1289. $breadcrumb = array_reverse($breadcrumb);
  1290. return drupal_set_breadcrumb($breadcrumb);
  1291. }
  1292. // This is also used to set the breadcrumbs in the faq_preprocess_page()
  1293. // so we need to return a valid trail.
  1294. return drupal_get_breadcrumb();
  1295. }
  1296. /**
  1297. * Implements template_preprocess_page().
  1298. *
  1299. * Override the breadcrumbs for faq nodes.
  1300. */
  1301. function faq_preprocess_page(&$variables) {
  1302. if (!empty($variables['node']) && isset($variables['node']->type) && $variables['node']->type == 'faq' && module_exists('taxonomy')) {
  1303. if (!empty($variables['node']->taxonomy)) {
  1304. foreach ($variables['node']->taxonomy as $term) {
  1305. continue;
  1306. }
  1307. }
  1308. else {
  1309. $term = NULL;
  1310. }
  1311. $variables['breadcrumb'] = theme('breadcrumb', array('breadcrumb' => faq_set_breadcrumb($term)));
  1312. }
  1313. }
  1314. /**
  1315. * Helper function for when i18ntaxonomy module is not installed.
  1316. */
  1317. function faq_tt($string_id, $default, $language = NULL) {
  1318. return function_exists('tt') ? tt($string_id, $default, $language) : $default;
  1319. }
  1320. /**
  1321. * Implements hook_filter_info().
  1322. */
  1323. function faq_filter_info() {
  1324. $filters['faq_embed'] = array(
  1325. 'title' => t('Embed FAQ page'),
  1326. 'cache' => FALSE,
  1327. 'description' => t('Embed FAQ page using [faq] type tags. Disables filter caching so not recommended for all input formats.'),
  1328. 'tips callback' => 'faq_filter_tips_faq_embed',
  1329. 'process callback' => '_faq_filter_process',
  1330. 'settings callback' => '_faq_filter_settings',
  1331. );
  1332. return $filters;
  1333. }
  1334. function _faq_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
  1335. return array();
  1336. }
  1337. function _faq_filter_process($text) {
  1338. $text = preg_replace_callback('/\[faq:?([^\]]*)\]/', '_faq_faq_page_filter_replacer', $text);
  1339. // Remove comments, as they're not supported by all input formats.
  1340. $text = preg_replace('/<!--.*?-->/', '', $text);
  1341. return $text;
  1342. }
  1343. /**
  1344. * Filter tips callback function for $filters[0] in hook_filter_info().
  1345. */
  1346. function faq_filter_tips_faq_embed($format, $long = FALSE) {
  1347. return t('[faq] or [faq:123,questions_inline,categories_inline] - insert FAQ content based on the optional category, question style and category style.');
  1348. }
  1349. /**
  1350. * Helper function for faq input filter.
  1351. */
  1352. function _faq_faq_page_filter_replacer($matches) {
  1353. $tid = 0;
  1354. $faq_display = '';
  1355. $category_display = '';
  1356. if (drupal_strlen($matches[1])) {
  1357. list($tid, $faq_display, $category_display) = explode(',', $matches[1] . ',,');
  1358. $tid = (int) trim($tid);
  1359. $faq_display = trim($faq_display);
  1360. $category_display = trim($category_display);
  1361. // These two checks ensure that a typo in the faq_display or
  1362. // category_display string still results in the FAQ showing.
  1363. if ($faq_display && !in_array($faq_display, array('questions_top', 'hide_answer', 'questions_inline', 'new_page'))) {
  1364. $faq_display = '';
  1365. }
  1366. if ($category_display && !in_array($category_display, array('hide_qa', 'new_page', 'categories_inline'))) {
  1367. $category_display = '';
  1368. }
  1369. }
  1370. return faq_page($tid, $faq_display, $category_display);
  1371. }
  1372. /**
  1373. * Count number of nodes for a term and its children.
  1374. */
  1375. function faq_taxonomy_term_count_nodes($tid) {
  1376. static $count;
  1377. if (!isset($count) || !isset($count[$tid])) {
  1378. $query = db_select('node', 'n')
  1379. ->fields('n', array('nid'))
  1380. ->addTag('node_access');
  1381. $query->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
  1382. $query->condition('n.type', 'faq')
  1383. ->condition('n.status', 1)
  1384. ->condition('ti.tid', $tid);
  1385. $count[$tid] = $query->countQuery()->execute()->fetchField();
  1386. }
  1387. $children_count = 0;
  1388. foreach (faq_taxonomy_term_children($tid) as $child_term) {
  1389. $children_count += faq_taxonomy_term_count_nodes($child_term);
  1390. }
  1391. return $count[$tid] + $children_count;
  1392. }
  1393. /**
  1394. * Helper function to faq_taxonomy_term_count_nodes() to return list of child terms.
  1395. */
  1396. function faq_taxonomy_term_children($tid) {
  1397. static $children;
  1398. if (!isset($children)) {
  1399. $result = db_select('taxonomy_term_hierarchy', 'tth')
  1400. ->fields('tth', array('parent', 'tid'))
  1401. ->execute();
  1402. while ($term = $result->fetch()) {
  1403. $children[$term->parent][] = $term->tid;
  1404. }
  1405. }
  1406. return isset($children[$tid]) ? $children[$tid] : array();
  1407. }
  1408. /**
  1409. * Theme function for faq page wrapper divs.
  1410. */
  1411. function theme_faq_page($variables) {
  1412. $content = $variables['content'];
  1413. $answers = $variables['answers'];
  1414. $description = $variables['description'];
  1415. $output = '<div class="faq-content"><div class="faq">';
  1416. if (!empty($description)) {
  1417. $output .= '<div class="faq-description">' . $description . "</div>\n";
  1418. }
  1419. if (variable_get('faq_show_expand_all', FALSE)) {
  1420. $output .= '<div id="faq-expand-all">';
  1421. $output .= '<a class="faq-expand-all-link" href="#faq-expand-all-link">[' . t('expand all') . ']</a>';
  1422. $output .= '<a class="faq-collapse-all-link" href="#faq-collapse-all-link">[' . t('collapse all') . ']</a>';
  1423. $output .= "</div>\n";
  1424. }
  1425. $output .= $content;
  1426. $output .= $answers . "</div></div>\n";
  1427. return $output;
  1428. }
  1429. /**
  1430. * Theme function for question ordering drag and drop table.
  1431. */
  1432. function theme_faq_draggable_question_order_table($variables) {
  1433. $form = $variables['form'];
  1434. drupal_add_tabledrag('question-sort', 'order', 'sibling', 'sort');
  1435. $header = array('', t('Question'), '', t('Sort'));
  1436. $rows = array();
  1437. foreach (element_children($form) as $key) {
  1438. // Add class to group weight fields for drag and drop.
  1439. $form[$key]['sort']['#attributes']['class'] = array('sort');
  1440. $row = array('');
  1441. $row[] = drupal_render($form[$key]['title']);
  1442. $row[] = drupal_render($form[$key]['nid']);
  1443. $row[] = drupal_render($form[$key]['sort']);
  1444. $rows[] = array(
  1445. 'data' => $row,
  1446. 'class' => array('draggable'),
  1447. );
  1448. }
  1449. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'question-sort')));
  1450. $output .= drupal_render_children($form);
  1451. return $output;
  1452. }