You are here

function mostpopular_intervals_form_submit in Drupal Most Popular 6

File

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

Code

function mostpopular_intervals_form_submit($form, $form_state) {
  switch ($form_state['values']['op']) {
    case $form_state['values']['reset']:
      MostPopularInterval::reset();
      drupal_set_message(t('The interval configuration has been reset.'));
      mostpopular_clear_caches();
      break;
    default:
      $intervals = $form_state['values']['intervals'];
      foreach ($intervals as $i => $inv) {
        $title = $inv['title'];
        $string = $inv['string'];
        $weight = $inv['weight'];
        $object = $inv['object'];
        $iid = $object->iid;

        // If there is data, save it
        if (!empty($title) && !empty($string)) {

          // If the interval changed, reset the throttles for this interval
          if ($iid > 0 && $object->string != $string) {
            MostPopularLastRun::resetThrottles(NULL, $iid);
            drupal_set_message(t('The service throttles were reset for interval %title.', array(
              '%title' => $title,
            )));
          }

          // Update the interval and save it
          $object
            ->update(array(
            'title' => $title,
            'string' => $string,
            'weight' => $weight,
          ));
          if ($iid == 0) {
            drupal_set_message(t('Interval %title was added. The default service throttles will be used for it.', array(
              '%title' => $object
                ->fullTitle(),
            )));
          }
          $object
            ->save();
        }
        elseif ($iid > 0) {

          // Remove the interval
          $object
            ->remove();

          // Reset the throttles for the interval
          MostPopularLastRun::resetThrottles(NULL, $iid);

          // Remove any cached most popular items for the interval
          MostPopularItem::reset(NULL, $iid);
          drupal_set_message(t('Interval %title was removed and the custom service throttles for it were cleared.', array(
            '%title' => $object
              ->fullTitle(),
          )));
        }
      }
      drupal_set_message(t('The interval configuration has been saved.'));
      break;
  }
}