You are here

function patterns_import_server_submit in Patterns 7

Same name and namespace in other branches
  1. 6.2 patterns.module \patterns_import_server_submit()
  2. 6 patterns.module \patterns_import_server_submit()

Form validation handler for patterns_import_server().

See also

patterns_import_server_submit()

1 string reference to 'patterns_import_server_submit'
patterns_import_server in includes/servers.inc
Form constructor for the pattern importing from server form.

File

includes/servers.inc, line 120
Functions related to importing and publishing patterns on the web.

Code

function patterns_import_server_submit($form, &$form_state) {
  if ($form_state['storage']['step'] == 'get url') {
    return;
  }
  $pids = array_keys(array_filter($form_state['values']['patterns']));
  $feed = $form_state['storage']['feed'];
  $errors = array();
  foreach ($pids as $pid) {
    $url = $feed['info']['url'] . '/admin/patterns/get/' . $pid;
    $pattern_info = $feed['patterns'][$pid];
    if (!($source = file_get_contents($url))) {
      $errors['get file'][] = $pattern_info;
      continue;
    }

    // real_path(public) returns the path to the files dir
    $path = drupal_realpath('public://') . "/" . variable_get('patterns_save_file', 'patterns');
    $path .= '/' . $pattern_info['name'] . '.' . $pattern_info['file_type'];
    if (!($saved = file_save_data($source, $path, FILE_EXISTS_ERROR))) {
      $errors['save file'][] = $pattern_info;
      continue;
    }

    // Choose appropriate function based on the file extension.
    $func = 'patterns_load_' . $pattern_info['file_type'];

    // Load pattern.
    if (!($pattern = $func($saved))) {
      $errors['load pattern'][] = $pattern_info;
      unlink($saved);
      continue;
    }
    patterns_save_pattern($pattern, $saved, $pattern_info['name']);
  }
  if (!empty($errors)) {
    $patterns = array();
    foreach ($errors as $type => $files) {
      foreach ($files as $file) {
        $patterns[] = $file['title'] . ' (cause: "' . $type . '" failed)';

        // TODO: t()
      }
      $patterns = theme('item_list', array(
        'items' => $patterns,
      ));

      // TODO: ???
    }
    drupal_set_message(t('Import failed for the following patterns:') . '<br/>' . $patterns, 'error');

    // TODO: proper t()
    return;
  }
  unset($form_state['storage']);
  $form_state['redirect'] = 'admin/patterns';
}