Mercurial > hg > rr-repo
comparison sites/all/modules/ctools/bulk_export/bulk_export.module @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
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 */ | |
198 function bulk_export_export_form($form, &$form_state) { | |
199 | |
200 $files = system_rebuild_module_data(); | |
201 | |
202 $form['additional_settings'] = array( | |
203 '#type' => 'vertical_tabs', | |
204 ); | |
205 | |
206 $options = $tables = array(); | |
207 foreach ($form_state['exportables'] as $table => $list) { | |
208 if (empty($list)) { | |
209 continue; | |
210 } | |
211 | |
212 foreach ($list as $id => $title) { | |
213 $options[$table][$id] = array($title); | |
214 $options[$table][$id]['#attributes'] = array('class' => array('bulk-selection')); | |
215 } | |
216 | |
217 $module = $form_state['export_tables'][$table]; | |
218 $header = array($table); | |
219 $module_name = $files[$module]->info['name']; | |
220 $tables[] = $table; | |
221 | |
222 if (!isset($form[$module_name])) { | |
223 $form[$files[$module]->info['name']] = array( | |
224 '#type' => 'fieldset', | |
225 '#group' => 'additional_settings', | |
226 '#title' => $module_name, | |
227 ); | |
228 } | |
229 | |
230 $form[$module_name]['tables'][$table] = array( | |
231 '#prefix' => '<div class="export-container">', | |
232 '#suffix' => '</div>', | |
233 '#type' => 'tableselect', | |
234 '#header' => $header, | |
235 '#options' => $options[$table], | |
236 ); | |
237 } | |
238 | |
239 $form['tables'] = array( | |
240 '#type' => 'value', | |
241 '#value' => $tables, | |
242 ); | |
243 | |
244 $form['name'] = array( | |
245 '#type' => 'textfield', | |
246 '#title' => t('Module name'), | |
247 '#description' => t('Enter the module name to export code to.'), | |
248 ); | |
249 | |
250 $form['submit'] = array( | |
251 '#type' => 'submit', | |
252 '#value' => t('Export'), | |
253 ); | |
254 | |
255 $form['#action'] = url('admin/structure/bulk-export/results'); | |
256 $form['#attached']['css'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.css'; | |
257 $form['#attached']['js'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.js'; | |
258 return $form; | |
259 } | |
260 | |
261 /** | |
262 * Process the bulk export submit form and make the results available. | |
263 */ | |
264 function bulk_export_export_form_submit($form, &$form_state) { | |
265 $code = array(); | |
266 $name = empty($form_state['values']['name']) ? 'foo' : $form_state['values']['name']; | |
267 $tables = $form_state['values']['tables']; | |
268 | |
269 foreach ($tables as $table) { | |
270 $names = array_keys(array_filter($form_state['values'][$table])); | |
271 if ($names) { | |
272 natcasesort($names); | |
273 ctools_export_to_hook_code($code, $table, $names, $name); | |
274 } | |
275 } | |
276 | |
277 $form_state['code'] = $code; | |
278 $form_state['module'] = $name; | |
279 } |