You are here

function feed_import_dynamic_func_form in Feed Import 7.3

Dynamic functions form.

1 string reference to 'feed_import_dynamic_func_form'
feed_import_menu in ./feed_import.module
Implements hook_menu().

File

./feed_import.module, line 2543
User interface, cron functions for feed_import module

Code

function feed_import_dynamic_func_form($form, &$form_state, $feed) {
  if (empty($form_state['values'])) {
    drupal_set_message(t('Dynamic filter functions are created using eval() which can be dangerous for import!'), 'warning');
    drupal_set_title(t('Dynamic filter functions - @name', array(
      '@name' => $feed->name,
    )), PASS_THROUGH);
  }
  $form['description'] = array(
    '#markup' => t('If possible please use a php file including filters (see Filter settings).') . '<br>' . t('Import will not start if any of these functions have syntax errors. If you have problems please check logs.') . '<br>' . '<strong>' . t('If an error occurs on runtime in one of these functions the whole script stops immediately, resulting a broken import process!') . '<br>' . t('Main purpose of these filter functions is testing.') . '<br>' . '</strong>' . '<i>' . t('Ok, eval() can be generally dangerous, but Feed Import assumes that access is enabled only for administrator.') . '</i>',
  );
  $form['machine_name'] = array(
    '#type' => 'value',
    '#value' => $feed->machine_name,
  );
  $form['fields'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#attributes' => array(
      'id' => 'feed_import_func_fields',
    ),
  );
  $func = array();
  if (isset($form_state['#current_item'])) {
    $fv =& $form_state['values']['fields'];
    for ($i = 0; $i <= $form_state['#current_item']; $i++) {
      if (!isset($fv['container_' . $i])) {
        continue;
      }
      $field =& $fv['container_' . $i];
      $func += feed_import_generate_func_item($i, $field);
      unset($field);
    }
    unset($fv);
  }
  else {
    $form_state['#current_item'] = -1;
    foreach ($feed->settings['functions'] as &$field) {
      $form_state['#current_item']++;
      $func += feed_import_generate_func_item($form_state['#current_item'], $field, TRUE);
    }
    unset($field);
  }
  $cbk = isset($form_state['triggering_element']['#name']) ? $form_state['triggering_element']['#name'] : '';
  if ($cbk == 'add_new_func') {
    $fv =& $form_state['values'];
    $form_state['#field_added'] = FALSE;
    if ($field = drupal_strtolower($fv['func'])) {
      $i = -1;
      $exists = FALSE;
      while (++$i <= $form_state['#current_item']) {
        if (isset($fv['fields']['container_' . $i]['name']) && $fv['fields']['container_' . $i]['name'] == $field) {
          $exists = TRUE;
          break;
        }
      }
      if (!$exists) {
        $form_state['#field_added'] = TRUE;
        $form_state['#current_item']++;
        $func += feed_import_generate_func_item($form_state['#current_item'], array(
          'name' => $field,
          'args' => '',
          'body' => '',
        ));
      }
    }
  }
  elseif (preg_match('/remove_container_([0-9]{1,9})/', $cbk, $match)) {

    // Delete container.
    unset($func['container_' . $match[1]]);
  }

  // Add fields.
  $form['fields'] += $func;
  $form['func'] = array(
    '#type' => 'textfield',
    '#title' => t('Function name'),
    '#attributes' => array(
      'id' => 'func-name',
    ),
    '#description' => t('Name must start with underscore and can contain only alphanumeric chars or underscores.'),
  );
  $form['add_new_func'] = array(
    '#type' => 'button',
    '#name' => 'add_new_func',
    '#value' => t('Add function'),
    '#ajax' => array(
      'event' => 'click',
      'callback' => 'feed_import_ajax_add_new_item',
      'wrapper' => 'feed_import_func_fields',
      'method' => 'append',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => 'save',
    '#value' => t('Save functions'),
  );

  // Add js.
  $form['#attached'] = array(
    'js' => array(
      drupal_get_path('module', 'feed_import') . '/feed_import.js' => array(
        'type' => 'file',
      ),
    ),
  );
  return $form;
}