You are here

function mostpopular_service_config_form in Drupal Most Popular 6

Same name and namespace in other branches
  1. 7 mostpopular.services.inc \mostpopular_service_config_form()
1 string reference to 'mostpopular_service_config_form'
mostpopular_menu in ./mostpopular.module
Implements hook_menu().

File

./mostpopular.admin.inc, line 513
Defines all the administration forms for the Most Popular module.

Code

function mostpopular_service_config_form(&$form_values, $sid = 1) {
  $service = MostPopularService::fetch($sid);
  $intervals = MostPopularInterval::fetchAll();

  // If the service provides any configuration form, show it first
  $form = module_invoke($service->module, 'mostpopular_service', 'config', $service->delta);
  if (empty($form)) {
    $form = array();
  }

  //---------------------------------------------------------------------------

  // Add the throttle config options
  $form['sid'] = array(
    '#type' => 'value',
    '#value' => $sid,
  );

  // Add a fieldset for the throttle
  $form['throttle_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Throttle'),
    '#tree' => TRUE,
    '#theme' => 'mostpopular_config_service_form_throttles',
    '#description' => '<p>' . t("This service may have a quota limiting the number of times it can be called\nwithin a given period of time.  By setting the throttle, you can restrict the\nfrequency with which the service is called.") . '</p>' . '<p>' . t("You must specify the time as a <em>positive interval relative to the time the\nservice was last run</em>, in a format the <a href='@strtotime' target='php'>strtotime()</a>\nfunction can understand, such as '%example1 or %example2.  If the result is\nless than the current time, the service can be called again.", array(
      '%example1' => '1 hour',
      '%example2' => '1 day 00:00:00',
      '@strtotime' => 'http://php.net/manual/en/function.strtotime.php',
    )) . '</p>' . '<p>' . t("<em>If you leave this field empty</em>, the service will be called every time\nthe cron job is run or the administrator goes to the Refresh Stats tab.") . '</p>',
  );
  foreach ($intervals as $interval) {
    $iid = $interval->iid;
    $run = MostPopularLastRun::fetch($sid, $iid);
    $form['throttle_fieldset'][$iid]['interval'] = array(
      '#type' => 'item',
      '#title' => t('Interval'),
      '#value' => $interval->title,
    );

    // Add the throttle field
    $form['throttle_fieldset'][$iid]['throttle'] = array(
      '#type' => 'textfield',
      '#title' => t('Minimum elapsed interval'),
      '#default_value' => $run->throttle,
      '#element_validate' => array(
        'mostpopular_service_config_form_validate_throttle',
      ),
    );

    // Show the last time the service was run for this interval
    $form['throttle_fieldset'][$iid]['last_run'] = array(
      '#type' => 'item',
      '#title' => t('The service was last run at'),
      '#value' => $run->last_run == 0 ? 'never' : date('M d, Y H:i:s', $run->last_run),
    );
    $next_run = $run
      ->nextRun();

    // Show the next time the service will run
    $form['throttle_fieldset'][$iid]['next_run'] = array(
      '#type' => 'item',
      '#title' => t('The service can next run at'),
      '#value' => $next_run == time() ? 'now' : date('M d, Y H:i:s', $next_run),
    );
  }
  $form['#submit'][] = 'mostpopular_service_config_form_submit';
  $form = system_settings_form($form);

  // Add a reset button
  $form['buttons']['clear'] = array(
    '#type' => 'submit',
    '#attributes' => array(
      'onclick' => 'javascript:return confirm("' . t("This service will be reset and its most popular items will be cleared. You should\nrefresh the stats after clearing the caches. Are you sure you want to do this?") . '");',
    ),
    '#value' => t('Clear cached values'),
  );
  $form['buttons']['back'] = array(
    '#type' => 'submit',
    '#value' => t('Back'),
  );
  return $form;
}