You are here

dynamic_banner.module in Dynamic Banner 7

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$

/**
 * 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
 */

// load the include file
include_once 'includes/callbacks.inc';

/**
 * Module Defaults
 */
define('BANNER_DEFAULT_OUTPUT', 'urltext');
define('BANNER_DEFAULT_ERROR', FALSE);
define('BANNER_DEFAULT_BLOCK_NAME', 'Dynamic Banner Block');
define('BANNER_DEFAULT_SAVE_LOCATION', 'public://banners/');
define('BANNER_DEFAULT_BANNER_MODE', 'normal');

/**
 * Hook Functions (Drupal)
 */

/**
 * Implements hook_init().
 * Will add the js and css files to display on anypage that the banner block is on
 */
function dynamic_banner_init() {
  drupal_add_css(drupal_get_path('module', 'dynamic_banner') . '/css/banner.css');
  drupal_add_js(drupal_get_path('module', 'dynamic_banner') . '/js/banner.js');
}

/**
 * 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;
  }

  // end switch
}

// end help function

/**
 * Implements hook_permission().
 * Permissions for dynamic banner
 */
function dynamic_banner_permission() {
  return array(
    'administer dynamic_banner' => array(
      'title' => t('administer dynamic_banner'),
      'description' => t('Allows users to change what banners are displayed and to create thier own'),
    ),
  );
}

// end permission function

/**
 * Implements hook_node_presave().
 */
function dynamic_banner_node_presave($node) {

  // About to be saved (before insert/update)
}

/**
 * was using this for debuging
 */
function dynamic_banner_form_alter(&$form, &$form_state, $form_id) {

  //dpm($form_id);

  //dpm($form);
}

/**
 * Implements hook_node_insert()
 */
function dynamic_banner_node_insert($node) {

  // just made a new page so make that connection
}

/**
 * Implements hook_node_update().
 */
function dynamic_banner_node_update($node) {

  // change the old value to the new one
  // grab the old one search the table and insert (replace)
}

/**
 * Implements hook_node_delete().
 */
function dynamic_banner_node_delete($node) {

  // delete this value from the database
  // grab what it is then delete from table
}

/**
 * Implements hook_node_view().
 */
function dynamic_banner_node_view($node, $view_mode = 'full') {
}

/**
 * Implements hook_menu().
 * it is key to note here access arguments is referring to permissions
 */
function dynamic_banner_menu() {
  $items = array();

  // This is the root path and will be used to add dynamic banner to the admin page
  $items['admin/structure/banners'] = array(
    'title' => 'Dynamic Banner',
    'description' => 'Configure the Dynamic Banner.',
    'page callback' => 'dynamic_banner_admin_page',
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'file' => 'includes/callbacks.inc',
  );

  // The dynamic banner module settings page
  $items['admin/structure/banners/settings'] = array(
    'title' => '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_ACTION,
    'file' => 'includes/callbacks.inc',
  );

  // This will be the page to delete a selected banner
  $items['admin/structure/banners/delete/%'] = array(
    'title' => 'Delete Banner',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_delete_confirm',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'file' => 'includes/callbacks.inc',
  );

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

  /**
   * These next three are the same destination but since we want the user to know
   * We add them to the menu deal with logic from inside the form
   */
  $items['admin/structure/banners/add/0'] = array(
    'title' => 'Add Banner',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_form',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'includes/callbacks.inc',
  );
  $items['admin/structure/banners/default'] = array(
    'title' => 'Default Banner',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_form',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'includes/callbacks.inc',
  );

  // This will be the page to edit a banner from a form
  $items['admin/structure/banners/edit/%'] = array(
    'title' => 'Edit Banner',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_banner_admin_form',
    ),
    'access arguments' => array(
      'administer dynamic_banner',
    ),
    'file' => 'includes/callbacks.inc',
  );
  return $items;
}

// end menu function

/**
 * Implements hook_theme().
 * theme template used in module.
 */
function dynamic_banner_theme() {
  $theme = array(
    'banner_output' => array(
      'variables' => array(
        'url' => NULL,
        'text' => NULL,
        'link' => NULL,
        'display_setting' => NULL,
        'display_errors' => NULL,
      ),
      'path' => drupal_get_path('module', 'dynamic_banner') . '/templates',
      'template' => 'banner_output_template',
    ),
  );
  return $theme;
}

/**
 * Implements hook_block_info().
 */
