You are here

public function FeedsJSONPathParser::sourceForm in Feeds JSONPath Parser 7

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

Source form.

1 call to FeedsJSONPathParser::sourceForm()
FeedsJSONPathParser::configForm in ./FeedsJSONPathParser.inc
Override parent::configForm().

File

./FeedsJSONPathParser.inc, line 198
Contains FeedsJSONPathParser.

Class

FeedsJSONPathParser
Parses JSON using JSONPath.

Code

public function sourceForm($source_config) {
  $form = array();
  if (empty($source_config)) {
    $source_config = $this->config;
  }
  if (isset($source_config['allow_override']) && !$source_config['allow_override'] && empty($source_config['config'])) {
    return;
  }

  // Add extensions that might get importerd.
  $fetcher = feeds_importer($this->id)->fetcher;
  if (isset($fetcher->config['allowed_extensions'])) {
    if (strpos($fetcher->config['allowed_extensions'], 'json') === FALSE) {
      $fetcher->config['allowed_extensions'] .= ' json';
    }
  }
  $mappings_ = feeds_importer($this->id)->processor->config['mappings'];
  $uniques = $mappings = array();
  foreach ($mappings_ as $mapping) {
    if (strpos($mapping['source'], 'jsonpath_parser:') === 0) {
      $mappings[$mapping['source']] = $mapping['target'];
      if (!empty($mapping['unique'])) {
        $uniques[] = $mapping['target'];
      }
    }
  }
  $form['jsonpath'] = array(
    '#type' => 'fieldset',
    '#title' => t('JSONPath Parser Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => 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['jsonpath']['error_message']['#markup'] = '<div class="help">' . t('No JSONPath mappings are defined. Define mappings !link.', array(
      '!link' => l(t('here'), $feeds_base . $this->id . '/mapping'),
    )) . '</div><br />';
    return $form;
  }
  $form['jsonpath']['context'] = array(
    '#type' => 'textfield',
    '#title' => t('Context'),
    '#required' => TRUE,
    '#description' => t('This is the base query, all other queries will execute in this context.'),
    '#default_value' => isset($source_config['context']) ? $source_config['context'] : '',
    '#maxlength' => 1024,
    '#size' => 80,
  );
  $form['jsonpath']['sources'] = array(
    '#type' => 'fieldset',
  );
  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['jsonpath']['sources']['help']['#markup'] = '<div class="help">' . theme('item_list', array(
      'items' => $items,
    )) . '</div>';
  }
  $variables = array();
  foreach ($mappings as $source => $target) {
    $form['jsonpath']['sources'][$source] = array(
      '#type' => 'textfield',
      '#title' => $target,
      '#description' => t('The JSONPath expression to execute.'),
      '#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 %v is available for replacement.', array(
        '%v' => implode(', ', $variables),
      )), t('The variables %v are available for replacement.', array(
        '%v' => implode(', ', $variables),
      )));
      $form['jsonpath']['sources'][$source]['#description'] .= '<br />' . $variable_text;
    }
    $variables[] = '{' . $target . '}';
  }
  $form['jsonpath']['debug'] = array(
    '#type' => 'fieldset',
    '#title' => t('Debug'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['jsonpath']['debug']['options'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Debug query'),
    '#options' => array_merge(array(
      'context' => 'context',
    ), $mappings),
    '#default_value' => isset($source_config['debug']['options']) ? $source_config['debug']['options'] : array(),
  );
  return $form;
}