2
// $Id: views_export.module,v 1.4 2009/04/08 16:11:51 merlinofchaos Exp $
5
* @file views_export.module
7
* Provides functionality to export multiple items at once to make it easy to
8
* dump a set of views into code.
12
* Implementation of hook_menu().
14
function views_export_menu() {
16
$items['admin/build/views/tools/export'] = array(
17
'title' => 'Bulk export',
18
'access arguments' => array('use views exporter'),
19
'page callback' => 'views_export_export',
20
'type' => MENU_LOCAL_TASK,
23
$items['admin/build/views/tools/export/results'] = array(
24
'access arguments' => array('use views exporter'),
25
'page callback' => 'views_export_export',
26
'type' => MENU_CALLBACK,
32
function views_export_theme() {
34
'views_export_export_form' => array(
35
'args' => array('form' => NULL),
41
* Implementation of hook_perm().
43
function views_export_perm() {
44
return array('use views exporter');
48
* Page callback to export views in bulk.
50
function views_export_export() {
52
if (!empty($_GET['tags'])) {
53
$tags = explode(',', $_GET['tags']);
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]);
65
'no_redirect' => TRUE,
66
'exportables' => $exportables,
70
$output = drupal_build_form('views_export_export_form', $form_state);
72
$output = $form_state['output'];
77
return t('There are no views to be exported at this time.');
82
* Form to choose a group of views to export.
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'];
90
$form['modules']['#tree'] = TRUE;
91
$form['modules'][$module] = array(
92
'#type' => 'checkboxes',
93
'#options' => $options,
94
'#default_value' => array(),
99
foreach (views_get_all_views() as $name => $view) {
100
if (!empty($view->tag)) {
101
$tags[$view->tag] = $view->tag;
107
$form['tags'] = array(
109
'#title' => t('Show only these tags'),
111
'#default_value' => $form_state['tags'],
115
$form['apply'] = array(
117
'#value' => t('Apply'),
118
'#submit' => array('views_export_export_form_apply'),
121
$form['name'] = array(
122
'#type' => 'textfield',
123
'#title' => t('Module name'),
124
'#description' => t('Enter the module name to export code to.'),
127
$form['submit'] = array(
129
'#value' => t('Export'),
132
$form['#action'] = url('admin/build/views/tools/export/results');
133
$form['#redirect'] = FALSE;
134
$form['#exportables'] = $form_state['exportables'];
138
function theme_views_export_export_form($form) {
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">';
146
foreach ($exportables as $module => $views) {
147
$header = array(theme('table_select_header_cell'), $files[$module]->info['name'], t('Tag'), t('Description'));
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>');
154
$output .= '<div class="export-container">';
155
$output .= theme('table', $header, $rows);
156
$output .= "</div>\n";
159
drupal_add_css(drupal_get_path('module', 'views_export') . '/views_export.css');
160
$output .= drupal_render($form);
164
function views_export_export_form_apply(&$form, &$form_state) {
165
$tags = $form_state['values']['tags'];
167
drupal_goto('admin/build/views/tools/export', array('tags' => implode(',', $tags)));
170
drupal_goto('admin/build/views/tools/export');
174
function views_export_export_form_submit(&$form, &$form_state) {
176
if (empty($form_state['values']['name'])) {
177
$form_state['values']['name'] = 'foo';
180
foreach ($form_state['values']['modules'] as $module => $views) {
181
$views = array_filter($views);
183
$code .= module_invoke($module, 'views_exportables', 'export', $views, $form_state['values']['name']) . "\n\n";
187
$lines = substr_count($code, "\n");
189
$types = system_elements();
192
$api .= " * Implementation of hook_views_api().\n";
194
$api .= "function @module_views_api() {\n";
195
$api .= " return array(\n";
196
$api .= " 'api' => 2,\n";
197
$api .= " 'path' => drupal_get_path('module', '@module'),\n";
198
$api .= " //'path' => drupal_get_path('module', '@module') . '/includes',\n";
202
$api = strtr($api, array('@module' => check_plain($form_state['values']['name'])));
204
$element_api = array(
205
'#title' => t('Put this in @module.module in your modules/@module directory', array('@module' => $form_state['values']['name'])),
206
'#type' => 'textarea',
207
'#id' => 'export-api-textarea',
208
'#name' => 'export-api-textarea',
209
'#attributes' => array(),
213
'#parents' => array('dummy'),
214
'#required' => FALSE,
215
) + $types['textarea'];
217
$element_hook = array(
218
'#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'])),
219
'#type' => 'textarea',
220
'#id' => 'export-textarea',
221
'#name' => 'export-textarea',
222
'#attributes' => array(),
223
'#rows' => min($lines, 150),
225
'#parents' => array('dummy'),
226
'#required' => FALSE,
227
) + $types['textarea'];
230
$form_state['output'] = theme('textarea', $element_api);
231
$form_state['output'] .= theme('textarea', $element_hook);