faq.admin.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the faq module.
  5. */
  6. /**
  7. * Generates the settings form for the FAQ module.
  8. *
  9. * @param string $op
  10. * Default value is NULL; determines what are the permissions of the current
  11. * user on the FAQ.
  12. *
  13. * @return string
  14. * The output, which contains the HTML code for the settings form generated by
  15. * drupal_get_form() function.
  16. */
  17. function faq_settings_page($op = NULL) {
  18. $output = drupal_get_form('faq_general_settings_form');
  19. return $output;
  20. }
  21. /**
  22. * Define a form to edit the page header and descriptive text.
  23. *
  24. * @return array
  25. * The general settings form code stored in the $form variable, before
  26. * converted to HTML.
  27. */
  28. function faq_general_settings_form($form) {
  29. $form['faq_title'] = array(
  30. '#type' => 'textfield',
  31. '#title' => t('Title'),
  32. '#default_value' => variable_get('faq_title', 'Frequently Asked Questions'),
  33. );
  34. $faq_description_default = array('value' => '', 'format' => filter_fallback_format());
  35. $faq_description = variable_get('faq_description', $faq_description_default);
  36. $form['faq_description'] = array(
  37. '#type' => 'text_format',
  38. '#title' => t('FAQ Description'),
  39. '#default_value' => $faq_description['value'],
  40. '#description' => t('Your FAQ description. This will be placed at the top of the page, above the questions and can serve as an introductory text.'),
  41. '#rows' => 5,
  42. '#format' => $faq_description['format'],
  43. );
  44. $form['faq_custom_breadcrumbs'] = array(
  45. '#type' => 'checkbox',
  46. '#title' => t('Create custom breadcrumbs for the FAQ'),
  47. '#description' => t('This option set the breadcrumb path to "%home > %faqtitle > category trail".',
  48. array(
  49. '%home' => t('Home'),
  50. '%faqtitle' => variable_get('faq_title', 'Frequently Asked Questions'),
  51. )
  52. ),
  53. '#default_value' => variable_get('faq_custom_breadcrumbs', TRUE),
  54. );
  55. $form['faq_path'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('FAQ Path'),
  58. '#description' => t('This option sets the path to the faq page. DO NOT append with a \'/\''),
  59. '#default_value' => _faq_path(),
  60. );
  61. return system_settings_form($form);
  62. }
  63. /**
  64. * Validate for general settings form.
  65. */
  66. function faq_general_settings_form_validate($form, &$form_state) {
  67. $path = $form_state['values']['faq_path'];
  68. // Check if there is no special characters and trailing slash.
  69. $pattern = "/[\w-\/]+(?<!\/)$/";
  70. if (!preg_match($pattern, $path)) {
  71. form_set_error('faq_path', t('FAQ path is not correct.'));
  72. }
  73. }
  74. /**
  75. * Rebuilds the menu to account for adjustments to the faq_path variable.
  76. */
  77. function faq_general_settings_form_submit($form, &$form_state) {
  78. menu_rebuild();
  79. }
  80. /**
  81. * Define the elements for the FAQ Settings page - Questions tab.
  82. *
  83. * @return array
  84. * The form code inside the $form array.
  85. */
  86. function faq_questions_settings_form($form, &$form_state) {
  87. $form['faq_category_display'] = array(
  88. '#type' => 'hidden',
  89. '#value' => variable_get('faq_category_display', 'categories_inline'),
  90. );
  91. $form['faq_use_categories'] = array(
  92. '#type' => 'hidden',
  93. '#value' => variable_get('faq_use_categories', FALSE),
  94. );
  95. $display_options['questions_inline'] = t('Questions inline');
  96. $display_options['questions_top'] = t('Clicking on question takes user to answer further down the page');
  97. $display_options['hide_answer'] = t('Clicking on question opens/hides answer under question');
  98. $display_options['new_page'] = t('Clicking on question opens the answer in a new page');
  99. $form['faq_display'] = array(
  100. '#type' => 'radios',
  101. '#options' => $display_options,
  102. '#title' => t('Page layout'),
  103. '#description' => t('This controls how the questions and answers are displayed on the page and what happens when someone clicks on the question.'),
  104. '#default_value' => variable_get('faq_display', 'questions_top'),
  105. );
  106. $form['faq_questions_misc'] = array(
  107. '#type' => 'fieldset',
  108. '#title' => t('Miscellaneous layout settings'),
  109. '#collapsible' => TRUE,
  110. );
  111. $form['faq_questions_misc']['faq_question_listing'] = array(
  112. '#type' => 'select',
  113. '#options' => array(
  114. 'ol' => t('Ordered list'),
  115. 'ul' => t('Unordered list'),
  116. ),
  117. '#title' => t('Questions listing style'),
  118. '#default_value' => variable_get('faq_question_listing', 'ul'),
  119. '#states' => array(
  120. 'visible' => array(
  121. ':input[name="faq_display"], dummy-0' => array('!value' => 'hide_answer'),
  122. ':input[name="faq_display"], dummy-1' => array('!value' => 'questions_inline'),
  123. ),
  124. ),
  125. );
  126. $form['faq_questions_misc']['faq_qa_mark'] = array(
  127. '#type' => 'checkbox',
  128. '#title' => t('Label questions and answers'),
  129. '#description' => t('It labels all questions on the faq page with the "question label" setting and all answers with the "answer label" setting. For example these could be set to "Q:" and "A:".'),
  130. '#default_value' => variable_get('faq_qa_mark', FALSE),
  131. '#states' => array(
  132. 'visible' => array(
  133. ':input[name="faq_display"], dummy-0' => array('!value' => 'hide_answer'),
  134. ':input[name="faq_display"], dummy-1' => array('!value' => 'new_page'),
  135. ),
  136. ),
  137. );
  138. $form['faq_questions_misc']['faq_question_label'] = array(
  139. '#type' => 'textfield',
  140. '#title' => t('Question Label'),
  141. '#default_value' => variable_get('faq_question_label', 'Q:'),
  142. '#states' => array(
  143. 'visible' => array(
  144. ':input[name="faq_display"], dummy-0' => array('!value' => 'hide_answer'),
  145. ':input[name="faq_display"], dummy-1' => array('!value' => 'new_page'),
  146. ':input[name="faq_qa_mark"]' => array('checked' => TRUE),
  147. ),
  148. ),
  149. );
  150. $form['faq_questions_misc']['faq_answer_label'] = array(
  151. '#type' => 'textfield',
  152. '#title' => t('Answer Label'),
  153. '#default_value' => variable_get('faq_answer_label', 'A:'),
  154. '#states' => array(
  155. 'visible' => array(
  156. ':input[name="faq_display"], dummy-0' => array('!value' => 'hide_answer'),
  157. ':input[name="faq_display"], dummy-1' => array('!value' => 'new_page'),
  158. ':input[name="faq_qa_mark"]' => array('checked' => TRUE),
  159. ),
  160. ),
  161. );
  162. $form['faq_questions_misc']['faq_question_long_form'] = array(
  163. '#type' => 'checkbox',
  164. '#title' => t('Allow long question text to be configured'),
  165. '#default_value' => variable_get('faq_question_long_form', 1),
  166. );
  167. $form['faq_questions_misc']['faq_question_length'] = array(
  168. '#type' => 'radios',
  169. '#title' => t('Question length'),
  170. '#options' => array(
  171. 'long' => t('Display longer text'),
  172. 'short' => t('Display short text'),
  173. 'both' => t('Display both short and long questions'),
  174. ),
  175. '#description' => t('The length of question text to display on the FAQ page. The short question will always be displayed in the FAQ blocks.'),
  176. '#default_value' => variable_get('faq_question_length', 'short'),
  177. '#states' => array(
  178. 'visible' => array(
  179. ':input[name="faq_question_long_form"]' => array('checked' => TRUE),
  180. ),
  181. ),
  182. );
  183. $form['faq_questions_misc']['faq_hide_qa_accordion'] = array(
  184. '#type' => 'checkbox',
  185. '#title' => t('Use accordion effect'),
  186. '#description' => t('This enables an "accordion" style effect where when a question is clicked, the answer appears beneath, and is then hidden when another question is opened.'),
  187. '#default_value' => variable_get('faq_hide_qa_accordion', FALSE),
  188. '#states' => array(
  189. 'visible' => array(
  190. ':input[name="faq_display"]' => array('value' => 'hide_answer'),
  191. ),
  192. ),
  193. );
  194. $form['faq_questions_misc']['faq_show_expand_all'] = array(
  195. '#type' => 'checkbox',
  196. '#title' => t('Show "expand / collapse all" links for collapsed questions'),
  197. '#default_value' => variable_get('faq_show_expand_all', FALSE),
  198. '#states' => array(
  199. 'invisible' => array(
  200. ':input[name="faq_display"]' => array('!value' => 'hide_answer'),
  201. ':input[name="faq_use_categories"]' => array('checked' => FALSE),
  202. ':input[name="faq_category_display"]' => array('!value' => 'hide_qa'),
  203. ),
  204. ),
  205. );
  206. $form['faq_questions_misc']['faq_use_teaser'] = array(
  207. '#type' => 'checkbox',
  208. '#title' => t('Use answer teaser'),
  209. '#default_value' => variable_get('faq_use_teaser', FALSE),
  210. '#states' => array(
  211. 'invisible' => array(
  212. ':input[name="faq_display"]' => array('value' => 'new_page'),
  213. ),
  214. ),
  215. );
  216. $form['faq_questions_misc']['faq_show_node_links'] = array(
  217. '#type' => 'checkbox',
  218. '#title' => t('Show node links'),
  219. '#description' => t('This enables the display of links under the answer text on the faq page. Examples of these links include "Read more", "Add comment".'),
  220. '#default_value' => variable_get('faq_show_node_links', FALSE),
  221. '#states' => array(
  222. 'invisible' => array(
  223. ':input[name="faq_display"]' => array('value' => 'new_page'),
  224. ),
  225. ),
  226. );
  227. $form['faq_questions_misc']['faq_back_to_top'] = array(
  228. '#type' => 'textfield',
  229. '#title' => t('"Back to Top" link text'),
  230. '#description' => t('This allows the user to change the text displayed for the links which return the user to the top of the page. Defaults to "Back to Top". Leave blank to have no link.'),
  231. '#default_value' => variable_get('faq_back_to_top', 'Back to Top'),
  232. '#states' => array(
  233. 'invisible' => array(
  234. ':input[name="faq_display"]' => array('value' => 'new_page'),
  235. ),
  236. ),
  237. );
  238. $form['faq_questions_misc']['faq_disable_node_links'] = array(
  239. '#type' => 'checkbox',
  240. '#title' => t('Disable question links to nodes'),
  241. '#description' => t('This allows the user to prevent the questions being links to the faq node.'),
  242. '#default_value' => variable_get('faq_disable_node_links', FALSE),
  243. '#states' => array(
  244. 'invisible' => array(
  245. ':input[name="faq_display"]' => array('value' => 'new_page'),
  246. ),
  247. ),
  248. );
  249. $form['faq_questions_misc']['faq_default_sorting'] = array(
  250. '#type' => 'select',
  251. '#title' => t('Default sorting for unordered FAQs'),
  252. '#options' => array(
  253. 'DESC' => t('Date Descending'),
  254. 'ASC' => t('Date Ascending'),
  255. ),
  256. '#description' => t("This controls the default ordering behaviour for new FAQ nodes which haven't been assigned a position."),
  257. '#default_value' => variable_get('faq_default_sorting', 'DESC'),
  258. );
  259. return system_settings_form($form);
  260. }
  261. /**
  262. * Define the elements for the FAQ Settings page - categories tab.
  263. *
  264. * @return array
  265. * The form code inside the $form array.
  266. */
  267. function faq_categories_settings_form($form, &$form_state) {
  268. $categorization_enabled = variable_get('faq_use_categories', FALSE);
  269. $vocabularies = array();
  270. $vocab_omit = array();
  271. $vocab_options = array();
  272. // Display error messages if the taxonomy module isn't enabled or if there is
  273. // no valid FAQ vocabulary.
  274. if (!module_exists("taxonomy")) {
  275. drupal_set_message(t('Categorization of questions will not work without the "taxonomy" module being enabled.'), 'error');
  276. }
  277. else {
  278. $vocabularies = taxonomy_get_vocabularies('faq');
  279. $vocab_omit = variable_get('faq_omit_vocabulary', array());
  280. foreach ($vocabularies as $vid => $vobj) {
  281. $vocab_options[$vid] = $vobj->name;
  282. }
  283. }
  284. // Set up a hidden variable.
  285. $form['faq_display'] = array(
  286. '#type' => 'hidden',
  287. '#value' => variable_get('faq_display', 'questions_top'),
  288. );
  289. $form['faq_use_categories'] = array(
  290. '#type' => 'checkbox',
  291. '#title' => t('Categorize questions'),
  292. '#description' => t('This allows the user to display the questions according to the categories configured on the add/edit FAQ page. Use of sub-categories is only recommended for large lists of questions. The Taxonomy module must be enabled.'),
  293. '#default_value' => $categorization_enabled,
  294. );
  295. $category_options['none'] = t("Don't display");
  296. $category_options['categories_inline'] = t('Categories inline');
  297. $category_options['hide_qa'] = t('Clicking on category opens/hides questions and answers under category');
  298. $category_options['new_page'] = t('Clicking on category opens the questions/answers in a new page');
  299. $form['faq_category_display'] = array(
  300. '#type' => 'radios',
  301. '#options' => $category_options,
  302. '#title' => t('Categories layout'),
  303. '#description' => t('This controls how the categories are displayed on the page and what happens when someone clicks on the category.'),
  304. '#default_value' => variable_get('faq_category_display', 'categories_inline'),
  305. '#states' => array(
  306. 'visible' => array(
  307. ':input[name="faq_use_categories"]' => array('checked' => TRUE),
  308. ),
  309. ),
  310. );
  311. $form['faq_category_misc'] = array(
  312. '#type' => 'fieldset',
  313. '#title' => t('Miscellaneous layout settings'),
  314. '#collapsible' => TRUE,
  315. '#states' => array(
  316. 'visible' => array(
  317. ':input[name="faq_use_categories"]' => array('checked' => TRUE),
  318. ),
  319. ),
  320. );
  321. $form['faq_category_misc']['faq_category_listing'] = array(
  322. '#type' => 'select',
  323. '#options' => array(
  324. 'ol' => t('Ordered list'),
  325. 'ul' => t('Unordered list'),
  326. ),
  327. '#title' => t('Categories listing style'),
  328. '#description' => t('This allows to select how the categories listing is presented. An ordered listing would number the categories, whereas an unordered list will have a bullet to the left of each category.'),
  329. '#default_value' => variable_get('faq_category_listing', 'ul'),
  330. '#states' => array(
  331. 'visible' => array(
  332. ':input[name="faq_category_display"]' => array('value' => 'new_page'),
  333. ),
  334. ),
  335. );
  336. $form['faq_category_misc']['faq_category_hide_qa_accordion'] = array(
  337. '#type' => 'checkbox',
  338. '#title' => t('Use accordion effect'),
  339. '#description' => t('This enables an "accordion" style effect where when a category is clicked, the questions appears beneath, and is then hidden when another category is opened.'),
  340. '#default_value' => variable_get('faq_category_hide_qa_accordion', FALSE),
  341. '#states' => array(
  342. 'visible' => array(
  343. ':input[name="faq_category_display"]' => array('value' => 'hide_qa'),
  344. ),
  345. ),
  346. );
  347. $form['faq_category_misc']['faq_count'] = array(
  348. '#type' => 'checkbox',
  349. '#title' => t('Show FAQ count'),
  350. '#description' => t('This displays the number of questions in a category after the category name.'),
  351. '#default_value' => variable_get('faq_count', FALSE),
  352. );
  353. $form['faq_category_misc']['faq_answer_category_name'] = array(
  354. '#type' => 'checkbox',
  355. '#title' => t('Display category name for answers'),
  356. '#description' => t('This allows the user to toggle the visibility of the category name above each answer section.'),
  357. '#default_value' => variable_get('faq_answer_category_name', FALSE),
  358. '#states' => array(
  359. 'visible' => array(
  360. ':input[name="faq_display"]' => array('value' => 'hide_qa'),
  361. ),
  362. ),
  363. );
  364. $form['faq_category_misc']['faq_group_questions_top'] = array(
  365. '#type' => 'checkbox',
  366. '#title' => t("Group questions and answers"),
  367. '#default_value' => variable_get('faq_group_questions_top', FALSE),
  368. '#states' => array(
  369. 'visible' => array(
  370. ':input[name="faq_category_display"]' => array('value' => 'categories_inline'),
  371. ':input[name="faq_display"]' => array('value' => 'hide_qa'),
  372. ),
  373. ),
  374. );
  375. $form['faq_category_misc']['faq_hide_child_terms'] = array(
  376. '#type' => 'checkbox',
  377. '#title' => t('Only show sub-categories when parent category is selected'),
  378. '#description' => t('This allows the user more control over how and when sub-categories are displayed.'),
  379. '#default_value' => variable_get('faq_hide_child_terms', FALSE),
  380. '#states' => array(
  381. 'invisible' => array(
  382. ':input[name="faq_category_display"]' => array('value' => 'categories_inline'),
  383. ),
  384. ),
  385. );
  386. $form['faq_category_misc']['faq_show_term_page_children'] = array(
  387. '#type' => 'checkbox',
  388. '#title' => t('Show sub-categories on FAQ category pages'),
  389. '#description' => t("Sub-categories with 'faq' nodes will be displayed on the per category FAQ page. This will also happen if 'Only show sub-categories when parent category is selected' is set."),
  390. '#default_value' => variable_get('faq_show_term_page_children', FALSE),
  391. '#states' => array(
  392. 'visible' => array(
  393. ':input[name="faq_hide_child_terms"]' => array('checked' => FALSE),
  394. ':input[name="faq_category_display"]' => array('!value' => 'categories_inline'),
  395. ),
  396. ),
  397. );
  398. if (module_exists('taxonomy') && !empty($vocab_options)) {
  399. $form['faq_category_advanced'] = array(
  400. '#type' => 'fieldset',
  401. '#title' => t('Advanced category settings'),
  402. '#collapsible' => TRUE,
  403. '#collapsed' => TRUE,
  404. '#states' => array(
  405. 'visible' => array(
  406. ':input[name="faq_use_categories"]' => array('checked' => TRUE),
  407. ),
  408. ),
  409. );
  410. $form['faq_category_advanced']['faq_omit_vocabulary'] = array(
  411. '#type' => 'checkboxes',
  412. '#title' => t('Omit vocabulary'),
  413. '#description' => t('Terms from these vocabularies will be <em>excluded</em> from the FAQ pages.'),
  414. '#default_value' => variable_get('faq_omit_vocabulary', array()),
  415. '#options' => $vocab_options,
  416. '#multiple' => TRUE,
  417. );
  418. }
  419. $form['#submit'][] = 'faq_categories_settings_form_submit';
  420. return system_settings_form($form);
  421. }
  422. /**
  423. * Display an error if no valid FAQ vocabularies available.
  424. */
  425. function faq_categories_settings_form_submit($form, &$form_state) {
  426. if ($form_state['values']['faq_use_categories'] && isset($form_state['values']['faq_omit_vocabulary'])) {
  427. $vocabularies = taxonomy_get_vocabularies('faq');
  428. $vocab_omit = $form_state['values']['faq_omit_vocabulary'];
  429. $valid_vocab = FALSE;
  430. foreach ($vocabularies as $vid => $vobj) {
  431. if (empty($vocab_omit[$vid])) {
  432. $valid_vocab = TRUE;
  433. }
  434. }
  435. if (!$valid_vocab) {
  436. 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');
  437. }
  438. }
  439. }
  440. /**
  441. * Define the elements for the FAQ Settings page - order tab.
  442. *
  443. * @param array $form_state
  444. * Store the submitted form values.
  445. *
  446. * @return array
  447. * The form code, before being converted to HTML format.
  448. */
  449. function faq_order_settings_form($form, $form_state, $category = NULL) {
  450. $order = $date_order = '';
  451. drupal_add_js(array('faq' => array('faq_hide_qa_accordion' => variable_get('faq_hide_qa_accordion', FALSE))), array('type' => 'setting', 'scope' => JS_DEFAULT));
  452. 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));
  453. drupal_add_js(drupal_get_path('module', 'faq') . '/faq.js');
  454. drupal_add_css(drupal_get_path('module', 'faq') . '/faq.css');
  455. $use_categories = variable_get('faq_use_categories', FALSE);
  456. if (!$use_categories) {
  457. $step = "order";
  458. }
  459. elseif (!isset($form_state['values']) && empty($category)) {
  460. $step = "categories";
  461. }
  462. else {
  463. $step = "order";
  464. }
  465. $form['step'] = array(
  466. '#type' => 'value',
  467. '#value' => $step,
  468. );
  469. // Categorized q/a.
  470. if ($step == "categories") {
  471. // Get list of categories.
  472. $vocabularies = taxonomy_get_vocabularies('faq');
  473. $options = array();
  474. foreach ($vocabularies as $vid => $vobj) {
  475. $tree = taxonomy_get_tree($vid);
  476. foreach ($tree as $term) {
  477. if (!faq_taxonomy_term_count_nodes($term->tid)) {
  478. continue;
  479. }
  480. $options[$term->tid] = faq_tt("taxonomy:term:$term->tid:name", $term->name);
  481. $form['choose_cat']['faq_category'] = array(
  482. '#type' => 'select',
  483. '#title' => t('Choose a category'),
  484. '#description' => t('Choose a category that you wish to order the questions for.'),
  485. '#options' => $options,
  486. '#multiple' => FALSE,
  487. );
  488. $form['choose_cat']['search'] = array(
  489. '#type' => 'submit',
  490. '#value' => t('Search'),
  491. '#submit' => array('faq_order_settings_choose_cat_form_submit'),
  492. );
  493. }
  494. }
  495. }
  496. else {
  497. $default_sorting = variable_get('faq_default_sorting', 'DESC');
  498. $default_weight = 0;
  499. if ($default_sorting != 'DESC') {
  500. $default_weight = 1000000;
  501. }
  502. $options = array();
  503. if (!empty($form_state['values']['faq_category'])) {
  504. $category = $form_state['values']['faq_category'];
  505. }
  506. // Uncategorized ordering.
  507. $query = db_select('node', 'n')
  508. ->fields('n', array('nid', 'title'))
  509. ->addTag('node_access')
  510. ->condition('n.type', 'faq')
  511. ->condition('n.status', 1);
  512. // Works, but involves variable concatenation - safe though, since
  513. // $default_weight is an integer.
  514. $query->addExpression("COALESCE(w.weight, $default_weight)", 'effective_weight');
  515. // Doesn't work in Postgres.
  516. // $query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));
  517. if (empty($category)) {
  518. $category = 0;
  519. $w_alias = $query->leftJoin('faq_weights', 'w', 'n.nid = %alias.nid AND %alias.tid = :category', array(':category' => $category));
  520. $query->orderBy('effective_weight', 'ASC')
  521. ->orderBy('n.sticky', 'DESC')
  522. ->orderBy('n.created', $default_sorting == 'DESC' ? 'DESC' : 'ASC');
  523. }
  524. // Categorized ordering.
  525. else {
  526. $ti_alias = $query->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)');
  527. $w_alias = $query->leftJoin('faq_weights', 'w', 'n.nid = %alias.nid AND %alias.tid = :category', array(':category' => $category));
  528. $query->condition('ti.tid', $category);
  529. $query->orderBy('effective_weight', 'ASC')
  530. ->orderBy('n.sticky', 'DESC')
  531. ->orderBy('n.created', $default_sorting == 'DESC' ? 'DESC' : 'ASC');
  532. }
  533. $options = $query->execute()->fetchAll();
  534. $form['weight']['faq_category'] = array(
  535. '#type' => 'value',
  536. '#value' => $category,
  537. );
  538. // Show table ordering form.
  539. $form['order_no_cats']['#tree'] = TRUE;
  540. $form['order_no_cats']['#theme'] = 'faq_draggable_question_order_table';
  541. $i = 0;
  542. foreach ($options as $record) {
  543. $form['order_no_cats'][$i]['nid'] = array(
  544. '#type' => 'hidden',
  545. '#value' => $record->nid,
  546. );
  547. $form['order_no_cats'][$i]['title'] = array('#markup' => check_plain($record->title));
  548. $form['order_no_cats'][$i]['sort'] = array(
  549. '#type' => 'weight',
  550. '#delta' => count($options),
  551. '#default_value' => $i,
  552. );
  553. $i++;
  554. }
  555. $form['submit'] = array(
  556. '#type' => 'submit',
  557. '#value' => t('Save order'),
  558. '#weight' => 3,
  559. '#submit' => array('faq_order_settings_reorder_form_submit'),
  560. );
  561. }
  562. return $form;
  563. }
  564. /**
  565. * Function set the rebuild of the form in the FAQ Settings - Weight tab.
  566. *
  567. * @param array $form
  568. * Array, containing the form structure.
  569. *
  570. * @param array &$form_state
  571. * The 'rebuild' key inside $form_state['rebuild'] structure, overrides the
  572. * 'redirect' key: when it is set to TRUE, the form will be rebuilt from
  573. * scratch and displayed on screen.
  574. */
  575. function faq_order_settings_choose_cat_form_submit($form, &$form_state) {
  576. $form_state['rebuild'] = TRUE;
  577. }
  578. /**
  579. * Save the options set by the user in the FAQ Settings - Weight tab.
  580. *
  581. * @param array $form
  582. * Array, containing the form structure.
  583. *
  584. * @param array &$form_state
  585. * $form_state['values'] stores the submitted values from the form.
  586. */
  587. function faq_order_settings_reorder_form_submit($form, &$form_state) {
  588. if ($form_state['values']['op'] == t('Save order') && !empty($form_state['values']['order_no_cats'])) {
  589. foreach ($form_state['values']['order_no_cats'] as $i => $faq) {
  590. $nid = $faq['nid'];
  591. $index = $faq['sort'];
  592. db_merge('faq_weights')
  593. ->fields(array(
  594. 'weight' => $index,
  595. ))
  596. ->key(array(
  597. 'tid' => $form_state['values']['faq_category'],
  598. 'nid' => $nid,
  599. ))
  600. ->execute();
  601. $result = NULL;
  602. // If node translation module enabled, update order of the translation
  603. // node counterparts.
  604. if (module_exists('translation')) {
  605. $node = node_load($nid);
  606. if ($node->tnid) {
  607. $translations = translation_node_get_translations($node->tnid);
  608. if (!empty($translations)) {
  609. foreach ($translations as $language => $tnode) {
  610. db_merge('faq_weights')
  611. ->fields(array(
  612. 'weight' => $index,
  613. ))
  614. ->key(array(
  615. 'tid' => $form_state['values']['faq_category'],
  616. 'nid' => $tnode->nid,
  617. ))
  618. ->execute();
  619. }
  620. }
  621. }
  622. }
  623. }
  624. drupal_set_message(t('Configuration has been updated.'));
  625. }
  626. }