You are here

ad_text.module in Advertisement 5.2

Enhances the ad module to support static text ads.

Copyright (c) 2005-2008. Jeremy Andrews <jeremy@kerneltrap.org>. All rights reserved.

File

text/ad_text.module
View source
<?php

/**
 * @file
 * Enhances the ad module to support static text ads.
 *
 * Copyright (c) 2005-2008.
 *   Jeremy Andrews <jeremy@kerneltrap.org>.  All rights reserved.
 */

/**
 * Function used to display the selected ad.
 */
function ad_text_display_ad($ad) {
  $ad_text = "<div class=\"text-advertisement\" id=\"ad-{$ad->aid}\"><div class=\"ad-header\"><a href=\"{$ad->redirect}/@HOSTID___\"" . ad_link_attributes() . '>' . ad_text_display_prepare($ad->adheader) . "</a></div><div class=\"ad-body\">" . ad_text_display_prepare($ad->adbody) . "</div></div>";
  if (variable_get('ad_filter', 0)) {
    $ad_text = check_markup($ad_text, $ad->format, FALSE);
  }
  return $ad_text;
}

/**
 * Strip illegal characters, encode the rest UTF-8.
 */
function ad_text_display_prepare($string, $format = FALSE) {
  $string = preg_replace("/[\\b\f\n\r\t]/", ' ', $string);
  $string = htmlentities($string, ENT_QUOTES, 'UTF-8');
  if ($format !== FALSE && variable_get('ad_filter', 0)) {
    $string = check_markup($string, $format, FALSE);
  }
  return $string;
}
function ad_text_init() {
  if (function_exists('drupal_set_content')) {
    if (module_exists('views')) {
      include drupal_get_path('module', 'ad_text') . '/ad_text_views.inc';
    }
  }
}

/**
 * Implementation of hook_help().
 */
