Issue #1093844 by dww, Fix PHP warnings from content_migrate.filefield.inc on undefin...
[project/cck.git] / modules / content_migrate / modules / content_migrate.filefield.inc
1 <?php
2 /**
3 * @file content_migrate.filefield.inc
4 * Code to implement hook_content_migrate_field_alter, content_migrate_instance_alter() and content_migrate_data_record_alter()
5 * on behalf of the former filefield and imagefield modules, moved into a separate file for efficiency.
6 */
7
8 /**
9 * Implements hook_content_migrate_field_alter().
10 *
11 * Use this to tweak the conversion of field settings
12 * from the D6 style to the D7 style for specific
13 * situations not handled by basic conversion,
14 * as when field types or settings are changed.
15 */
16 function content_migrate_filefield_field_alter(&$field_value, $instance_value) {
17
18 switch ($instance_value['widget']['module']) {
19 case 'imagefield':
20 // Module names and types changed.
21 $field_value['module'] = 'image';
22 $field_value['type'] = 'image';
23 // default_image is now a field setting.
24 $field_value['settings']['default_image'] = $instance_value['widget']['settings']['default_image'];
25 break;
26 case 'filefield':
27 // Module names and types changed.
28 $field_value['module'] = 'file';
29 $field_value['type'] = 'file';
30 break;
31 }
32 }
33
34 /**
35 * Implements hook_content_migrate_instance_alter().
36 *
37 * Use this to tweak the conversion of instance or widget settings
38 * from the D6 style to the D7 style for specific
39 * situations not handled by basic conversion, as when
40 * formatter or widget names or settings are changed.
41 */
42 function content_migrate_filefield_instance_alter(&$instance_value, $field_value) {
43
44 switch ($instance_value['widget']['module']) {
45
46 case 'imagefield':
47 // Module names and types changed.
48 $instance_value['widget']['module'] = 'image';
49 $instance_value['widget']['type'] = 'image';
50
51 // Most settings became instance settings instead of widget settings, with a couple name changes.
52 $instance_value['settings']['file_directory'] = $instance_value['widget']['settings']['file_path'];
53 unset($instance_value['widget']['settings']['file_path']);
54 $instance_value['settings']['file_extensions'] = $instance_value['widget']['settings']['file_extensions'];
55 unset($instance_value['widget']['settings']['file_extensions']);
56 $settings = array(
57 'max_filesize' => 0,
58 'max_resolution' => 0,
59 'min_resolution' => 0,
60 );
61 foreach ($settings as $setting => $default_value) {
62 $instance_value['settings'][$setting] = isset($instance_value['widget']['settings'][$setting]) ? $instance_value['widget']['settings'][$setting] : $default_value;
63 unset($instance_value['widget']['settings'][$setting]);
64 }
65
66 // What is the difference between alt and custom_alt on the old field?
67 $instance_value['settings']['alt_field'] = $instance_value['widget']['settings']['custom_alt'];
68 unset($instance_value['widget']['settings']['custom_alt']);
69 $instance_value['settings']['title_field'] = $instance_value['widget']['settings']['custom_title'];
70 unset($instance_value['widget']['settings']['custom_title']);
71
72 // Many settings have no place in the new field array.
73 unset($instance_value['widget']['settings']['title']);
74 unset($instance_value['widget']['settings']['alt']);
75 unset($instance_value['widget']['settings']['max_filesize_per_node']);
76 unset($instance_value['widget']['settings']['title_type']);
77
78 // default_image is now a field setting.
79 unset($instance_value['widget']['settings']['default_image']);
80 break;
81
82 case 'filefield':
83 // Module names and types changed.
84 $instance_value['widget']['module'] = 'file';
85 $instance_value['widget']['type'] = 'file_generic';
86
87 // Some settings had name changes.
88 $instance_value['widget']['settings']['file_directory'] = $instance_value['widget']['settings']['file_path'];
89 unset($instance_value['widget']['settings']['file_path']);
90 $instance_value['widget']['settings']['max_filesize'] = $instance_value['widget']['settings']['max_filesize_per_file'];
91 unset($instance_value['widget']['settings']['max_filesize_per_file']);
92
93 break;
94 }
95
96 }
97
98
99 /**
100 * Implements hook_content_migrate_data_record_alter().
101 *
102 * Tweaks individual records in a field.
103 */
104 function content_migrate_filefield_data_record_alter(&$record, $field) {
105
106 switch($field['type']) {
107 case 'image':
108 // Map D6 imagefield field columns to D7 image field columns.
109 if (!empty($record[$field['field_name'] . '_title']) && ($data = unserialize($record[$field['field_name'] . '_title']))) {
110 $record[$field['field_name'] . '_alt'] = $data['alt'];
111 $record[$field['field_name'] . '_title'] = $data['title'];
112 }
113 else {
114 unset($record[$field['field_name'] . '_alt']);
115 unset($record[$field['field_name'] . '_title']);
116 }
117
118 // Fall through.
119 case 'file':
120 // Map D6 filefield field columns to D7 file field columns.
121 if (!empty($record[$field['field_name'] . '_description']) && ($data = unserialize($record[$field['field_name'] . '_description']))) {
122 $record[$field['field_name'] . '_description'] = $data['description'];
123 }
124 else {
125 unset($record[$field['field_name'] . '_description']);
126 }
127
128 // Copies imagefield data from the old 'files' table into 'files_managed' and sets file_usage
129 // Mostly copied from system_update_7061, which does the same for the D6 core 'upload' module
130
131 $nid = $record['entity_id'];
132 $fid = $record[$field['field_name'] . '_fid'];
133
134 if (empty($fid)) {
135 $record = NULL;
136 return;
137 };
138
139 $file = db_select('files', 'f')->fields('f', array('fid', 'uid', 'filename', 'filepath', 'filemime', 'filesize', 'timestamp', 'status'))->condition('fid', $fid)->execute()->fetchObject();
140
141 $basename = variable_get('file_directory_path', conf_path() . '/files');
142 $scheme = file_default_scheme() . '://';
143
144 // We will convert filepaths to uri using the default scheme
145 // and stripping off the existing file directory path.
146 $file->uri = $scheme . str_replace($basename, '', $file->filepath);
147 $file->uri = file_stream_wrapper_uri_normalize($file->uri);
148 unset($file->filepath);
149
150 // Insert into the file_managed table.
151 // Each fid should only be stored once in file_managed.
152 db_merge('file_managed')
153 ->key(array(
154 'fid' => $file->fid,
155 ))
156 ->fields(array(
157 'uid' => $file->uid,
158 'filename' => $file->filename,
159 'uri' => $file->uri,
160 'filemime' => $file->filemime,
161 'filesize' => $file->filesize,
162 'status' => $file->status,
163 'timestamp' => $file->timestamp,
164 ))
165 ->execute();
166
167 // Add the usage entry for the file.
168 file_usage_add($file, 'file', 'node', $nid);
169 break;
170 }
171 }