views_export.module

  1. 3.x views_export/views_export.module
  2. 2.x views_export/views_export.module

views_export.module

Provides functionality to export multiple items at once to make it easy to dump a set of views into code.

File

views_export/views_export.module
View source
  1. <?php
  2. /**
  3. * @file views_export.module
  4. *
  5. * Provides functionality to export multiple items at once to make it easy to
  6. * dump a set of views into code.
  7. */
  8. /**
  9. * Implementation of hook_menu().
  10. */
  11. function views_export_menu() {
  12. $items = array();
  13. $items['admin/build/views/tools/export'] = array(
  14. 'title' => 'Bulk export',
  15. 'access arguments' => array('use views exporter'),
  16. 'page callback' => 'views_export_export',
  17. 'type' => MENU_LOCAL_TASK,
  18. );
  19. $items['admin/build/views/tools/export/results'] = array(
  20. 'title' => 'Bulk export results',
  21. 'access arguments' => array('use views exporter'),
  22. 'page callback' => 'views_export_export',
  23. 'type' => MENU_LOCAL_TASK,
  24. );
  25. return $items;
  26. }
  27. function views_export_theme() {
  28. return array(
  29. 'views_export_export_form' => array(
  30. 'args' => array('form' => NULL),
  31. ),
  32. );
  33. }
  34. /**
  35. * Implementation of hook_perm().
  36. */
  37. function views_export_perm() {
  38. return array('use views exporter');
  39. }
  40. /**
  41. * Page callback to export views in bulk.
  42. */
  43. function views_export_export() {
  44. $tags = array();
  45. if (!empty($_GET['tags'])) {
  46. $tags = explode(',', $_GET['tags']);
  47. }
  48. $exportables = array();
  49. foreach (module_implements('views_exportables') as $module) {
  50. $function = $module . '_views_exportables';
  51. $exportables[$module] = $function('list', $tags);
  52. asort($exportables[$module]);
  53. }
  54. if ($exportables) {
  55. $form_state = array(
  56. 'no_redirect' => TRUE,
  57. 'exportables' => $exportables,
  58. 'tags' => $tags,
  59. );
  60. $output = drupal_build_form('views_export_export_form', $form_state);
  61. if (!$output) {
  62. $output = $form_state['output'];
  63. }
  64. return $output;
  65. }
  66. else {
  67. return t('There are no views to be exported at this time.');
  68. }
  69. }
  70. /**
  71. * Form to choose a group of views to export.
  72. */
  73. function views_export_export_form(&$form_state) {
  74. foreach ($form_state['exportables'] as $module => $views) {
  75. foreach ($views as $name => $data) {
  76. $options[$name] = $data['name'];
  77. }
  78. $form['modules']['#tree'] = TRUE;
  79. $form['modules'][$module] = array(
  80. '#type' => 'checkboxes',
  81. '#options' => $options,
  82. '#default_value' => array(),
  83. );
  84. }
  85. $tags = array();
  86. foreach (views_get_all_views() as $name => $view) {
  87. if (!empty($view->tag)) {
  88. $tags[$view->tag] = $view->tag;
  89. }
  90. }
  91. asort($tags);
  92. $form['tags'] = array(
  93. '#type' => 'select',
  94. '#title' => t('Show only these tags'),
  95. '#options' => $tags,
  96. '#default_value' => $form_state['tags'],
  97. '#multiple' => TRUE,
  98. );
  99. $form['apply'] = array(
  100. '#type' => 'submit',
  101. '#value' => t('Apply'),
  102. '#submit' => array('views_export_export_form_apply'),
  103. );
  104. $form['name'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Module name'),
  107. '#description' => t('Enter the module name to export code to.'),
  108. );
  109. $form['submit'] = array(
  110. '#type' => 'submit',
  111. '#value' => t('Export'),
  112. );
  113. $form['#action'] = url('admin/build/views/tools/export/results');
  114. $form['#redirect'] = FALSE;
  115. $form['#exportables'] = $form_state['exportables'];
  116. return $form;
  117. }
  118. function theme_views_export_export_form($form) {
  119. $output = '';
  120. $files = module_rebuild_cache();
  121. $exportables = $form['#exportables'];
  122. $output .= drupal_render($form['tags']);
  123. $output .= drupal_render($form['apply']);
  124. $output .= '<div class="clear-block">';
  125. foreach ($exportables as $module => $views) {
  126. $header = array(theme('table_select_header_cell'), $files[$module]->info['name'], t('Tag'), t('Description'));
  127. $rows = array();
  128. foreach ($views as $name => $view) {
  129. $title = $form['modules'][$module][$name]['#title'];
  130. unset($form['modules'][$module][$name]['#title']);
  131. $rows[] = array(drupal_render($form['modules'][$module][$name]), $title, $view['tag'], '<div class="description">' . $view['desc'] . '</div>');
  132. }
  133. $output .= '<div class="export-container">';
  134. $output .= theme('table', $header, $rows);
  135. $output .= "</div>\n";
  136. }
  137. $output .= '</div>';
  138. drupal_add_css(drupal_get_path('module', 'views_export') . '/views_export.css');
  139. $output .= drupal_render($form);
  140. return $output;
  141. }
  142. function views_export_export_form_apply(&$form, &$form_state) {
  143. $tags = $form_state['values']['tags'];
  144. if ($tags) {
  145. drupal_goto('admin/build/views/tools/export', array('tags' => implode(',', $tags)));
  146. }
  147. else {
  148. drupal_goto('admin/build/views/tools/export');
  149. }
  150. }
  151. function views_export_export_form_submit(&$form, &$form_state) {
  152. $code = '';
  153. if (empty($form_state['values']['name'])) {
  154. $form_state['values']['name'] = 'foo';
  155. }
  156. foreach ($form_state['values']['modules'] as $module => $views) {
  157. $views = array_filter($views);
  158. asort($views);
  159. if ($views) {
  160. $code .= module_invoke($module, 'views_exportables', 'export', $views, $form_state['values']['name']) . "\n\n";
  161. }
  162. }
  163. $lines = substr_count($code, "\n");
  164. $types = system_elements();
  165. $info = "; \$Id" . ": $\n"; // The break in the string prevents CVS.
  166. $info .= "\n";
  167. $info .= strtr("name = @module Export Module\n", array('@module' => $form_state['values']['name']));
  168. $info .= strtr("description = Exports some views of @module\n", array('@module' => $form_state['values']['name']));
  169. $info .= "dependencies[] = views\n";
  170. $info .= "core = 6.x\n";
  171. $element_info = array(
  172. '#title' => t('Put this in @module.info in your modules/@module directory', array('@module' => $form_state['values']['name'])),
  173. '#type' => 'textarea',
  174. '#id' => 'export-info-textarea',
  175. '#name' => 'export-info-textarea',
  176. '#attributes' => array(),
  177. '#rows' => 9,
  178. '#cols' => 60,
  179. '#value' => $info,
  180. '#parents' => array('dummy'),
  181. '#required' => FALSE,
  182. ) + $types['textarea'];
  183. $api = "/**\n";
  184. $api .= " * Implementation of hook_views_api().\n";
  185. $api .= " */\n";
  186. $api .= "function @module_views_api() {\n";
  187. $api .= " return array(\n";
  188. $api .= " 'api' => '" . views_api_version() . "',\n";
  189. $api .= " 'path' => drupal_get_path('module', '@module'),\n";
  190. $api .= " //'path' => drupal_get_path('module', '@module') . '/includes',\n";
  191. $api .= " );\n";
  192. $api .= "}";
  193. $api = strtr($api, array('@module' => check_plain($form_state['values']['name'])));
  194. $element_api = array(
  195. '#title' => t('Put this in @module.module in your modules/@module directory (place a &lt;?php at the top of the file so the webserver knows that it is PHP code)', array('@module' => $form_state['values']['name'])),
  196. '#type' => 'textarea',
  197. '#id' => 'export-api-textarea',
  198. '#name' => 'export-api-textarea',
  199. '#attributes' => array( 'dir' => 'ltr' ),
  200. '#rows' => 9,
  201. '#cols' => 60,
  202. '#value' => $api,
  203. '#parents' => array('dummy'),
  204. '#required' => FALSE,
  205. ) + $types['textarea'];
  206. $element_hook = array(
  207. '#title' => t('Put this in @module.views_default.inc in your modules/@module directory or modules/@module/includes directory (place a &lt;?php at the top of the file so the webserver knows that it is PHP code)', array('@module' => $form_state['values']['name'])),
  208. '#type' => 'textarea',
  209. '#id' => 'export-textarea',
  210. '#name' => 'export-textarea',
  211. '#attributes' => array( 'dir' => 'ltr' ),
  212. '#rows' => min($lines, 150),
  213. '#value' => $code,
  214. '#parents' => array('dummy'),
  215. '#required' => FALSE,
  216. ) + $types['textarea'];
  217. $form_state['output'] = theme('textarea', $element_info);
  218. $form_state['output'] .= theme('textarea', $element_api);
  219. $form_state['output'] .= theme('textarea', $element_hook);
  220. }