filefield_paths.install 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the File (Field) Paths module.
  5. */
  6. /**
  7. * Implements hook_schema_alter().
  8. *
  9. * @param $schema
  10. * The system-wide schema
  11. */
  12. function filefield_paths_schema_alter(&$schema) {
  13. $schema['file_managed']['fields']['origname'] = array(
  14. 'description' => 'Original name of the file.',
  15. 'type' => 'varchar',
  16. 'length' => 255,
  17. 'not null' => TRUE,
  18. 'default' => '',
  19. );
  20. }
  21. /**
  22. * Implements hook_install().
  23. */
  24. function filefield_paths_install() {
  25. // Add origname field to {file_managed}, and populate with the current
  26. // filenames.
  27. db_add_field('file_managed', 'origname', array(
  28. 'description' => 'Original name of the file with no path components. Used by the filefield_paths module.',
  29. 'type' => 'varchar',
  30. 'length' => 255,
  31. 'not null' => TRUE,
  32. 'default' => '',
  33. ));
  34. db_update('file_managed')
  35. ->expression('origname', 'filename')
  36. ->execute();
  37. }
  38. /**
  39. * Implements hook_uninstall().
  40. */
  41. function filefield_paths_uninstall() {
  42. db_drop_field('file_managed', 'origname');
  43. }
  44. /**
  45. * Implements hook_update_last_removed().
  46. */
  47. function hook_update_last_removed() {
  48. return 6103;
  49. }
  50. /**
  51. * Implements hook_update_dependencies().
  52. */
  53. function filefield_paths_dependencies() {
  54. // Update 7103 uses the {file_managed} table, so make sure it is available.
  55. $dependencies['filefield_paths'][7103] = array(
  56. 'system' => 7034,
  57. );
  58. return $dependencies;
  59. }
  60. /**
  61. * Add origname field to {file_managed}.
  62. */
  63. function filefield_paths_update_7103() {
  64. // Clean-up an unused variable.
  65. variable_del('filefield_paths_schema_version');
  66. // Add origname field to {file_managed}, and populate with the current
  67. // filenames.
  68. if (!db_field_exists('file_managed', 'origname')) {
  69. db_add_field('file_managed', 'origname', array(
  70. 'description' => 'Original name of the file with no path components. Used by the filefield_paths module.',
  71. 'type' => 'varchar',
  72. 'length' => 255,
  73. 'not null' => TRUE,
  74. 'default' => '',
  75. ));
  76. }
  77. db_update('file_managed')
  78. ->expression('origname', 'filename')
  79. ->condition('origname', '')
  80. ->execute();
  81. }
  82. /**
  83. * Add active updating flag to {filefield_paths}
  84. */
  85. function filefield_paths_update_7104() {
  86. db_add_field('filefield_paths', 'active_updating', array(
  87. 'type' => 'int',
  88. 'size' => 'tiny',
  89. 'not null' => TRUE,
  90. 'default' => '0'
  91. ));
  92. // migrate variable to filefield_paths table
  93. $result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'ffp_%%_field_%%'");
  94. foreach ($result as $row) {
  95. if (preg_match('/ffp_(.+)_field_(.+)$/', $row->name, $match)) {
  96. $active_updating = unserialize($row->value);
  97. if ($active_updating) {
  98. db_update('filefield_paths')
  99. ->fields(array(
  100. 'active_updating' => $active_updating
  101. ))
  102. ->condition('type', $match[1])
  103. ->condition('field', $match[2])
  104. ->execute();
  105. }
  106. variable_del($row->name);
  107. }
  108. }
  109. }
  110. /**
  111. * Correct the default value for {filefield_paths}.active_updating field.
  112. */
  113. function filefield_paths_update_7105() {
  114. db_change_field('filefield_paths', 'active_updating', 'active_updating', array(
  115. 'type' => 'int',
  116. 'size' => 'tiny',
  117. 'not null' => TRUE,
  118. 'default' => 0
  119. ));
  120. }
  121. /**
  122. * Increase length of 'type' and 'field' columns.
  123. */
  124. function filefield_paths_update_7106() {
  125. db_change_field('filefield_paths', 'type', 'type', array(
  126. 'type' => 'varchar',
  127. 'length' => 128,
  128. 'not null' => TRUE,
  129. 'default' => ''
  130. ));
  131. db_change_field('filefield_paths', 'field', 'field', array(
  132. 'type' => 'varchar',
  133. 'length' => 128,
  134. 'not null' => TRUE,
  135. 'default' => ''
  136. ));
  137. }
  138. /**
  139. * Removed filefield_paths table/schema.
  140. */
  141. function filefield_paths_update_7107() {
  142. // Remove filefield_paths table/schema.
  143. db_drop_table('filefield_paths');
  144. // Update field instance settings.
  145. $field_types = array_keys(_filefield_paths_get_field_types());
  146. foreach (field_info_fields() as $field) {
  147. if (in_array($field['type'], $field_types)) {
  148. foreach ($field['bundles'] as $entity_type => $bundles) {
  149. foreach ($bundles as $bundle_name) {
  150. $instance = field_info_instance($entity_type, $field['field_name'], $bundle_name);
  151. if ($instance["ffp_{$field['field_name']}"] && !isset($instance['settings']['filefield_paths'])) {
  152. $instance['settings']['filefield_paths'] = array(
  153. 'file_path' => array(
  154. 'value' => $instance["ffp_{$field['field_name']}"]['file_path'],
  155. 'options' => array(
  156. 'pathauto' => $instance["ffp_{$field['field_name']}"]['file_path_cleanup']['file_path_pathauto'],
  157. 'transliterate' => $instance["ffp_{$field['field_name']}"]['file_path_cleanup']['file_path_transliterate'],
  158. ),
  159. ),
  160. 'file_name' => array(
  161. 'value' => $instance["ffp_{$field['field_name']}"]['file_name'],
  162. 'options' => array(
  163. 'pathauto' => $instance["ffp_{$field['field_name']}"]['file_name_cleanup']['file_name_pathauto'],
  164. 'transliterate' => $instance["ffp_{$field['field_name']}"]['file_name_cleanup']['file_name_transliterate'],
  165. ),
  166. ),
  167. 'retroactive_update' => $instance["ffp_{$field['field_name']}"]['retroactive_update'],
  168. 'active_updating' => $instance["ffp_{$field['field_name']}"]['active_updating'],
  169. );
  170. unset($instance["ffp_{$field['field_name']}"]);
  171. field_update_instance($instance);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }