faq.admin.inc 24 KB

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