You are here

public function FeedsXPathParserBase::configFormValidate in Feeds XPath Parser 6

Same name and namespace in other branches
  1. 7 FeedsXPathParserBase.inc \FeedsXPathParserBase::configFormValidate()

Override parent::sourceFormValidate().

Overrides FeedsConfigurable::configFormValidate

1 call to FeedsXPathParserBase::configFormValidate()
FeedsXPathParserBase::sourceFormValidate in ./FeedsXPathParserBase.inc
Override parent::sourceFormValidate().

File

./FeedsXPathParserBase.inc, line 342
Provides the abstract base class for FeedsXPathParserHTML and FeedsXPathParserXML.

Class

FeedsXPathParserBase
Base class for the HTML and XML parsers.

Code

public function configFormValidate(&$values) {
  $mappings = $this
    ->getOwnMappings();

  // This tests if we're validating configForm or sourceForm.
  $config_form = FALSE;
  if (isset($values['xpath'])) {
    $values = $values['xpath'];
    $config_form = TRUE;
  }
  $class = get_class($this);
  $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . "\n<items></items>");
  $use_errors = libxml_use_internal_errors(TRUE);
  $values['context'] = trim($values['context']);
  if (!empty($values['context'])) {
    $result = $xml
      ->xpath($values['context']);
  }
  $error = libxml_get_last_error();

  // Error code 1219 is undefined namespace prefix.
  // Our sample doc doesn't have any namespaces let alone the one they're
  // trying to use. Besides, if someone is trying to use a namespace in an
  // XPath query, they're probably right.
  if ($error && $error->code != 1219) {
    $element = 'feeds][' . $class . '][xpath][context';
    if ($config_form) {
      $element = 'xpath][context';
    }
    form_set_error($element, t('There was an error with the XPath selector: %error', array(
      '%error' => $error->message,
    )));
    libxml_clear_errors();
  }
  foreach ($values['sources'] as $key => &$query) {
    $query = trim($query);
    if (!empty($query)) {
      $result = $xml
        ->xpath($query);
      $error = libxml_get_last_error();
      if ($error && $error->code != 1219) {
        $variable_present = FALSE;

        // Our variable substitution options can cause syntax errors, check
        // if we're doing that.
        if ($error->code == 1207) {
          foreach ($mappings as $target) {
            if (strpos($query, '$' . $target) !== FALSE) {
              $variable_present = TRUE;
            }
          }
        }
        if (!$variable_present) {
          $element = 'feeds][' . $class . '][xpath][sources][' . $key;
          if ($config_form) {
            $element = 'xpath][sources][' . $key;
          }
          form_set_error($element, t('There was an error with the XPath selector: %error', array(
            '%error' => $error->message,
          )));
          libxml_clear_errors();
        }
      }
    }
  }
  libxml_use_internal_errors($use_errors);
}