1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file views_export.module |
---|
5 | * |
---|
6 | * Provides functionality to export multiple items at once to make it easy to |
---|
7 | * dump a set of views into code. |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Implementation of hook_menu(). |
---|
12 | */ |
---|
13 | function views_export_menu() { |
---|
14 | $items = array(); |
---|
15 | $items['admin/build/views/tools/export'] = array( |
---|
16 | 'title' => 'Bulk export', |
---|
17 | 'access arguments' => array('use views exporter'), |
---|
18 | 'page callback' => 'views_export_export', |
---|
19 | 'type' => MENU_LOCAL_TASK, |
---|
20 | ); |
---|
21 | |
---|
22 | $items['admin/build/views/tools/export/results'] = array( |
---|
23 | 'title' => 'Bulk export results', |
---|
24 | 'access arguments' => array('use views exporter'), |
---|
25 | 'page callback' => 'views_export_export', |
---|
26 | 'type' => MENU_LOCAL_TASK, |
---|
27 | ); |
---|
28 | |
---|
29 | return $items; |
---|
30 | } |
---|
31 | |
---|
32 | function views_export_theme() { |
---|
33 | return array( |
---|
34 | 'views_export_export_form' => array( |
---|
35 | 'args' => array('form' => NULL), |
---|
36 | ), |
---|
37 | ); |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Implementation of hook_perm(). |
---|
42 | */ |
---|
43 | function views_export_perm() { |
---|
44 | return array('use views exporter'); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Page callback to export views in bulk. |
---|
49 | */ |
---|
50 | function views_export_export() { |
---|
51 | $tags = array(); |
---|
52 | if (!empty($_GET['tags'])) { |
---|
53 | $tags = explode(',', $_GET['tags']); |
---|
54 | } |
---|
55 | |
---|
56 | $exportables = array(); |
---|
57 | foreach (module_implements('views_exportables') as $module) { |
---|
58 | $function = $module . '_views_exportables'; |
---|
59 | $exportables[$module] = $function('list', $tags); |
---|
60 | asort($exportables[$module]); |
---|
61 | } |
---|
62 | |
---|
63 | if ($exportables) { |
---|
64 | $form_state = array( |
---|
65 | 'no_redirect' => TRUE, |
---|
66 | 'exportables' => $exportables, |
---|
67 | 'tags' => $tags, |
---|
68 | ); |
---|
69 | |
---|
70 | $output = drupal_build_form('views_export_export_form', $form_state); |
---|
71 | if (!$output) { |
---|
72 | $output = $form_state['output']; |
---|
73 | } |
---|
74 | return $output; |
---|
75 | } |
---|
76 | else { |
---|
77 | return t('There are no views to be exported at this time.'); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Form to choose a group of views to export. |
---|
83 | */ |
---|
84 | function views_export_export_form(&$form_state) { |
---|
85 | foreach ($form_state['exportables'] as $module => $views) { |
---|
86 | foreach ($views as $name => $data) { |
---|
87 | $options[$name] = $data['name']; |
---|
88 | } |
---|
89 | |
---|
90 | $form['modules']['#tree'] = TRUE; |
---|
91 | $form['modules'][$module] = array( |
---|
92 | '#type' => 'checkboxes', |
---|
93 | '#options' => $options, |
---|
94 | '#default_value' => array(), |
---|
95 | ); |
---|
96 | } |
---|
97 | |
---|
98 | $tags = array(); |
---|
99 | foreach (views_get_all_views() as $name => $view) { |
---|
100 | if (!empty($view->tag)) { |
---|
101 | $tags[$view->tag] = $view->tag; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | asort($tags); |
---|
106 | |
---|
107 | $form['tags'] = array( |
---|
108 | '#type' => 'select', |
---|
109 | '#title' => t('Show only these tags'), |
---|
110 | '#options' => $tags, |
---|
111 | '#default_value' => $form_state['tags'], |
---|
112 | '#multiple' => TRUE, |
---|
113 | ); |
---|
114 | |
---|
115 | $form['apply'] = array( |
---|
116 | '#type' => 'submit', |
---|
117 | '#value' => t('Apply'), |
---|
118 | '#submit' => array('views_export_export_form_apply'), |
---|
119 | ); |
---|
120 | |
---|
121 | $form['name'] = array( |
---|
122 | '#type' => 'textfield', |
---|
123 | '#title' => t('Module name'), |
---|
124 | '#description' => t('Enter the module name to export code to.'), |
---|
125 | ); |
---|
126 | |
---|
127 | $form['submit'] = array( |
---|
128 | '#type' => 'submit', |
---|
129 | '#value' => t('Export'), |
---|
130 | ); |
---|
131 | |
---|
132 | $form['#action'] = url('admin/build/views/tools/export/results'); |
---|
133 | $form['#redirect'] = FALSE; |
---|
134 | $form['#exportables'] = $form_state['exportables']; |
---|
135 | return $form; |
---|
136 | } |
---|
137 | |
---|
138 | function theme_views_export_export_form($form) { |
---|
139 | $output = ''; |
---|
140 | $files = module_rebuild_cache(); |
---|
141 | $exportables = $form['#exportables']; |
---|
142 | $output .= drupal_render($form['tags']); |
---|
143 | $output .= drupal_render($form['apply']); |
---|
144 | $output .= '<div class="clear-block">'; |
---|
145 | |
---|
146 | foreach ($exportables as $module => $views) { |
---|
147 | $header = array(theme('table_select_header_cell'), $files[$module]->info['name'], t('Tag'), t('Description')); |
---|
148 | $rows = array(); |
---|
149 | foreach ($views as $name => $view) { |
---|
150 | $title = $form['modules'][$module][$name]['#title']; |
---|
151 | unset($form['modules'][$module][$name]['#title']); |
---|
152 | $rows[] = array(drupal_render($form['modules'][$module][$name]), $title, $view['tag'], '<div class="description">' . $view['desc'] . '</div>'); |
---|
153 | } |
---|
154 | $output .= '<div class="export-container">'; |
---|
155 | $output .= theme('table', $header, $rows); |
---|
156 | $output .= "</div>\n"; |
---|
157 | } |
---|
158 | $output .= '</div>'; |
---|
159 | drupal_add_css(drupal_get_path('module', 'views_export') . '/views_export.css'); |
---|
160 | $output .= drupal_render($form); |
---|
161 | return $output; |
---|
162 | } |
---|
163 | |
---|
164 | function views_export_export_form_apply(&$form, &$form_state) { |
---|
165 | $tags = $form_state['values']['tags']; |
---|
166 | if ($tags) { |
---|
167 | drupal_goto('admin/build/views/tools/export', array('tags' => implode(',', $tags))); |
---|
168 | } |
---|
169 | else { |
---|
170 | drupal_goto('admin/build/views/tools/export'); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | function views_export_export_form_submit(&$form, &$form_state) { |
---|
175 | $code = "<?php \n"; |
---|
176 | if (empty($form_state['values']['name'])) { |
---|
177 | $form_state['values']['name'] = 'foo'; |
---|
178 | } |
---|
179 | |
---|
180 | foreach ($form_state['values']['modules'] as $module => $views) { |
---|
181 | $views = array_filter($views); |
---|
182 | asort($views); |
---|
183 | if ($views) { |
---|
184 | $code .= module_invoke($module, 'views_exportables', 'export', $views, $form_state['values']['name']) . "\n\n"; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | $lines = substr_count($code, "\n"); |
---|
189 | |
---|
190 | $types = system_elements(); |
---|
191 | |
---|
192 | $info = '; $Id$'."\n"; |
---|
193 | $info .= "\n"; |
---|
194 | $info .= strtr("name = @module Export Module\n", array('@module' => $form_state['values']['name'])); |
---|
195 | $info .= strtr("description = Exports some views of @module\n", array('@module' => $form_state['values']['name'])); |
---|
196 | $info .= "dependencies[] = views\n"; |
---|
197 | $info .= "core = 6.x\n"; |
---|
198 | |
---|
199 | |
---|
200 | $element_info = array( |
---|
201 | '#title' => t('Put this in @module.info in your modules/@module directory', array('@module' => $form_state['values']['name'])), |
---|
202 | '#type' => 'textarea', |
---|
203 | '#id' => 'export-info-textarea', |
---|
204 | '#name' => 'export-info-textarea', |
---|
205 | '#attributes' => array(), |
---|
206 | '#rows' => 9, |
---|
207 | '#cols' => 60, |
---|
208 | '#value' => $info, |
---|
209 | '#parents' => array('dummy'), |
---|
210 | '#required' => FALSE, |
---|
211 | ) + $types['textarea']; |
---|
212 | |
---|
213 | $api = "<?php\n"; |
---|
214 | $api .= "/**\n"; |
---|
215 | $api .= " * Implementation of hook_views_api().\n"; |
---|
216 | $api .= " */\n"; |
---|
217 | $api .= "function @module_views_api() {\n"; |
---|
218 | $api .= " return array(\n"; |
---|
219 | $api .= " 'api' => '" . views_api_version() . "',\n"; |
---|
220 | $api .= " 'path' => drupal_get_path('module', '@module'),\n"; |
---|
221 | $api .= " //'path' => drupal_get_path('module', '@module') . '/includes',\n"; |
---|
222 | $api .= " );\n"; |
---|
223 | $api .= "}"; |
---|
224 | |
---|
225 | $api = strtr($api, array('@module' => check_plain($form_state['values']['name']))); |
---|
226 | |
---|
227 | $element_api = array( |
---|
228 | '#title' => t('Put this in @module.module in your modules/@module directory (place a <?php at the top of the file so the webserver knows that it is PHP code)', array('@module' => $form_state['values']['name'])), |
---|
229 | '#type' => 'textarea', |
---|
230 | '#id' => 'export-api-textarea', |
---|
231 | '#name' => 'export-api-textarea', |
---|
232 | '#attributes' => array( 'dir' => 'ltr' ), |
---|
233 | '#rows' => 9, |
---|
234 | '#cols' => 60, |
---|
235 | '#value' => $api, |
---|
236 | '#parents' => array('dummy'), |
---|
237 | '#required' => FALSE, |
---|
238 | ) + $types['textarea']; |
---|
239 | |
---|
240 | $element_hook = array( |
---|
241 | '#title' => t('Put this in @module.views_default.inc in your modules/@module directory or modules/@module/includes directory (place a <?php at the top of the file so the webserver knows that it is PHP code)', array('@module' => $form_state['values']['name'])), |
---|
242 | '#type' => 'textarea', |
---|
243 | '#id' => 'export-textarea', |
---|
244 | '#name' => 'export-textarea', |
---|
245 | '#attributes' => array( 'dir' => 'ltr' ), |
---|
246 | '#rows' => min($lines, 150), |
---|
247 | '#value' => $code, |
---|
248 | '#parents' => array('dummy'), |
---|
249 | '#required' => FALSE, |
---|
250 | ) + $types['textarea']; |
---|
251 | |
---|
252 | |
---|
253 | $form_state['output'] = theme('textarea', $element_info); |
---|
254 | $form_state['output'] .= theme('textarea', $element_api); |
---|
255 | $form_state['output'] .= theme('textarea', $element_hook); |
---|
256 | } |
---|