You are here

function mobile_tools_get_device in Mobile Tools 7.2

Same name and namespace in other branches
  1. 5 mobile_tools.module \mobile_tools_get_device()
  2. 6.3 mobile_tools.module \mobile_tools_get_device()
  3. 6 mobile_tools.module \mobile_tools_get_device()
  4. 6.2 mobile_tools.module \mobile_tools_get_device()

Get $device object. Check if the 'device' argument is present or a cookie is set to overwrite the device:

  • device=mobile => mobile view
  • device=desktop => desktop view
  • device=[group] => specific group view
  • device=auto => reset overwrite

Return value

$device The $device object

File

./mobile_tools.module, line 633
Functionality to ease the creation of mixed device environments.

Code

function mobile_tools_get_device() {

  // @todo replace with session api sid to track override
  global $cookie_domain, $mobile_tools_device;

  // currently the boot method saves the result in a global variable.
  if (isset($mobile_tools_device)) {
    return $mobile_tools_device;
  }

  // Checking the possible arguments
  $session_time = REQUEST_TIME + variable_get('mobile_tools_cookie_session', 3600 * 24 * 30);

  //first check if the device type is forced in the device argument
  if (isset($_GET['device'])) {
    switch ($_GET['device']) {
      case 'desktop':
      case 'mobile':
        setCookie('mt_device', $_GET['device'], variable_get('mobile_tools_cookie_lifetime', $session_time), '/', $cookie_domain);
        return array(
          'type' => $_GET['device'],
          'group' => '',
        );
        break;
      case 'auto':
        setCookie('mt_device', '', REQUEST_TIME - 3600 * 24 * 30, '/', $cookie_domain);
        break;
      default:
        $device_groups = mobile_tools_device_groups();
        if (isset($device_groups[$_GET['device']])) {
          setCookie('mt_device', $_GET['device'], variable_get('mobile_tools_cookie_lifetime', $session_time), '/', $cookie_domain);
          return array(
            'type' => 'mobile',
            'group' => $_GET['device'],
          );
        }
    }
  }
  elseif (isset($_COOKIE['mt_device'])) {
    switch ($_COOKIE['mt_device']) {
      case 'desktop':
      case 'mobile':
        return array(
          'type' => $_COOKIE['mt_device'],
          'group' => '',
        );
        break;
      case 'auto':
        setCookie('mt_device', '', REQUEST_TIME - 3600);
        break;
      default:
        $device_groups = mobile_tools_device_groups();
        if (isset($device_groups[$_COOKIE['mt_device']])) {
          setCookie('mt_device', $_COOKIE['mt_device'], variable_get('mobile_tools_cookie_lifetime', $session_time), '/');
          return array(
            'type' => 'mobile',
            'group' => $_COOKIE['mt_device'],
          );
        }
    }
  }

  // we default to the real detection
  return mobile_tools_detect_device();
}