You are here

function browscap_block_block_list_alter in Browscap Block 7

Implements hook_block_list_alter(). Check the mobile device specific visibilty settings. Remove the block if the visibility conditions are not met.

File

./browscap_block.module, line 79
Browscap Block. Block visibility options for mobile devices using Browscap detection.

Code

function browscap_block_block_list_alter(&$blocks) {
  global $theme_key;
  $browser = browscap_get_browser();
  $is_mobile = FALSE;
  if (isset($browser['ismobiledevice'])) {
    if ($browser['ismobiledevice'] == 1 || variable_get('mobile_switch_ismobiledevice', FALSE)) {
      $is_mobile = TRUE;
    }
  }
  $browscap_block = array();
  $result = db_query('SELECT module, delta, ismobile FROM {browscap_block}');
  foreach ($result as $record) {
    $browscap_block[$record->module][$record->delta] = (int) $record->ismobile;
  }
  foreach ($blocks as $key => $block) {
    if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {

      // This block was added by a contrib module, leave it in the list.
      continue;
    }
    foreach ($browscap_block as $module => $delta) {
      if ($module == $block->module) {
        foreach ($delta as $delta_key => $delta_value) {
          if ($delta_key == $block->delta) {
            if ($delta_value == 2 && $is_mobile == FALSE) {
              unset($blocks[$key]);
            }
            if ($delta_value == 1 && $is_mobile == TRUE) {
              unset($blocks[$key]);
            }
          }
        }
      }
    }
  }
}