You are here

function patterns_import_server_validate in Patterns 6

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

File

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

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 retreive 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';
  }
}