You are here

function mostpopular_intervals_admin_form_validate in Drupal Most Popular 7

File

./mostpopular.intervals.inc, line 128
Provides an admin GUI for configuring intervals.

Code

function mostpopular_intervals_admin_form_validate($form, &$form_state) {
  $intervals = $form_state['intervals'];

  // Validate the intervals
  foreach ($form_state['values']['blocks'] as $bid => $block) {
    if (isset($block['intervals'])) {
      $good = FALSE;
      foreach ($block['intervals'] as $iid => $values) {

        // Lookup the interval definition.
        if (isset($intervals[$bid][$iid])) {
          $interval = $intervals[$bid][$iid];
        }
        $title = $values['title'];
        $string = $values['string'];
        if (empty($title)) {
          if (empty($string)) {

            // Ignore blank rows
            unset($form_state['intervals'][$bid][$iid]);
          }
          else {
            form_set_error("blocks][{$bid}][intervals][{$iid}][title", t('You must specify the title to show users for this interval.'));
          }
        }
        else {
          if (empty($string) || strtotime($string) === FALSE) {
            form_set_error("blocks][{$bid}][intervals][{$iid}][string", t("You must specify an interval that can be understood by <a href='@strtotime' target='php'>strtotime()</a>.", array(
              '@strtotime' => 'http://php.net/manual/en/function.strtotime.php',
            )));
          }
          elseif (strtotime($string) > time()) {
            form_set_error("blocks][{$bid}][intervals][{$iid}][string", t('You must specify a negative interval relative to the current time, i.e. "-1 week"'));
          }
          else {

            // Update the interval object
            if ($string != $interval->string) {
              $interval->dirty = TRUE;
            }
            $interval->string = $string;
            if ($title != $interval->title) {
              $interval->dirty = TRUE;
            }
            $interval->title = $title;
            $interval->bid = $values['bid'];
            $interval->weight = $values['weight'];
            $form_state['intervals'][$bid][$iid] = $interval;
            $good = TRUE;
          }
        }
      }
      if (!$good) {
        form_set_error(NULL, t('You must define at least one interval for each block.'));
      }
    }
  }
}