You are here

function dynamic_banner_block in Dynamic Banner 6

Implements hook_block().

File

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

Code

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