You are here

function patterns_import_server_submit in Patterns 6

Same name and namespace in other branches
  1. 6.2 patterns.module \patterns_import_server_submit()
  2. 7 includes/servers.inc \patterns_import_server_submit()
1 string reference to 'patterns_import_server_submit'
patterns_import_server in ./patterns.module
Display the import pattern from server form

File

./patterns.module, line 413
Enables extremely simple adding/removing features to your site with minimal to no configuration

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/build/patterns/get/' . $pid;
    $pattern_info = $feed['patterns'][$pid];
    if (!($source = file_get_contents($url))) {
      $errors['get file'][] = $pattern_info;
      continue;
    }

    // save file
    $path = file_create_path(variable_get('patterns_save_xml', '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)';
      }
      $patterns = theme('item_list', $patterns);
    }
    drupal_set_message(t('Import failed for the following patterns: ') . '<br>' . $patterns, 'error');
    return;
  }
  unset($form_state['storage']);
  $form_state['redirect'] = 'admin/build/patterns';
}