quicktabs.install 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the quicktabs module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function quicktabs_schema() {
  10. $schema['quicktabs'] = array(
  11. 'description' => 'The quicktabs table.',
  12. 'export' => array(
  13. 'key' => 'machine_name',
  14. 'identifier' => 'quicktabs',
  15. 'default hook' => 'quicktabs_default_quicktabs',
  16. 'api' => array(
  17. 'owner' => 'quicktabs',
  18. 'api' => 'quicktabs',
  19. 'minimum_version' => 1,
  20. 'current_version' => 1,
  21. ),
  22. 'export callback' => 'quicktabs_export',
  23. ),
  24. 'fields' => array(
  25. 'machine_name' => array(
  26. 'description' => 'The primary identifier for a qt block.',
  27. 'type' => 'varchar',
  28. 'length' => 255,
  29. 'not null' => TRUE,
  30. ),
  31. 'ajax' => array(
  32. 'description' => 'Whether this is an ajax views block.',
  33. 'type' => 'int',
  34. 'unsigned' => TRUE,
  35. 'not null' => TRUE,
  36. 'default' => 0,
  37. ),
  38. 'hide_empty_tabs' => array(
  39. 'description' => 'Whether this tabset hides empty tabs.',
  40. 'type' => 'int',
  41. 'size' => 'tiny',
  42. 'unsigned' => TRUE,
  43. 'not null' => TRUE,
  44. 'default' => 0,
  45. ),
  46. 'default_tab' => array(
  47. 'description' => 'Default tab.',
  48. 'type' => 'int',
  49. 'unsigned' => TRUE,
  50. 'not null' => TRUE,
  51. 'default' => 0,
  52. ),
  53. 'title' => array(
  54. 'description' => 'The title of this quicktabs block.',
  55. 'type' => 'varchar',
  56. 'length' => 255,
  57. 'not null' => TRUE,
  58. ),
  59. 'tabs' => array(
  60. 'description' => 'A serialized array of the contents of this qt block.',
  61. 'type' => 'text',
  62. 'size' => 'medium',
  63. 'not null' => TRUE,
  64. 'serialize' => TRUE,
  65. ),
  66. 'renderer' => array(
  67. 'description' => 'The rendering mechanism.',
  68. 'type' => 'varchar',
  69. 'length' => 255,
  70. 'not null' => TRUE,
  71. ),
  72. 'style' => array(
  73. 'description' => 'The tab style.',
  74. 'type' => 'varchar',
  75. 'length' => 255,
  76. 'not null' => TRUE,
  77. ),
  78. 'options' => array(
  79. 'description' => 'A serialized array of the options for this qt instance.',
  80. 'type' => 'text',
  81. 'size' => 'medium',
  82. 'not null' => FALSE,
  83. 'serialize' => TRUE,
  84. ),
  85. ),
  86. 'primary key' => array('machine_name'),
  87. );
  88. return $schema;
  89. }
  90. /**
  91. * Update to 7.x-3.x
  92. */
  93. function quicktabs_update_7300() {
  94. if (!db_field_exists('quicktabs', 'machine_name')) {
  95. // Pull all existing quicktabs, and then delete existing quicktabs. We will reinsert.
  96. $result = db_query("SELECT * FROM {quicktabs}");
  97. if (!db_query("DELETE FROM {quicktabs}")) {
  98. throw new DrupalUpdateException(t('Could not complete the update.'));
  99. }
  100. db_drop_field('quicktabs', 'qtid');
  101. $name_field = array(
  102. 'description' => 'The primary identifier for a qt block.',
  103. 'type' => 'varchar',
  104. 'length' => 255,
  105. 'not null' => TRUE,
  106. );
  107. db_add_field('quicktabs', 'machine_name', $name_field);
  108. db_add_primary_key('quicktabs', array('machine_name'));
  109. $output = $used = array();
  110. foreach ($result as $qt) {
  111. $row = (array)$qt;
  112. // Generate a machine-readable string
  113. $qt_name = strtolower(preg_replace('/[^a-zA-Z0-9_]+/', '_', $row['title']));
  114. $i = 0;
  115. while (in_array($i == 0 ? $qt_name : "{$qt_name}_{$i}", $used)) {
  116. $i++;
  117. }
  118. $row['machine_name'] = $used[] = $i == 0 ? $qt_name : "{$qt_name}_{$i}";
  119. unset($row['qtid']);
  120. unset($row['style']);
  121. $row['renderer'] = 'tabs';
  122. $placeholders = implode(', ', array_keys($row));
  123. $values = array();
  124. // Ugh - really?? Somebody tell me there's a better way to do this :-/
  125. foreach ($row as $name => $value) {
  126. $values[':' . $name] = $value;
  127. }
  128. $tokens = implode(', ', array_keys($values));
  129. db_query("INSERT INTO {quicktabs} ($placeholders) VALUES($tokens)", $values);
  130. $output[] = "Converted quicktab {$row['machine_name']}.";
  131. }
  132. }
  133. // Add the renderer field
  134. $renderer_field = array(
  135. 'description' => 'The rendering mechanism.',
  136. 'type' => 'varchar',
  137. 'length' => 255,
  138. 'not null' => TRUE,
  139. 'default' => 'quicktabs',
  140. );
  141. db_add_field('quicktabs', 'renderer', $renderer_field);
  142. $output[] = "Added the renderer field";
  143. return implode('<br />', $output);
  144. }
  145. /**
  146. * Add the options field which will hold renderer-specific options.
  147. */
  148. function quicktabs_update_7301() {
  149. $options_field = array(
  150. 'description' => 'A serialized array of the options for this qt instance.',
  151. 'type' => 'text',
  152. 'size' => 'medium',
  153. 'not null' => FALSE,
  154. 'serialize' => TRUE,
  155. );
  156. db_add_field('quicktabs', 'options', $options_field);
  157. return "Added the options field";
  158. }
  159. /**
  160. * Rebuild the registry because of changed method name.
  161. */
  162. function quicktabs_update_7302() {
  163. registry_rebuild();
  164. }