-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilefield_paths.install
196 lines (158 loc) · 6.38 KB
/
filefield_paths.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* @file
* Install, update and uninstall functions for the FileField Paths module.
*/
/**
* Implements hook_schema().
*/
function filefield_paths_schema() {
$schema['filefield_paths'] = array(
'fields' => array(
'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
'field' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
'filename' => array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE),
'filepath' => array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE),
),
'unique keys' => array(
'type_field' => array('type', 'field'),
),
);
return $schema;
}
/**
* Implements hook_schema_alter().
*
* @param $schema
* The system-wide schema
*/
function filefield_paths_schema_alter(&$schema) {
$schema['files']['fields']['origname'] = array(
'description' => 'Original name of the file.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
);
}
/**
* Implements hook_install().
*/
function filefield_paths_install() {
drupal_install_schema('filefield_paths');
db_add_field($ret = array(), 'files', 'origname', array('description' => 'Original name of the file.', 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
$result = db_query("SELECT fid, filename FROM {files}");
while ($file = db_fetch_object($result)) {
db_query("UPDATE {files} SET origname = '%s' WHERE fid = %d", $file->filename, $file->fid);
}
variable_set('filefield_paths_schema_version', 6103);
}
/**
* Implements hook_uninstall().
*/
function filefield_paths_uninstall() {
drupal_uninstall_schema('filefield_paths');
db_drop_field($ret = array(), 'files', 'origname');
variable_del('filefield_paths_schema_version');
}
/**
* Implements hook_update_N().
*/
function filefield_paths_update_6100() {
$ret = array();
if (db_table_exists('filefield_paths')) {
return $ret;
}
$schema['filefield_paths'] = array(
'fields' => array(
'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
'field' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
'filename' => array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE),
'filepath' => array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE),
),
'unique keys' => array(
'type_field' => array('type', 'field'),
),
);
db_create_table($ret, 'filefield_paths', $schema['filefield_paths']);
$fields = array();
$content_type = content_types();
foreach ($content_type as $type) {
$types[] = $type['type'];
foreach ($type['fields'] as $field) {
if (preg_match('/\bfilefield\b|\bimage\b/', $field['type'])) {
if (!in_array($field['field_name'], $fields)) {
$fields[] = $field['field_name'];
}
}
}
}
$types = implode('|', $types);
$fields = implode('|', $fields);
$result = db_query("SELECT * FROM {variable} WHERE name LIKE 'filefield_paths_%'");
while ($variable = db_fetch_object($result)) {
$pattern = '/filefield_paths_(' . $types . ')_(' . $fields . ')_(file_name|file_path)_?(.*)/';
if (preg_match($pattern, $variable->name, $results)) {
if (empty($results[4])) {
$results[4] = 'value';
}
$data[$results[1]][$results[2]][$results[3]][$results[4]] = unserialize($variable->value);
}
}
foreach ($data as $type => $type_data) {
foreach ($type_data as $field => $field_data) {
$result = db_fetch_object(
db_query("SELECT widget_settings FROM {content_node_field_instance} ".
"WHERE field_name = '%s' AND type_name = '%s'", $field, $type)
);
$widget_settings = unserialize($result->widget_settings);
$field_data['file_path']['value'] = $widget_settings['file_path'];
$result = db_query(
"INSERT INTO {filefield_paths} (type, field, filename, filepath) VALUES ('%s', '%s', '%s', '%s')",
$type, $field, serialize($field_data['file_name']), serialize($field_data['file_path']), TRUE
);
$ret[] = array('success' => $result !== FALSE, 'query' => check_plain("
INSERT INTO {filefield_paths} (type, field, filename, filepath) VALUES ('". $type ."', '".
$field ."', '". serialize($field_data['file_name']) ."', '". serialize($field_data['file_path']) ."')
"));
}
}
$ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'filefield_paths_%'");
cache_clear_all('variables', 'cache');
return $ret;
}
function filefield_paths_update_6101() {
variable_set('filefield_paths_schema_version', 6101);
return array();
}
function filefield_paths_update_6102() {
variable_set('filefield_paths_schema_version', 6102);
$fields = module_invoke_all('filefield_paths_field_settings');
$result = db_query("SELECT * FROM {filefield_paths}");
while ($data = db_fetch_object($result)) {
foreach ($fields as $field) {
$cols[] = $field['sql'] ." = '%s'";
$val = unserialize($data->$field['sql']);
$val = str_replace('[filefield_paths-name]', '[filefield-onlyname]', $val);
$val = str_replace('[filefield_paths-ext]', '[filefield-extension]', $val);
$vals[] = serialize($val);
}
db_query(
"UPDATE {filefield_paths} SET ". implode(', ', $cols) ." WHERE type = '%s' AND field = '%s'",
array_merge($vals, array($data->type, $data->field))
);
}
return array();
}
function filefield_paths_update_6103() {
$schema_version = variable_get('filefield_paths_schema_version', '6102');
variable_set('filefield_paths_schema_version', 6103);
if ($schema_version < 5103 || ($schema_version > 6000 && $schema_version < 6103)) {
db_add_field($ret = array(), 'files', 'origname', array('description' => 'Original name of the file.', 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
$result = db_query("SELECT fid, filename FROM {files}");
while ($file = db_fetch_object($result)) {
db_query("UPDATE {files} SET origname = '%s' WHERE fid = %d", $file->filename, $file->fid);
}
}
return array();
}