~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to modules/views/views_export/views_export.module

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: views_export.module,v 1.4 2009/04/08 16:11:51 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
    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
  $api = "/**\n";
 
192
  $api .= " * Implementation of hook_views_api().\n";
 
193
  $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";
 
199
  $api .= "  );\n";
 
200
  $api .= "}";
 
201
 
 
202
  $api = strtr($api, array('@module' => check_plain($form_state['values']['name'])));
 
203
 
 
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(),
 
210
    '#rows' => 9,
 
211
    '#cols' => 60,
 
212
    '#value' => $api,
 
213
    '#parents' => array('dummy'),
 
214
    '#required' => FALSE,
 
215
  ) + $types['textarea'];
 
216
 
 
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),
 
224
    '#value' => $code,
 
225
    '#parents' => array('dummy'),
 
226
    '#required' => FALSE,
 
227
  ) + $types['textarea'];
 
228
 
 
229
 
 
230
  $form_state['output'] = theme('textarea', $element_api);
 
231
  $form_state['output'] .= theme('textarea', $element_hook);
 
232
}
 
233