faq.module 56 KB

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