5 * FileField: Defines a CCK file field type.
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
11 include_once('field_file.inc');
14 * Implementation of hook_install().
16 function filefield_install() {
17 drupal_load('module', 'content');
18 content_notify('install', 'filefield');
22 * Implementation of hook_uninstall().
24 function filefield_uninstall() {
25 drupal_load('module', 'content');
26 content_notify('uninstall', 'filefield');
30 * Implementation of hook_enable().
32 function filefield_enable() {
33 drupal_load('module', 'content');
34 content_notify('enable', 'filefield');
38 * Implementation of hook_disable().
40 function filefield_disable() {
41 drupal_load('module', 'content');
42 content_notify('disable', 'filefield');
46 * Implementation of hook_update_last_removed().
48 function filefield_update_last_removed() {
53 * Upgrade FileField to Drupal 6.
55 function filefield_update_6001() {
56 if ($abort = content_check_update('filefield')) {
61 include_once(drupal_get_path('module','content') .
'/includes/content.admin.inc');
63 // rename the field type from file to filefield. adhere to module namespace.
64 $ret[] = update_sql("UPDATE {content_node_field} SET type='filefield' WHERE type='file'");
65 // rename default widget to filefield_widget. adhere to module namespace.
66 $ret[] = update_sql("UPDATE {content_node_field_instance} SET widget_type='filefield_widget' WHERE widget_type='file'");
68 // update list default value and force list settings.
69 $result = db_query("SELECT * FROM {content_node_field} WHERE type = 'filefield'");
70 while ($field = db_fetch_object($result)) {
72 $field_settings = unserialize($field->global_settings
);
73 if (!isset($field_settings['list_default']) || !is_numeric($field_settings['list_default'])) {
74 $field_settings['list_default'] = 1;
78 // set behavior to match old force_list behavior.
79 if (!empty($field_settings['force_list'])) {
80 $field_settings['list_default'] = 1;
81 $field_settings['force_list_default'] = 1;
85 db_query("UPDATE {content_node_field} SET global_settings = '%s' WHERE field_name = '%s'", serialize($field_settings), $field->field_name
);
90 // add data column to filefields.
91 $fields = content_fields();
92 foreach($fields as
$field) {
93 if ($field['type'] != 'filefield') continue;
95 $new_field['columns']['data'] = array('type' => 'text');
96 content_alter_db($field, $new_field);
99 // Build a batch that migrates the data in each filefield
101 'title' => t('Migrating filefield values'),
102 'operations' => array(),
103 'file' => drupal_get_path('module', 'filefield') .
'/filefield.install',
105 $content_info = _content_type_info();
106 foreach ($content_info['content types'] as
$node_type => $node_info) {
107 foreach ($node_info['fields'] as
$field_name => $field) {
108 if ($field['type'] == 'filefield') {
109 $batch['operations'][] = array('_filefield_update_6001_move_operation', array($field));
110 $batch['operations'][] = array('_filefield_update_6001_drop_operation', array($field));
117 // clear them caches.
118 cache_clear_all('*', content_cache_tablename(), true
);
119 cache_clear_all('*', 'cache', true
);
124 * Migrate field settings from 'force_list_default' and 'show_description'.
126 function filefield_update_6100() {
129 module_load_include('inc', 'content', 'includes/content.crud');
131 $fields = content_fields();
132 foreach ($fields as
$field) {
133 if ($field['type'] == 'filefield') {
134 $field['list_field'] = empty($field['force_list_default']);
135 $field['description_field'] = $field['show_description'];
136 _content_field_write($field);
137 $ret[] = array('success' => TRUE
, 'query' => t('The File field %field has been updated with new settings.', array('%field' => $field['field_name'])));
146 * Move the list and descriptions column into the serialized data column.
148 function _filefield_update_6001_move_operation($field, &$context) {
149 // Setup the first through
150 if (!isset($context['sandbox']['progress'])) {
151 $db_info = content_database_info($field);
152 $context['sandbox']['db_info'] = $db_info;
153 $context['sandbox']['table'] = $db_info['table'];
154 $context['sandbox']['col_data'] = $db_info['columns']['data']['column'];
155 $context['sandbox']['col_desc'] = $db_info['columns']['description']['column'];
156 $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(*) FROM {".
$db_info['table'] .
"}"));
157 $context['sandbox']['progress'] = 0;
158 $context['sandbox']['current_node'] = 0;
161 // Work our way through the field values 50 rows at a time.
163 $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
164 while ($row = db_fetch_array($result)) {
165 // Try to unserialize the data column.
166 if (!empty($row[$context['sandbox']['col_data']])) {
167 $data = unserialize($row[$context['sandbox']['col_data']]);
173 // Copy move the values from the columns into the array...
174 $data['description'] = $row[$context['sandbox']['col_desc']];
176 // ...serialize it and store it back to the db.
177 db_query("UPDATE {{$context['sandbox']['table']}} SET {$context['sandbox']['col_data']} = '%s' WHERE vid = %d", serialize($data), $row['vid']);
179 // Update our progress information.
180 $context['sandbox']['progress']++;
181 $context['sandbox']['current_node'] = $row['vid'];
184 // Inform the batch engine that we are not finished,
185 // and provide an estimation of the completion level we reached.
186 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
187 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
192 * Drop the list and description columns.
194 function _filefield_update_6001_drop_operation($field, &$context) {
196 $db_info = content_database_info($field);
197 // TODO: Now that the data has been migrated we can drop the columns.
198 db_drop_field($ret, $db_info['table'], $db_info['columns']['description']['column']);
199 $context['finished'] = 1;