[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/ctools/bulk_export/ -> bulk_export.module (source)

   1  <?php
   2  // $Id: bulk_export.module,v 1.3.2.2 2010/07/16 20:38:01 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * Perform bulk exports.
   7   */
   8  
   9  /**
  10   * Implementation of hook_perm().
  11   */
  12  function bulk_export_perm() {
  13    return array('use bulk exporter');
  14  }
  15  
  16  /**
  17   * Implementation of hook_theme().
  18   */
  19  function bulk_export_theme() {
  20    return array('bulk_export_export_form' => array(
  21        'arguments' => array('form' => NULL),
  22      ),
  23    );
  24  }
  25  
  26  /**
  27   * Implementation of hook_menu().
  28   */
  29  function bulk_export_menu() {
  30    $items['admin/build/bulkexport'] = array(
  31      'title' => 'Bulk Exporter',
  32      'description' => 'Bulk-export multiple CTools-handled data objects to code.',
  33      'access arguments' => array('use bulk exporter'),
  34      'page callback' => 'bulk_export_export',
  35    );
  36    $items['admin/build/bulkexport/results'] = array(
  37      'access arguments' => array('use bulk exporter'),
  38      'page callback' => 'bulk_export_export',
  39      'type' => MENU_CALLBACK,
  40    );
  41    return $items;
  42  }
  43  
  44  /**
  45   * FAPI gateway to the bulk exporter.
  46   */
  47  function bulk_export_export() {
  48    ctools_include('export');
  49    $schemas = ctools_export_get_schemas(TRUE);
  50    $exportables = $export_tables = array();
  51  
  52    foreach ($schemas as $table => $schema) {
  53      if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) {
  54        $exportables[$table] = $schema['export']['list callback']();
  55      }
  56      else {
  57        $exportables[$table] = ctools_export_default_list($table, $schema);
  58      }
  59      natcasesort($exportables[$table]);
  60      $export_tables[$table] = $schema['module'];
  61    }
  62    if ($exportables) {
  63      ctools_include('form');
  64      $form_state = array(
  65        're_render' => FALSE,
  66        'no_redirect' => TRUE,
  67        'exportables' => $exportables,
  68        'export_tables' => $export_tables,
  69      );
  70      $output = ctools_build_form('bulk_export_export_form', $form_state);
  71      if (!$output) {
  72        drupal_set_title(t('Bulk export results'));
  73        $output = '';
  74        $module_code = '';
  75        $api_code = '';
  76        $dependencies = array();
  77        foreach ($form_state['code'] as $module => $api_info) {
  78          if ($module == 'general') {
  79            $module_code .= $api_info;
  80          }
  81          else {
  82            foreach ($api_info as $api => $info) {
  83              $api_code .= "  if (\$module == '$module' && \$api == '$api') {\n";
  84              $api_code .= "    return array('version' => $info[version]);\n";
  85              $api_code .= "  }\n";
  86              $dependencies[$module] = TRUE;
  87  
  88              $file = $form_state['module'] . '.' . $api . '.inc';
  89              $code = "<?php\n";
  90              $code .= "// \$Id" . ": $\n\n";
  91              $code .= "/**\n";
  92              $code .= " * @file\n";
  93              $code .= " * Bulk export of $api objects generated by Bulk export module.\n";
  94              $code .= " */\n\n";
  95              $code .= $info['code'];
  96              $output .= drupal_get_form('ctools_export_form', $code, t('Place this in @file', array('@file' => $file)));
  97            }
  98          }
  99        }
 100  
 101        // Add hook_ctools_plugin_api at the top of the module code, if there is any.
 102        if ($api_code) {
 103          $api =  "/**\n";
 104          $api .= " * Implementation of hook_ctools_plugin_api().\n";
 105          $api .= " */\n";
 106          $api .= "function $form_state[module]_ctools_plugin_api(\$module, \$api) {\n";
 107          $api .= $api_code;
 108          $api .= "}\n";
 109          $module_code = $api . $module_code;
 110        }
 111  
 112        if ($module_code) {
 113          $module =  "<?php\n";
 114          $module .= "// \$Id" . ": $\n\n";
 115          $module .= "/**\n";
 116          $module .= " * @file\n";
 117          $module .= " * Bulk export of objects generated by Bulk export module.\n";
 118          $module .= " */\n\n";
 119          $module .= $module_code;
 120          $output = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array('@file' => $form_state['module'] . '.module'))) . $output;
 121        }
 122  
 123        $info  = "; \$Id" . ": $\n"; // The break in the string prevents CVS from subbing the ID.
 124        $info .= strtr("name = @module export module\n", array('@module' => $form_state['module']));
 125        $info .= strtr("description = Export objects from CTools\n", array('@module' => $form_state['values']['name']));
 126        foreach ($dependencies as $module => $junk) {
 127          $info .= "dependencies[] = $module\n";
 128        }
 129        $info .= "package = Chaos tool suite\n";
 130        $info .= "core = 6.x\n";
 131        $output = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info'))) . $output;
 132  
 133      }
 134      return $output;
 135    }
 136    else {
 137      return t('There are no objects to be exported at this time.');
 138    }
 139  }
 140  
 141  /**
 142   * FAPI definition for the bulk exporter form.
 143   *
 144   */
 145  function bulk_export_export_form(&$form_state) {
 146    $form = array();
 147    $form['tables'] = array(
 148      '#prefix' => '<div class="clear-block">',
 149      '#suffix' => '</div>',
 150      '#tree' => TRUE,
 151    );
 152  
 153    foreach ($form_state['exportables'] as $table => $list) {
 154      $form['tables'][$table] = array(
 155        '#type' => 'checkboxes',
 156        '#options' => $list,
 157        '#default_value' => array(),
 158      );
 159    }
 160  
 161    $form['name'] = array(
 162      '#type' => 'textfield',
 163      '#title' => t('Module name'),
 164      '#description' => t('Enter the module name to export code to.'),
 165    );
 166  
 167    $form['submit'] = array(
 168      '#type' => 'submit',
 169      '#value' => t('Export'),
 170    );
 171  
 172    $form['#action'] = url('admin/build/bulkexport/results');
 173    $form['#exportables'] = $form_state['exportables'];
 174    $form['#export_tables'] = $form_state['export_tables'];
 175    return $form;
 176  }
 177  
 178  /**
 179   * Display the bulk export form.
 180   */
 181  function theme_bulk_export_export_form($form) {
 182    $files = module_rebuild_cache();
 183    $exportables = $form['#exportables'];
 184    $export_tables = $form['#export_tables'];
 185    $output = '';
 186  
 187    foreach ($export_tables as $table => $module) {
 188      $header = array(theme('table_select_header_cell'), "{$files[$module]->info['name']}: $table");
 189      $rows = array();
 190      foreach ($exportables[$table] as $name => $title) {
 191        // $title = $form['tables'][$table][$name]['#title'];
 192        unset($form['tables'][$table][$name]['#title']);
 193        $rows[] = array(drupal_render($form['tables'][$table][$name]), $title);
 194      }
 195  
 196      if ($rows) {
 197        $output .= '<div class="export-container">';
 198        $output .= theme('table', $header, $rows);
 199        $output .= "</div>\n";
 200      }
 201    }
 202  
 203    if (empty($output)) {
 204      $output = t('There are no objects in your system that may be exported at this time.');
 205    }
 206  
 207    drupal_add_css(drupal_get_path('module', 'bulk_export') . '/bulk_export.css');
 208    $output .= drupal_render($form);
 209    return $output;
 210  }
 211  
 212  /**
 213   * Process the bulk export submit form and make the results available.
 214   */
 215  function bulk_export_export_form_submit($form, &$form_state) {
 216    $code = array();
 217    $name = empty($form_state['values']['name']) ? 'foo' : $form_state['values']['name'];
 218  
 219    foreach ($form_state['values']['tables'] as $table => $names) {
 220      $names = array_keys(array_filter($names));
 221      if ($names) {
 222        natcasesort($names);
 223        ctools_export_to_hook_code($code, $table, $names, $name);
 224      }
 225    }
 226  
 227    $form_state['code'] = $code;
 228    $form_state['module'] = $name;
 229  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7