bulk_export.module 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @file
  4. * Perform bulk exports.
  5. */
  6. /**
  7. * Implements hook_permission().
  8. */
  9. function bulk_export_permission() {
  10. return array(
  11. 'use bulk exporter' => array(
  12. 'title' => t('Access Bulk Exporter'),
  13. 'description' => t('Export various system objects into code.'),
  14. ),
  15. );
  16. }
  17. /**
  18. * Implements hook_menu().
  19. */
  20. function bulk_export_menu() {
  21. $items['admin/structure/bulk-export'] = array(
  22. 'title' => 'Bulk Exporter',
  23. 'description' => 'Bulk-export multiple CTools-handled data objects to code.',
  24. 'access arguments' => array('use bulk exporter'),
  25. 'page callback' => 'bulk_export_export',
  26. );
  27. $items['admin/structure/bulk-export/results'] = array(
  28. 'access arguments' => array('use bulk exporter'),
  29. 'page callback' => 'bulk_export_export',
  30. 'type' => MENU_CALLBACK,
  31. );
  32. return $items;
  33. }
  34. /**
  35. * FAPI gateway to the bulk exporter.
  36. *
  37. * @param $cli
  38. * Whether this function is called from command line.
  39. * @param $options
  40. * A collection of options, only passed in by drush_ctools_export().
  41. */
  42. function bulk_export_export($cli = FALSE, $options = array()) {
  43. ctools_include('export');
  44. $form = array();
  45. $schemas = ctools_export_get_schemas(TRUE);
  46. $exportables = $export_tables = array();
  47. foreach ($schemas as $table => $schema) {
  48. if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) {
  49. $exportables[$table] = $schema['export']['list callback']();
  50. }
  51. else {
  52. $exportables[$table] = ctools_export_default_list($table, $schema);
  53. }
  54. natcasesort($exportables[$table]);
  55. $export_tables[$table] = $schema['module'];
  56. }
  57. if ($exportables) {
  58. $form_state = array(
  59. 're_render' => FALSE,
  60. 'no_redirect' => TRUE,
  61. 'exportables' => $exportables,
  62. 'export_tables' => $export_tables,
  63. 'name' => '',
  64. 'code' => '',
  65. 'module' => '',
  66. );
  67. // If called from drush_ctools_export, get the module name and
  68. // select all exportables and call the submit function directly.
  69. if ($cli) {
  70. $module_name = $options['name'];
  71. $form_state['values']['name'] = $module_name;
  72. if (isset($options['selections'])) {
  73. $exportables = $options['selections'];
  74. }
  75. $form_state['values']['tables'] = array();
  76. foreach ($exportables as $table => $names) {
  77. if (!empty($names)) {
  78. $form_state['values']['tables'][] = $table;
  79. $form_state['values'][$table] = array();
  80. foreach ($names as $name => $title) {
  81. $form_state['values'][$table][$name] = $name;
  82. }
  83. }
  84. }
  85. $output = bulk_export_export_form_submit($form, $form_state);
  86. }
  87. else {
  88. $output = drupal_build_form('bulk_export_export_form', $form_state);
  89. $module_name = $form_state['module'];
  90. }
  91. if (!empty($form_state['submitted']) || $cli) {
  92. drupal_set_title(t('Bulk export results'));
  93. $output = '';
  94. $module_code = '';
  95. $api_code = array();
  96. $dependencies = $file_data = array();
  97. foreach ($form_state['code'] as $module => $api_info) {
  98. if ($module == 'general') {
  99. $module_code .= $api_info;
  100. }
  101. else {
  102. foreach ($api_info as $api => $info) {
  103. $api_hook = ctools_plugin_api_get_hook($module, $api);
  104. if (empty($api_code[$api_hook])) {
  105. $api_code[$api_hook] = '';
  106. }
  107. $api_code[$api_hook] .= " if (\$module == '$module' && \$api == '$api') {\n";
  108. $api_code[$api_hook] .= " return array('version' => $info[version]);\n";
  109. $api_code[$api_hook] .= " }\n";
  110. $dependencies[$module] = TRUE;
  111. $file = $module_name . '.' . $api . '.inc';
  112. $code = "<?php\n\n";
  113. $code .= "/**\n";
  114. $code .= " * @file\n";
  115. $code .= " * Bulk export of $api objects generated by Bulk export module.\n";
  116. $code .= " */\n\n";
  117. $code .= $info['code'];
  118. if ($cli) {
  119. $file_data[$file] = $code;
  120. }
  121. else {
  122. $export_form = drupal_get_form('ctools_export_form', $code, t('Place this in @file', array('@file' => $file)));
  123. $output .= drupal_render($export_form);
  124. }
  125. }
  126. }
  127. }
  128. // Add hook_ctools_plugin_api at the top of the module code, if there is any.
  129. if ($api_code) {
  130. foreach ($api_code as $api_hook => $text) {
  131. $api = "\n/**\n";
  132. $api .= " * Implements hook_$api_hook().\n";
  133. $api .= " */\n";
  134. $api .= "function {$module_name}_$api_hook(\$module, \$api) {\n";
  135. $api .= $text;
  136. $api .= "}\n";
  137. $module_code = $api . $module_code;
  138. }
  139. }
  140. if ($module_code) {
  141. $module = "<?php\n\n";
  142. $module .= "/**\n";
  143. $module .= " * @file\n";
  144. $module .= " * Bulk export of objects generated by Bulk export module.\n";
  145. $module .= " */\n";
  146. $module .= $module_code;
  147. if ($cli) {
  148. $file_data[$module_name . '.module'] = $module;
  149. }
  150. else {
  151. $export_form = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array('@file' => $form_state['module'] . '.module')));
  152. $output = drupal_render($export_form) . $output;
  153. }
  154. }
  155. $info = strtr("name = @module export module\n", array('@module' => $form_state['module']));
  156. $info .= strtr("description = Export objects from CTools\n", array('@module' => $form_state['values']['name']));
  157. foreach ($dependencies as $module => $junk) {
  158. $info .= "dependencies[] = $module\n";
  159. }
  160. $info .= "package = Chaos tool suite\n";
  161. $info .= "core = 7.x\n";
  162. if ($cli) {
  163. $file_data[$module_name . '.info'] = $info;
  164. }
  165. else {
  166. $export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info')));
  167. $output = drupal_render($export_form) . $output;
  168. }
  169. }
  170. if ($cli) {
  171. return $file_data;
  172. }
  173. else {
  174. return $output;
  175. }
  176. }
  177. else {
  178. return t('There are no objects to be exported at this time.');
  179. }
  180. }
  181. /**
  182. * FAPI definition for the bulk exporter form.
  183. */
  184. function bulk_export_export_form($form, &$form_state) {
  185. $files = system_rebuild_module_data();
  186. $form['additional_settings'] = array(
  187. '#type' => 'vertical_tabs',
  188. );
  189. $options = $tables = array();
  190. foreach ($form_state['exportables'] as $table => $list) {
  191. if (empty($list)) {
  192. continue;
  193. }
  194. foreach ($list as $id => $title) {
  195. $options[$table][$id] = array($title);
  196. $options[$table][$id]['#attributes'] = array('class' => array('bulk-selection'));
  197. }
  198. $module = $form_state['export_tables'][$table];
  199. $header = array($table);
  200. $module_name = $files[$module]->info['name'];
  201. $tables[] = $table;
  202. if (!isset($form[$module_name])) {
  203. $form[$files[$module]->info['name']] = array(
  204. '#type' => 'fieldset',
  205. '#group' => 'additional_settings',
  206. '#title' => $module_name,
  207. );
  208. }
  209. $form[$module_name]['tables'][$table] = array(
  210. '#prefix' => '<div class="export-container">',
  211. '#suffix' => '</div>',
  212. '#type' => 'tableselect',
  213. '#header' => $header,
  214. '#options' => $options[$table],
  215. );
  216. }
  217. $form['tables'] = array(
  218. '#type' => 'value',
  219. '#value' => $tables,
  220. );
  221. $form['name'] = array(
  222. '#type' => 'textfield',
  223. '#title' => t('Module name'),
  224. '#description' => t('Enter the module name to export code to.'),
  225. );
  226. $form['submit'] = array(
  227. '#type' => 'submit',
  228. '#value' => t('Export'),
  229. );
  230. $form['#action'] = url('admin/structure/bulk-export/results');
  231. $form['#attached']['css'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.css';
  232. $form['#attached']['js'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.js';
  233. return $form;
  234. }
  235. /**
  236. * Process the bulk export submit form and make the results available.
  237. */
  238. function bulk_export_export_form_submit($form, &$form_state) {
  239. $code = array();
  240. $name = empty($form_state['values']['name']) ? 'foo' : $form_state['values']['name'];
  241. $tables = $form_state['values']['tables'];
  242. foreach ($tables as $table) {
  243. $names = array_keys(array_filter($form_state['values'][$table]));
  244. if ($names) {
  245. natcasesort($names);
  246. ctools_export_to_hook_code($code, $table, $names, $name);
  247. }
  248. }
  249. $form_state['code'] = $code;
  250. $form_state['module'] = $name;
  251. }