faq.module 56 KB

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