You are here

function ad in Advertisement 5.2

Same name and namespace in other branches
  1. 5 ad.module \ad()
  2. 6.3 ad.module \ad()
  3. 6 ad.module \ad()
  4. 6.2 ad.module \ad()
  5. 7 ad.module \ad()

Use this function to display ads from a specified group.

Parameters

$group: The ad group tid to display ads from.

$quantity: Optionally specify the number of unique ads to display.

$options: Any number of options from this list: hostid, nids.

4 calls to ad()
ad_block in ./ad.module
Drupal _block hook.
ad_embed_nodeapi in embed/ad_embed.module
Drupal _nodeapi hook.
ad_embed_replace in embed/ad_embed.module
Replaces [[ad]] and <!--ad--> style tags with JavaScript for displaying ads.
ad_remote_form_submit in remote/ad_remote.module
Prepare quantity and group.
11 string references to 'ad'
ad_add in ./ad.module
Present a list of ad types to choose from.
ad_confirm_group_delete_submit in ./ad.module
Delete ad group.
ad_cron in ./ad.module
Drupal _cron hook.
ad_external_adapi in external/ad_external.module
ad_form in ./ad.module
Drupal _form hook.

... See full list

File

./ad.module, line 21
An advertising system for Drupal powered websites.

Code

function ad($group = FALSE, $quantity = 1, $options = array()) {
  global $base_url;
  $adserve = variable_get('adserve', '');
  $adserveinc = variable_get('adserveinc', '');
  if (empty($adserve) || empty($adserveinc)) {

    // This is probably the first time ad() has been called.
    _ad_check_install();
    $adserve = variable_get('adserve', '');
    $adserveinc = variable_get('adserveinc', '');
  }
  if (!file_exists($adserve) || !file_exists($adserveinc)) {
    drupal_set_message(t('Ads cannot be displayed.  The ad module is <a href="@misconfigured">misconfigured</a>, failed to locate the required <em>serve.php</em> ond/or <em>adserve.inc</em> file.', array(
      '@misconfigured' => url('admin/content/ad/configure'),
    )), 'error');
    _ad_check_install();
    return t('The ad module is <a href="@misconfigured">misconfigured</a>.', array(
      '@misconfigured' => url('admin/content/ad/configure'),
    ));
  }

  // Be sure a display method has been chosen.
  if (!isset($options['ad_display'])) {
    $options['ad_display'] = variable_get('ad_display', 'javascript');
  }
  $options['quantity'] = isset($quantity) ? $quantity : 1;
  if (!isset($options['tids'])) {
    $options['tids'] = $group;
  }
  $options['cache'] = variable_get('ad_cache', 'none');
  switch ($options['ad_display']) {
    case 'raw':
      require_once drupal_get_path('module', 'ad') . '/adserve.inc';
      require_once drupal_get_path('module', 'ad') . '/adcache.inc';
      $output = adserve_ad($options);
      break;
    case 'iframe':
    case 'jquery':
      $display_variables = 'm=' . $options['ad_display'];

    // Fall through...
    case 'javascript':
    default:
      if ($display_variables) {
        $display_variables .= "&amp;q={$quantity}";
      }
      else {
        $display_variables = "q={$quantity}";
      }
      if ($hostid = $options['hostid']) {
        $display_variables .= "&amp;k={$hostid}";
      }

      // Allow external cache files to define additional display variables.
      if ($options['cache'] != 'none') {
        $display_variables .= '&amp;c=' . $options['cache'];
        $cache_variables = module_invoke('ad_cache_' . $options['cache'], 'adcacheapi', 'display_variables', array());
        if (is_array($cache_variables)) {
          foreach ($cache_variables as $key => $value) {
            $display_variables .= "&amp;{$key}={$value}";
          }
        }
      }

      // Allow ad_type modules to define additional display variables.
      $type_variables = module_invoke_all('adapi', 'display_variables', array());
      if (is_array($type_variables)) {
        foreach ($type_variables as $key => $value) {
          $display_variables .= "&amp;{$key}={$value}";
        }
      }
      if ($nids = $options['nids']) {

        // Choose ads from the provided list of node Id's.
        $display_variables .= "&amp;n={$nids}";
        $group = "nids-{$nids}";
      }
      else {
        if ($tids = $options['tids']) {

          // Choose ads from the provided list of taxonomy terms.
          $display_variables .= "&amp;t={$tids}";
          $group = "tids-{$tids}";
        }
        else {

          // Choose ads from the specified group.
          $display_variables .= "&amp;t={$group}";
          $options['tids'] = $group;
        }
      }
      if (isset($options['url'])) {
        $display_variables .= '&amp;u=' . $options['url'];
      }
      else {
        $display_variables .= '&amp;u=' . $_GET['q'];
      }
      $src = url("{$base_url}/{$adserve}?{$display_variables}");
      if ($options['ad_display'] == 'iframe') {

        // TODO: We need to know the IFrame size before it is displayed.  This
        // limits the flexibility of what can be displayed in these frames.
        // For now we'll have a global value, later we'll add per-group
        // over-rides.
        $append = 'frameborder="' . variable_get('ad_iframe_frameborder', 0) . '" ';
        $append .= 'scrolling="' . variable_get('ad_iframe_scroll', 'auto') . '" ';
        $append .= "name=\"{$group}\" ";
        if ($height = variable_get('ad_iframe_height', '')) {
          $append .= "height=\"{$height}\" ";
        }
        if ($width = variable_get('ad_iframe_width', '')) {
          $append .= "width=\"{$width}\" ";
        }
        $output = "<iframe src=\"{$src}\" {$append}></iframe>";
      }
      else {
        if ($options['ad_display'] == 'jquery') {

          // The theme function uses this to generate a CSS id for jQuery to use.
          $output = $src;
        }
        else {
          $output = "<script type='text/javascript' src='{$src}'></script>";
        }
      }
      break;
  }
  if (user_access('show advertisements')) {
    if ($options['div'] !== FALSE) {
      return theme('ad_display', $group, $output, $options['ad_display']);
    }
    else {
      return theme('ad_display', $group, $output, 'raw');
    }
  }
  else {
    return theme('ad_display', 'none', "<!-- Enable 'show advertisements' permission if you wish to display ads here. -->");
  }
}