You are here

panels_export.module in Panels 6.2

panels_export.module

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

File

panels_export/panels_export.module
View source
<?php

/**
 * @file panels_export.module
 *
 * Provides functionality to export multiple items at once to make it easy to
 * dump a set of panels into code.
 */

/**
 * Implementation of hook_menu().
 */
function panels_export_menu() {
  $items = array();
  $items['admin/panels/export'] = array(
    'title' => 'Export panels',
    'access arguments' => array(
      'use panels exporter',
    ),
    'page callback' => 'panels_export_export',
    'description' => 'Export panels in bulk.',
  );
  $items['admin/panels/export/results'] = array(
    'access arguments' => array(
      'use panels exporter',
    ),
    'page callback' => 'panels_export_export',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_theme()
 */
function panels_export_theme() {
  return array(
    'panels_export_export_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function panels_export_perm() {
  return array(
    'use panels exporter',
  );
}

/**
 * Page callback to export panels in bulk.
 */
function panels_export_export() {
  $exportables = array();
  foreach (module_implements('panels_exportables') as $module) {
    $function = $module . '_panels_exportables';
    $exportables[$module] = $function('list');
  }
  if ($exportables) {
    $form_state = array(
      're_render' => FALSE,
      'no_redirect' => TRUE,
      'exportables' => $exportables,
    );
    $output = drupal_build_form('panels_export_export_form', $form_state);
    if (!$output) {
      $output = $form_state['output'];
    }
    return $output;
  }
  else {
    return t('There are no panels to be exported at this time.');
  }
}

/**
 * Form to choose a group of panels to export.
 */
function panels_export_export_form(&$form_state) {
  foreach ($form_state['exportables'] as $module => $panels) {
    $form['modules'] = array(
      '#prefix' => '<div class="clear-block">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
    );
    $form['modules'][$module] = array(
      '#type' => 'checkboxes',
      '#options' => $panels,
      '#default_value' => array(),
    );
  }
  $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/panels/export/results');
  $form['#exportables'] = $form_state['exportables'];
  return $form;
}
function theme_panels_export_export_form($form) {
  $files = module_rebuild_cache();
  $exportables = $form['#exportables'];
  $output = '';
  foreach ($exportables as $module => $panels) {
    $header = array(
      theme('table_select_header_cell'),
      $files[$module]->info['name'],
    );
    $rows = array();
    foreach ($panels as $name => $panel) {
      $title = $form['modules'][$module][$name]['#title'];
      unset($form['modules'][$module][$name]['#title']);
      $rows[] = array(
        drupal_render($form['modules'][$module][$name]),
        $title,
      );
    }
    $output .= '<div class="export-container">';
    $output .= theme('table', $header, $rows);
    $output .= "</div>\n";
  }
  drupal_add_css(panels_get_path('panels_export/panels_export.css'));
  $output .= drupal_render($form);
  return $output;
}
function panels_export_export_form_submit($form, &$form_state) {
  $code = '';
  if (empty($form_state['values']['name'])) {
    $form_state['values']['name'] = 'foo';
  }
  foreach ($form_state['values']['modules'] as $module => $panels) {
    $panels = array_filter($panels);
    if ($panels) {
      $code .= module_invoke($module, 'panels_exportables', 'export', $panels, $form_state['values']['name']) . "\n\n";
    }
  }
  $lines = substr_count($code, "\n");
  $element = array(
    '#type' => 'textarea',
    '#id' => 'export-textarea',
    '#name' => 'export-textarea',
    '#attributes' => array(),
    '#rows' => min($lines, 150),
    '#value' => $code,
    '#parents' => array(),
  );
  $form_state['output'] = theme('textarea', $element);
}

Functions

Namesort descending Description
panels_export_export Page callback to export panels in bulk.
panels_export_export_form Form to choose a group of panels to export.
panels_export_export_form_submit
panels_export_menu Implementation of hook_menu().
panels_export_perm Implementation of hook_perm().
panels_export_theme Implementation of hook_theme()
theme_panels_export_export_form