You are here

function _ad_check_install in Advertisement 5.2

Same name and namespace in other branches
  1. 5 ad.module \_ad_check_install()
5 calls to _ad_check_install()
ad in ./ad.module
Use this function to display ads from a specified group.
ad_admin_configure_settings in ./ad.module
Display a form for the ad module settings.
ad_admin_groups_list in ./ad.module
ad_admin_list in ./ad.module
Build default ad administration page.
ad_channel_admin_overview in channel/ad_channel.module
Display containers and channels.

File

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

Code

function _ad_check_install() {

  // Verify serve.php exists and is readable.
  $adserve = variable_get('adserve', '');
  $adserveinc = variable_get('adserveinc', '');
  if (!file_exists($adserve)) {

    // The serve.php file should be in the same directory as the ad.module.
    $adserve = drupal_get_path('module', 'ad') . '/serve.php';
    variable_set('adserve', $adserve);
  }
  if (!is_readable($adserve)) {
    variable_set('adserve', '');
    drupal_set_message(t('Failed to read the required file %filename.  Please make the file readable by the webserver process.  No ads can be displayed until this problem is resolved.', array(
      '%filename' => $adserve,
    )), 'error');
  }
  if (!file_exists($adserveinc)) {

    // The adserve.inc file should be in the same directory as the ad.module.
    $adserveinc = drupal_get_path('module', 'ad') . '/adserve.inc';
    variable_set('adserveinc', $adserveinc);
  }
  if (!is_readable($adserveinc)) {
    variable_set('adserveinc', '');
    drupal_set_message(t('Failed to read the required file %filename.  Please make the file readable by the webserver process.  No ads can be displayed until this problem is resolved.', array(
      '%filename' => $adserveinc,
    )), 'error');
  }

  // Validate vid in vocabulary table.
  $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = 'ad'"));
  if ($vid != variable_get('ad_group_vid', '')) {
    drupal_set_message(t('Invalid vocabulary defined for advertisements, attempting to auto-fix.'), 'error');
    if ($vid) {
      db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d OR type = 'ad'", variable_get('ad_group_vid', ''));
      variable_set('ad_group_vid_restore', variable_get('ad_group_vid', ''));
    }
    variable_del('ad_group_vid');
  }
  else {

    // Validate vid in vocabulary_node_types table.
    $result = db_query("SELECT vid FROM {vocabulary_node_types} WHERE type = 'ad'");
    $found = FALSE;
    while ($vocab = db_fetch_object($result)) {
      if ($vocab->vid == variable_get('ad_group_vid', '')) {
        $found = TRUE;
      }
    }
    if (!$found) {
      drupal_set_message(t('Missing vocabulary node type for advertisements, attempting to auto-fix.'), 'error');
      db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d OR type = 'ad'", variable_get('ad_group_vid', ''));
      db_query("DELETE FROM {vocabulary} WHERE vid = %d", variable_get('ad_group_vid', ''));
      variable_set('ad_group_vid_restore', variable_get('ad_group_vid', ''));
      variable_del('ad_group_vid');
    }
  }
  _ad_get_vid();

  // Preserve old ad groups, if any.
  if (($old = variable_get('ad_group_vid_restore', '')) && ($vid = variable_get('ad_group_vid', ''))) {
    drupal_set_message(t('Restoring orphaned ad group configuration.'));
    db_query('UPDATE {term_data} SET vid = %d WHERE vid = %d', $vid, $old);
    variable_set('ad_group_vid_restore', '');
  }
  $result = db_query("SELECT rid FROM {permission} WHERE perm LIKE '%%show advertisements%%'");
  if (!db_num_rows($result)) {
    drupal_set_message(t('Be sure to enable "!show" permissions for all roles that you wish to see advertisements.', array(
      '!show' => l(t('show advertisements'), 'admin/user/access'),
    )));
  }

  // Allow modules to define an action to take each time an ad is served.
  // When modules define 'adserve_select' or 'adserve_filter', they must set
  // the 'function' and 'path' parameters.  The 'weight' parameter can
  // optionally be set.
  //  function: the function to call when serving an add
  //  path: the path to the include file where $function is defined
  // Modules can define actions that happen when advertisements are served.
  // Currently support actions are:
  //  - init_text   (display content before displaying ads) // TODO
  //  - select      (alter which ads are selected to be displayed) // TODO
  //  - filter      (filter selected ads before they are displayed) // TODO
  //  - exit_text   (display content after displaying ads) // TODO
  $hooks = array(
    'init_text',
    'select',
    'filter',
    'exit_text',
  );
  foreach ($hooks as $hook) {
    $adserve_actions = module_invoke_all('adapi', "adserve_{$hook}", array());
    $actions = array();
    foreach ($adserve_actions as $name => $action) {
      if (is_numeric($action['weight'])) {
        $weight = $action['weight'];
      }
      else {

        // weight is an optional, defaults to 0
        $weight = $action['weight'] = 0;
      }
      $actions["{$weight}.{$name}"] = $action;
      $actions["{$weight}.{$name}"]['name'] = $name;
    }

    // order actions by weight (multiple same-weight actions sorted by alpha)
    ksort($actions);
    variable_set("adserve_{$hook}", serialize($actions));
  }
  module_invoke_all('adapi', 'check_install', array());
}