You are here

function _ad_channel_validate_nodes in Advertisement 6.3

Same name and namespace in other branches
  1. 5.2 channel/ad_channel.module \_ad_channel_validate_nodes()
  2. 6.2 channel/ad_channel.module \_ad_channel_validate_nodes()
  3. 7 channel/ad_channel.module \_ad_channel_validate_nodes()

Be sure that the enabled channels actually can be enabled.

1 call to _ad_channel_validate_nodes()
ad_channel_nodeapi in channel/ad_channel.module
Implementation of hook_nodeapi().

File

channel/ad_channel.module, line 731
Ad Channel module.

Code

function _ad_channel_validate_nodes($node) {
  $channels = _ad_channel_get_enabled($node);

  /* We need to get the prior state to know if this is added as an active ad
   * to the channel or if the ad has been set to active. The limit is on the
   * number of *active* advertisements in a channel. */
  $channels_preedit = _ad_channel_load_nid_channels($node->nid);
  $adstatus_preedit = db_result(db_query("SELECT adstatus FROM {ads} WHERE aid = %d", $node->aid));
  $transitioned_to_active = $node->adstatus == 'active' && $adstatus_preedit != 'active';
  foreach ($channels as $chid) {
    $channel_added_active = !in_array($chid, $channels_preedit) && $node->adstatus == 'active';
    if ($channel_added_active || $transitioned_to_active) {
      $channel = _ad_channel_get_channels($chid);
      if (isset($channel->inventory) && $channel->inventory > 0) {
        $num_ads = _ad_channel_get_channel_active_ad_count($chid);
        if ($num_ads + 1 > $channel->inventory) {
          form_set_error("channel[{$channel->conid}][channel-{$chid}]", t('You may not assign more than !inventory to %name.', array(
            '!inventory' => format_plural($channel->inventory, '1 active advertisement', '@count active advertisements'),
            '%name' => $channel->name,
          )));
        }
      }
    }
  }
  $limit = variable_get('ad_channel_ad_limit', 0);
  if ($limit && sizeof($channels) > $limit) {
    $quantity_error = TRUE;
  }
  else {
    $quantity_error = FALSE;
  }
  foreach ($channels as $chid) {
    $channel = _ad_channel_get_channels($chid);
    if ($quantity_error) {
      $quantity_error = FALSE;
      form_set_error("channel[{$channel->conid}][channel-{$chid}]", t('You can not assign this advertisement to more than !limit.', array(
        '!limit' => format_plural(sizeof($limit), '1 channel', '@count channels'),
      )));
    }
    $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array();
    $groups = unserialize($channel->groups);
    if (!empty($groups)) {
      $enabled = FALSE;
      foreach ($groups as $group) {
        if ($group) {
          $enabled = TRUE;
          break;
        }
      }
      if ($enabled) {
        $ad_groups = taxonomy_get_tree(_ad_get_vid());
        foreach ($ad_groups as $ad_group) {
          if (is_array($taxonomy[$ad_group->vid]) && isset($taxonomy[$ad_group->vid][$ad_group->tid]) && isset($groups[$ad_group->tid]) && !$groups[$ad_group->tid] && !isset($groups[''])) {
            form_set_error("channel[{$channel->conid}][channel-{$chid}]", t('The %channel channel does not allow advertisements from the %group group.', array(
              '%channel' => $channel->name,
              '%group' => $ad_group->name,
            )));
          }
        }
      }
    }
  }
}