1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Perform bulk exports.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_permission().
|
10
|
*/
|
11
|
function bulk_export_permission() {
|
12
|
return array(
|
13
|
'use bulk exporter' => array(
|
14
|
'title' => t('Access Bulk Exporter'),
|
15
|
'description' => t('Export various system objects into code.'),
|
16
|
),
|
17
|
);
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Implements hook_menu().
|
22
|
*/
|
23
|
function bulk_export_menu() {
|
24
|
$items['admin/structure/bulk-export'] = array(
|
25
|
'title' => 'Bulk Exporter',
|
26
|
'description' => 'Bulk-export multiple CTools-handled data objects to code.',
|
27
|
'access arguments' => array('use bulk exporter'),
|
28
|
'page callback' => 'bulk_export_export',
|
29
|
);
|
30
|
$items['admin/structure/bulk-export/results'] = array(
|
31
|
'access arguments' => array('use bulk exporter'),
|
32
|
'page callback' => 'bulk_export_export',
|
33
|
'type' => MENU_CALLBACK,
|
34
|
);
|
35
|
return $items;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* FAPI gateway to the bulk exporter.
|
40
|
*
|
41
|
* @param $cli
|
42
|
* Whether this function is called from command line.
|
43
|
* @param $options
|
44
|
* A collection of options, only passed in by drush_ctools_export().
|
45
|
*/
|
46
|
function bulk_export_export($cli = FALSE, $options = array()) {
|
47
|
ctools_include('export');
|
48
|
$form = array();
|
49
|
$schemas = ctools_export_get_schemas(TRUE);
|
50
|
$exportables = $export_tables = array();
|
51
|
|
52
|
foreach ($schemas as $table => $schema) {
|
53
|
if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) {
|
54
|
$exportables[$table] = $schema['export']['list callback']();
|
55
|
}
|
56
|
else {
|
57
|
$exportables[$table] = ctools_export_default_list($table, $schema);
|
58
|
}
|
59
|
natcasesort($exportables[$table]);
|
60
|
$export_tables[$table] = $schema['module'];
|
61
|
}
|
62
|
if ($exportables) {
|
63
|
$form_state = array(
|
64
|
're_render' => FALSE,
|
65
|
'no_redirect' => TRUE,
|
66
|
'exportables' => $exportables,
|
67
|
'export_tables' => $export_tables,
|
68
|
'name' => '',
|
69
|
'code' => '',
|
70
|
'module' => '',
|
71
|
);
|
72
|
|
73
|
// If called from drush_ctools_export, get the module name and
|
74
|
// select all exportables and call the submit function directly.
|
75
|
if ($cli) {
|
76
|
$module_name = $options['name'];
|
77
|
$form_state['values']['name'] = $module_name;
|
78
|
if (isset($options['selections'])) {
|
79
|
$exportables = $options['selections'];
|
80
|
}
|
81
|
$form_state['values']['tables'] = array();
|
82
|
foreach ($exportables as $table => $names) {
|
83
|
if (!empty($names)) {
|
84
|
$form_state['values']['tables'][] = $table;
|
85
|
$form_state['values'][$table] = array();
|
86
|
foreach ($names as $name => $title) {
|
87
|
$form_state['values'][$table][$name] = $name;
|
88
|
}
|
89
|
}
|
90
|
}
|
91
|
$output = bulk_export_export_form_submit($form, $form_state);
|
92
|
}
|
93
|
else {
|
94
|
$output = drupal_build_form('bulk_export_export_form', $form_state);
|
95
|
$module_name = $form_state['module'];
|
96
|
}
|
97
|
|
98
|
if (!empty($form_state['submitted']) || $cli) {
|
99
|
drupal_set_title(t('Bulk export results'));
|
100
|
$output = '';
|
101
|
$module_code = '';
|
102
|
$api_code = array();
|
103
|
$dependencies = $file_data = array();
|
104
|
foreach ($form_state['code'] as $module => $api_info) {
|
105
|
if ($module == 'general') {
|
106
|
$module_code .= $api_info;
|
107
|
}
|
108
|
else {
|
109
|
foreach ($api_info as $api => $info) {
|
110
|
$api_hook = ctools_plugin_api_get_hook($module, $api);
|
111
|
if (empty($api_code[$api_hook])) {
|
112
|
$api_code[$api_hook] = '';
|
113
|
}
|
114
|
$api_code[$api_hook] .= " if (\$module == '$module' && \$api == '$api') {\n";
|
115
|
$api_code[$api_hook] .= " return array('version' => $info[version]);\n";
|
116
|
$api_code[$api_hook] .= " }\n";
|
117
|
$dependencies[$module] = TRUE;
|
118
|
|
119
|
$file = $module_name . '.' . $api . '.inc';
|
120
|
$code = "<?php\n\n";
|
121
|
$code .= "/**\n";
|
122
|
$code .= " * @file\n";
|
123
|
$code .= " * Bulk export of $api objects generated by Bulk export module.\n";
|
124
|
$code .= " */\n\n";
|
125
|
$code .= $info['code'];
|
126
|
if ($cli) {
|
127
|
$file_data[$file] = $code;
|
128
|
}
|
129
|
else {
|
130
|
$export_form = drupal_get_form('ctools_export_form', $code, t('Place this in @file', array('@file' => $file)));
|
131
|
$output .= drupal_render($export_form);
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
|
137
|
// Add hook_ctools_plugin_api at the top of the module code, if there is any.
|
138
|
if ($api_code) {
|
139
|
foreach ($api_code as $api_hook => $text) {
|
140
|
$api = "\n/**\n";
|
141
|
$api .= " * Implements hook_$api_hook().\n";
|
142
|
$api .= " */\n";
|
143
|
$api .= "function {$module_name}_$api_hook(\$module, \$api) {\n";
|
144
|
$api .= $text;
|
145
|
$api .= "}\n";
|
146
|
$module_code = $api . $module_code;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
if ($module_code) {
|
151
|
$module = "<?php\n\n";
|
152
|
$module .= "/**\n";
|
153
|
$module .= " * @file\n";
|
154
|
$module .= " * Bulk export of objects generated by Bulk export module.\n";
|
155
|
$module .= " */\n";
|
156
|
$module .= $module_code;
|
157
|
if ($cli) {
|
158
|
$file_data[$module_name . '.module'] = $module;
|
159
|
}
|
160
|
else {
|
161
|
$export_form = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array('@file' => $form_state['module'] . '.module')));
|
162
|
$output = drupal_render($export_form) . $output;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
$info = strtr("name = @module export module\n", array('@module' => $form_state['module']));
|
167
|
$info .= strtr("description = Export objects from CTools\n", array('@module' => $form_state['values']['name']));
|
168
|
foreach ($dependencies as $module => $junk) {
|
169
|
$info .= "dependencies[] = $module\n";
|
170
|
}
|
171
|
$info .= "package = Chaos tool suite\n";
|
172
|
$info .= "core = 7.x\n";
|
173
|
if ($cli) {
|
174
|
$file_data[$module_name . '.info'] = $info;
|
175
|
}
|
176
|
else {
|
177
|
$export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info')));
|
178
|
$output = drupal_render($export_form) . $output;
|
179
|
}
|
180
|
}
|
181
|
|
182
|
if ($cli) {
|
183
|
return $file_data;
|
184
|
}
|
185
|
else {
|
186
|
return $output;
|
187
|
}
|
188
|
}
|
189
|
else {
|
190
|
return t('There are no objects to be exported at this time.');
|
191
|
}
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* FAPI definition for the bulk exporter form.
|
196
|
*/
|
197
|
function bulk_export_export_form($form, &$form_state) {
|
198
|
|
199
|
$files = system_rebuild_module_data();
|
200
|
|
201
|
$form['additional_settings'] = array(
|
202
|
'#type' => 'vertical_tabs',
|
203
|
);
|
204
|
|
205
|
$options = $tables = array();
|
206
|
foreach ($form_state['exportables'] as $table => $list) {
|
207
|
if (empty($list)) {
|
208
|
continue;
|
209
|
}
|
210
|
|
211
|
foreach ($list as $id => $title) {
|
212
|
$options[$table][$id] = array($title);
|
213
|
$options[$table][$id]['#attributes'] = array('class' => array('bulk-selection'));
|
214
|
}
|
215
|
|
216
|
$module = $form_state['export_tables'][$table];
|
217
|
$header = array($table);
|
218
|
$module_name = $files[$module]->info['name'];
|
219
|
$tables[] = $table;
|
220
|
|
221
|
if (!isset($form[$module_name])) {
|
222
|
$form[$files[$module]->info['name']] = array(
|
223
|
'#type' => 'fieldset',
|
224
|
'#group' => 'additional_settings',
|
225
|
'#title' => $module_name,
|
226
|
);
|
227
|
}
|
228
|
|
229
|
$form[$module_name]['tables'][$table] = array(
|
230
|
'#prefix' => '<div class="export-container">',
|
231
|
'#suffix' => '</div>',
|
232
|
'#type' => 'tableselect',
|
233
|
'#header' => $header,
|
234
|
'#options' => $options[$table],
|
235
|
);
|
236
|
}
|
237
|
|
238
|
$form['tables'] = array(
|
239
|
'#type' => 'value',
|
240
|
'#value' => $tables,
|
241
|
);
|
242
|
|
243
|
$form['name'] = array(
|
244
|
'#type' => 'textfield',
|
245
|
'#title' => t('Module name'),
|
246
|
'#description' => t('Enter the module name to export code to.'),
|
247
|
);
|
248
|
|
249
|
$form['submit'] = array(
|
250
|
'#type' => 'submit',
|
251
|
'#value' => t('Export'),
|
252
|
);
|
253
|
|
254
|
$form['#action'] = url('admin/structure/bulk-export/results');
|
255
|
$form['#attached']['css'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.css';
|
256
|
$form['#attached']['js'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.js';
|
257
|
return $form;
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* Process the bulk export submit form and make the results available.
|
262
|
*/
|
263
|
function bulk_export_export_form_submit($form, &$form_state) {
|
264
|
$code = array();
|
265
|
$name = empty($form_state['values']['name']) ? 'foo' : $form_state['values']['name'];
|
266
|
$tables = $form_state['values']['tables'];
|
267
|
|
268
|
foreach ($tables as $table) {
|
269
|
$names = array_keys(array_filter($form_state['values'][$table]));
|
270
|
if ($names) {
|
271
|
natcasesort($names);
|
272
|
ctools_export_to_hook_code($code, $table, $names, $name);
|
273
|
}
|
274
|
}
|
275
|
|
276
|
$form_state['code'] = $code;
|
277
|
$form_state['module'] = $name;
|
278
|
}
|