You are here

mobile_switch_block.module in Mobile Switch Block 7.2

Same filename and directory in other branches
  1. 6 mobile_switch_block.module
  2. 7 mobile_switch_block.module

Extends the Mobile Switch module with an theme switch block.

The block content provides a link to manually switch the current theme to the mobile or desktop theme.

File

mobile_switch_block.module
View source
<?php

/**
 * @file
 * Extends the Mobile Switch module with an theme switch block.
 *
 * The block content provides a link to manually switch the current theme to
 * the mobile or desktop theme.
 */

/**
 * Implements hook_boot().
 */
function mobile_switch_block_boot() {

  // No functionalities here.
  // We use this hook to set autmatically the system table bootstrap value
  // for this module to 1.
}

/**
 * Implements hook_mobile_switch_mobile_boot_alter().
 *
 * @param $conf
 *   The associative array contains values to alter.
 * @param $get
 *   The associative array contains various parameters to help to alter.
 *
 * Insert a virtual system variable. Virtual means, the variable is not
 * inserted in system variables registry.
 *   - theme_cookie: The default value is FALSE. If the theme switch cookie
 *                   exists the value can be 'standard' or 'mobile'.
 */
function mobile_switch_block_mobile_switch_boot_alter(&$conf, $get) {
  $get['mode'] = mobile_switch_get_operating_mode();
  if ($get['mode'] === 'none' || $get['mode'] === 'detectonly') {
    return;
  }
  $get['theme_cookie'] = FALSE;

  // Theme switch from URL.
  // Set users cookie.
  if (isset($_GET['mobile_switch'])) {
    _mobile_switch_block_set_cookie($_GET['mobile_switch']);
    $get['theme_cookie'] = check_plain($_GET['mobile_switch']);
  }

  // Provide cookie value.
  $get['theme_cookie'] = _mobile_switch_block_get_cookie();

  // No theme cookie exist.

  //if ((bool) $get['theme_cookie'] === FALSE && (bool) $get['browser']['ismobiledevice'] == TRUE) {

  //$conf['theme_default'] = $conf['mobile_switch_mobile_theme'];

  //}

  // Theme switch from URL as visitor action.
  if (isset($_GET['mobile_switch'])) {
    switch ($_GET['mobile_switch']) {
      case 'standard':
      case 'standard-rm':
        $conf['theme_default'] = $conf['mobile_switch_theme_default'];
        $conf['theme_mobile'] = $conf['mobile_switch_theme_default'];

        // Use the mobile theme on admin pages.
        if (stristr($_GET['q'], 'admin') && $get['admin_usage'] === TRUE) {
          $conf['admin_theme'] = $conf['mobile_switch_admin_theme'];
        }
        break;
      case 'mobile':
      case 'mobile-rm':
        if ($get['mode'] === 'redirect') {
          $conf['theme_default'] = $conf['mobile_switch_theme_default'];
        }
        if ($get['mode'] === 'themeswitch') {
          $conf['theme_default'] = $conf['mobile_switch_mobile_theme'];
        }
        break;
    }
  }
  elseif ($get['theme_cookie']) {
    switch ($get['theme_cookie']) {
      case 'standard':
      case 'standard-rm':
        $conf['theme_default'] = $conf['mobile_switch_theme_default'];
        $conf['theme_mobile'] = $conf['mobile_switch_theme_default'];

        // Use the mobile theme on admin pages.
        if (stristr($_GET['q'], 'admin') && $get['admin_usage'] === TRUE) {
          $conf['admin_theme'] = $conf['mobile_switch_admin_theme'];
        }
        break;
      case 'mobile':
      case 'mobile-rm':
        if ($get['mode'] === 'redirect') {
          $conf['theme_default'] = $conf['mobile_switch_theme_default'];
        }
        if ($get['mode'] === 'themeswitch') {
          $conf['theme_default'] = $conf['mobile_switch_mobile_theme'];
        }
        break;
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mobile_switch_block_form_mobile_switch_advanced_settings_form_alter(&$form, &$form_state, $form_id) {
  $mode = mobile_switch_get_operating_mode();
  if ($mode === 'none' || $mode === 'detectonly') {
    return;
  }
  $readme_link = 'README.txt';
  if (module_exists('help')) {
    $readme_link = l('README.txt', 'admin/help/mobile_switch_block');
  }
  $form['switch_block'] = array(
    '#type' => 'fieldset',
    '#title' => t('Switch block'),
    '#description' => t('Take a look at the !readme.', array(
      '!readme' => $readme_link,
    )),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['switch_block']['mobile_switch_block_content'] = array(
    '#type' => 'select',
    '#title' => t('Block content'),
    '#default_value' => variable_get('mobile_switch_block_content', 'link'),
    '#options' => _mobile_switch_block_content_options(),
  );
  $form['switch_block']['mobile_switch_block_desktop_device_name'] = array(
    '#type' => 'select',
    '#title' => t('Desktop device designation'),
    '#description' => t('Select the designation for the desktop device. Example output: Standard view, Default view, Desktop view'),
    '#default_value' => variable_get('mobile_switch_block_desktop_device_name', 'standard'),
    '#options' => array(
      'standard' => t('standard'),
      'default' => t('default'),
      'desktop' => t('desktop'),
    ),
  );
  $form['switch_block']['mobile_switch_block_switch_link_text'] = array(
    '#type' => 'select',
    '#title' => t('Switch link text'),
    '#description' => t('Select the second part of the switch link text. Example output: Mobile view, Mobile site, Mobile variant, Mobile version'),
    '#default_value' => variable_get('mobile_switch_block_switch_link_text', 'view'),
    '#options' => array(
      'view' => t('view'),
      'site' => t('site'),
      'variant' => t('variant'),
      'version' => t('version'),
    ),
  );

  // Cookie expire time.
  $form['switch_block']['mobile_switch_block_cookie_expire'] = array(
    '#type' => 'textfield',
    '#title' => t('Cookie lifetime'),
    '#description' => t('The time after the <em>Mobile Switch</em> theme cookie expires, in seconds.'),
    '#default_value' => variable_get('mobile_switch_block_cookie_expire', 31536000),
    '#size' => 20,
    '#maxlength' => 12,
    '#required' => TRUE,
    '#element_validate' => array(
      '_mobile_switch_block_cookie_expire_validate',
    ),
  );

  // Link target.
  $form['switch_block']['mobile_switch_block_link_target'] = array(
    '#type' => 'checkbox',
    '#title' => t('Link to front page'),
    '#description' => t('This is relevant for the building of the link URL. Checked: Always linked to the <em>Base URL</em>. Unchecked: Linked to a URL corresponding to the current URL.'),
    '#default_value' => variable_get('mobile_switch_block_link_target', 1),
  );

  // Non-tablet usage.
  $form['switch_block']['mobile_switch_block_no_tablet_usage'] = array(
    '#type' => 'checkbox',
    '#title' => t('Non-tablet usage'),
    '#description' => t('Use the block on mobile devices when a non-tablet device is detected.'),
    '#default_value' => variable_get('mobile_switch_block_no_tablet_usage', 0),
  );
}

/**
 * Render API callback: Validates the cookie expire value.
 *
 * Ensures that only numbers and no white spaces has been entered.
 *
 * This function is assigned as an #element_validate callback in
 * mobile_switch_block_form_mobile_switch_advanced_settings_form_alter().
 */
function _mobile_switch_block_cookie_expire_validate($element, &$form_state) {
  if (preg_match("/[^0-9]/", $element['#value'])) {
    form_error($element, t('%title: Only numbers and no white spaces are possible.', array(
      '%title' => t($element['#title']),
    )));
  }
}

/**
 * Implements hook_theme().
 */
function mobile_switch_block_theme() {
  return array(
    'mobile_switch_block_switch_content' => array(
      'template' => 'mobile-switch-block-switch-content',
      'render element' => 'content',
    ),
  );
}

/**
 * Processes variables for mobile-switch-block-switch-content.tpl.php.
 *
 * Returns HTML content for the Mobile switch block.
 *
 * @see mobile_switch_block_view()
 * @see mobile_switch_block_get_block_content()
 *
 * @ingroup themeable
 */
function template_preprocess_mobile_switch_block_switch_content(&$variables) {
  global $conf;
  $variables['op_mode'] = mobile_switch_get_operating_mode();
  $detect = mobile_switch_mobile_detect();
  $variables['mobile_switch_detect'] = $detect;
  $variables['mobile_switch_ismobiledevice'] = $detect['ismobiledevice'];
  $variables['mobile_switch_block_content'] = variable_get('mobile_switch_block_content', 'link');
  $variables['query_value'] = '';
  $variables['version'] = '';
  $variables['version_text'] = '';
  $variables['class'] = '';
  $variables['switch_link'] = '';
  $variables['switch_message'] = '';
  $items = array();
  $theme_cookie_use = _mobile_switch_check_theme_cookie_use();

  // Make it possible to use the manual switch by visitor if the
  // operating mode 'redirect' enabled.
  $query_value_extend = '';
  if ($variables['op_mode'] === 'redirect') {

    // The letters 'rm' mean the operating 'redirect' mode.
    $query_value_extend = '-rm';
  }

  // Prepare the link values.
  if ($variables['mobile_switch_ismobiledevice'] && $theme_cookie_use == FALSE || ($theme_cookie_use === 'mobile' || $theme_cookie_use === 'mobile-rm')) {
    $variables['query_value'] = 'standard' . $query_value_extend;
    $variables['version'] = 'mobile';
    $variables['version_text'] = variable_get('mobile_switch_block_desktop_device_name', 'standard');
    $variables['class'] = 'mobile-switch-to-standard';
  }
  else {
    $variables['query_value'] = 'mobile' . $query_value_extend;
    $variables['version'] = variable_get('mobile_switch_block_desktop_device_name', 'standard');
    $variables['version_text'] = 'mobile';
    $variables['class'] = 'mobile-switch-to-mobile';
  }

  // Prepare the switch content.
  switch ($variables['mobile_switch_block_content']) {
    case 'link':
      $variables['switch_link'] = t('<a href="!switch-url">!version !view</a>', array(
        '!view' => variable_get('mobile_switch_block_switch_link_text', 'view'),
        '!switch-url' => _mobile_switch_block_url($variables['query_value'], TRUE),
        '!version' => ucfirst($variables['version_text']),
      ));
      $items[] = array(
        'class' => array(
          'leaf',
        ),
        'data' => $variables['switch_link'],
      );
      break;
    case 'message_link':
      $switch_message = $variables['switch_message'] = '<span class="mobile-switch-inform">' . t('This is the !version-text !version of the site.', array(
        '!version-text' => $variables['version'],
        '!version' => variable_get('mobile_switch_block_switch_link_text', 'view'),
      )) . '</span>' . "\n";
      $variables['switch_link'] = t('<a href="!switch-url">!version !view</a>', array(
        '!view' => variable_get('mobile_switch_block_switch_link_text', 'view'),
        '!switch-url' => _mobile_switch_block_url($variables['query_value'], TRUE),
        '!version' => ucfirst($variables['version_text']),
      ));
      $items[] = array(
        'class' => array(
          'leaf',
        ),
        'data' => $variables['switch_link'],
      );
      break;
  }

  // Usage of theme_item_list() needed for the use with a Mobile jQuery sub-theme.
  // This ensures the Mobile jQuery like display of the switch content.
  $variables['content'] = $variables['switch_message'] . theme('item_list', array(
    'attributes' => array(
      'class' => array(
        'menu clearfix',
      ),
    ),
    'items' => $items,
  ));
}

/**
 * Implements hook_block_info().
 */
function mobile_switch_block_block_info() {
  $blocks['switch']['info'] = t('Mobile switch');
  $blocks['switch']['properties']['administrative'] = TRUE;

  // Necessary for the correct functioning of the switch function for
  // anonymous users if enabled block caching in the performance settings.
  $blocks['switch']['cache'] = DRUPAL_NO_CACHE;

  //DRUPAL_CACHE_PER_PAGE
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function mobile_switch_block_block_configure($delta = '') {
  $form['mobile_switch_block_content'] = array(
    '#type' => 'select',
    '#title' => t('Block content'),
    '#default_value' => variable_get('mobile_switch_block_content', 'link'),
    '#options' => _mobile_switch_block_content_options(),
  );
  return $form;
}

/**
 * Implements hook_block_save().
 */
function mobile_switch_block_block_save($delta = '', $edit = array()) {
  variable_set('mobile_switch_block_content', $edit['mobile_switch_block_content']);
}

/**
 * Implements hook_block_view()
 */
function mobile_switch_block_block_view($delta) {
  $block = array();
  $mode = mobile_switch_get_operating_mode();

  // Not supported operating modes.
  if ($mode === 'none' || $mode === 'detectonly') {
    return $block;
  }

  // Setting from basic settings.
  $get['tablet_usage'] = (bool) variable_get('mobile_switch_tablet_usage', TRUE);

  // Setting from block settings
  $get['no_tablet_usage'] = (bool) variable_get('mobile_switch_block_no_tablet_usage', 0);
  $get['deskbrowser'] = (bool) variable_get('mobile_switch_deskbrowser', FALSE);
  $get['developer'] = (bool) variable_get('mobile_switch_developer', FALSE);
  $get['browser'] = mobile_switch_mobile_detect($get['developer']);

  // Not supported usage by configuration.
  switch ($get['browser']['istablet']) {

    // Tablet usage.
    case TRUE:
      if ($get['tablet_usage'] === FALSE) {
        return $block;
      }
      break;

    // Non-tablet usage - configuration option.
    // Use the block on mobile devices when a non-tablet device is detected.
    case FALSE:
      if ($get['no_tablet_usage'] === FALSE && (bool) $get['browser']['ismobiledevice'] === TRUE) {
        return $block;
      }
      break;
  }
  switch ($delta) {
    case 'switch':
      if ((bool) $get['browser']['prevent_device'] === FALSE || $get['deskbrowser'] === TRUE && (bool) $get['browser']['ismobiledevice'] === FALSE) {
        $block['subject'] = t('Theme switch');
        $block['content'] = theme('mobile_switch_block_switch_content');
      }
      break;
  }
  return $block;
}

/**
 * Wrapper function to get the switch block content.
 *
 * @return string
 *   HTML switch block content.
 */
function mobile_switch_block_get_block_content() {
  return theme('mobile_switch_block_switch_content');
}

/**
 * Get the cookie value.
 *
 * @return boolean|string
 *   FALSE if not set or one of the two values standard/mobile.
 *
 * @see mobile_switch_block_mobile_switch_boot_alter()
 */
function _mobile_switch_block_get_cookie() {
  $name = 'mobile_switch_mode';
  return isset($_COOKIE[$name]) ? $_COOKIE[$name] : FALSE;
}

/**
 * Set the cookie value.
 *
 * @param $value
 *   The string contains one of the possible values:
 *   - standard
 *   - mobile
 *
 * @see mobile_switch_block_mobile_switch_boot_alter()
 */
function _mobile_switch_block_set_cookie($value) {
  setrawcookie('mobile_switch_mode', $value, REQUEST_TIME + variable_get('mobile_switch_block_cookie_expire', 31536000), '/');
}

/**
 * Get the mobile switch URL.
 *
 * @param $mode
 *   A string with the possible values:
 *   - standard
 *   - mobile
 * @param $manual
 *   A boolean value.
 *
 * @see theme_mobile_switch_block()
 */
function _mobile_switch_block_url($mode, $manual = FALSE) {
  $get['op_mode'] = mobile_switch_get_operating_mode();
  $get['link_target'] = (bool) variable_get('mobile_switch_block_link_target', 1);
  $url = '<front>';
  $current_path = current_path();
  if ($get['link_target'] === TRUE) {
    $current_path = '';
  }
  switch ($get['op_mode']) {

    // Operating mode redirect.
    case 'redirect':
      $options['query']['mobile_switch'] = $manual ? $mode : 0;
      $get['redirect_url_to_mobile'] = variable_get('mobile_switch_redirect_url_to_mobile', '');
      $get['redirect_url_to_desktop'] = variable_get('mobile_switch_redirect_url_to_desktop', '');

      // Multisite with shared content?
      $get['shared_content'] = (bool) variable_get('mobile_switch_redirect_shared_content', 0);

      // The current host URL.
      $get['current_url'] = 'http://' . $_SERVER['HTTP_HOST'];

      // Identify the current website version - desktop or mobile.
      $get['site_variant'] = FALSE;
      if ($get['current_url'] == $get['redirect_url_to_desktop']) {
        $get['site_variant'] = 'desktop';
      }
      if ($get['current_url'] == $get['redirect_url_to_mobile']) {
        $get['site_variant'] = 'mobile';
      }
      switch ($get['site_variant']) {
        case 'desktop':
          $path = $get['redirect_url_to_mobile'];
          if ($get['shared_content']) {
            $path .= '/' . $current_path;
          }
          break;
        case 'mobile':
          $path = $get['redirect_url_to_desktop'];
          if ($get['shared_content']) {
            $path .= '/' . $current_path;
          }
          break;
      }
      $url = url($path, $options);
      break;

    // Operating mode theme switch.
    case 'themeswitch':
      $options['query']['mobile_switch'] = $manual ? $mode : 0;
      $options['absolute'] = TRUE;
      $url = url($current_path, $options);
      break;
  }
  return $url;
}

/**
 * Helper function to get block content options.
 *
 * @see mobile_switch_block_form_mobile_switch_advanced_settings_form_alter()
 * @see mobile_switch_block_block_configure()
 */
function _mobile_switch_block_content_options() {
  return array(
    'link' => t('Only the switch link'),
    'message_link' => t('Message and switch link'),
  );
}

/**
 * Implements hook_help().
 */
function mobile_switch_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#mobile_switch_block':
      return check_markup(file_get_contents(dirname(__FILE__) . '/README.txt'));
  }
}