You are here

public function FeedsXPathParserBase::sourceForm in Feeds XPath Parser 7

Same name and namespace in other branches
  1. 6 FeedsXPathParserBase.inc \FeedsXPathParserBase::sourceForm()

Overrides parent::sourceForm().

1 call to FeedsXPathParserBase::sourceForm()
FeedsXPathParserBase::configForm in ./FeedsXPathParserBase.inc
Overrides parent::configForm().

File

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

Class

FeedsXPathParserBase
Base class for the HTML and XML parsers.

Code

public function sourceForm($source_config) {
  $form = array();
  $importer = feeds_importer($this->id);
  $importer_config = $importer
    ->getConfig();
  $mappings_ = $importer->processor
    ->getMappings();
  if (empty($source_config)) {
    $source_config = $this
      ->getConfig();
  }
  if (isset($source_config['allow_override']) && !$source_config['allow_override'] && empty($source_config['config'])) {
    return;
  }

  // Add extensions that might get importerd.
  $allowed_extensions = isset($importer_config['fetcher']['config']['allowed_extensions']) ? $importer_config['fetcher']['config']['allowed_extensions'] : FALSE;
  if ($allowed_extensions) {
    if (strpos($allowed_extensions, 'html') === FALSE) {
      $importer->fetcher->config['allowed_extensions'] .= ' html htm';
    }
  }
  $uniques = $this
    ->getUniques();
  $mappings = $this
    ->getOwnMappings();
  $targets = $importer->processor
    ->getMappingTargets();
  $form['xpath'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('XPath Parser Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  if (empty($mappings)) {

    // Detect if Feeds menu structure has changed. This will take a while to
    // be released, but since I run dev it needs to work.
    $feeds_menu = feeds_ui_menu();
    if (isset($feeds_menu['admin/structure/feeds/list'])) {
      $feeds_base = 'admin/structure/feeds/edit/';
    }
    else {
      $feeds_base = 'admin/structure/feeds/';
    }
    $form['xpath']['error_message']['#markup'] = '<div class="help">' . t('No XPath mappings are defined. Define mappings !link.', array(
      '!link' => l(t('here'), $feeds_base . $this->id . '/mapping'),
    )) . '</div><br />';
    return $form;
  }
  $form['xpath']['context'] = array(
    '#type' => 'textfield',
    '#title' => t('Context'),
    '#required' => TRUE,
    '#description' => t('This is the base query, all other queries will run in this context.'),
    '#default_value' => isset($source_config['context']) ? $source_config['context'] : '',
    '#maxlength' => 1024,
    '#size' => 80,
  );
  $form['xpath']['sources'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
  );
  if (!empty($uniques)) {
    $items = array(
      format_plural(count($uniques), t('Field <strong>!column</strong> is mandatory and considered unique: only one item per !column value will be created.', array(
        '!column' => implode(', ', $uniques),
      )), t('Fields <strong>!columns</strong> are mandatory and values in these columns are considered unique: only one entry per value in one of these columns will be created.', array(
        '!columns' => implode(', ', $uniques),
      ))),
    );
    $form['xpath']['sources']['help']['#markup'] = '<div class="help">' . theme('item_list', array(
      'items' => $items,
    )) . '</div>';
  }
  $variables = array();
  foreach ($mappings as $source => $target) {
    $form['xpath']['sources'][$source] = array(
      '#type' => 'textfield',
      '#title' => isset($targets[$target]['name']) ? check_plain($targets[$target]['name']) : check_plain($target),
      '#description' => t('The XPath query to run.'),
      '#default_value' => isset($source_config['sources'][$source]) ? $source_config['sources'][$source] : '',
      '#maxlength' => 1024,
      '#size' => 80,
    );
    if (!empty($variables)) {
      $variable_text = format_plural(count($variables), t('The variable %variable is available for replacement.', array(
        '%variable' => implode(', ', $variables),
      )), t('The variables %variable are available for replacement.', array(
        '%variable' => implode(', ', $variables),
      )));
      $form['xpath']['sources'][$source]['#description'] .= '<br />' . $variable_text;
    }
    $variables[] = '$' . $target;
  }
  $form['xpath']['rawXML'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Select the queries you would like to return raw XML or HTML'),
    '#options' => $this
      ->getOwnMappings(TRUE),
    '#default_value' => isset($source_config['rawXML']) ? $source_config['rawXML'] : array(),
  );
  $form['xpath']['exp'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#title' => t('Debug Options'),
  );
  $form['xpath']['exp']['errors'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show error messages.'),
    '#default_value' => isset($source_config['exp']['errors']) ? $source_config['exp']['errors'] : FALSE,
  );
  if (extension_loaded('tidy')) {
    $form['xpath']['exp']['tidy'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use Tidy'),
      '#description' => t('The Tidy PHP extension has been detected.
                              Select this to clean the markup before parsing.'),
      '#default_value' => isset($source_config['exp']['tidy']) ? $source_config['exp']['tidy'] : FALSE,
    );
    $form['xpath']['exp']['tidy_encoding'] = array(
      '#type' => 'textfield',
      '#title' => t('Tidy encoding'),
      '#description' => t('Set the encoding for tidy. See the !phpdocs for possible values.', array(
        '!phpdocs' => l(t('PHP docs'), 'http://www.php.net/manual/en/tidy.parsestring.php/'),
      )),
      '#default_value' => isset($source_config['exp']['tidy_encoding']) ? $source_config['exp']['tidy_encoding'] : 'UTF8',
      '#states' => array(
        'visible' => array(
          ':input[name$="[tidy]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  $form['xpath']['exp']['debug'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Debug query'),
    '#options' => array_merge(array(
      'context' => t('Context'),
    ), $this
      ->getOwnMappings(TRUE)),
    '#default_value' => isset($source_config['exp']['debug']) ? $source_config['exp']['debug'] : array(),
  );
  return $form;
}