You are here

function patterns_lab_submit in Patterns 7.2

Same name and namespace in other branches
  1. 7 includes/forms/lab.inc \patterns_lab_submit()

Exports selected patterns either in a file or as a zip-archive

_state

Parameters

$form:

1 string reference to 'patterns_lab_submit'
patterns_lab in includes/forms/lab.inc
@file Functions related to exporting patterns.

File

includes/forms/lab.inc, line 159
Functions related to exporting patterns.

Code

function patterns_lab_submit($form, &$form_state) {
  global $base_url;

  // Load the patterns from database.
  @($patterns = patterns_db_get_patterns());
  $path = patterns_path_get_files_dir();
  $format = $form_state['values']['format'];
  $filename = $form_state['values']['export_name'];
  $zip_flag = $form_state['values']['to'] === PATTERNS_EXPORT_TO_ZIP ? TRUE : FALSE;

  // Merged all the selected patterns.
  $export = patterns_api_add_info_section($form_state['values']['info']);
  $count = 0;
  $errors = array();
  $parents = array();
  foreach ($form_state['values']['patterns_table'] as $pid) {
    if (!is_int($pid)) {
      $pattern = patterns_get_pattern($pid);
      if (!$pattern) {
        drupal_set_message(t('Error reading pattern with id :pid', array(
          ':pid' => $pid,
        )), 'error');
        continue;
      }
      if (empty($pattern->pattern)) {
        $errors[] = patterns_utils_toString($pattern);
        continue;
      }
      $included = _patterns_lab_include_pattern($pattern, $form_state['values']['mode']);
      $included = array(
        'pattern' => $included,
      );
      $export['lab'][] = array(
        'include' => $included,
      );
      $parents[] = $pattern->uuuid;
      $count++;
    }
  }

  // Add parent header including all the merged patterns as parents.
  // D2D ID will be null in case the module is not enabled.
  $d2d_id = function_exists('d2d_api_own_d2d_id_get') ? d2d_api_own_d2d_id_get() : NULL;
  $url_rpc = $base_url . '/xmlrpc.php';
  patterns_history_set_parent($export, $url_rpc, $d2d_id, 'merge', $parents);

  // Prepare a zip file or a regular one
  if ($zip_flag) {

    // Cast to the expected format
    $p[$filename] = $export;
    $zip_path = patterns_create_zip($p, $format, $path, $filename);
    if (!$zip_path) {
      return FALSE;
    }
    else {
      return patterns_download_zip($zip_path);
    }
  }
  else {

    // Parse into a file and add info to the exported pattern
    $file = patterns_parser_dump($export, $format);
    $file = patterns_parser_dump_comment("Patterns exported: " . $count, $format, $file);
    $file = patterns_parser_dump_comment("Date and time: " . date('r'), $format, $file);

    // Store only in the database or return as regular file
    if ($form_state['values']['to'] === PATTERNS_EXPORT_TO_DB) {
      patterns_io_save_pattern($file, $filename, $format, $path);
      drupal_goto('admin/patterns/lab/');
    }
    else {
      drupal_add_http_header("Content-type", " text/plain; charset=utf-8");
      drupal_add_http_header("Content-Disposition", "attachment;filename=" . $filename . '.' . $format);
      print $file;
      exit;
    }
  }
}