You are here

patterns_export.module in Patterns 7

Same filename and directory in other branches
  1. 7.2 patterns_export/patterns_export.module

File

patterns_export/patterns_export.module
View source
<?php

/**
 * @file
 * Experimental. Extends Patterns, and enables automatic extraction of the web
 * site configuration, and supplies an API to write pattern files
 * programmatically.
 *
 */
module_load_include('inc', 'patterns_export', 'api');
module_load_include('inc', 'patterns_export', 'core');
module_load_include('inc', 'patterns_export', 'finalize');

/**
 * Implements hook_menu_alter()
 * 
 * Adds a tab in the main Patterns menu
 */
function patterns_export_menu_alter(&$items) {
  $items['admin/patterns/export'] = array(
    'title' => 'Export',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'patterns_export',
    ),
    'access arguments' => array(
      'administer patterns',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Forms to export the current web site configuration
 * 
 * Displays a form from which any number of export functions can be
 * selected to generate the exported pattern file
 */
function patterns_export($form, &$form_state) {
  $form = array();
  if (!patterns_parser_ready()) {
    $messages = t('No available patterns parser was found.</br>');
    $messages .= t(' Go to the !modules page to enable more Patterns parsers.', array(
      '!modules' => l(t('modules'), 'admin/modules'),
    ));
    drupal_set_message($messages, 'warning');
    return $form;
  }
  patterns_io_load_components();
  return patterns_export_page1($form, $form_state);
}
function patterns_export_page1($form, &$form_state) {

  //Get all the modules implementing patterns and filter those which implement an EXPORT funtion
  $tm_index = patterns_tagmodules_get_index();
  $tm_index = patterns_tagmodules_filter($tm_index, NULL, PATTERNS_EXPORT);
  if (count($tm_index) == 0) {
    drupal_set_message(t('The components currently installed do not allow automatic export of the configuration of the web site'), 'error');
    $form['disabled'] = array(
      '#markup' => l(t('Click here to back'), 'admin/patterns'),
    );
    return $form;
  }
  $text = 'In this area you can export the current configuration of your web site to a new pattern file that can be inserted in the database, or downloaded.';
  $title = 'Export';
  patterns_forms_add_page_header($form, $title, $text);
  $form['all'] = array(
    '#type' => 'fieldset',
    '#title' => 'Components',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['all']['all'] = array(
    '#type' => 'fieldset',
    '#title' => 'I want to select the components individually',
    '#type' => 'checkbox',
  );
  $form['all']['ext'] = array(
    '#type' => 'fieldset',
    '#title' => 'Components with automatic export enabled',
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#states' => array(
      'visible' => array(
        // action to take.
        ':input[name="all"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  foreach ($tm_index as $module => $tags) {
    $form['all']['ext'][$module] = array(
      '#type' => 'fieldset',
      '#title' => $module,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['all']['ext'][$module]['options'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Export options'),
      '#options' => array_combine(array_keys($tags), array_keys($tags)),
    );
    foreach ($tags as $tag => $forms) {
      $input_name = _patterns_export_build_input_name($module, $tag);
      $form['all']['ext'][$module][$tag]['options'] = array(
        '#type' => 'checkboxes',
        '#title' => $tag,
        '#options' => array_combine(array_keys($forms), array_keys($forms)),
        '#states' => array(
          'visible' => array(
            // action to take.
            ':input[name="' . $input_name . '"]' => array(
              'checked' => TRUE,
            ),
          ),
          'checked' => array(
            // action to take.
            ':input[name="' . $input_name . '"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
      );
    }
  }
  patterns_forms_get_info_section($form, array(), array(
    'collapsed' => TRUE,
  ));
  patterns_forms_get_formats_selector($form, patterns_forms_get_default_format(), t('Export data in this format'), FALSE);
  patterns_forms_get_name_selector($form);
  patterns_forms_get_export_to_selector($form, array(
    'title' => 'Export options',
  ));
  patterns_forms_get_exec_mode_selector($form['export']);
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Export'),
  );
  $form['#validate'][] = 'patterns_export_validate';
  return $form;
}

/**
 * validation function for import_module_form
 */
function patterns_export_validate($form, &$form_state) {
  if (!isset($form_state['values']['format'])) {
    form_set_error('format', t('No valid format selected.'));
  }
  if (!empty($form_state['values']['filename'])) {
    form_set_error('format', t('Please specify a valid file name.'));
  }
  if (!isset($form_state['values']['export']['to'])) {
    form_set_error('export[to]', t('Please specify a valid export action.'));
  }
  if (!isset($form_state['values']['export']['mode'])) {
    form_set_error('export[mode]', t('Please specify a valid export mode.'));
  }
  if (!isset($form_state['values']['info'])) {
    form_set_error('to', t('No info section found.'));
  }
  return TRUE;
}

/**
 * Submit hook of the export form
 */
function patterns_export_submit($form, &$form_state) {
  $tm_index = patterns_moduletags_get_index();
  $format = $form_state['values']['format'];
  $filename = $form_state['values']['pattern_name'];

  // TODO: Loop here and create a modules section
  // Create the array of exports functions.
  // Can either be the full module_tags index,
  // or we must go through all the checkbox and build the array from scratch
  if (!isset($form_state['values']['all']) || $form_state['values']['all'] == 0) {
    $exports = $tm_index;
  }
  else {

    // Individual modules have selected and the tagmodules index needs to be
    // refactored.
    $exports = array();
    foreach ($form_state['values']['ext'] as $module => $data) {
      foreach ($data['options'] as $tag => $enabled) {
        if (empty($enabled)) {
          continue;
        }
        $exports[$module][$tag][PATTERNS_EXPORT] = array();

        // Loop along all enabled export functions
        foreach ($data[$tag]['options'] as $export_key => $func) {
          if (!empty($func)) {
            $exports[$module][$tag][PATTERNS_EXPORT][] = $tm_index[$module][$tag][PATTERNS_EXPORT][$func];
          }
        }

        // If no sub-tag was selected (should not be the case)
        // then we execute EXPORT_ALL (if available)
        if (empty($exports[$module][$tag][PATTERNS_EXPORT])) {
          if (isset($tm_index[$module][$tag][PATTERNS_EXPORT][PATTERNS_EXPORT_ALL])) {
            $exports[$module][$tag][PATTERNS_EXPORT][] = $tm_index[$module][$tag][PATTERNS_EXPORT][PATTERNS_EXPORT_ALL];
          }
        }

        // Add necessary information
        if (isset($tm_index[$module][$tag][PATTERNS_FILES])) {
          $exports[$module][$tag][PATTERNS_FILES] = $tm_index[$module][$tag][PATTERNS_FILES];
        }
      }
    }
  }
  if (empty($exports)) {
    form_set_error('ext', t('No valid component selected.'));
    return FALSE;
  }
  $form_state['redirect'] = 'admin/patterns/export';
  patterns_export_start_engine($filename, $exports, $form_state['values']['info'], $form_state['values']['export']['to'], $format, $form_state['values']['export']['mode']);
}

/**
 * 
 * Start the exporting process
 * 
 * @param string $filename The name or path of the exported file 
 * @param array $tagmodules The index of modules/tags/exports_functions 
 * @param array $info The info section of the export
 * @param string $to Optional. A valid export destionation (e.g. zip,file,database).
 * 	 Defaults, 'db'.
 * @param string $format A valid patterns format (e.g. yaml,xml,php)
 * @param string $mode Optional. A valid patterns execution mode. E.g. (php,batch).
 *   Defaults 'php'.
 */
function patterns_export_start_engine($filename, $tagmodules = array(), $info = array(), $to = PATTERNS_EXPORT_TO_DB, $format = PATTERNS_FORMAT_UNKNOWN, $mode = PATTERNS_EXEC_PHP) {
  $format = patterns_io_get_format_from_file($filename, $format);
  if (!$format) {
    drupal_set_message(t('Unkwnown export format. Aborting.'), 'error');
    return FALSE;
  }
  if (!in_array($mode, array(
    'php',
    'batch',
  ))) {
    drupal_set_message(t('Unknown execution mode: %mode', array(
      '%mode' => $mode,
    )), 'error');
    return FALSE;
  }
  $func = 'patterns_export_' . $mode;
  return $func($filename, $tagmodules, $info, $to, $format);
}

// Helper functions

/**
 * Returns a list with the names of the components.
 *
 * @param bool $reset (optional) If TRUE, always forces reloading
 *   the components from the file system. Defaults FALSE
 *
 * @return array Array containing the names of the patterns components
 * 
 * @see patterns_io_load_components()
 */
function patterns_export_list_export_functions($reset = TRUE) {
  $moduletags = patterns_moduletags_get_index(NULL, $reset, $reset);
  return patterns_moduletags_filter($moduletags, NULL, PATTERNS_EXPORT);
}

/**
 * Returns TRUE if a string is a supported export mode
 * 
 * @param string $mode The mode to check
 * @return TRUE, if a string is a supported export mode
 */
function patterns_export_is_valid_mode($mode = NULL) {
  if (empty($mode)) {
    return FALSE;
  }
  return in_array($mode, array(
    PATTERNS_EXPORT_TO_DB,
    PATTERNS_EXPORT_TO_FILE,
    PATTERNS_EXPORT_TO_ZIP,
  ));
}

/**
 * Helper function to build the form of exportable components
 * 
 * @param string $module The name of the module
 * @param string $tag The name of the tag
 * 
 * @return string A valid input name for a checkbox input
 */
function _patterns_export_build_input_name($module, $tag) {
  return 'ext[' . $module . '][options][' . $tag . ']';
}

Functions

Namesort descending Description
patterns_export Forms to export the current web site configuration
patterns_export_is_valid_mode Returns TRUE if a string is a supported export mode
patterns_export_list_export_functions Returns a list with the names of the components.
patterns_export_menu_alter Implements hook_menu_alter()
patterns_export_page1
patterns_export_start_engine Start the exporting process
patterns_export_submit Submit hook of the export form
patterns_export_validate validation function for import_module_form
_patterns_export_build_input_name Helper function to build the form of exportable components