summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/bulk_export/bulk_export.module
blob: afb15b9e575c229798d28bb9f373ef7006cdb33d (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php

/**
 * @file
 * Perform bulk exports.
 */

/**
 * Implements hook_permission().
 */
function bulk_export_permission() {
  return array(
    'use bulk exporter' => array(
      'title' => t('Access Bulk Exporter'),
      'description' => t('Export various system objects into code.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function bulk_export_menu() {
  $items['admin/structure/bulk-export'] = array(
    'title' => 'Bulk Exporter',
    'description' => 'Bulk-export multiple CTools-handled data objects to code.',
    'access arguments' => array('use bulk exporter'),
    'page callback' => 'bulk_export_export',
  );
  $items['admin/structure/bulk-export/results'] = array(
    'access arguments' => array('use bulk exporter'),
    'page callback' => 'bulk_export_export',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * FAPI gateway to the bulk exporter.
 *
 * @param $cli
 *   Whether this function is called from command line.
 * @param $options
 *   A collection of options, only passed in by drush_ctools_export().
 */
function bulk_export_export($cli = FALSE, $options = array()) {
  ctools_include('export');
  $form = array();
  $schemas = ctools_export_get_schemas(TRUE);
  $exportables = $export_tables = array();

  foreach ($schemas as $table => $schema) {
    if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) {
      $exportables[$table] = $schema['export']['list callback']();
    }
    else {
      $exportables[$table] = ctools_export_default_list($table, $schema);
    }
    natcasesort($exportables[$table]);
    $export_tables[$table] = $schema['module'];
  }
  if ($exportables) {
    $form_state = array(
      're_render' => FALSE,
      'no_redirect' => TRUE,
      'exportables' => $exportables,
      'export_tables' => $export_tables,
      'name' => '',
      'code' => '',
      'module' => '',
    );

    // If called from drush_ctools_export, get the module name and
    // select all exportables and call the submit function directly.
    if ($cli) {
      $module_name = $options['name'];
      $form_state['values']['name'] = $module_name;
      if (isset($options['selections'])) {
        $exportables = $options['selections'];
      }
      $form_state['values']['tables'] = array();
      foreach ($exportables as $table => $names) {
        if (!empty($names)) {
          $form_state['values']['tables'][] = $table;
          $form_state['values'][$table] = array();
          foreach ($names as $name => $title) {
            $form_state['values'][$table][$name] = $name;
          }
        }
      }
      $output = bulk_export_export_form_submit($form, $form_state);
    }
    else {
      $output = drupal_build_form('bulk_export_export_form', $form_state);
      $module_name = $form_state['module'];
    }

    if (!empty($form_state['submitted']) || $cli) {
      drupal_set_title(t('Bulk export results'));
      $output = '';
      $module_code = '';
      $api_code = array();
      $dependencies = $file_data = array();
      foreach ($form_state['code'] as $module => $api_info) {
        if ($module == 'general') {
          $module_code .= $api_info;
        }
        else {
          foreach ($api_info as $api => $info) {
            $api_hook = ctools_plugin_api_get_hook($module, $api);
            if (empty($api_code[$api_hook])) {
              $api_code[$api_hook] = '';
            }
            $api_code[$api_hook] .= "  if (\$module == '$module' && \$api == '$api') {\n";
            $api_code[$api_hook] .= "    return array('version' => $info[version]);\n";
            $api_code[$api_hook] .= "  }\n";
            $dependencies[$module] = TRUE;

            $file = $module_name . '.' . $api . '.inc';
            $code = "<?php\n\n";
            $code .= "/**\n";
            $code .= " * @file\n";
            $code .= " * Bulk export of $api objects generated by Bulk export module.\n";
            $code .= " */\n\n";
            $code .= $info['code'];
            if ($cli) {
              $file_data[$file] = $code;
            }
            else {
              $export_form = drupal_get_form('ctools_export_form', $code, t('Place this in @file', array('@file' => $file)));
              $output .= drupal_render($export_form);
            }
          }
        }
      }

      // Add hook_ctools_plugin_api at the top of the module code, if there is any.
      if ($api_code) {
        foreach ($api_code as $api_hook => $text) {
          $api =  "\n/**\n";
          $api .= " * Implements hook_$api_hook().\n";
          $api .= " */\n";
          $api .= "function {$module_name}_$api_hook(\$module, \$api) {\n";
          $api .= $text;
          $api .= "}\n";
          $module_code = $api . $module_code;
        }
      }

      if ($module_code) {
        $module =  "<?php\n\n";
        $module .= "/**\n";
        $module .= " * @file\n";
        $module .= " * Bulk export of objects generated by Bulk export module.\n";
        $module .= " */\n";
        $module .= $module_code;
        if ($cli) {
          $file_data[$module_name . '.module'] = $module;
        }
        else {
          $export_form = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array('@file' => $form_state['module'] . '.module')));
          $output = drupal_render($export_form) . $output;
        }
      }

      $info = strtr("name = @module export module\n", array('@module' => $form_state['module']));
      $info .= strtr("description = Export objects from CTools\n", array('@module' => $form_state['values']['name']));
      foreach ($dependencies as $module => $junk) {
        $info .= "dependencies[] = $module\n";
      }
      $info .= "package = Chaos tool suite\n";
      $info .= "core = 7.x\n";
      if ($cli) {
        $file_data[$module_name . '.info'] = $info;
      }
      else {
        $export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info')));
        $output = drupal_render($export_form) . $output;
      }
    }

    if ($cli) {
      return $file_data;
    }
    else {
      return $output;
    }
  }
  else {
    return t('There are no objects to be exported at this time.');
  }
}

/**
 * FAPI definition for the bulk exporter form.
 *
 */
function bulk_export_export_form($form, &$form_state) {

  $files = system_rebuild_module_data();

  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
  );

  $options = $tables = array();
  foreach ($form_state['exportables'] as $table => $list) {
    if (empty($list)) {
      continue;
    }

    foreach ($list as $id => $title) {
      $options[$table][$id] = array($title);
      $options[$table][$id]['#attributes'] = array('class' => array('bulk-selection'));
    }

    $module = $form_state['export_tables'][$table];
    $header = array($table);
    $module_name = $files[$module]->info['name'];
    $tables[] = $table;

    if (!isset($form[$module_name])) {
      $form[$files[$module]->info['name']] = array(
        '#type' => 'fieldset',
        '#group' => 'additional_settings',
        '#title' => $module_name,
      );
    }

    $form[$module_name]['tables'][$table] = array(
      '#prefix' => '<div class="export-container">',
      '#suffix' => '</div>',
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options[$table],
    );
  }

  $form['tables'] = array(
    '#type' => 'value',
    '#value' => $tables,
  );

  $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/bulk-export/results');
  $form['#attached']['css'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.css';
  $form['#attached']['js'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.js';
  return $form;
}

/**
 * Process the bulk export submit form and make the results available.
 */
function bulk_export_export_form_submit($form, &$form_state) {
  $code = array();
  $name = empty($form_state['values']['name']) ? 'foo' : $form_state['values']['name'];
  $tables = $form_state['values']['tables'];

  foreach ($tables as $table) {
    $names = array_keys(array_filter($form_state['values'][$table]));
    if ($names) {
      natcasesort($names);
      ctools_export_to_hook_code($code, $table, $names, $name);
    }
  }

  $form_state['code'] = $code;
  $form_state['module'] = $name;
}