You are here

function patterns_import_validate in Patterns 7.2

Same name and namespace in other branches
  1. 5 patterns.module \patterns_import_validate()
  2. 6.2 patterns.module \patterns_import_validate()
  3. 6 patterns.module \patterns_import_validate()
  4. 7 includes/forms/import.inc \patterns_import_validate()
3 string references to 'patterns_import_validate'
patterns_import_file in includes/forms/import.inc
Display the import pattern file form
patterns_import_source in includes/forms/import.inc
Form constructor for the Pattern import form.
patterns_import_url in includes/forms/import.inc
Display the import pattern url form

File

includes/forms/import.inc, line 264
Importing Patterns from a file or using an URL.

Code

function patterns_import_validate($form, &$form_state) {
  $destination = $form_state['values']['destination'];

  // If not automatically returns
  _patterns_import_check_dirs($destination);
  _patterns_import_check_name($form_state['values']['name']);

  // Retreiving the format
  if (isset($form_state['values']['format']) && $form_state['values']['format'] != PATTERNS_FORMAT_UNKNOWN) {
    $format = $form_state['values']['format'];
  }
  elseif (isset($_FILES['files']['name']['pattern_uri'])) {
    $format = pathinfo($_FILES['files']['name']['pattern_uri'], PATHINFO_EXTENSION);
  }
  elseif (isset($form_state['values']['pattern_uri'])) {
    $format = pathinfo($form_state['values']['pattern_uri'], PATHINFO_EXTENSION);
  }
  else {
    $format = PATTERNS_FORMAT_UNKNOWN;
  }
  if (!patterns_parser_exists($format)) {
    form_set_error('format', t('The pattern format seems to be invalid.'));
    return FALSE;
  }

  // Safety assign
  $form_state['values']['format'] = $format;

  // FILE or URI
  if (!isset($form_state['values']['content'])) {
    $err_file = FALSE;

    // Local
    if (isset($form_state['values']['local'])) {
      $validators = array(
        'file_validate_extensions' => array(
          implode(' ', patterns_parser_get_formats(FALSE)),
        ),
      );
      if ($file = file_save_upload('pattern_uri', $validators)) {
        $form_state['values']['content'] = file_get_contents($file->uri);
        $form_state['pattern_uri'] = $file;
        $form_state['pattern_uri']->destination = $destination;
      }
      else {
        $err_file = TRUE;
      }
    }
    else {
      if (!ini_get('allow_url_fopen')) {
        form_set_error('url', t('allow_url_fopen must be enabled in your php configuration in order to use this feature.'));
        return FALSE;
      }
      if ($content = file_get_contents($form_state['values']['pattern_uri'])) {
        $form_state['values']['content'] = $content;
      }
      else {
        $err_file = TRUE;
      }
    }
    if ($err_file) {
      form_set_error('pattern_uri', t('Failed to retrieve the pattern specified: %uri. Check your file and try again.', array(
        '%uri' => $form_state['values']['pattern_uri'],
      )));
      return FALSE;
    }
  }

  // End FILE or URI
  // Parse
  $pattern = patterns_parser_parse($form_state['values']['content'], $format);
  if (!$pattern) {
    form_set_error('content', t('Pattern could not parsed. Please make sure it is well formed.'));
    return FALSE;
  }
  $patternscan = patterns_scan_pattern($pattern, FALSE, PATTERNS_VALIDATE_TAG_EXISTS);
  if (!_patterns_scan_validate_patternscan($patternscan)) {
    $errors_on_scan = _patterns_scan_analyze_patternscan($patternscan);
    form_set_error('name', t('Error parsing the file: ' . implode(', ', $errors_on_scan)));

    // TODO: proper t()
  }

  // We save here the parsed pattern
  $form_state['values']['pattern'] = $pattern;
  return TRUE;
}