You are here

function ad in Advertisement 7

Same name and namespace in other branches
  1. 5.2 ad.module \ad()
  2. 5 ad.module \ad()
  3. 6.3 ad.module \ad()
  4. 6 ad.module \ad()
  5. 6.2 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_view in ./ad.module
Implementation of hook_block_view().
ad_embed_nodeapi in embed/ad_embed.module
Implementation of hook_nodeapi().
ad_embed_replace in embed/ad_embed.module
Replaces [[ad]] and <!--ad--> style tags with JavaScript for displaying ads.
ad_remote_form in remote/ad_remote.module
A simple page providing source snippets for displaying ads on remote websites. When form is being submitted, it rebuilds with needed code snippet.
18 string references to 'ad'
ad_cache_file.module in cache/file/ad_cache_file.module
A plug in for the ad.module, providing a file cache mechanism for improved performance when displaying ads.
ad_check_permission in ./ad.module
Determine whether the user has a given privilege.
ad_confirm_group_delete_submit in ./ad.admin.inc
Delete ad group.
ad_cron in ./ad.module
Implementation of hook_cron().
ad_external_adapi in external/ad_external.module
Implementation of hook_adapi().

... See full list

File

./ad.module, line 84

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_installation();
    $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_installation();
    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':
      $query['m'] = $options['ad_display'];

    // Fall through...
    case 'javascript':
    default:
      $query['q'] = $quantity;
      if (isset($options['hostid'])) {
        $query['k'] = $options['hostid'];
      }

      // Allow external cache files to define additional display variables.
      if ($options['cache'] != 'none') {
        $query['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) {
            $query[$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) {
          $query[$key] = $value;
        }
      }
      if (isset($options['nids'])) {

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

          // Choose ads from the provided list of taxonomy terms.
          $tids = $options['tids'];
          $query['t'] = $tids;
          $group = "tids-{$tids}";
        }
        else {

          // Choose ads from the specified group.
          $query['t'] = $group;
          $options['tids'] = $group;
        }
      }
      if (isset($options['url'])) {
        $query['u'] = $options['url'];
      }
      else {
        $query['u'] = $_GET['q'];
        if ($alias = drupal_get_path_alias($_GET['q'])) {
          $query['l'] = $alias;
        }
      }
      $src = url($base_url . '/' . $adserve, array(
        'query' => $query,
      ));
      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="' . htmlentities($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='" . htmlentities($src) . "'></script>";
        }
      }
      break;
  }
  if (user_access('show advertisements')) {
    return theme('ad_display', array(
      'group' => $group,
      'display' => $output,
      'method' => $options['ad_display'],
    ));
    return theme('ad_display', array(
      'group' => $group,
      'display' => $output,
      'method' => $options['ad_display'],
    ));
  }
  else {
    return theme('ad_display', array(
      'group' => 'none',
      'display' => "<!-- Enable 'show advertisements' permission if you wish to display ads here. -->",
    ));
  }
}