You are here

function ad_weight_percent_settings_validate in Advertisement 6

Same name and namespace in other branches
  1. 5.2 weight/percent/ad_weight_percent.module \ad_weight_percent_settings_validate()
  2. 5 weight/percent/ad_weight_percent.module \ad_weight_percent_settings_validate()
  3. 6.3 weight/percent/ad_weight_percent.module \ad_weight_percent_settings_validate()
  4. 6.2 weight/percent/ad_weight_percent.module \ad_weight_percent_settings_validate()
  5. 7 weight/percent/ad_weight_percent.module \ad_weight_percent_settings_validate()

Be sure that all enabled groups add up to a total of 100%.

File

weight/percent/ad_weight_percent.module, line 116
A plug in for the ad.module, providing a percentage based weighting mechanism for the random selection of ads.

Code

function ad_weight_percent_settings_validate($form, &$form_state) {
  $groups = module_invoke('ad', 'groups_list', TRUE);
  foreach ($groups as $tid => $group) {
    if ($form_state['values']["enable-{$tid}"]) {
      $result = db_query('SELECT nid FROM {term_node} WHERE tid = %d', $group->tid);
      $total = 0;

      // Add up total percentages for all nids in group, confirm equals 100%.
      $first = 0;
      while ($nid = db_fetch_object($result)) {
        if (!$first) {
          $first = $nid->nid;
        }
        $total = $total + (int) $form_state['values']["percent-{$tid}-{$nid->nid}"];
      }

      // Confirmed that total equals 100%.
      if ($total != 100) {
        form_set_error("percent-{$tid}-{$first}", t('The total percentage for all ads in the %group group combined must equal 100%.  It currently equals %percent.', array(
          '%group' => $group->name,
          '%percent' => "{$total}%",
        )));
      }
    }
  }
}