function ad_text_help($path) {
  switch ($path) {
    case 'node/add/ad#text':
      $output = t('A text advertisement.');
      break;
  }
  return $output;
}
function ad_text_global_settings($edit = array()) {
  $form = array();
  $form['header_min'] = array(
    '#type' => 'textfield',
    '#title' => t('Header minimum length'),
    '#size' => 3,
    '#maxlength' => 3,
    '#default_value' => variable_get('header_min', 0),
    '#description' => t('Optionally specify the minimum number of characters allowed in the header of a text ad.  Set to <em>0</em> to specify no minimum length.'),
  );
  $form['header_max'] = array(
    '#type' => 'textfield',
    '#title' => t('Header maximum length'),
    '#size' => 3,
    '#maxlength' => 3,
    '#default_value' => variable_get('header_max', 0),
    '#description' => t('Optionally specify the maximum number of characters allowed in the header of a text ad.  Set to <em>0</em> to specify no maximum length.'),
  );
  $form['body_min'] = array(
    '#type' => 'textfield',
    '#title' => t('Body minimum length'),
    '#size' => 10,
    '#maxlength' => 10,
    '#default_value' => variable_get('body_min', 0),
    '#description' => t('Optionally specify the minimum number of characters allowed in the body of a text ad.  Set to <em>0</em> to specify no minimum length.'),
  );
  $form['body_max'] = array(
    '#type' => 'textfield',
    '#title' => t('Body maximum length'),
    '#size' => 10,
    '#maxlength' => 10,
    '#default_value' => variable_get('body_max', 0),
    '#description' => t('Optionally specify the maximum number of characters allowed in the body of a text ad.  Set to <em>0</em> to specify no maximum length.'),
  );
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * 
 */
function ad_text_global_settings_validate($form_id, $form_values) {
  if ((int) $form_values['header_min'] > (int) $form_values['header_max']) {
    form_set_error('header_min', t('The header minimum length can not be greater than the header maximum length.'));
  }
  if ((int) $form_values['body_min'] > (int) $form_values['body_max']) {
    form_set_error('body_min', t('The body minimum length can not be greater than the body maximum length.'));
  }
}

/**
 *
 */
function ad_text_global_settings_submit($form_id, $form_values) {
  variable_set('header_min', $form_values['header_min']);
  variable_set('header_max', $form_values['header_max']);
  variable_set('body_min', $form_values['body_min']);
  variable_set('body_max', $form_values['body_max']);
}
function ad_text_adapi($op, &$node) {
  switch ($op) {
    case 'load':
      $return = db_fetch_array(db_query('SELECT * FROM {ad_text} WHERE aid = %d', $node['aid']));
      $return['ad'] = ad_text_display_prepare($return['adheader'], $node->format) . '<br />' . ad_text_display_prepare($return['adbody'], $node->format);
      return $return;
    case 'insert':
      db_query("INSERT INTO {ad_text} (aid, url, adheader, adbody) VALUES(%d, '%s', '%s', '%s')", $node->nid, $node->url, $node->adheader, $node->adbody);
      break;
    case 'update':
      db_query("UPDATE {ad_text} SET url = '%s', adheader = '%s', adbody = '%s' WHERE aid = %d", $node->url, $node->adheader, $node->adbody, $node->nid);
      break;
    case 'delete':
      db_query('DELETE FROM {ad_text} WHERE aid = %d', $node->nid);
      break;
    case 'form':
      return ad_text_node_form($node);
    case 'view':
      return ad_text_node_view($node);
    case 'redirect':
      return db_result(db_query('SELECT url FROM {ad_text} WHERE aid = %d', $node->nid));
    case 'validate':
      return ad_text_node_validate($node);
    case 'type':
      return 'text';
    case 'permissions':
      if (!isset($node->adtype) || $node->adtype == 'text') {
        return array(
          'manage ad text' => TRUE,
        );
      }
  }
}

/**
 * Adapi helper function for displaying a node form.
 */
function ad_text_node_form(&$node) {
  $form = array();
  $form['ad_text'] = array(
    '#type' => 'fieldset',
    '#title' => t('Text'),
    '#collapsible' => TRUE,
  );
  $form['ad_text']['text'] = array(
    '#type' => 'markup',
    '#value' => ad_text_display_ad($node),
  );
  if (ad_permission($node->nid, 'manage ad text') || arg(1) == 'add' && user_access('create advertisements')) {
    $form['ad_text']['url'] = array(
      '#type' => 'textfield',
      '#title' => t('Destination URL'),
      '#required' => TRUE,
      '#maxlength' => 255,
      '#default_value' => $node->url,
      '#description' => t('Enter the complete URL where you want people to be redirected when they click on this advertisement.  The URL must begin with http:// or https://.  For example, %url.', array(
        '%url' => t('http://www.sample.org/'),
      )),
    );
    $form['ad_text']['adheader'] = array(
      '#type' => 'textfield',
      '#title' => t('Ad header'),
      '#required' => TRUE,
      '#default_value' => $node->adheader,
      '#description' => t('This is the first line of the ad which will be linked to the URL entered above.'),
    );
    $form['ad_text']['adbody'] = array(
      '#type' => 'textarea',
      '#title' => t('Ad body'),
      '#required' => TRUE,
      '#default_value' => $node->adbody,
      '#description' => t('This is the rest of the ad.'),
    );
  }
  return $form;
}
function ad_text_node_view(&$node) {
  $node->content['ad'] = array(
    '#value' => theme('box', '', stripslashes(ad_text_display_ad($node))),
    '#weight' => -1,
  );
  $link = t('Links to !url.', array(
    '!url' => $node->url,
  ));
  if (variable_get('ad_filter', 0)) {
    $link = check_markup($link, $node->format, FALSE);
  }
  $node->content['ad-link'] = array(
    '#value' => "<div class=\"links-to\">{$link}</div>",
    '#weight' => 1,
  );
}
function ad_text_node_validate($node) {

  // Enforce minimum and maximum lengths.
  $header_len = strlen($node->adheader);
  $header_min = variable_get('header_min', 0);
  $header_max = variable_get('header_max', 0);
  if ($header_min && $header_len < $header_min) {
    form_set_error('adheader', t('Your text ad header is only %cur characters but must be at least %min characters.', array(
      '%cur' => $header_len,
      '%min' => $header_min,
    )));
  }
  else {
    if ($header_max && $header_len > $header_max) {
      form_set_error('adheader', t('Your text ad header is %cur characters but can not be more than %max characters.', array(
        '%cur' => $header_len,
        '%max' => $header_max,
      )));
    }
  }
  $body_len = strlen($node->adbody);
  $body_min = variable_get('body_min', 0);
  $body_max = variable_get('body_max', 0);
  if ($body_min && $body_len < $body_min) {
    form_set_error('adbody', t('Your text ad body is only %cur characters but must be at least %min characters.', array(
      '%cur' => $body_len,
      '%min' => $body_min,
    )));
  }
  else {
    if ($body_max && $body_len > $body_max) {
      form_set_error('adbody', t('Your text ad body is %cur characters but can not be more than %max characters.', array(
        '%cur' => $body_len,
        '%max' => $body_max,
      )));
    }
  }
  if (variable_get('ad_validate_url', 1) && !valid_url($node->url, TRUE)) {
    form_set_error('url', t('You must specify a valid %field.', array(
      '%field' => t('Destination URL'),
    )));
  }
}

Functions

Namesort descending Description
ad_text_adapi
ad_text_display_ad Function used to display the selected ad.
ad_text_display_prepare Strip illegal characters, encode the rest UTF-8.
ad_text_global_settings
ad_text_global_settings_submit
ad_text_global_settings_validate
ad_text_help Implementation of hook_help().
ad_text_init
ad_text_node_form Adapi helper function for displaying a node form.
ad_text_node_validate
ad_text_node_view