| 1 |
<?php
|
| 2 |
// $Id: views_export.module,v 1.8 2009/07/26 15:07:26 merlinofchaos Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file views_export.module
|
| 6 |
*
|
| 7 |
* Provides functionality to export multiple items at once to make it easy to
|
| 8 |
* dump a set of views into code.
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_menu().
|
| 13 |
*/
|
| 14 |
function views_export_menu() {
|
| 15 |
$items = array();
|
| 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,
|
| 21 |
);
|
| 22 |
|
| 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,
|
| 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 = '';
|
| 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"; // The break in the string prevents CVS.
|
| 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 = "/**\n";
|
| 214 |
$api .= " * Implementation of hook_views_api().\n";
|
| 215 |
$api .= " */\n";
|
| 216 |
$api .= "function @module_views_api() {\n";
|
| 217 |
$api .= " return array(\n";
|
| 218 |
$api .= " 'api' => 2,\n";
|
| 219 |
$api .= " 'path' => drupal_get_path('module', '@module'),\n";
|
| 220 |
$api .= " //'path' => drupal_get_path('module', '@module') . '/includes',\n";
|
| 221 |
$api .= " );\n";
|
| 222 |
$api .= "}";
|
| 223 |
|
| 224 |
$api = strtr($api, array('@module' => check_plain($form_state['values']['name'])));
|
| 225 |
|
| 226 |
$element_api = array(
|
| 227 |
'#title' => t('Put this in @module.module in your modules/@module directory', array('@module' => $form_state['values']['name'])),
|
| 228 |
'#type' => 'textarea',
|
| 229 |
'#id' => 'export-api-textarea',
|
| 230 |
'#name' => 'export-api-textarea',
|
| 231 |
'#attributes' => array( 'dir' => 'ltr' ),
|
| 232 |
'#rows' => 9,
|
| 233 |
'#cols' => 60,
|
| 234 |
'#value' => $api,
|
| 235 |
'#parents' => array('dummy'),
|
| 236 |
'#required' => FALSE,
|
| 237 |
) + $types['textarea'];
|
| 238 |
|
| 239 |
$element_hook = array(
|
| 240 |
'#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'])),
|
| 241 |
'#type' => 'textarea',
|
| 242 |
'#id' => 'export-textarea',
|
| 243 |
'#name' => 'export-textarea',
|
| 244 |
'#attributes' => array( 'dir' => 'ltr' ),
|
| 245 |
'#rows' => min($lines, 150),
|
| 246 |
'#value' => $code,
|
| 247 |
'#parents' => array('dummy'),
|
| 248 |
'#required' => FALSE,
|
| 249 |
) + $types['textarea'];
|
| 250 |
|
| 251 |
|
| 252 |
$form_state['output'] = theme('textarea', $element_info);
|
| 253 |
$form_state['output'] .= theme('textarea', $element_api);
|
| 254 |
$form_state['output'] .= theme('textarea', $element_hook);
|
| 255 |
}
|
| 256 |
|