You are here

function dynamic_banner_block_view in Dynamic Banner 8.x

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

Implements hook_block_view().

File

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

Code

function dynamic_banner_block_view($delta) {
  if ($delta == 'dynamic_banner_block') {

    // Store the path of the page the block is loading from, this will seed our first searches.
    $system_path = current_path();
    $path = drupal_get_path_alias($system_path);

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

        // We 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', 0),
        );

        // 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', 0),
        );

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

      // 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', 0),
        );

        // 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 paramount to not modify the path before this point.
      $last_slash_position = strrpos($path, '/');
      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
    // TODO: store default banner in the variables table
    $query = db_select('dynamic_banner', 'd');
    $query
      ->condition('d.path', 'DEFAULT', '=')
      ->fields('d');
    $result = $query
      ->execute();

    // For the resultant row (SHOULD ALWAYS BE ONE)
    if ($result
      ->rowCount() == 1) {
      $default_banner = $result
        ->fetchObject();
      $default_image = dynamic_banner_image_handler($default_banner->imgurl, $default_banner->imgfid);
      $variables = array(
        'url' => $default_image,
        'text' => $default_banner->text,
        'link' => $default_banner->link,
        'display_setting' => variable_get('dynamic_banner_display_setting', BANNER_DEFAULT_OUTPUT),
        'display_errors' => variable_get('dynamic_banner_display_errors', 0),
      );

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