You are here

public static function UltimateCronPlugin::jobSettingsForm in Ultimate Cron 7.2

Job settings form.

1 method overrides UltimateCronPlugin::jobSettingsForm()
UltimateCronPluginMultiple::jobSettingsForm in ./ultimate_cron.plugin.inc
Job settings form.

File

./ultimate_cron.plugin.inc, line 479
Plugin framework for Ultimate Cron.

Class

UltimateCronPlugin
This is the base class for all Ultimate Cron plugins.

Code

public static function jobSettingsForm(&$form, &$form_state, $plugin_type, $job) {

  // Check valid plugins.
  $plugins = _ultimate_cron_plugin_load_all($plugin_type);
  foreach ($plugins as $name => $plugin) {
    if (!$plugin
      ->isValid($job)) {
      unset($plugins[$name]);
    }
  }

  // No plugins = no settings = no vertical tabs for you mister!
  if (empty($plugins)) {
    return;
  }
  ctools_include('plugins');
  $plugin_types = ctools_plugin_get_plugin_type_info();
  $plugin_info = $plugin_types['ultimate_cron'][$plugin_type];
  $static = $plugin_info['defaults']['static'];

  // Find plugin selected on this page.
  // If "0" (meaning default) use the one defined in the hook.
  if (empty($form_state['values']['settings'][$plugin_type]['name'])) {
    $form_state['values']['settings'][$plugin_type]['name'] = 0;
    $current_plugin = $plugins[$job->hook[$plugin_type]['name']];
  }
  else {
    $current_plugin = $plugins[$form_state['values']['settings'][$plugin_type]['name']];
  }
  $form_state['previous_plugin'][$plugin_type] = $current_plugin->name;

  // Determine original plugin.
  $original_plugin = !empty($job->settings[$plugin_type]['name']) ? $job->settings[$plugin_type]['name'] : $job->hook[$plugin_type]['name'];

  // Ensure blank array.
  if (empty($form_state['values']['settings'][$plugin_type][$current_plugin->name])) {
    $form_state['values']['settings'][$plugin_type][$current_plugin->name] = array();
  }

  // Default values for current selection. If selection differs from current
  // job, then take the job into account.
  $defaults = $current_plugin->name == $original_plugin ? $job->settings : array();
  $defaults += $current_plugin
    ->getDefaultSettings($job);

  // Plugin settings fieldset with vertical tab reference.
  $form['settings'][$plugin_type] = array(
    '#type' => 'fieldset',
    '#title' => $static['title singular proper'],
    '#group' => 'settings_tabs',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );

  // Ajax wrapper.
  $wrapper = 'wrapper-plugin-' . $plugin_type . '-settings';

  // Setup plugin selector.
  $options = array();
  $options[''] = t('Default (@default)', array(
    '@default' => $plugins[$job->hook[$plugin_type]['name']]->title,
  ));
  foreach ($plugins as $name => $plugin) {
    $options[$name] = $plugin->title;
  }
  $form['settings'][$plugin_type]['name'] = array(
    '#weight' => -10,
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $form_state['values']['settings'][$plugin_type]['name'],
    '#title' => $static['title singular proper'],
    '#description' => t('Select which @plugin to use for this job.', array(
      '@plugin' => $static['title singular'],
    )),
    '#ajax' => array(
      'callback' => 'ultimate_cron_job_plugin_settings_ajax',
      'wrapper' => $wrapper,
      'method' => 'replace',
      'effect' => 'none',
    ),
  );
  $default_settings_link = l(t('(change default settings)'), 'admin/config/system/cron/' . $current_plugin->type . '/' . $current_plugin->name);

  // Plugin specific settings wrapper for ajax replace.
  $form['settings'][$plugin_type][$current_plugin->name] = array(
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#title' => $current_plugin->title,
    '#description' => $current_plugin->description,
    '#prefix' => '<div id="' . $wrapper . '">',
    '#suffix' => '</div>',
  );
  $form_state['default_values']['settings'][$plugin_type][$current_plugin->name] = $defaults;
  if ($current_plugin->name == $original_plugin && isset($job->settings[$plugin_type][$current_plugin->name]) && is_array($job->settings[$plugin_type][$current_plugin->name])) {
    $form_state['values']['settings'][$plugin_type][$current_plugin->name] += $job->settings[$plugin_type][$current_plugin->name];
  }
  $form_state['values']['settings'][$plugin_type][$current_plugin->name] += ultimate_cron_blank_values($defaults);
  $current_plugin
    ->settingsForm($form, $form_state, $job);
  if (empty($form['settings'][$plugin_type][$current_plugin->name]['no_settings'])) {
    $current_plugin
      ->fallbackalize($form['settings'][$plugin_type][$current_plugin->name], $form_state['values']['settings'][$plugin_type][$current_plugin->name], $form_state['default_values']['settings'][$plugin_type][$current_plugin->name], FALSE);
    $form['settings'][$plugin_type][$current_plugin->name]['#description'] .= ' ' . $default_settings_link . '.';
  }
}