styles.install 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Styles module.
  5. */
  6. /**
  7. * Implement hook_install().
  8. */
  9. function styles_install() {
  10. return array();
  11. }
  12. /**
  13. * Implement hook_uninstall().
  14. */
  15. function styles_uninstall() {
  16. foreach (styles_variable_default() as $variable => $value) {
  17. styles_variable_del($variable);
  18. }
  19. return array(array('success' => TRUE, 'query' => "Deleted all variables in the Styles namespace."));
  20. }
  21. /**
  22. * Implement hook_schema().
  23. */
  24. function styles_schema() {
  25. $schema = array();
  26. $schema['cache_styles'] = drupal_get_schema_unprocessed('system', 'cache');
  27. $schema['cache_styles']['description'] = 'Cache table used to store information display manipulations that are in-progress.';
  28. $schema['styles'] = array(
  29. 'description' => 'Stores configuration options for styles.',
  30. 'fields' => array(
  31. 'sid' => array(
  32. 'description' => 'The primary identifier for a style.',
  33. 'type' => 'serial',
  34. 'unsigned' => TRUE,
  35. 'not null' => TRUE,
  36. ),
  37. 'field_type' => array(
  38. 'description' => 'The field type.',
  39. 'type' => 'varchar',
  40. 'length' => 128,
  41. 'not null' => TRUE,
  42. ),
  43. 'name' => array(
  44. 'description' => 'The style name.',
  45. 'type' => 'varchar',
  46. 'length' => 255,
  47. 'not null' => TRUE,
  48. ),
  49. 'description' => array(
  50. 'description' => 'The style description.',
  51. 'type' => 'text',
  52. 'not null' => TRUE,
  53. 'size' => 'big',
  54. ),
  55. ),
  56. 'primary key' => array('sid'),
  57. 'indexes' => array(
  58. 'field_type' => array('field_type'),
  59. 'name' => array('name'),
  60. ),
  61. );
  62. $schema['styles_presets'] = array(
  63. 'description' => 'Stores configuration options for styles presets.',
  64. 'fields' => array(
  65. 'pid' => array(
  66. 'description' => 'The primary identifier for a style preset.',
  67. 'type' => 'serial',
  68. 'unsigned' => TRUE,
  69. 'not null' => TRUE,
  70. ),
  71. 'field_type' => array(
  72. 'description' => 'The field type.',
  73. 'type' => 'varchar',
  74. 'length' => 128,
  75. 'not null' => TRUE,
  76. ),
  77. 'container_name' => array(
  78. 'description' => 'The container name',
  79. 'type' => 'varchar',
  80. 'length' => 128,
  81. 'not null' => TRUE,
  82. ),
  83. 'name' => array(
  84. 'description' => 'The preset name.',
  85. 'type' => 'varchar',
  86. 'length' => 255,
  87. 'not null' => TRUE,
  88. ),
  89. ),
  90. 'primary key' => array('pid'),
  91. 'indexes' => array(
  92. 'field_type' => array('field_type'),
  93. 'container_name' => array('container_name'),
  94. 'name' => array('name'),
  95. ),
  96. );
  97. $schema['styles_preset_instances'] = array(
  98. 'description' => 'Stores the settings for each container/style preset.',
  99. 'fields' => array(
  100. 'sid' => array(
  101. 'description' => 'The primary identifier for a style.',
  102. 'type' => 'int',
  103. 'unsigned' => TRUE,
  104. 'not null' => TRUE,
  105. 'default' => 0,
  106. ),
  107. 'pid' => array(
  108. 'description' => 'The primary identifier for a style preset.',
  109. 'type' => 'int',
  110. 'unsigned' => TRUE,
  111. 'not null' => TRUE,
  112. 'default' => 0,
  113. ),
  114. ),
  115. 'foreign keys' => array(
  116. 'sid' => array(
  117. 'table' => 'styles',
  118. 'columns' => array('sid' => 'sid'),
  119. ),
  120. 'pid' => array(
  121. 'table' => 'styles_presets',
  122. 'columns' => array('pid' => 'pid'),
  123. ),
  124. ),
  125. 'indexes' => array(
  126. 'sid' => array('sid'),
  127. 'pid' => array('pid'),
  128. ),
  129. );
  130. return $schema;
  131. }
  132. /**
  133. * Add new theme functions.
  134. */
  135. function styles_update_7200() {
  136. drupal_theme_rebuild();
  137. return array();
  138. }
  139. /**
  140. * Clear old cache table of any styles data.
  141. */
  142. function styles_update_7201() {
  143. cache_clear_all('styles_', 'cache', TRUE);
  144. return array();
  145. }
  146. /**
  147. * Add the field_type column to the {styles} table.
  148. */
  149. function styles_update_7202() {
  150. db_add_field('styles', 'field_type', array(
  151. 'description' => 'The field type.',
  152. 'type' => 'varchar',
  153. 'length' => 128,
  154. 'not null' => TRUE,
  155. ));
  156. db_add_index('styles', 'field_type', array('field_type'));
  157. }
  158. /**
  159. * Add the label & description columns to the {styles} table.
  160. */
  161. function styles_update_7204() {
  162. db_add_field('styles', 'label', array(
  163. 'description' => 'The human readable label for the style.',
  164. 'type' => 'varchar',
  165. 'length' => 255,
  166. 'not null' => TRUE,
  167. 'default' => '',
  168. ));
  169. db_add_field('styles', 'description', array(
  170. 'description' => 'The style description.',
  171. 'type' => 'varchar',
  172. 'length' => 255,
  173. 'not null' => TRUE,
  174. 'default' => '',
  175. ));
  176. db_add_index('styles', 'label', array('label'));
  177. }
  178. /**
  179. * Drop the label column from the {styles} table; alter the description column.
  180. */
  181. function styles_update_7206() {
  182. db_drop_field('styles', 'label');
  183. db_drop_index('styles', 'label');
  184. db_change_field('styles', 'description', 'description',
  185. array(
  186. 'description' => 'The style description.',
  187. 'type' => 'text',
  188. 'not null' => TRUE,
  189. 'size' => 'big',
  190. )
  191. );
  192. }
  193. /**
  194. * Clear style and preset caches.
  195. */
  196. function styles_update_7208() {
  197. return array();
  198. }
  199. /**
  200. * Ensure we catch included file Styles.inc.
  201. */
  202. function styles_update_7209() {
  203. return array();
  204. }
  205. /**
  206. * Drop style_effects table.
  207. */
  208. function styles_update_7211() {
  209. db_drop_table('style_effects');
  210. }
  211. /**
  212. * Create the styles_presets and styles_preset_instances tables.
  213. */
  214. function styles_update_7212() {
  215. $schema = array();
  216. $schema['styles_presets'] = array(
  217. 'description' => 'Stores configuration options for styles presets.',
  218. 'fields' => array(
  219. 'pid' => array(
  220. 'description' => 'The primary identifier for a style preset.',
  221. 'type' => 'serial',
  222. 'unsigned' => TRUE,
  223. 'not null' => TRUE,
  224. ),
  225. 'field_type' => array(
  226. 'description' => 'The field type.',
  227. 'type' => 'varchar',
  228. 'length' => 128,
  229. 'not null' => TRUE,
  230. ),
  231. 'container_name' => array(
  232. 'description' => 'The container name',
  233. 'type' => 'varchar',
  234. 'length' => 128,
  235. 'not null' => TRUE,
  236. ),
  237. 'name' => array(
  238. 'description' => 'The preset name.',
  239. 'type' => 'varchar',
  240. 'length' => 255,
  241. 'not null' => TRUE,
  242. ),
  243. 'description' => array(
  244. 'description' => 'The preset description.',
  245. 'type' => 'text',
  246. 'not null' => TRUE,
  247. 'size' => 'big',
  248. ),
  249. ),
  250. 'primary key' => array('pid'),
  251. 'indexes' => array(
  252. 'field_type' => array('field_type'),
  253. 'container_name' => array('container_name'),
  254. 'name' => array('name'),
  255. ),
  256. );
  257. $schema['styles_preset_instances'] = array(
  258. 'description' => 'Stores the settings for each container/style preset.',
  259. 'fields' => array(
  260. 'sid' => array(
  261. 'description' => 'The primary identifier for a style.',
  262. 'type' => 'int',
  263. 'unsigned' => TRUE,
  264. 'not null' => TRUE,
  265. 'default' => 0,
  266. ),
  267. 'pid' => array(
  268. 'description' => 'The primary identifier for a style preset.',
  269. 'type' => 'int',
  270. 'unsigned' => TRUE,
  271. 'not null' => TRUE,
  272. 'default' => 0,
  273. ),
  274. ),
  275. 'foreign keys' => array(
  276. 'sid' => array(
  277. 'table' => 'styles',
  278. 'columns' => array('sid' => 'sid'),
  279. ),
  280. 'pid' => array(
  281. 'table' => 'styles_presets',
  282. 'columns' => array('pid' => 'pid'),
  283. ),
  284. ),
  285. 'indexes' => array(
  286. 'sid' => array('sid'),
  287. 'pid' => array('pid'),
  288. ),
  289. );
  290. if (!db_table_exists('styles_presets')) {
  291. db_create_table('styles_presets', $schema['styles_presets']);
  292. }
  293. if (!db_table_exists('styles_preset_instances')) {
  294. db_create_table('styles_preset_instances', $schema['styles_preset_instances']);
  295. }
  296. }
  297. /**
  298. * Remove the description from styles_presets.
  299. */
  300. function styles_update_7213() {
  301. db_drop_field('styles_presets', 'description');
  302. }
  303. /**
  304. * Update old File Style formatters on Image fields to the Image formatter.
  305. */
  306. function styles_update_7214() {
  307. $instances = array();
  308. $fields = field_read_fields(array('type' => 'image'), array('include_inactive' => TRUE));
  309. foreach ($fields as $field) {
  310. $instances = array_merge($instances, field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE)));
  311. }
  312. foreach ($instances as $instance) {
  313. $update_instance = FALSE;
  314. foreach ($instance['display'] as $view_mode => $display) {
  315. if (strpos($display['type'], 'styles_image_') === 0) {
  316. $update_instance = TRUE;
  317. $image_style = substr($display['type'], strlen('styles_image_'));
  318. $instance['display'][$view_mode]['type'] = 'image';
  319. $instance['display'][$view_mode]['settings'] = array('image_style' => $image_style, 'image_link' => FALSE);
  320. }
  321. }
  322. if ($update_instance) {
  323. field_update_instance($instance);
  324. }
  325. }
  326. }
  327. /**
  328. * Delete duplicate entries from the styles table.
  329. */
  330. function styles_update_7215() {
  331. $names = db_query("SELECT name FROM {styles} GROUP BY name HAVING COUNT(*) > 1")->fetchCol();
  332. foreach ($names as $name) {
  333. $sids = db_select('styles')
  334. ->fields('styles', array('sid', 'name'))
  335. ->condition('name', $name)
  336. ->orderBy('sid', 'DESC')
  337. ->execute()
  338. ->fetchCol();
  339. array_shift($sids);
  340. foreach ($sids as $sid) {
  341. db_delete('styles')
  342. ->condition('sid', $sid)
  343. ->execute();
  344. }
  345. }
  346. }