nodeblock.install 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Implements hook_schema().
  4. */
  5. function nodeblock_schema() {
  6. $schema['nodeblock'] = array(
  7. 'description' => 'The base table for nodeblock.module.',
  8. 'fields' => array(
  9. 'nid' => array(
  10. 'description' => 'The primary identifier for a node.',
  11. 'type' => 'int',
  12. 'unsigned' => TRUE,
  13. 'not null' => TRUE,
  14. ),
  15. 'enabled' => array(
  16. 'description' => 'A flag indicating whether a block should be created.',
  17. 'type' => 'int',
  18. 'size' => 'tiny',
  19. 'not null' => TRUE,
  20. 'default' => 1,
  21. ),
  22. 'machine_name' => array(
  23. 'description' => 'The machine name for a block.',
  24. 'type' => 'varchar',
  25. 'length' => 32,
  26. 'not null' => TRUE,
  27. ),
  28. 'block_title' => array(
  29. 'description' => 'The block title for a block.',
  30. 'type' => 'varchar',
  31. 'length' => 255,
  32. 'not null' => TRUE,
  33. 'default' => '',
  34. ),
  35. 'view_mode' => array(
  36. 'description' => 'The view mode used to render the node.',
  37. 'type' => 'varchar',
  38. 'length' => 255,
  39. 'not null' => TRUE,
  40. ),
  41. 'node_link' => array(
  42. 'description' => 'Whether the node links should be shown.',
  43. 'type' => 'varchar',
  44. 'length' => 255,
  45. 'not null' => TRUE,
  46. 'default' => '0',
  47. ),
  48. 'comment_link' => array(
  49. 'description' => 'Whether the comment links should be shown.',
  50. 'type' => 'varchar',
  51. 'length' => 255,
  52. 'not null' => TRUE,
  53. 'default' => '0',
  54. ),
  55. 'translation_fallback' => array(
  56. 'description' => 'A flag indicating whether the translation fallback is enabled.',
  57. 'type' => 'int',
  58. 'size' => 'tiny',
  59. 'not null' => TRUE,
  60. 'default' => 0,
  61. ),
  62. ),
  63. 'primary key' => array('nid'),
  64. 'unique keys' => array(
  65. 'machine_name' => array('machine_name'),
  66. ),
  67. );
  68. return $schema;
  69. }
  70. /**
  71. * Placeholder for potential Drupal 6 upgrade path
  72. */
  73. function nodeblock_update_7100() {
  74. }
  75. /**
  76. * Remove variable settings for nodes where blocks are not enabled.
  77. */
  78. function nodeblock_update_7101() {
  79. $variables = variable_initialize();
  80. foreach($variables as $name => $value) {
  81. if(strpos($name, 'nodeblock') !== false) {
  82. if(strpos($name, 'nodeblock_block_') !== false) {
  83. $blocks[str_replace('nodeblock_block_', '', $name)] = $name;
  84. }
  85. else if(strpos($name, 'nodeblock_comment_link_') === false && strpos($name, 'nodeblock_node_link_') === false && strpos($name, 'nodeblock_view_mode_') === false) {
  86. if((bool)$value) {
  87. $content_types[] = str_replace('nodeblock_', '', $name);
  88. }
  89. }
  90. }
  91. }
  92. if(!empty($content_types) && !empty($blocks)) {
  93. $query = db_select('node', 'n')
  94. ->fields('n', array ('nid'))
  95. ->condition('n.type', $content_types);
  96. $result = $query->execute()->fetchAll();
  97. if(!empty($result)) {
  98. foreach($result as $value) {
  99. $nids[$value->nid] = $value->nid;
  100. }
  101. if(!empty($nids)) {
  102. $blocks = array_diff_key($blocks, $nids);
  103. if(!empty($blocks)) {
  104. foreach($blocks as $name) {
  105. variable_del($name);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Adding a new nodeblock table and create entries for all existing nodeblocks.
  114. */
  115. function nodeblock_update_7102() {
  116. module_load_include('module', 'nodeblock');
  117. if (!db_table_exists('nodeblock')) {
  118. $schema = nodeblock_schema();
  119. db_create_table('nodeblock', $schema['nodeblock']);
  120. $types = node_type_get_types();
  121. $enabled_types = array();
  122. foreach ($types as $type_name => $type) {
  123. if (nodeblock_type_enabled($type_name)) {
  124. $enabled_types[] = $type_name;
  125. }
  126. }
  127. if (count($enabled_types)) {
  128. $results = db_select('node', 'n')
  129. ->fields('n', array('nid'))
  130. ->condition('type', $enabled_types)
  131. ->execute()
  132. ->fetchCol();
  133. $defaults = array(
  134. 'comment_link' => 'node_block_default',
  135. 'node_link' => 'node_block_default',
  136. 'view_mode' => 'node_block_default');
  137. foreach ($results as $nid) {
  138. $settings = variable_get('nodeblock_block_' . $nid, $defaults);
  139. $settings = array_merge($defaults, $settings);
  140. if(isset($settings['teaser']) && $settings['teaser'] == 1) {
  141. $settings['view_mode'] == 'teaser';
  142. }
  143. if(isset($settings['links']) && $settings['links'] == 1) {
  144. $settings['node_link'] = 1;
  145. $settings['comment_link'] = 1;
  146. }
  147. db_insert('nodeblock')
  148. ->fields(array('nid', 'machine_name', 'view_mode', 'node_link', 'comment_link', 'translation_fallback'))
  149. ->values(array($nid, $nid, $settings['view_mode'], $settings['node_link'], $settings['comment_link'], variable_get('nodeblock_translation_fallback_' . $nid, 0)))
  150. ->execute();
  151. variable_del('nodeblock_translation_fallback_' . $nid);
  152. variable_del('nodeblock_block_' . $nid);
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * Add new block title column in db.
  159. */
  160. function nodeblock_update_7103() {
  161. module_load_include('module', 'nodeblock');
  162. if (!db_field_exists('nodeblock', 'block_title')) {
  163. db_add_field('nodeblock', 'block_title', array(
  164. 'description' => 'The block title for a block.',
  165. 'type' => 'varchar',
  166. 'length' => 255,
  167. 'not null' => TRUE,
  168. 'default' => '',
  169. ));
  170. }
  171. $types = node_type_get_types();
  172. $enabled_types = array();
  173. foreach ($types as $type_name => $type) {
  174. if (nodeblock_type_enabled($type_name)) {
  175. $enabled_types[] = $type_name;
  176. }
  177. }
  178. if (count($enabled_types)) {
  179. $results = db_select('node', 'n')
  180. ->fields('n', array('nid', 'title'))
  181. ->condition('type', $enabled_types)
  182. ->execute();
  183. foreach ($results as $node) {
  184. db_update('nodeblock')
  185. ->fields(array('block_title' => $node->title))
  186. ->condition('nid', $node->nid)
  187. ->execute();
  188. }
  189. }
  190. }
  191. /**
  192. * `Show the Block title field in the Node Block fieldset` default value, change schema weight.
  193. */
  194. function nodeblock_update_7104() {
  195. $types = node_type_get_types();
  196. foreach ($types as $type_name => $type) {
  197. if ($var = variable_get('nodeblock_node_overrides_' . $type_name)) {
  198. $var[] = 'block_title_in_fieldset';
  199. variable_set('nodeblock_node_overrides_' . $type_name, $var);
  200. }
  201. }
  202. }
  203. /**
  204. * Implements hook_uninstall().
  205. */
  206. function nodeblock_uninstall() {
  207. $types = node_type_get_types();
  208. foreach ($types as $type_name => $type) {
  209. if (variable_get('nodeblock_' . $type_name, 0)) {
  210. variable_del('nodeblock_' . $type_name);
  211. variable_del('nodeblock_comment_link_' . $type_name);
  212. variable_del('nodeblock_node_link_' . $type_name);
  213. variable_del('nodeblock_view_mode_' . $type_name);
  214. variable_del('nodeblock_node_overrides_' . $type_name);
  215. variable_del('nodeblock_node_override_block_available_' . $type_name);
  216. variable_del('nodeblock_node_override_block_title_field_' . $type_name);
  217. }
  218. }
  219. db_delete('block')
  220. ->condition('module', 'nodeblock')
  221. ->execute();
  222. db_delete('block_role')
  223. ->condition('module', 'nodeblock')
  224. ->execute();
  225. variable_del('nodeblock_dangerous_force_block_caching');
  226. variable_del('nodeblock_table_exists');
  227. }