faq.install 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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_description_format');
  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. // Categories page.
  121. variable_del('faq_use_categories');
  122. variable_del('faq_category_display');
  123. variable_del('faq_category_listing');
  124. variable_del('faq_category_hide_qa_accordion');
  125. variable_del('faq_count');
  126. variable_del('faq_answer_category_name');
  127. variable_del('faq_group_questions_top');
  128. variable_del('faq_hide_child_terms');
  129. variable_del('faq_show_term_page_children');
  130. variable_del('faq_omit_vocabulary');
  131. variable_del('faq_enable_term_links');
  132. // Block settings.
  133. variable_del('faq_block_recent_faq_count');
  134. variable_del('faq_block_random_faq_count');
  135. // Custom breadcrumbs control.
  136. variable_del('faq_custom_breadcrumbs');
  137. // Deprecated.
  138. variable_del('faq_more_link');
  139. // Remove content type and the fields created.
  140. $faq_type = 'faq';
  141. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  142. $result = db_query($sql, array(':type' => $faq_type));
  143. $nodeids = array();
  144. foreach ($result as $row) {
  145. $nodeids[] = $row->nid;
  146. }
  147. node_delete_multiple($nodeids);
  148. _faq_delete_custom_fields();
  149. node_type_delete($faq_type);
  150. field_purge_batch(500);
  151. // Remove content type and the fields created.
  152. $faq_type = 'faq';
  153. $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  154. $result = db_query($sql, array(':type' => $faq_type));
  155. $nodeids = array();
  156. foreach ($result as $row) {
  157. $nodeids[] = $row->nid;
  158. }
  159. node_delete_multiple($nodeids);
  160. _faq_delete_custom_fields();
  161. node_type_delete($faq_type);
  162. field_purge_batch(500);
  163. // Clear the cache tables.
  164. cache_clear_all('*', 'cache', TRUE);
  165. cache_clear_all('*', 'cache_filter', TRUE);
  166. cache_clear_all('*', 'cache_menu', TRUE);
  167. cache_clear_all('*', 'cache_page', TRUE);
  168. }
  169. /**
  170. * Create 'faq_weights' table in order to upgrade from older installations.
  171. */
  172. function faq_update_1() {
  173. $schema['faq_weights'] = array(
  174. 'description' => 'A table containing the weight of each faq node by category.',
  175. 'fields' => array(
  176. 'tid' => array(
  177. 'type' => 'int',
  178. 'unsigned' => TRUE,
  179. 'not null' => TRUE,
  180. 'default' => 0,
  181. ),
  182. 'nid' => array(
  183. 'type' => 'int',
  184. 'unsigned' => TRUE,
  185. 'not null' => TRUE,
  186. 'default' => 0,
  187. ),
  188. 'weight' => array(
  189. 'type' => 'int',
  190. 'size' => 'tiny',
  191. 'not null' => TRUE,
  192. 'default' => 0,
  193. ),
  194. ),
  195. 'primary key' => array('nid', 'tid'),
  196. );
  197. $ret = array();
  198. db_create_table('faq_weights', $schema['faq_weights']);
  199. return t('FAQ weighting table created.');
  200. }
  201. /**
  202. * Create 'faq_questions' table in order to upgrade from older installations.
  203. */
  204. function faq_update_2() {
  205. $schema['faq_questions'] = array(
  206. 'description' => 'A table containing the long question text of each faq node revision.',
  207. 'fields' => array(
  208. 'nid' => array(
  209. 'type' => 'int',
  210. 'unsigned' => TRUE,
  211. 'not null' => TRUE,
  212. 'default' => 0,
  213. 'description' => 'The primary identifier for a node.',
  214. ),
  215. 'vid' => array(
  216. 'type' => 'int',
  217. 'unsigned' => TRUE,
  218. 'not null' => TRUE,
  219. 'default' => 0,
  220. 'description' => 'The primary identifier for a node revision.',
  221. ),
  222. 'question' => array(
  223. 'type' => 'text',
  224. 'size' => 'normal',
  225. 'not null' => TRUE,
  226. 'description' => 'The faq long question text.',
  227. ),
  228. ),
  229. 'primary key' => array('nid', 'vid'),
  230. );
  231. db_create_table('faq_questions', $schema['faq_questions']);
  232. // Pre-populate the questions table from the existing nodes.
  233. $select = db_select('node', 'n');
  234. $select->innerJoin('node_revisions', 'r', 'n.nid = %alias.nid');
  235. $select
  236. ->fields('r', array('nid', 'vid', 'title'))
  237. ->condition('n.type', 'faq');
  238. db_insert('faq_questions')
  239. ->fields(array('nid', 'vid', 'question'))
  240. ->from($select)
  241. ->execute();
  242. return t('FAQ Questions table created.');
  243. }
  244. /**
  245. * Add the 'detailed_question' column to the 'faq_questions' table.
  246. */
  247. function faq_update_6003() {
  248. $ret = array();
  249. db_add_field('faq_questions', 'detailed_question',
  250. array(
  251. 'type' => 'text',
  252. 'size' => 'normal',
  253. 'not null' => TRUE,
  254. )
  255. );
  256. db_update('faq_questions')
  257. ->expression('detailed_question', 'question')
  258. ->execute();
  259. return t('Detailed question column added. Existing nodes have been given the same detailed question as current question.');
  260. }
  261. /**
  262. * Make'detailed_question' column nullable.
  263. */
  264. function faq_update_7000() {
  265. db_change_field('faq_questions', 'detailed_question', 'detailed_question',
  266. array(
  267. 'type' => 'text',
  268. 'size' => 'normal',
  269. 'not null' => FALSE,
  270. )
  271. );
  272. return t('Detailed question field can now be null.');
  273. }
  274. /**
  275. * Delete obsolete variables.
  276. */
  277. function faq_update_7001() {
  278. variable_del('faq_block_recent_faq_count');
  279. variable_del('faq_block_random_faq_count');
  280. variable_del('faq_enable_term_links');
  281. return t('Deleted obsolete variables.');
  282. }
  283. /**
  284. * Convert old-style detailed questions to new fields.
  285. */
  286. function faq_update_7002(&$sandbox) {
  287. // Number of nodes to update each pass.
  288. define('BATCH_SIZE_7002', '5');
  289. // Do this the first time.
  290. if (!isset($sandbox['progress'])) {
  291. // Initialize sandbox structure for multi-pass update.
  292. $sandbox['progress'] = 0;
  293. $sandbox['current_idx'] = 0;
  294. // Get faq nodes and run the query as user 1.
  295. $query = new EntityFieldQuery();
  296. $query->entityCondition('entity_type', 'node')
  297. ->entityCondition('bundle', 'faq')
  298. ->addMetaData('account', user_load(1));
  299. $result = $query->execute();
  300. if (isset($result['node'])) {
  301. $sandbox['faq_items_nids'] = array_keys($result['node']);
  302. $sandbox['max'] = count($sandbox['faq_items_nids']);
  303. }
  304. else {
  305. $sandbox['faq_items_nids'] = array();
  306. $sandbox['max'] = 0;
  307. }
  308. // Add the detailed question field.
  309. _faq_add_custom_fields();
  310. // Adjust the weight of the field so that it is above the answer (body).
  311. _faq_shift_fields_down();
  312. }
  313. $count = 0;
  314. // Convert old-style detailed questions to new full field.
  315. while (($nid = $sandbox['faq_items_nids'][$sandbox['current_idx']]) && $count < BATCH_SIZE_7002) {
  316. // Load the full node to be updated.
  317. $node = node_load($nid);
  318. // Load the detailed question.
  319. $dq = isset($node->detailed_question) ? $node->detailed_question : '';
  320. if ($dq == '') {
  321. $select = db_select('faq_questions', 'f');
  322. $dq = $select->condition('f.nid', $node->nid)->fields('f', array('detailed_question'))->execute()->fetchField();
  323. }
  324. // Get the default text filter format from DB as this might be integer if upgraded site or tekststring if new D7 site.
  325. // Default filter format: Filtered HTML.
  326. $filter_formats = filter_formats();
  327. $filter_formats_keys = array_keys($filter_formats);
  328. $filter_format = reset($filter_formats_keys);
  329. // Get the language(s) from the body, making sure we have the same set for detailed question too.
  330. $langs = array_keys($node->body);
  331. // Add proper taxonomy fields.
  332. $txonselect = db_select('taxonomy_index', 't');
  333. $taxres = $txonselect->fields('t', array('tid'))->condition('t.nid', $node->nid)->execute();
  334. foreach ($taxres as $taxon) {
  335. $term = taxonomy_term_load($taxon->tid);
  336. $vocab = taxonomy_vocabulary_load($term->vid);
  337. foreach ($langs as $language) {
  338. // Find out if there is a field added with the vocabulary of this term.
  339. if (isset($node->{$vocab->module . "_" . $vocab->machine_name})) {
  340. $node->{$vocab->module . "_" . $vocab->machine_name}[$language][$term->tid] = (array) $term;
  341. }
  342. }
  343. }
  344. // Add detailed question field for all languages.
  345. foreach ($langs as $language) {
  346. $node->field_detailed_question[$language][0]['value'] = $dq;
  347. $node->field_detailed_question[$language][0]['format'] = $filter_format;
  348. $node->field_detailed_question[$language][0]['safe_value'] = check_markup($dq, $filter_format, $language);
  349. }
  350. // Save resulting node.
  351. node_save($node);
  352. // Should not be more than BATCH_SIZE_7002.
  353. $count++;
  354. // Progress counter.
  355. $sandbox['progress']++;
  356. // Node array index pointer.
  357. $sandbox['current_idx']++;
  358. }
  359. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  360. return t('Custom field added, @count questions converted into fields.', array('@count' => $sandbox['max'] + 1));
  361. }
  362. /**
  363. * Code examples modified.
  364. *
  365. * @see http://www.sitepoint.com/creating-a-new-drupal-node-type/
  366. * @see http://www.thecarneyeffect.co.uk/creating-custom-content-type-adding-fields-programmatically-drupal-7
  367. */
  368. function _faq_add_custom_fields() {
  369. foreach (_faq_installed_fields() as $field) {
  370. field_create_field($field);
  371. }
  372. foreach (_faq_installed_instances() as $fieldinstance) {
  373. $fieldinstance['entity_type'] = 'node';
  374. $fieldinstance['bundle'] = 'faq';
  375. field_create_instance($fieldinstance);
  376. }
  377. }
  378. /**
  379. * Return the detailed question field definition.
  380. */
  381. function _faq_installed_fields() {
  382. $t = get_t();
  383. return array(
  384. 'detailed_question' => array(
  385. 'translatable' => '0',
  386. 'entity_types' => array(),
  387. 'settings' => array(),
  388. 'storage' => array(
  389. 'type' => 'field_sql_storage',
  390. 'settings' => array(),
  391. 'module' => 'field_sql_storage',
  392. 'active' => '1',
  393. 'details' => array(
  394. 'sql' => array(
  395. 'FIELD_LOAD_CURRENT' => array(
  396. 'field_data_field_detailed_question' => array(
  397. 'value' => 'field_detailed_question_value',
  398. 'format' => 'field_detailed_question_format',
  399. ),
  400. ),
  401. 'FIELD_LOAD_REVISION' => array(
  402. 'field_revision_field_detailed_question' => array(
  403. 'value' => 'field_detailed_question_value',
  404. 'format' => 'field_detailed_question_format',
  405. ),
  406. ),
  407. ),
  408. ),
  409. ),
  410. 'foreign keys' => array(
  411. 'format' => array(
  412. 'table' => 'filter_format',
  413. 'columns' => array(
  414. 'format' => 'format',
  415. ),
  416. ),
  417. ),
  418. 'indexes' => array(
  419. 'format' => array(
  420. 'format',
  421. ),
  422. ),
  423. 'field_name' => 'field_detailed_question',
  424. 'type' => 'text_long',
  425. 'module' => 'text',
  426. 'active' => '1',
  427. 'locked' => '0',
  428. 'cardinality' => '1',
  429. 'deleted' => '0',
  430. 'columns' => array(
  431. 'value' => array(
  432. 'type' => 'text',
  433. 'size' => 'big',
  434. 'not null' => FALSE,
  435. ),
  436. 'format' => array(
  437. 'type' => 'varchar',
  438. 'length' => 255,
  439. 'not null' => FALSE,
  440. ),
  441. ),
  442. 'bundles' => array(
  443. 'node' => array(
  444. 'page',
  445. ),
  446. ),
  447. ),
  448. );
  449. }
  450. /**
  451. * Returns the detailed question field instance.
  452. */
  453. function _faq_installed_instances() {
  454. $t = get_t();
  455. // Position in the question and answer page. Body (Answer) is 0 and Question (Title) is -4.
  456. return array(
  457. 'detailed_question' => array(
  458. 'label' => 'Detailed question',
  459. 'widget' => array(
  460. 'weight' => '-3',
  461. 'type' => 'text_textarea',
  462. 'module' => 'text',
  463. 'active' => 1,
  464. 'settings' => array(
  465. 'rows' => '5',
  466. ),
  467. ),
  468. 'settings' => array(
  469. 'text_processing' => '1',
  470. 'user_register_form' => FALSE,
  471. ),
  472. 'display' => array(
  473. 'default' => array(
  474. 'label' => 'hidden',
  475. 'type' => 'text_default',
  476. 'weight' => '-3',
  477. 'settings' => array(),
  478. 'module' => 'text',
  479. ),
  480. 'teaser' => array(
  481. 'label' => 'hidden',
  482. 'type' => 'text_default',
  483. 'weight' => '-3',
  484. 'settings' => array(),
  485. 'module' => 'text',
  486. ),
  487. ),
  488. 'required' => 0,
  489. 'description' => 'Enter the detailed question text',
  490. 'default_value' => NULL,
  491. 'field_name' => 'field_detailed_question',
  492. 'entity_type' => 'node',
  493. 'bundle' => 'page',
  494. 'deleted' => '0',
  495. ),
  496. );
  497. }
  498. /**
  499. * Cleanup custom fields on uninstall.
  500. */
  501. function _faq_delete_custom_fields() {
  502. foreach (array_keys(_faq_installed_fields()) as $field) {
  503. field_delete_field($field);
  504. }
  505. $instances = field_info_instances('node', 'faq');
  506. foreach ($instances as $instance_name => $fieldinstance) {
  507. field_delete_instance($fieldinstance);
  508. }
  509. }
  510. /**
  511. * Shift fields down.
  512. */
  513. function _faq_shift_fields_down() {
  514. // Adjust the weight of the field so that it is above the answer (body).
  515. $instance = field_read_instance('node', 'field_detailed_question', 'faq');
  516. // Get all bundle instances.
  517. $instances = field_info_instances('node', 'faq');
  518. $body_widget_weight = $instances['body']['widget']['weight'];
  519. $body_default_weight = $instances['body']['display']['default']['weight'];
  520. $body_teaser_weight = $instances['body']['display']['teaser']['weight'];
  521. // Move all of them one down so that.
  522. foreach ($instances as $field => $settings) {
  523. if ($settings['widget']['weight'] >= $body_widget_weight) {
  524. $settings['widget']['weight']++;
  525. }
  526. if ($settings['display']['default']['weight'] >= $body_default_weight) {
  527. $settings['display']['default']['weight']++;
  528. }
  529. if ($settings['display']['teaser']['weight'] >= $body_teaser_weight) {
  530. $settings['display']['teaser']['weight']++;
  531. }
  532. field_update_instance($settings);
  533. }
  534. $instance['widget']['weight'] = $body_widget_weight;
  535. $instance['display']['default']['weight'] = $body_default_weight;
  536. $instance['display']['teaser']['weight'] = $body_teaser_weight;
  537. field_update_instance($instance);
  538. }