You are here

function ad_weight_probability_form_alter in Advertisement 7

Same name and namespace in other branches
  1. 5.2 weight/probability/ad_weight_probability.module \ad_weight_probability_form_alter()
  2. 6.3 weight/probability/ad_weight_probability.module \ad_weight_probability_form_alter()
  3. 6.2 weight/probability/ad_weight_probability.module \ad_weight_probability_form_alter()

Implementation of hook_form_alter(). Generate a form for assigning a weight to an advertisement.

File

weight/probability/ad_weight_probability.module, line 18
A plug in for the ad.module, allowing an admin to set the probability that a given advertisement will be displayed.

Code

function ad_weight_probability_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']) && $form_id == 'ad_node_form') {
    $node = $form['#node'];
    $form['weighting'] = array(
      '#type' => 'fieldset',
      '#access' => user_access('configure ad probability'),
      '#title' => t('Weight'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['weighting']['probability'] = array(
      '#type' => 'select',
      '#access' => user_access('configure ad probability'),
      '#title' => t('Probability'),
      '#options' => _ad_weight_probability_weights(),
      '#default_value' => isset($node->probability) ? $node->probability : 100,
      '#description' => t('The greater the probability, the more frequently this advertisement will be displayed.  An advertisement with a probablity of 2 will be displayed twice as frequently as an advertisement with a probability of 1.'),
    );
    $form['weighting']['#weight'] = -1;
  }
  else {
    if ($form_id == 'ad_admin_ads' && function_exists('ad_channel_form_alter')) {
      if (variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL) != AD_CHANNEL_LIST_GROUP) {
        $weights = _ad_weight_probability_weights();

        // ensure a filter has not been set that yeilds no results
        if (isset($form['title']) && is_array($form['title'])) {
          foreach ($form['title'] as $aid => $value) {
            $node->nid = $aid;
            $result = _ad_weight_probability_node_load($node);
            $form['probability'][$aid]['#value'] = $weights[$result['probability']];
          }
        }
      }
    }
  }
}