summaryrefslogtreecommitdiffstats
path: root/views_export/views_export.module
blob: 509c8913c2d562296f6a320e4a85f93751bf02c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?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.
 */

/**
 * Implementation of hook_menu().
 */
function views_export_menu() {
  $items = array();
  $items['admin/build/views/tools/export'] = array(
    'title' => 'Bulk export',
    'access arguments' => array('use views exporter'),
    'page callback' => 'views_export_export',
    'type' => MENU_LOCAL_TASK,
  );

  $items['admin/build/views/tools/export/results'] = array(
    'access arguments' => array('use views exporter'),
    'page callback' => 'views_export_export',
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function views_export_theme() {
  return array(
    'views_export_export_form' => array(
      'args' => array('form' => NULL),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function views_export_perm() {
  return array('use views exporter');
}

/**
 * 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,
    );

    $output = drupal_build_form('views_export_export_form', $form_state);
    if (!$output) {
      $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_state) {
  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/build/views/tools/export/results');
  $form['#redirect'] = FALSE;
  $form['#exportables'] = $form_state['exportables'];
  return $form;
}

function theme_views_export_export_form($form) {
  $output = '';
  $files = module_rebuild_cache();
  $exportables = $form['#exportables'];
  $output .= drupal_render($form['tags']);
  $output .= drupal_render($form['apply']);
  $output .= '<div class="clear-block">';

  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', $header, $rows);
    $output .= "</div>\n";
  }
  $output .= '</div>';
  drupal_add_css(drupal_get_path('module', 'views_export') . '/views_export.css');
  $output .= drupal_render($form);
  return $output;
}

function views_export_export_form_apply(&$form, &$form_state) {
  $tags = $form_state['values']['tags'];
  if ($tags) {
    drupal_goto('admin/build/views/tools/export', array('tags' => implode(',', $tags)));
  }
  else {
    drupal_goto('admin/build/views/tools/export');
  }
}

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");

  $types = system_elements();

  $info  = "; \$Id" . ": $\n"; // The break in the string prevents CVS.
  $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 .= " * Implementation of hook_views_api().\n";
  $api .= " */\n";
  $api .= "function @module_views_api() {\n";
  $api .= "  return array(\n";
  $api .= "    'api' => 2,\n";
  $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', $element_info);
  $form_state['output'] .= theme('textarea', $element_api);
  $form_state['output'] .= theme('textarea', $element_hook);
}