file_entity.install 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the file_entity module.
  5. */
  6. /**
  7. * Implements hook_schema_alter().
  8. */
  9. function file_entity_schema_alter(&$schema) {
  10. $schema['file_managed']['fields']['type'] = array(
  11. 'description' => 'The type of this file.',
  12. 'type' => 'varchar',
  13. 'length' => 50,
  14. 'not null' => TRUE,
  15. 'default' => '',
  16. );
  17. $schema['file_managed']['indexes']['file_type'] = array('type');
  18. }
  19. /**
  20. * Implements hook_schema().
  21. */
  22. function file_entity_schema() {
  23. $schema['file_display'] = array(
  24. 'description' => 'Stores configuration options for file displays.',
  25. 'fields' => array(
  26. // @todo Can be refactored as a compond primary key after
  27. // http://drupal.org/node/924236 is implemented.
  28. 'name' => array(
  29. 'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
  30. 'type' => 'varchar',
  31. 'length' => '255',
  32. 'not null' => TRUE,
  33. ),
  34. 'weight' => array(
  35. 'type' => 'int',
  36. 'not null' => TRUE,
  37. 'default' => 0,
  38. 'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
  39. ),
  40. 'status' => array(
  41. 'type' => 'int',
  42. 'unsigned' => TRUE,
  43. 'not null' => TRUE,
  44. 'default' => 0,
  45. 'size' => 'tiny',
  46. 'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
  47. ),
  48. 'settings' => array(
  49. 'type' => 'blob',
  50. 'not null' => FALSE,
  51. 'size' => 'big',
  52. 'serialize' => TRUE,
  53. 'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
  54. ),
  55. ),
  56. 'primary key' => array('name'),
  57. 'export' => array(
  58. // Integrate {file_display} with CTools Exportables.
  59. // @see help/export.html documentation bundled with the CTools project.
  60. 'key' => 'name',
  61. 'key name' => 'Name',
  62. 'primary key' => 'name',
  63. // The {file_display}.status field is used to control whether the display
  64. // is active in the display chain. CTools-level disabling is something
  65. // different, and it's not yet clear how to interpret it for file displays.
  66. // Until that's figured out, prevent CTools-level disabling.
  67. 'can disable' => FALSE,
  68. 'default hook' => 'file_default_displays',
  69. 'identifier' => 'file_display',
  70. 'api' => array(
  71. 'owner' => 'file_entity',
  72. 'api' => 'file_default_displays',
  73. 'minimum_version' => 1,
  74. 'current_version' => 1,
  75. ),
  76. ),
  77. );
  78. return $schema;
  79. }
  80. /**
  81. * Implements hook_install().
  82. */
  83. function file_entity_install() {
  84. $schema = array();
  85. file_entity_schema_alter($schema);
  86. $spec = $schema['file_managed']['fields']['type'];
  87. $indexes_new = array('indexes' => $schema['file_managed']['indexes']);
  88. // If another module (e.g., Media) had added a {file_managed}.type field,
  89. // then change it to the expected specification. Otherwise, add the field.
  90. if (db_field_exists('file_managed', 'type')) {
  91. // db_change_field() will fail if any records have type=NULL, so update
  92. // them to the new default value.
  93. db_update('file_managed')->fields(array('type' => $spec['default']))->isNull('type')->execute();
  94. // Indexes using a field being changed must be dropped prior to calling
  95. // db_change_field(). However, the database API doesn't provide a way to do
  96. // this without knowing what the old indexes are. Therefore, it is the
  97. // responsibility of the module that added them to drop them prior to
  98. // allowing this module to be installed.
  99. db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
  100. }
  101. else {
  102. db_add_field('file_managed', 'type', $spec, $indexes_new);
  103. }
  104. // Update all files with empty types to use the first part of filemime.
  105. db_update('file_managed')
  106. ->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)")
  107. ->condition('type', '')
  108. ->execute();
  109. // Set permissions.
  110. $roles = user_roles();
  111. foreach ($roles as $rid => $role) {
  112. user_role_grant_permissions($rid, array('view file'));
  113. }
  114. }
  115. /**
  116. * Implement hook_uninstall().
  117. */
  118. function file_entity_uninstall() {
  119. db_drop_field('file_managed', 'type');
  120. }
  121. /**
  122. * Create the {file_display} database table.
  123. */
  124. function file_entity_update_7000() {
  125. $schema['file_display'] = array(
  126. 'description' => 'Stores configuration options for file displays.',
  127. 'fields' => array(
  128. 'name' => array(
  129. 'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
  130. 'type' => 'varchar',
  131. 'length' => '255',
  132. 'not null' => TRUE,
  133. ),
  134. 'weight' => array(
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. 'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
  139. ),
  140. 'status' => array(
  141. 'type' => 'int',
  142. 'unsigned' => TRUE,
  143. 'not null' => TRUE,
  144. 'default' => 0,
  145. 'size' => 'tiny',
  146. 'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
  147. ),
  148. 'settings' => array(
  149. 'type' => 'blob',
  150. 'not null' => FALSE,
  151. 'size' => 'big',
  152. 'serialize' => TRUE,
  153. 'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
  154. ),
  155. ),
  156. 'primary key' => array('name'),
  157. );
  158. db_create_table('file_display', $schema['file_display']);
  159. }
  160. /**
  161. * Move file display configurations from the 'file_displays' variable to the
  162. * {file_display} database table.
  163. */
  164. function file_entity_update_7001() {
  165. $file_displays = variable_get('file_displays');
  166. if (!empty($file_displays)) {
  167. foreach ($file_displays as $file_type => $file_type_displays) {
  168. if (!empty($file_type_displays)) {
  169. foreach ($file_type_displays as $view_mode => $view_mode_displays) {
  170. if (!empty($view_mode_displays)) {
  171. foreach ($view_mode_displays as $formatter_name => $display) {
  172. if (!empty($display)) {
  173. db_merge('file_display')
  174. ->key(array(
  175. 'name' => implode('__', array($file_type, $view_mode, $formatter_name)),
  176. ))
  177. ->fields(array(
  178. 'status' => isset($display['status']) ? $display['status'] : 0,
  179. 'weight' => isset($display['weight']) ? $display['weight'] : 0,
  180. 'settings' => isset($display['settings']) ? serialize($display['settings']) : NULL,
  181. ))
  182. ->execute();
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. variable_del('file_displays');
  191. }
  192. /**
  193. * Empty update function to trigger a theme registry rebuild.
  194. */
  195. function file_entity_update_7100() { }
  196. /**
  197. * Update all files with empty types to use the first part of filemime.
  198. *
  199. * For example, an png image with filemime 'image/png' will be assigned a file
  200. * type of 'image'.
  201. */
  202. function file_entity_update_7101() {
  203. db_update('file_managed')
  204. ->expression('type', "SUBSTRING_INDEX(filemime, '/', 1)")
  205. ->condition('type', '')
  206. ->execute();
  207. }
  208. /**
  209. * Empty update function to trigger an entity cache rebuild.
  210. */
  211. function file_entity_update_7102() { }
  212. /**
  213. * Intentionally left blank.
  214. */
  215. function file_entity_update_7103() { }
  216. /**
  217. * Assign view file permission when updating without the Media module.
  218. */
  219. function file_entity_update_7104() {
  220. if (!module_exists('media')) {
  221. $roles = user_roles(FALSE, 'view file');
  222. if (empty($roles)) {
  223. // Set permissions.
  224. $roles = user_roles();
  225. foreach ($roles as $rid => $role) {
  226. user_role_grant_permissions($rid, array('view file'));
  227. }
  228. }
  229. }
  230. }