faq.module 57 KB

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