Newer
Older
The Great Git Migration
committed
<?php
/**
* @file views_export.module
*
* Provides functionality to export multiple items at once to make it easy to
* dump a set of views into code.
*/
/**
* Implements hook_menu().
The Great Git Migration
committed
*/
function views_export_menu() {
$items = array();
$items['admin/structure/views/tools/export'] = array(
The Great Git Migration
committed
'title' => 'Bulk export',
'access arguments' => array('use views exporter'),
'page callback' => 'views_export_export',
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/views/tools/export/results'] = array(
The Great Git Migration
committed
'access arguments' => array('use views exporter'),
'page callback' => 'views_export_export',
'type' => MENU_LOCAL_TASK,
The Great Git Migration
committed
);
return $items;
}
/**
* Implements hook_theme().
*/
The Great Git Migration
committed
function views_export_theme() {
return array(
'views_export_export_form' => array(
The Great Git Migration
committed
),
);
}
/**
* Implements hook_perm().
The Great Git Migration
committed
*/
function views_export_permission() {
return array(
'use views exporter' => array(
'title' => t('Use Views exporter'),
'description' => t('Use the Views exporter.'),
),
);
The Great Git Migration
committed
}
/**
* Page callback to export views in bulk.
*/
function views_export_export() {
$tags = array();
if (!empty($_GET['tags'])) {
$tags = explode(',', $_GET['tags']);
}
$exportables = array();
foreach (module_implements('views_exportables') as $module) {
$function = $module . '_views_exportables';
$exportables[$module] = $function('list', $tags);
asort($exportables[$module]);
}
if ($exportables) {
$form_state = array(
'no_redirect' => TRUE,
'exportables' => $exportables,
'tags' => $tags,
'build_info' => array(
'args' => array(),
),
The Great Git Migration
committed
);
$form = drupal_build_form('views_export_export_form', $form_state);
$output = drupal_render($form);
if ($form_state['submitted']) {
The Great Git Migration
committed
$output = $form_state['output'];
}
return $output;
}
else {
return t('There are no views to be exported at this time.');
}
}
/**
* Form to choose a group of views to export.
*/
function views_export_export_form($form, &$form_state) {
The Great Git Migration
committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
foreach ($form_state['exportables'] as $module => $views) {
foreach ($views as $name => $data) {
$options[$name] = $data['name'];
}
$form['modules']['#tree'] = TRUE;
$form['modules'][$module] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => array(),
);
}
$tags = array();
foreach (views_get_all_views() as $name => $view) {
if (!empty($view->tag)) {
$tags[$view->tag] = $view->tag;
}
}
asort($tags);
$form['tags'] = array(
'#type' => 'select',
'#title' => t('Show only these tags'),
'#options' => $tags,
'#default_value' => $form_state['tags'],
'#multiple' => TRUE,
);
$form['apply'] = array(
'#type' => 'submit',
'#value' => t('Apply'),
'#submit' => array('views_export_export_form_apply'),
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Module name'),
'#description' => t('Enter the module name to export code to.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Export'),
);
$form['#action'] = url('admin/structure/views/tools/export/results');
The Great Git Migration
committed
$form['#redirect'] = FALSE;
if (isset($form_state['exportables'])) {
$form['#exportables'] = $form_state['exportables'];
}
The Great Git Migration
committed
return $form;
}
function theme_views_export_export_form(&$vars) {
$form = $vars['form'];
The Great Git Migration
committed
$output = '';
// Get current list of modules.
$files = system_rebuild_module_data();
The Great Git Migration
committed
$exportables = $form['#exportables'];
$output .= drupal_render($form['tags']);
$output .= drupal_render($form['apply']);
$output .= '<div class="clearfix">';
The Great Git Migration
committed
foreach ($exportables as $module => $views) {
$header = array(theme('table_select_header_cell'), $files[$module]->info['name'], t('Tag'), t('Description'));
$rows = array();
foreach ($views as $name => $view) {
$title = $form['modules'][$module][$name]['#title'];
unset($form['modules'][$module][$name]['#title']);
$rows[] = array(drupal_render($form['modules'][$module][$name]), $title, $view['tag'], '<div class="description">' . $view['desc'] . '</div>');
}
$output .= '<div class="export-container">';
$output .= theme('table', array(
'header' => $header,
'rows' => $rows
));
The Great Git Migration
committed
$output .= "</div>\n";
}
$output .= '</div>';
$form['#attached']['css'] = array(drupal_add_css(drupal_get_path('module', 'views_export') . '/views_export.css'));
The Great Git Migration
committed
return $output;
}
function views_export_export_form_apply(&$form, &$form_state) {
$tags = $form_state['values']['tags'];
if ($tags) {
drupal_goto('admin/structure/views/tools/export', array('tags' => implode(',', $tags)));
The Great Git Migration
committed
}
else {
drupal_goto('admin/structure/views/tools/export');
The Great Git Migration
committed
}
}
function views_export_export_form_submit(&$form, &$form_state) {
$code = '';
if (empty($form_state['values']['name'])) {
$form_state['values']['name'] = 'foo';
}
foreach ($form_state['values']['modules'] as $module => $views) {
$views = array_filter($views);
asort($views);
if ($views) {
$code .= module_invoke($module, 'views_exportables', 'export', $views, $form_state['values']['name']) . "\n\n";
}
}
$lines = substr_count($code, "\n");
The Great Git Migration
committed
$info = '; $Id$'."\n";
The Great Git Migration
committed
$info .= "\n";
$info .= strtr("name = @module Export Module\n", array('@module' => $form_state['values']['name']));
$info .= strtr("description = Exports some views of @module\n", array('@module' => $form_state['values']['name']));
$info .= "dependencies[] = views\n";
$info .= "core = 6.x\n";
$element_info = array(
'#title' => t('Put this in @module.info in your modules/@module directory', array('@module' => $form_state['values']['name'])),
'#type' => 'textarea',
'#id' => 'export-info-textarea',
'#name' => 'export-info-textarea',
'#attributes' => array(),
'#rows' => 9,
'#cols' => 60,
'#value' => $info,
'#parents' => array('dummy'),
'#required' => FALSE,
) + $types['textarea'];
$api = "/**\n";
$api .= " * Implements hook_views_api().\n";
The Great Git Migration
committed
$api .= " */\n";
$api .= "function @module_views_api() {\n";
$api .= " return array(\n";
$api .= " 'api' => '" . views_api_version() . "',\n";
The Great Git Migration
committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
$api .= " 'path' => drupal_get_path('module', '@module'),\n";
$api .= " //'path' => drupal_get_path('module', '@module') . '/includes',\n";
$api .= " );\n";
$api .= "}";
$api = strtr($api, array('@module' => check_plain($form_state['values']['name'])));
$element_api = array(
'#title' => t('Put this in @module.module in your modules/@module directory', array('@module' => $form_state['values']['name'])),
'#type' => 'textarea',
'#id' => 'export-api-textarea',
'#name' => 'export-api-textarea',
'#attributes' => array( 'dir' => 'ltr' ),
'#rows' => 9,
'#cols' => 60,
'#value' => $api,
'#parents' => array('dummy'),
'#required' => FALSE,
) + $types['textarea'];
$element_hook = array(
'#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'])),
'#type' => 'textarea',
'#id' => 'export-textarea',
'#name' => 'export-textarea',
'#attributes' => array( 'dir' => 'ltr' ),
'#rows' => min($lines, 150),
'#value' => $code,
'#parents' => array('dummy'),
'#required' => FALSE,
) + $types['textarea'];
$form_state['output'] = theme('textarea', array(
'element' => $element_info));
$form_state['output'] .= theme('textarea', array(
'element' => $element_api));
$form_state['output'] .= theme('textarea', array(
'element' => $element_hook));