You are here

dynamic_banner.module in Dynamic Banner 6

Distributed under GNU GPL version 3

Will create menus and the block needed to view all of the information Also will handle database writing and other cleanup functions

File

dynamic_banner.module
View source
<?php

// $Id: dynamic_banner.module,v 1.13 2011/01/31 21:27:46 coolestdude1 Exp $

/**
 * Distributed under GNU GPL version 3
 * @file
 * Will create menus and the block needed to view all of the information
 * Also will handle database writing and other cleanup functions
 */

/**
 * Module Defaults
 */
define("DEFAULT_OUTPUT", "urltext");
define("DEFAULT_ERROR", FALSE);

/**
 * Hook Functions (Drupal)
 */

/**
 * Implements hook_help().
 */
function dynamic_banner_help($path, $arg) {
  switch ($path) {
    case 'admin/help#dynamic_banner':
      $output = t('
        <p>Provides a mechanism for Pages to automatically generate a banner for use inside a block.</p>
        <h2>Settings</h2>
        <p>Move the block to the part of your theme you wish it to display on.
          I suggest adding in your own template theme for location and printining (see drupal theming)</p>
        <p>Change the theme file (located in dynamic banner module folder) so that you can change what the html is when dynamic banner prints.</p>
        <p>Change the css and js files to do what you like with the banner</p>
        <p>Modify the links inside of the admin/site_building menu or when you create new pages a specific banner appears on it, and / or make a default banner for non specific pages. </p>
        <p>Follow the convention of * for wild cards (no slash required) and ! for randoms (see examples)</p>
        <p>Example normal path = about/us</p>
        <p>Example random path = about/us!</p>
        <p>Example wildcard path = about/us*</p>
        <p>Please note that there is no leading slash or terminating slash (no slashing the begining or end)</p>
        <p>In the future you will be able to navigate to the image and select it and the url will be inserted for you</p>
        <p>Or when you choose to upload one on the spot the url will be grabbed from that.<p>
        <p>Example img path = sites/all/themes/banners/default.jpg</p>');
      return $output;
  }
}

/**
 * Implements hook_perm().
 */
function dynamic_banner_perm() {
  return array(
    'administer dynamic_banner',
  );
}

/**
 * Implements hook_menu().
 */
function dynamic_banner_menu() {
  $items = array();

  // settings page located in admin pages
  $items['admin/build/banners/settings'] = array(
    'title' => t('Settings'),
    'description' => 'A page for the dynamic banner settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_settings',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/callbacks.inc',
  );

  // admin menu located in site building
  $items['admin/build/banners'] = array(
    'title' => t('Dynamic Banner'),
    'description' => 'Configure the Dynamic Banner.',
    'page callback' => 'dynamic_banner_admin_page',
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'file' => 'includes/callbacks.inc',
  );

  // the defaulted page to load on menu click
  $items['admin/build/banners/list'] = array(
    'title' => t('List Banners'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

  // part of the path when using querys
  $items['admin/build/banners/edit/%'] = array(
    'title' => t('Edit Banner'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_form',
      4,
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/callbacks.inc',
  );

  // part of the path when using querys
  $items['admin/build/banners/delete'] = array(
    'title' => t('Delete Banner'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_delete_confirm',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/callbacks.inc',
  );

  // a part of the local menu (top)
  $items['admin/build/banners/add'] = array(
    'title' => t('Add Banner'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_form',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/callbacks.inc',
  );

  // a part of the local menu (top)
  $items['admin/build/banners/default'] = array(
    'title' => t('Default Banner'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_default_admin_form',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/callbacks.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function dynamic_banner_theme() {
  $theme = array(
    'banner_output' => array(
      'arguments' => array(
        'url' => NULL,
        'text' => NULL,
        'link' => NULL,
        'display_setting' => NULL,
        'display_errors' => NULL,
      ),
      'template' => 'dynamic_banner_template',
    ),
  );
  return $theme;
}

/**
 * Implements hook_block().
 */
function dynamic_banner_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks = array();
      $blocks[0] = array(
        'info' => t('Dynamic Banner Block'),
        'cache' => BLOCK_NO_CACHE,
      );
      return $blocks;
    case 'view':
      switch ($delta) {
        case 0:

          // store the path of the page the block is loading from, this will seed our searches
          $path = drupal_substr(check_plain(request_uri()), 1);

          // construct the mysql statement and query the database, does not need to be setup every time
          $sql = "SELECT * FROM {dynamic_banner} WHERE path ='%s'";

          // loop until we find the top down hirarchy
          do {

            // exact matches //
            $result = NULL;
            $result = db_query($sql, $path);
            $object = db_fetch_object($result);

            // search for that path string exact match
            if ($object->imgurl) {
              $content = theme('banner_output', $object->imgurl, $object->text, $object->link, variable_get('dynamic_banner_display_setting', DEFAULT_OUTPUT), variable_get('dynamic_banner_display_errors', DEFAULT_ERROR));
              return array(
                'content' => $content,
              );
            }

            // wild section //
            $result = NULL;
            $wild_search = $path . '*';
            $result = db_query($sql, $wild_search);
            $object = db_fetch_object($result);

            // search for the wild card string exact match
            if ($object->imgurl) {
              $content = theme('banner_output', $object->imgurl, $object->text, $object->link, variable_get('dynamic_banner_display_setting', DEFAULT_OUTPUT), variable_get('dynamic_banner_display_errors', DEFAULT_ERROR));
              return array(
                'content' => $content,
              );
            }

            // random section //
            $result = NULL;
            $random_search = $path . '!';
            $result = db_query($sql, $random_search);
            $object = db_fetch_object($result);

            // search for that random string exact match
            if ($object->imgurl) {

              // get extra stuff associated with randoms
              $images = $object->imgurl;

              // support for random text if needed
              $texts = $object->text;

              // explode comma seperated images and text
              $image = explode(",", $images);

              // support for random text if needed
              $text = explode(",", $texts);

              // count how many there are
              $count = count($image);

              // handle the random with ints (deal with array start at 0 problems)
              // so if there are 3 elements in the array it is 0-2 not 1-3 so generate random based on that
              $random = $count - rand(0, $count - 1) - 1;

              // remember text is optional
              $content = theme('banner_output', $image[$random], $text[$random], $object->link, variable_get('dynamic_banner_display_setting', DEFAULT_OUTPUT), variable_get('dynamic_banner_display_errors', DEFAULT_ERROR));
              return array(
                'content' => $content,
              );
            }

            // chop off more of the string and try again, it is key to not modify the path before this point
            $last_slash_position = strrpos($path, "/");

            // returns false if not found
            if ($last_slash_position !== FALSE) {
              $path = drupal_substr($path, 0, $last_slash_position);
            }
            else {
              $path = FALSE;
            }
          } while ($path != FALSE);

          // well no banner was found for this specific page if we have a default banner then display it
          $default_banner_json = variable_get('dynamic_banner_default_banner', '');
          if ($default_banner_json !== '') {
            $default_banner = json_decode($default_banner_json);
            $content = theme('banner_output', $default_banner->imgurl, $default_banner->text, $default_banner->link, variable_get('dynamic_banner_display_setting', DEFAULT_OUTPUT), variable_get('dynamic_banner_display_errors', DEFAULT_ERROR));
            return array(
              'content' => $content,
            );
          }
          break;
      }
  }
}

Functions

Constants

Namesort descending Description
DEFAULT_ERROR
DEFAULT_OUTPUT Module Defaults