filefield_paths.install 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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')->expression('origname', 'filename')->execute();
  35. }
  36. /**
  37. * Implements hook_uninstall().
  38. */
  39. function filefield_paths_uninstall() {
  40. db_drop_field('file_managed', 'origname');
  41. }
  42. /**
  43. * Implements hook_update_last_removed().
  44. */
  45. function hook_update_last_removed() {
  46. return 6103;
  47. }
  48. /**
  49. * Implements hook_update_dependencies().
  50. */
  51. function filefield_paths_dependencies() {
  52. // Update 7103 uses the {file_managed} table, so make sure it is available.
  53. $dependencies['filefield_paths'][7103] = array(
  54. 'system' => 7034,
  55. );
  56. return $dependencies;
  57. }
  58. /**
  59. * Add origname field to {file_managed}.
  60. */
  61. function filefield_paths_update_7103() {
  62. // Clean-up an unused variable.
  63. variable_del('filefield_paths_schema_version');
  64. // Add origname field to {file_managed}, and populate with the current
  65. // filenames.
  66. if (!db_field_exists('file_managed', 'origname')) {
  67. db_add_field('file_managed', 'origname', array(
  68. 'description' => 'Original name of the file with no path components. Used by the filefield_paths module.',
  69. 'type' => 'varchar',
  70. 'length' => 255,
  71. 'not null' => TRUE,
  72. 'default' => '',
  73. ));
  74. }
  75. db_update('file_managed')
  76. ->expression('origname', 'filename')
  77. ->condition('origname', '')
  78. ->execute();
  79. }
  80. /**
  81. * Add active updating flag to {filefield_paths}
  82. */
  83. function filefield_paths_update_7104() {
  84. db_add_field('filefield_paths', 'active_updating', array(
  85. 'type' => 'int',
  86. 'size' => 'tiny',
  87. 'not null' => TRUE,
  88. 'default' => '0'
  89. ));
  90. // migrate variable to filefield_paths table
  91. $result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'ffp_%%_field_%%'");
  92. foreach ($result as $row) {
  93. if (preg_match('/ffp_(.+)_field_(.+)$/', $row->name, $match)) {
  94. $active_updating = unserialize($row->value);
  95. if ($active_updating) {
  96. db_update('filefield_paths')
  97. ->fields(array(
  98. 'active_updating' => $active_updating
  99. ))
  100. ->condition('type', $match[1])
  101. ->condition('field', $match[2])
  102. ->execute();
  103. }
  104. variable_del($row->name);
  105. }
  106. }
  107. }
  108. /**
  109. * Correct the default value for {filefield_paths}.active_updating field.
  110. */
  111. function filefield_paths_update_7105() {
  112. db_change_field('filefield_paths', 'active_updating', 'active_updating', array(
  113. 'type' => 'int',
  114. 'size' => 'tiny',
  115. 'not null' => TRUE,
  116. 'default' => 0
  117. ));
  118. }
  119. /**
  120. * Increase length of 'type' and 'field' columns.
  121. */
  122. function filefield_paths_update_7106() {
  123. db_change_field('filefield_paths', 'type', 'type', array(
  124. 'type' => 'varchar',
  125. 'length' => 128,
  126. 'not null' => TRUE,
  127. 'default' => ''
  128. ));
  129. db_change_field('filefield_paths', 'field', 'field', array(
  130. 'type' => 'varchar',
  131. 'length' => 128,
  132. 'not null' => TRUE,
  133. 'default' => ''
  134. ));
  135. }
  136. /**
  137. * Removed filefield_paths table/schema.
  138. */
  139. function filefield_paths_update_7107() {
  140. $results = db_select('filefield_paths', 'ffp')->fields('ffp')->execute();
  141. foreach ($results as $result) {
  142. $instance = field_info_instance('node', $result->field, $result->type);
  143. if (!is_null($instance) && isset($instance["ffp_{$result->field}}"])) {
  144. $filepath = unserialize($result->filepath);
  145. $filename = unserialize($result->filename);
  146. $instance["ffp_{$result->field}"] = array(
  147. 'file_path' => $filepath['value'],
  148. 'file_path_cleanup' => array(
  149. 'file_path_pathauto' => $filepath['pathauto'],
  150. 'file_path_transliterate' => $filepath['transliterate'],
  151. ),
  152. 'file_name' => $filename['value'],
  153. 'file_name_cleanup' => array(
  154. 'file_name_pathauto' => $filename['pathauto'],
  155. 'file_name_transliterate' => $filename['transliterate'],
  156. ),
  157. 'active_updating' => $result->active_updating,
  158. );
  159. field_update_instance($instance);
  160. }
  161. }
  162. // Remove filefield_paths table/schema.
  163. db_drop_table('filefield_paths');
  164. // Update field instance settings.
  165. drupal_load('module', 'filefield_paths');
  166. $field_types = array_keys(_filefield_paths_get_field_types());
  167. foreach (field_info_fields() as $field) {
  168. if (in_array($field['type'], $field_types)) {
  169. foreach ($field['bundles'] as $entity_type => $bundles) {
  170. foreach ($bundles as $bundle_name) {
  171. $instance = field_info_instance($entity_type, $field['field_name'], $bundle_name);
  172. if (isset($instance["ffp_{$field['field_name']}"]) && !isset($instance['settings']['filefield_paths'])) {
  173. $instance['settings']['filefield_paths'] = array(
  174. 'file_path' => array(
  175. 'value' => $instance["ffp_{$field['field_name']}"]['file_path'],
  176. 'options' => array(
  177. 'pathauto' => $instance["ffp_{$field['field_name']}"]['file_path_cleanup']['file_path_pathauto'],
  178. 'transliterate' => $instance["ffp_{$field['field_name']}"]['file_path_cleanup']['file_path_transliterate'],
  179. ),
  180. ),
  181. 'file_name' => array(
  182. 'value' => $instance["ffp_{$field['field_name']}"]['file_name'],
  183. 'options' => array(
  184. 'pathauto' => $instance["ffp_{$field['field_name']}"]['file_name_cleanup']['file_name_pathauto'],
  185. 'transliterate' => $instance["ffp_{$field['field_name']}"]['file_name_cleanup']['file_name_transliterate'],
  186. ),
  187. ),
  188. 'active_updating' => $instance["ffp_{$field['field_name']}"]['active_updating'],
  189. );
  190. unset($instance["ffp_{$field['field_name']}"]);
  191. field_update_instance($instance);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }