faq.install 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /**
  3. * @file
  4. * FAQ module install file.
  5. */
  6. /**
  7. * Define the 'faq_weights' and 'faq_questions' table structures.
  8. *
  9. * @return array
  10. * The schema which contains the structure for the faq module's tables.
  11. */
  12. function faq_schema() {
  13. $schema['faq_weights'] = array(
  14. 'description' => 'A table containing the weight of each faq node by category.',
  15. 'fields' => array(
  16. 'tid' => array(
  17. 'type' => 'int',
  18. 'unsigned' => TRUE,
  19. 'not null' => TRUE,
  20. 'default' => 0,
  21. 'description' => 'The primary identifier for a term or category. This will be 0 for non-categorized nodes.',
  22. ),
  23. 'nid' => array(
  24. 'type' => 'int',
  25. 'unsigned' => TRUE,
  26. 'not null' => TRUE,
  27. 'default' => 0,
  28. 'description' => 'The primary identifier for a node.',
  29. ),
  30. 'weight' => array(
  31. 'type' => 'int',
  32. 'size' => 'tiny',
  33. 'not null' => TRUE,
  34. 'default' => 0,
  35. 'description' => 'A number representing the weight of a node. Nodes with lower weight values will appear above those with higher weight values.',
  36. ),
  37. ),
  38. 'primary key' => array('nid', 'tid'),
  39. );
  40. $schema['faq_questions'] = array(
  41. 'description' => 'A table containing the long question text of each faq node revision.',
  42. 'fields' => array(
  43. 'nid' => array(
  44. 'type' => 'int',
  45. 'unsigned' => TRUE,
  46. 'not null' => TRUE,
  47. 'default' => 0,
  48. 'description' => 'The primary identifier for a node.',
  49. ),
  50. 'vid' => array(
  51. 'type' => 'int',
  52. 'unsigned' => TRUE,
  53. 'not null' => TRUE,
  54. 'default' => 0,
  55. 'description' => 'The primary identifier for a node revision.',
  56. ),
  57. 'question' => array(
  58. 'type' => 'text',
  59. 'size' => 'normal',
  60. 'not null' => TRUE,
  61. 'description' => 'The faq short question text.',
  62. ),
  63. 'detailed_question' => array(
  64. 'type' => 'text',
  65. 'size' => 'normal',
  66. 'not null' => FALSE,
  67. 'description' => 'The faq long question text.',
  68. ),
  69. ),
  70. 'primary key' => array('nid', 'vid'),
  71. );
  72. return $schema;
  73. }
  74. /**
  75. * Implements hook_install().
  76. *
  77. * Inserts the FAQ module's schema in the SQL database.
  78. */
  79. function faq_install() {
  80. variable_set('node_type_faq', array('status'));
  81. $t = get_t();
  82. // Ensure the FAQ node type is available.
  83. node_types_rebuild();
  84. $types = node_type_get_types();
  85. node_add_body_field($types['faq']);
  86. // Change the default label on the body field.
  87. $body_instance = field_info_instance('node', 'body', 'faq');
  88. $body_instance['label'] = $t('Answer');
  89. field_update_instance($body_instance);
  90. // Add the detailed question field.
  91. _faq_add_custom_fields();
  92. // Shift all fields below the body field one down and put detailed question field where the body field was.
  93. _faq_shift_fields_down();
  94. }
  95. /**
  96. * Implements hook_uninstall().
  97. *
  98. * Remove the variables, nodes and schema corresponding to the FAQ module.
  99. */
  100. function faq_uninstall() {
  101. // Delete the variables we created.
  102. // General settings.
  103. variable_del('faq_title');
  104. variable_del('faq_description');
  105. variable_del('faq_path');
  106. // Questions page.
  107. variable_del('faq_display');
  108. variable_del('faq_question_listing');
  109. variable_del('faq_qa_mark');
  110. variable_del('faq_question_label');
  111. variable_del('faq_answer_label');
  112. variable_del('faq_question_length');
  113. variable_del('faq_hide_qa_accordion');
  114. variable_del('faq_show_expand_all');
  115. variable_del('faq_use_teaser');
  116. variable_del('faq_show_node_links');
  117. variable_del('faq_back_to_top');
  118. variable_del('faq_disable_node_links');
  119. variable_del('faq_default_sorting');
  120. variable_del('faq_question_long_form');
  121. // Categories page.
  122. variable_del('faq_use_categories');
  123. variable_del('faq_category_display');
  124. variable_del('faq_category_listing');
  125. variable_del('faq_category_hide_qa_accordion');
  126. variable_del('faq_count');
  127. variable_del('faq_answer_category_name');
  128. variable_del('faq_group_questions_top');
  129. variable_del('faq_hide_child_terms');
  130. variable_del('faq_show_term_page_children');
  131. variable_del('faq_omit_vocabulary');
  132. variable_del('faq_enable_term_links');
  133. // Block settings.
  134. variable_del('faq_block_recent_faq_count');
  135. variable_del('faq_block_random_faq_count');
  136. // Custom breadcrumbs control.
  137. variable_del('faq_custom_breadcrumbs');
  138. // Deprecated.
  139. variable_del('faq_more_link');
  140. // Remove content type and the fields created.
  141. $faq_type = 'faq';
  142. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  143. $result = db_query($sql, array(':type' => $faq_type));
  144. $nodeids = array();
  145. foreach ($result as $row) {
  146. $nodeids[] = $row->nid;
  147. }
  148. node_delete_multiple($nodeids);
  149. _faq_delete_custom_fields();
  150. node_type_delete($faq_type);
  151. field_purge_batch(500);
  152. // Remove content type and the fields created.
  153. $faq_type = 'faq';
  154. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  155. $result = db_query($sql, array(':type' => $faq_type));
  156. $nodeids = array();
  157. foreach ($result as $row) {
  158. $nodeids[] = $row->nid;
  159. }
  160. node_delete_multiple($nodeids);
  161. _faq_delete_custom_fields();
  162. node_type_delete($faq_type);
  163. field_purge_batch(500);
  164. // Clear the cache tables.
  165. cache_clear_all('*', 'cache', TRUE);
  166. cache_clear_all('*', 'cache_filter', TRUE);
  167. cache_clear_all('*', 'cache_menu', TRUE);
  168. cache_clear_all('*', 'cache_page', TRUE);
  169. }
  170. /**
  171. * Create 'faq_weights' table in order to upgrade from older installations.
  172. */
  173. function faq_update_1() {
  174. $schema['faq_weights'] = array(
  175. 'description' => 'A table containing the weight of each faq node by category.',
  176. 'fields' => array(
  177. 'tid' => array(
  178. 'type' => 'int',
  179. 'unsigned' => TRUE,
  180. 'not null' => TRUE,
  181. 'default' => 0,
  182. ),
  183. 'nid' => array(
  184. 'type' => 'int',
  185. 'unsigned' => TRUE,
  186. 'not null' => TRUE,
  187. 'default' => 0,
  188. ),
  189. 'weight' => array(
  190. 'type' => 'int',
  191. 'size' => 'tiny',
  192. 'not null' => TRUE,
  193. 'default' => 0,
  194. ),
  195. ),
  196. 'primary key' => array('nid', 'tid'),
  197. );
  198. $ret = array();
  199. db_create_table('faq_weights', $schema['faq_weights']);
  200. return t('FAQ weighting table created.');
  201. }
  202. /**
  203. * Create 'faq_questions' table in order to upgrade from older installations.
  204. */
  205. function faq_update_2() {
  206. $schema['faq_questions'] = array(
  207. 'description' => 'A table containing the long question text of each faq node revision.',
  208. 'fields' => array(
  209. 'nid' => array(
  210. 'type' => 'int',
  211. 'unsigned' => TRUE,
  212. 'not null' => TRUE,
  213. 'default' => 0,
  214. 'description' => 'The primary identifier for a node.',
  215. ),
  216. 'vid' => array(
  217. 'type' => 'int',
  218. 'unsigned' => TRUE,
  219. 'not null' => TRUE,
  220. 'default' => 0,
  221. 'description' => 'The primary identifier for a node revision.',
  222. ),
  223. 'question' => array(
  224. 'type' => 'text',
  225. 'size' => 'normal',
  226. 'not null' => TRUE,
  227. 'description' => 'The faq long question text.',
  228. ),
  229. ),
  230. 'primary key' => array('nid', 'vid'),
  231. );
  232. db_create_table('faq_questions', $schema['faq_questions']);
  233. // Pre-populate the questions table from the existing nodes.
  234. $select = db_select('node', 'n');
  235. $select->innerJoin('node_revisions', 'r', 'n.nid = %alias.nid');
  236. $select
  237. ->fields('r', array('nid', 'vid', 'title'))
  238. ->condition('n.type', 'faq');
  239. db_insert('faq_questions')
  240. ->fields(array('nid', 'vid', 'question'))
  241. ->from($select)
  242. ->execute();
  243. return t('FAQ Questions table created.');
  244. }
  245. /**
  246. * Add the 'detailed_question' column to the 'faq_questions' table.
  247. */
  248. function faq_update_6003() {
  249. $ret = array();
  250. db_add_field('faq_questions', 'detailed_question',
  251. array(
  252. 'type' => 'text',
  253. 'size' => 'normal',
  254. 'not null' => TRUE,
  255. )
  256. );
  257. db_update('faq_questions')
  258. ->expression('detailed_question', 'question')
  259. ->execute();
  260. return t('Detailed question column added. Existing nodes have been given the same detailed question as current question.');
  261. }
  262. /**
  263. * Make'detailed_question' column nullable.
  264. */
  265. function faq_update_7000() {
  266. db_change_field('faq_questions', 'detailed_question', 'detailed_question',
  267. array(
  268. 'type' => 'text',
  269. 'size' => 'normal',
  270. 'not null' => FALSE,
  271. )
  272. );
  273. return t('Detailed question field can now be null.');
  274. }
  275. /**
  276. * Delete obsolete variables.
  277. */
  278. function faq_update_7001() {
  279. variable_del('faq_block_recent_faq_count');
  280. variable_del('faq_block_random_faq_count');
  281. variable_del('faq_enable_term_links');
  282. return t('Deleted obsolete variables.');
  283. }
  284. /**
  285. * Convert old-style detailed questions to new fields.
  286. */
  287. function faq_update_7002(&$sandbox) {
  288. // Number of nodes to update each pass.
  289. define('BATCH_SIZE_7002', '5');
  290. // Do this the first time.
  291. if (!isset($sandbox['progress'])) {
  292. // Initialize sandbox structure for multi-pass update.
  293. $sandbox['progress'] = 0;
  294. $sandbox['current_idx'] = 0;
  295. // Get faq nodes and run the query as user 1.
  296. $query = new EntityFieldQuery();
  297. $query->entityCondition('entity_type', 'node')
  298. ->entityCondition('bundle', 'faq')
  299. ->addMetaData('account', user_load(1));
  300. $result = $query->execute();
  301. if (isset($result['node'])) {
  302. $sandbox['faq_items_nids'] = array_keys($result['node']);
  303. $sandbox['max'] = count($sandbox['faq_items_nids']);
  304. }
  305. else {
  306. $sandbox['faq_items_nids'] = array();
  307. $sandbox['max'] = 0;
  308. }
  309. // Add the detailed question field.
  310. _faq_add_custom_fields();
  311. // Adjust the weight of the field so that it is above the answer (body).
  312. _faq_shift_fields_down();
  313. }
  314. $count = 0;
  315. // Convert old-style detailed questions to new full field.
  316. while (($nid = $sandbox['faq_items_nids'][$sandbox['current_idx']]) && $count < BATCH_SIZE_7002) {
  317. // Load the full node to be updated.
  318. $node = node_load($nid);
  319. // Load the detailed question.
  320. $dq = isset($node->detailed_question) ? $node->detailed_question : '';
  321. if ($dq == '') {
  322. $select = db_select('faq_questions', 'f');
  323. $dq = $select->condition('f.nid', $node->nid)->fields('f', array('detailed_question'))->execute()->fetchField();
  324. }
  325. // Get the default text filter format from DB as this might be integer if upgraded site or tekststring if new D7 site.
  326. // Default filter format: Filtered HTML.
  327. $filter_formats = filter_formats();
  328. $filter_formats_keys = array_keys($filter_formats);
  329. $filter_format = reset($filter_formats_keys);
  330. // Get the language(s) from the body, making sure we have the same set for detailed question too.
  331. $langs = array_keys($node->body);
  332. // Add proper taxonomy fields.
  333. $txonselect = db_select('taxonomy_index', 't');
  334. $taxres = $txonselect->fields('t', array('tid'))->condition('t.nid', $node->nid)->execute();
  335. foreach ($taxres as $taxon) {
  336. $term = taxonomy_term_load($taxon->tid);
  337. $vocab = taxonomy_vocabulary_load($term->vid);
  338. foreach ($langs as $language) {
  339. // Find out if there is a field added with the vocabulary of this term.
  340. if (isset($node->{$vocab->module . "_" . $vocab->machine_name})) {
  341. $node->{$vocab->module . "_" . $vocab->machine_name}[$language][$term->tid] = (array) $term;
  342. }
  343. }
  344. }
  345. // Add detailed question field for all languages.
  346. foreach ($langs as $language) {
  347. $node->field_detailed_question[$language][0]['value'] = $dq;
  348. $node->field_detailed_question[$language][0]['format'] = $filter_format;
  349. $node->field_detailed_question[$language][0]['safe_value'] = check_markup($dq, $filter_format, $language);
  350. }
  351. // Save resulting node.
  352. node_save($node);
  353. // Should not be more than BATCH_SIZE_7002.
  354. $count++;
  355. // Progress counter.
  356. $sandbox['progress']++;
  357. // Node array index pointer.
  358. $sandbox['current_idx']++;
  359. }
  360. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  361. return t('Custom field added, @count questions converted into fields.', array('@count' => $sandbox['max'] + 1));
  362. }
  363. /**
  364. * Convert faq page description to array.
  365. */
  366. function faq_update_7003(&$sandbox) {
  367. $description = variable_get('faq_description', '');
  368. $format = variable_get('faq_description_format', filter_fallback_format());
  369. $faq_description = array(
  370. 'value' => $description,
  371. 'format' => $format,
  372. );
  373. variable_set('faq_description', $faq_description);
  374. variable_del('faq_description_format');
  375. return t('Converted faq page description to array.');
  376. }
  377. /**
  378. * Code examples modified.
  379. *
  380. * @see http://www.sitepoint.com/creating-a-new-drupal-node-type/
  381. * @see http://www.thecarneyeffect.co.uk/creating-custom-content-type-adding-fields-programmatically-drupal-7
  382. */
  383. function _faq_add_custom_fields() {
  384. foreach (_faq_installed_fields() as $field) {
  385. field_create_field($field);
  386. }
  387. foreach (_faq_installed_instances() as $fieldinstance) {
  388. $fieldinstance['entity_type'] = 'node';
  389. $fieldinstance['bundle'] = 'faq';
  390. field_create_instance($fieldinstance);
  391. }
  392. }
  393. /**
  394. * Return the detailed question field definition.
  395. */
  396. function _faq_installed_fields() {
  397. $t = get_t();
  398. return array(
  399. 'detailed_question' => array(
  400. 'translatable' => '0',
  401. 'entity_types' => array(),
  402. 'settings' => array(),
  403. 'storage' => array(
  404. 'type' => 'field_sql_storage',
  405. 'settings' => array(),
  406. 'module' => 'field_sql_storage',
  407. 'active' => '1',
  408. 'details' => array(
  409. 'sql' => array(
  410. 'FIELD_LOAD_CURRENT' => array(
  411. 'field_data_field_detailed_question' => array(
  412. 'value' => 'field_detailed_question_value',
  413. 'format' => 'field_detailed_question_format',
  414. ),
  415. ),
  416. 'FIELD_LOAD_REVISION' => array(
  417. 'field_revision_field_detailed_question' => array(
  418. 'value' => 'field_detailed_question_value',
  419. 'format' => 'field_detailed_question_format',
  420. ),
  421. ),
  422. ),
  423. ),
  424. ),
  425. 'foreign keys' => array(
  426. 'format' => array(
  427. 'table' => 'filter_format',
  428. 'columns' => array(
  429. 'format' => 'format',
  430. ),
  431. ),
  432. ),
  433. 'indexes' => array(
  434. 'format' => array(
  435. 'format',
  436. ),
  437. ),
  438. 'field_name' => 'field_detailed_question',
  439. 'type' => 'text_long',
  440. 'module' => 'text',
  441. 'active' => '1',
  442. 'locked' => '0',
  443. 'cardinality' => '1',
  444. 'deleted' => '0',
  445. 'columns' => array(
  446. 'value' => array(
  447. 'type' => 'text',
  448. 'size' => 'big',
  449. 'not null' => FALSE,
  450. ),
  451. 'format' => array(
  452. 'type' => 'varchar',
  453. 'length' => 255,
  454. 'not null' => FALSE,
  455. ),
  456. ),
  457. 'bundles' => array(
  458. 'node' => array(
  459. 'page',
  460. ),
  461. ),
  462. ),
  463. );
  464. }
  465. /**
  466. * Returns the detailed question field instance.
  467. */
  468. function _faq_installed_instances() {
  469. $t = get_t();
  470. // Position in the question and answer page. Body (Answer) is 0 and Question (Title) is -4.
  471. return array(
  472. 'detailed_question' => array(
  473. 'label' => 'Detailed question',
  474. 'widget' => array(
  475. 'weight' => '-3',
  476. 'type' => 'text_textarea',
  477. 'module' => 'text',
  478. 'active' => 1,
  479. 'settings' => array(
  480. 'rows' => '5',
  481. ),
  482. ),
  483. 'settings' => array(
  484. 'text_processing' => '1',
  485. 'user_register_form' => FALSE,
  486. ),
  487. 'display' => array(
  488. 'default' => array(
  489. 'label' => 'hidden',
  490. 'type' => 'text_default',
  491. 'weight' => '-3',
  492. 'settings' => array(),
  493. 'module' => 'text',
  494. ),
  495. 'teaser' => array(
  496. 'label' => 'hidden',
  497. 'type' => 'text_default',
  498. 'weight' => '-3',
  499. 'settings' => array(),
  500. 'module' => 'text',
  501. ),
  502. ),
  503. 'required' => 0,
  504. 'description' => 'Enter the detailed question text',
  505. 'default_value' => NULL,
  506. 'field_name' => 'field_detailed_question',
  507. 'entity_type' => 'node',
  508. 'bundle' => 'page',
  509. 'deleted' => '0',
  510. ),
  511. );
  512. }
  513. /**
  514. * Cleanup custom fields on uninstall.
  515. */
  516. function _faq_delete_custom_fields() {
  517. foreach (array_keys(_faq_installed_fields()) as $field) {
  518. field_delete_field($field);
  519. }
  520. $instances = field_info_instances('node', 'faq');
  521. foreach ($instances as $instance_name => $fieldinstance) {
  522. field_delete_instance($fieldinstance);
  523. }
  524. }
  525. /**
  526. * Shift fields down.
  527. */
  528. function _faq_shift_fields_down() {
  529. // Adjust the weight of the field so that it is above the answer (body).
  530. $instance = field_read_instance('node', 'field_detailed_question', 'faq');
  531. // Get all bundle instances.
  532. $instances = field_info_instances('node', 'faq');
  533. $body_widget_weight = $instances['body']['widget']['weight'];
  534. $body_default_weight = $instances['body']['display']['default']['weight'];
  535. $body_teaser_weight = $instances['body']['display']['teaser']['weight'];
  536. // Move all of them one down so that.
  537. foreach ($instances as $field => $settings) {
  538. if ($settings['widget']['weight'] >= $body_widget_weight) {
  539. $settings['widget']['weight']++;
  540. }
  541. if ($settings['display']['default']['weight'] >= $body_default_weight) {
  542. $settings['display']['default']['weight']++;
  543. }
  544. if ($settings['display']['teaser']['weight'] >= $body_teaser_weight) {
  545. $settings['display']['teaser']['weight']++;
  546. }
  547. field_update_instance($settings);
  548. }
  549. $instance['widget']['weight'] = $body_widget_weight;
  550. $instance['display']['default']['weight'] = $body_default_weight;
  551. $instance['display']['teaser']['weight'] = $body_teaser_weight;
  552. field_update_instance($instance);
  553. }