function dynamic_banner_block_info() {
  $blocks = array();
  $blocks[BANNER_DEFAULT_BLOCK_NAME] = array(
    'info' => t('The default display of Dynamic Banner'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view()
 */
function dynamic_banner_block_view($delta) {
  if ($delta == BANNER_DEFAULT_BLOCK_NAME) {

    // store the path of the page the block is loading from, this will sead our first searches
    $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

    // debug statements enable these if you want to see the bahvior of these methods or if you want to defualt a path

    //dpm($path);

    //dpm($_GET['q']);

    //$path = 'home';

    // loop until we find the top down hirarchy
    do {
      $result = NULL;

      // exact match section //
      // create and execute query
      $query = db_select('dynamic_banner', 'd');
      $query
        ->condition('d.path', $path, '=')
        ->fields('d');
      $result = $query
        ->execute()
        ->fetchObject();

      // search for that path string exact match
      if ($result) {

        // have to translate if we have fids
        // image should always be in path format (sites/default/banners/pic0.jpg,sites/default/banners/pic1.jpg)
        $image = dynamic_banner_image_handler($result->imgurl, $result->imgfid);
        $variables = array(
          'url' => $image,
          'text' => $result->text,
          'link' => $result->link,
          'display_setting' => variable_get('dynamic_banner_display_setting', BANNER_DEFAULT_OUTPUT),
          'display_errors' => variable_get('dynamic_banner_display_errors', BANNER_DEFAULT_ERROR),
        );

        // XSS prevention
        $variables['text'] = filter_xss($variables['text']);
        $variables['link'] = filter_xss($variables['link']);
        return array(
          'content' => theme('banner_output', $variables),
        );
      }

      // wild section //
      $result = NULL;
      $wild_search = $path . '*';

      // create and execute query
      $query = db_select('dynamic_banner', 'd');
      $query
        ->condition('d.path', $wild_search, '=')
        ->fields('d');
      $result = $query
        ->execute()
        ->fetchObject();

      // search for the wild card string exact match
      if ($result) {

        // have to translate if we have fids
        // image should always be in path format (sites/default/banners/pic0.jpg,sites/default/banners/pic1.jpg)
        $image = dynamic_banner_image_handler($result->imgurl, $result->imgfid);
        $variables = array(
          'url' => $image,
          'text' => $result->text,
          'link' => $result->link,
          'display_setting' => variable_get('dynamic_banner_display_setting', BANNER_DEFAULT_OUTPUT),
          'display_errors' => variable_get('dynamic_banner_display_errors', BANNER_DEFAULT_ERROR),
        );

        // XSS prevention
        $variables['text'] = filter_xss($variables['text']);
        $variables['link'] = filter_xss($variables['link']);
        return array(
          'content' => theme('banner_output', $variables),
        );
      }

      //search code STARTS (case /*)

      // wild section //
      $current_path_array = explode('/', $path);
      array_pop($current_path_array);
      $test_path = implode('/', $current_path_array);

      //dpm($test_path);
      if (!empty($test_path)) {
        $result = null;
        $wild_search = $test_path . '/*';

        //dpm($wild_search);

        // create and execute query
        $query = db_select('dynamic_banner', 'd');
        $query
          ->condition('d.path', $wild_search, '=')
          ->fields('d');
        $result = $query
          ->execute()
          ->fetchObject();

        // search for the wild card string exact match
        if ($result) {

          // have to translate if we have fids
          // image should always be in path format (sites/default/banners/pic0.jpg,sites/default/banners/pic1.jpg)
          $image = dynamic_banner_image_handler($result->imgurl, $result->imgfid);
          $variables = array(
            'url' => $image,
            'text' => $result->text,
            'link' => $result->link,
            'display_setting' => variable_get('dynamic_banner_display_setting', BANNER_DEFAULT_OUTPUT),
            'display_errors' => variable_get('dynamic_banner_display_errors', BANNER_DEFAULT_ERROR),
          );
          return array(
            'content' => theme('banner_output', $variables),
          );
        }
      }

      //search code ENDS

      // random section //
      $result = NULL;
      $random_search = $path . '!';

      // create and execute query
      $query = db_select('dynamic_banner', 'd');
      $query
        ->condition('d.path', $random_search, '=')
        ->fields('d');
      $result = $query
        ->execute()
        ->fetchObject();

      // search for that random string exact match
      if ($result) {

        // get extra stuff associated with randoms
        $images = dynamic_banner_image_handler($result->imgurl, $result->imgfid);

        // support for random text if needed
        $texts = $result->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
        $variables = array(
          'url' => $image[$random],
          'text' => $text[$random],
          'link' => $result->link,
          'display_setting' => variable_get('dynamic_banner_display_setting', BANNER_DEFAULT_OUTPUT),
          'display_errors' => variable_get('dynamic_banner_display_errors', BANNER_DEFAULT_ERROR),
        );

        // XSS prevention
        $variables['text'] = filter_xss($variables['text']);
        $variables['link'] = filter_xss($variables['link']);
        return array(
          'content' => theme('banner_output', $variables),
        );
      }

      // 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

    ///////////////////////////// this will soon be stored in the variables table ////////////////// TODO

    // create and execute query
    $query = db_select('dynamic_banner', 'd');
    $query
      ->condition('d.path', 'DEFAULT', '=')
      ->fields('d');
    $result = $query
      ->execute()
      ->fetchObject();

    // for the resultant row (SHOULD ALWAYS BE ONE)
    if ($result) {
      $image = dynamic_banner_image_handler($result->imgurl, $result->imgfid);
      $variables = array(
        'url' => $image,
        'text' => $result->text,
        'link' => $result->link,
        'display_setting' => variable_get('dynamic_banner_display_setting', BANNER_DEFAULT_OUTPUT),
        'display_errors' => variable_get('dynamic_banner_display_errors', BANNER_DEFAULT_ERROR),
      );

      // XSS prevention
      $variables['text'] = filter_xss($variables['text']);
      $variables['link'] = filter_xss($variables['link']);
      return array(
        'content' => theme('banner_output', $variables),
      );
    }

    // just encase something went wrong
  }
}

Functions

Namesort descending Description
dynamic_banner_block_info Implements hook_block_info().
dynamic_banner_block_view Implements hook_block_view()
dynamic_banner_form_alter was using this for debuging
dynamic_banner_help Implements hook_help().
dynamic_banner_init Implements hook_init(). Will add the js and css files to display on anypage that the banner block is on
dynamic_banner_menu Implements hook_menu(). it is key to note here access arguments is referring to permissions
dynamic_banner_node_delete Implements hook_node_delete().
dynamic_banner_node_insert Implements hook_node_insert()
dynamic_banner_node_presave Implements hook_node_presave().
dynamic_banner_node_update Implements hook_node_update().
dynamic_banner_node_view Implements hook_node_view().
dynamic_banner_permission Implements hook_permission(). Permissions for dynamic banner
dynamic_banner_theme Implements hook_theme(). theme template used in module.

Constants