/[drupal]/contributions/modules/imagefield/imagefield.install
ViewVC logotype

Contents of /contributions/modules/imagefield/imagefield.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.31 - (show annotations) (download) (as text)
Wed Apr 15 20:15:03 2009 UTC (7 months, 1 week ago) by quicksketch
Branch: MAIN
CVS Tags: DRUPAL-6--3-2, DRUPAL-6--3-0, DRUPAL-6--3-1, HEAD
Changes since 1.30: +21 -18 lines
File MIME type: text/x-php
Fixing notices in updates.
1 <?php
2 // $Id: imagefield.install,v 1.30 2009/03/21 03:06:07 quicksketch Exp $
3
4 /**
5 * Implementation of hook_install().
6 */
7 function imagefield_install() {
8 drupal_load('module', 'content');
9 content_notify('install', 'imagefield');
10 }
11
12 function imagefield_uninstall() {
13 drupal_load('module', 'content');
14 content_notify('uninstall', 'imagefield');
15 }
16
17 function imagefield_enable() {
18 drupal_load('module', 'content');
19 content_notify('enable', 'imagefield');
20 }
21
22 function imagefield_disable() {
23 drupal_load('module', 'content');
24 content_notify('disable', 'imagefield');
25 }
26
27 /**
28 * Implementation of hook_update_last_removed().
29 */
30 function imagefield_update_last_removed() {
31 // ImageField has later updates than this in the Drupal 5 version, however
32 // they deal with changing formatters that are not necessary for a successful
33 // upgrade to Drupal 6. Update 2 is as far as we really need.
34 return 2;
35 }
36
37 /**
38 * Upgrade to CCK 2 and Drupal 6.
39 */
40 function imagefield_update_6001() {
41 // This update was moved into 6004 so that it can be run again for users
42 // who were not properly updated.
43 return array();
44 }
45
46 /**
47 * Migrate fields to the new structure.
48 */
49 function imagefield_update_6002() {
50 // This update was moved to 6004 so that it can be run again for users
51 // who were not properly updated.
52 return array();
53 }
54
55 /**
56 * Convert image field type to filefield.
57 */
58 function imagefield_update_6003() {
59 $ret = array();
60
61 if (drupal_get_installed_schema_version('filefield', TRUE) < 6001) {
62 $ret['#abort'] = array('success' => FALSE, 'query' => t('FileField must be updated to Drupal 6 before ImageField can be updated.'));
63 return $ret;
64 }
65
66 $ret[] = update_sql("UPDATE {" . content_field_tablename() . "} SET type = 'filefield', module = 'filefield', active = 1 WHERE module = 'imagefield' OR type = 'image'");
67 $ret[] = update_sql("UPDATE {" . content_instance_tablename() . "} SET widget_type = 'imagefield_widget', widget_module = 'imagefield', widget_active = 1 WHERE widget_type = 'image' OR widget_type = 'imagefield_widget'");
68 content_clear_type_cache();
69
70 return $ret;
71 }
72
73 /**
74 * Migrate fields to the new structure.
75 */
76 function imagefield_update_6004(&$context) {
77 module_load_install('content');
78 module_load_include('inc', 'imagefield', 'imagefield_file');
79 module_load_include('inc', 'content', 'includes/content.admin');
80 module_load_include('inc', 'content', 'includes/content.crud');
81
82 $ret = array();
83
84 if (!isset($context['sandbox']['progress'])) {
85 // Get the latest cache values and schema.
86 content_clear_type_cache(TRUE);
87
88 // Grab the list of fields to update.
89 $context['sandbox']['fields'] = array();
90 foreach (content_types_install() as $type_name => $fields) {
91 foreach ($fields as $field) {
92 if ($field['type'] == 'filefield' && $field['widget']['type'] == 'imagefield_widget') {
93 // We only process a given field once.
94 $context['sandbox']['fields'][$field['field_name']] = $field;
95 // Widgets are each updated individually.
96 $context['sandbox']['widgets'][] = $field;
97 }
98 }
99 }
100
101 if (empty($context['sandbox']['fields'])) {
102 return $ret;
103 }
104
105 // Add/update the database fields.
106 foreach ($context['sandbox']['fields'] as $field) {
107 $db_info = content_database_info($field);
108
109 // Convert the default value for the FID field to NULL.
110 db_change_field($ret, $db_info['table'], $field['field_name'] . '_fid', $field['field_name'] . '_fid', array('type' => 'int'));
111 $ret[] = update_sql("UPDATE {" . $db_info['table'] . "} SET " . $field['field_name'] . "_fid = NULL WHERE " . $field['field_name'] . "_fid = 0");
112
113 // Set any entries that were abandoned by poor housekeeping to NULL.
114 $ret[] = update_sql("UPDATE {" . $db_info['table'] . "} SET " . $field['field_name'] . "_fid = NULL WHERE " . $field['field_name'] . "_fid NOT IN (SELECT fid FROM {files})");
115
116 // Add the "data" and "list" columns to the field if not there already.
117 if (!db_column_exists($db_info['table'], $field['field_name'] . '_list')) {
118 db_add_field($ret, $db_info['table'], $field['field_name'] . '_list', array('type' => 'int', 'size' => 'tiny'));
119 }
120 if (!db_column_exists($db_info['table'], $field['field_name'] . '_data')) {
121 db_add_field($ret, $db_info['table'], $field['field_name'] . '_data', array('type' => 'text'));
122 }
123
124 // Set the default state of the global settings.
125 $field['list_field'] = '0';
126 $field['list_default'] = '1';
127 $field['description_field'] = '0';
128
129 // Map 'max_number_images' parameter to CCK 'multiple'.
130 if (!empty($field['widget']['multiple']) && isset($field['widget']['max_number_images'])) {
131 if ($field['widget']['max_number_images'] == 0) {
132 $field['multiple'] = 1; // 1 means "Unlimited" in CCK.
133 }
134 elseif ($field['widget']['max_number_images'] == 1) {
135 $field['multiple'] = 0; // 0 means "Not Multiple" in CCK.
136 }
137 else {
138 $field['multiple'] = $field['widget']['max_number_images'];
139 }
140 }
141
142 // Update format names.
143 $display_settings = array('teaser', 'full');
144 foreach ($display_settings as $display_context) {
145 if (isset($field['display_settings'][$display_context])) {
146 switch ($field['display_settings'][$display_context]['format']) {
147 case 'imagefield_nodelink':
148 $field['display_settings'][$display_context]['format'] = 'image_nodelink';
149 break;
150 case 'imagefield_imagelink':
151 $field['display_settings'][$display_context]['format'] = 'image_imagelink';
152 break;
153 case 'imagefield_path':
154 $field['display_settings'][$display_context]['format'] = 'path_plain';
155 break;
156 case 'imagefield_url':
157 $field['display_settings'][$display_context]['format'] = 'url_plain';
158 break;
159 case 'imagefield_default':
160 case 'default':
161 $field['display_settings'][$display_context]['format'] = 'image_plain';
162 break;
163 }
164 }
165 }
166
167 // Move the default_image options to the widget level.
168 $field['widget']['default_image'] = isset($field['default_image']) ? $field['default_image'] : NULL;
169 $field['widget']['use_default_image'] = isset($field['use_default_image']) ? $field['use_default_image'] : 0;
170
171 // The default image needs to be saved in the files table.
172 if (isset($field['widget']['default_image']['filepath'])) {
173 $file = (object) $field['widget']['default_image'];
174 // Check if it's already in the files table.
175 if ($fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath))) {
176 $field['widget']['default_image']['fid'] = $fid;
177 }
178 // Otherwise add it.
179 else {
180 $file->uid = 1;
181 $file->status = 1;
182 drupal_write_record('files', $file);
183 $field['widget']['default_image']['fid'] = $file->fid;
184 }
185
186 // Unset the field-level definition, or it will take precedence.
187 unset($field['default_image']);
188 }
189
190 content_field_instance_update($field);
191 }
192
193 // Update each widget instance.
194 foreach ($context['sandbox']['widgets'] as $field) {
195 // Change file_path to image_path.
196 if (isset($field['widget']['image_path'])) {
197 $field['widget']['file_path'] = $field['widget']['image_path'];
198 }
199
200 // Update the formatters.
201 foreach ($field['display_settings'] as $key => $display) {
202 switch ($display['format']) {
203 case 'default':
204 $field['display_settings'][$key]['format'] = 'image_plain';
205 break;
206 case 'imagefield_nodelink':
207 $field['display_settings'][$key]['format'] = 'image_nodelink';
208 break;
209 case 'imagefield_imagelink':
210 $field['display_settings'][$key]['format'] = 'image_imagelink';
211 break;
212 case 'imagefield_path':
213 $field['display_settings'][$key]['format'] = 'path_plain';
214 break;
215 case 'imagefield_url':
216 $field['display_settings'][$key]['format'] = 'url_plain';
217 break;
218 }
219 }
220
221 content_field_instance_update($field);
222 }
223
224 $context['sandbox']['progress'] = 0;
225 $context['sandbox']['total'] = count($context['sandbox']['fields']);
226 $context['sandbox']['current_node'] = 0;
227 }
228
229 $field = array_shift($context['sandbox']['fields']);
230 $db_info = content_database_info($field);
231 $table = $db_info['table'];
232 $col_fid = $field['field_name'] .'_fid';
233 $col_alt = $field['field_name'] .'_alt';
234 $col_title = $field['field_name'] .'_title';
235 $col_data = $field['field_name'] .'_data';
236 $col_list = $field['field_name'] .'_list';
237
238 $limit = 100;
239 $result = db_query_range("SELECT * FROM {". $table ."} WHERE vid > %d ORDER BY vid ASC", $context['sandbox']['current_node'], 0, $limit);
240 $has_processed = FALSE;
241
242 // Loop through each ImageField row and convert its alt and title columns.
243 while ($row = db_fetch_array($result)) {
244 // Try to unserialize the data column.
245 if (!empty($row[$col_data])) {
246 $data = unserialize($row[$col_data]);
247 }
248 if (empty($data)) {
249 $data = array();
250 }
251
252 // Copy the values into the data array.
253 if (isset($row[$col_alt])) {
254 $data['alt'] = $row[$col_alt];
255 }
256 if (isset($row[$col_title])) {
257 $data['title'] = $row[$col_title];
258 }
259 $list = isset($row[$col_list]) ? $row[$col_list] : 1;
260
261 // Depending on if this is multivalue or not, update based on delta.
262 if ($field['multiple'] > 0) {
263 $query = "UPDATE {". $table ."} SET $col_data = '%s', $col_list = %d WHERE vid = %d AND delta = %d";
264 }
265 else {
266 $query = "UPDATE {". $table ."} SET $col_data = '%s', $col_list = %d WHERE vid = %d";
267 $row['delta'] = 0;
268 }
269
270 // Serialize it and store it back to the db.
271 db_query($query, serialize($data), $list, $row['vid'], $row['delta']);
272
273 // Update our progress information.
274 $context['sandbox']['current_node'] = $row['vid'];
275 $has_processed = TRUE;
276 }
277
278 if ($has_processed) {
279 // Not finished, put back the field in the array.
280 array_unshift($context['sandbox']['fields'], $field);
281 }
282 else {
283 // Cleanup the old columns.
284 if (db_column_exists($table, $col_alt)) {
285 db_drop_field($ret, $table, $col_alt);
286 }
287 if (db_column_exists($table, $col_title)) {
288 db_drop_field($ret, $table, $col_title);
289 }
290
291 // Process to next field.
292 $context['sandbox']['progress']++;
293 $context['sandbox']['current_node'] = 0;
294 }
295
296 if (!empty($context['sandbox']['fields'])) {
297 $ret['#finished'] = $context['sandbox']['progress'] / $context['sandbox']['total'];
298 }
299
300 return $ret;
301 }
302
303
304 /**
305 * Delete thumbnails spread throughout the files directory.
306 */
307 function imagefield_update_6005() {
308 $ret = array();
309
310 $result = db_query("SELECT * FROM {files} WHERE filemime LIKE 'image/%'");
311
312 while ($file = db_fetch_object($result)) {
313 // ImageField 6002 thumbnail path.
314 $thumb_path_a = $file->filepath . '.thumb.jpg';
315
316 // ImageField 6004 thumbnail path.
317 $extension_dot = strrpos($file->filepath, '.');
318 $extension = substr($file->filepath, $extension_dot + 1);
319 $basepath = substr($file->filepath, 0, $extension_dot);
320 $thumb_path_b = $basepath .'.thumb.'. $extension;
321
322 file_delete($thumb_path_a);
323 file_delete($thumb_path_b);
324 }
325
326 $ret[] = array('success' => TRUE, 'query' => t('Deleted admin thumbnails distributed throughout files directory. All thumbnails are now stored in the "imagefield_thumbs" directory.'));
327
328 return $ret;
329 }

  ViewVC Help
Powered by ViewVC 1.1.2