You are here

function patterns_import_server_validate in Patterns 7

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

Form submission handler for patterns_import_server().

See also

patterns_import_server_validate()

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

File

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

Code

function patterns_import_server_validate($form, &$form_state) {
  if (empty($form_state['storage'])) {
    if (!ini_get('allow_url_fopen')) {
      form_set_error('fopen', t('allow_url_fopen must be enabled in your php configuration in order to use this feature.'));
      return;
    }
    $feed_url = $form_state['values']['server_url'] . '/patterns.xml';
    if (empty($feed_url)) {
      form_set_error('server_url', t('Please enter Patterns Server URL.'));
      return;
    }
    if (!($xml = file_get_contents($feed_url))) {
      form_set_error('server_url', t('Failed to retrieve the patterns feed from: %url. Please check your URL and try again.', array(
        '%url' => $form_state['values']['server_url'],
      )));
      return;
    }

    // @todo: replace this with proper XML validation.
    if (strpos($xml, '<?xml') !== 0) {
      form_set_error('', t('URL %url is not a valid XML file.', array(
        '%url' => $form_state['values']['server_url'],
      )));
      return;
    }
    $feed = patterns_from_source($xml);
    if (!$feed) {
      form_set_error('', t('URL %url is not a valid patterns feed.', array(
        '%url' => $form_state['values']['server_url'],
      )));
      return;
    }
    $feed = patterns_feed_rearrange_data($feed);
    if (!$feed) {
      form_set_error('', t('URL %url is not a valid patterns feed.', array(
        '%url' => $form_state['values']['server_url'],
      )));
      return;
    }
    $form_state['storage']['step'] = 'get url';
    $form_state['storage']['server_url'] = $form_state['values']['server_url'];
    $form_state['storage']['feed_url'] = $feed_url;
    $form_state['storage']['feed'] = $feed;
  }
  else {
    $form_state['storage']['step'] = 'select patterns';
  }
}