You are here

function dynamic_banner_block_view in Dynamic Banner 7

Same name and namespace in other branches
  1. 7.2 dynamic_banner.module \dynamic_banner_block_view()
  2. 8.x dynamic_banner.module \dynamic_banner_block_view()

Implements hook_block_view()

File

./dynamic_banner.module, line 233
Distributed under GNU GPL version 3

Code

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