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