MobileComponent.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * Uses Session: User.mobile and User.nomobile
  5. * - mobile is the auto-detection (true/false)
  6. * - nomobile can be set by the user and overrides the default behavior/detection
  7. * (1=true/0=false or -1=null which will remove the override)
  8. *
  9. * TODO: differentaite between "isMobile" and "has/wants mobile"
  10. * @author Mark Scherer
  11. * @license MIT
  12. * 2011-12-28 ms
  13. */
  14. class MobileComponent extends Component {
  15. public $components = array('Session');
  16. public $isMobile = null;
  17. public $setMobile = null;
  18. public $Controller = null;
  19. protected $_defaults = array(
  20. 'engine' => 'cake',
  21. );
  22. public function __construct(ComponentCollection $collection, $settings = array()) {
  23. $settings = am($this->_defaults, $settings);
  24. parent::__construct($collection, $settings);
  25. }
  26. public function initialize(Controller $Controller) {
  27. parent::initialize($Controller);
  28. $this->Controller = $Controller;
  29. if (isset($this->Controller->request->params['named']['mobile'])) {
  30. if ($this->Controller->request->params['named']['mobile'] == '-1') {
  31. $noMobile = null;
  32. } else {
  33. $wantsMobile = (bool) $this->Controller->request->params['named']['mobile'];
  34. $noMobile = (int) (!$wantsMobile);
  35. }
  36. $this->Session->write('User.nomobile', $noMobile);
  37. }
  38. $this->setMobile();
  39. $urlParams = Router::getParams(true);
  40. if (!isset($urlParams['named'])) {
  41. $urlParams['named'] = array();
  42. }
  43. if (!isset($urlParams['pass'])) {
  44. $urlParams['pass'] = array();
  45. }
  46. $urlParams = am($urlParams, $urlParams['named'], $urlParams['pass']);
  47. unset($urlParams['named']);
  48. unset($urlParams['pass']);
  49. if (isset($urlParams['prefix'])) {
  50. unset($urlParams['prefix']);
  51. }
  52. if ($this->setMobile) {
  53. $url = Router::url(am($urlParams, array('mobile'=>0)));
  54. $this->Controller->set('desktopUrl', $url);
  55. } else {
  56. $url = Router::url(am($urlParams, array('mobile'=>1)));
  57. $this->Controller->set('mobileUrl', $url);
  58. }
  59. Configure::write('User.mobile', $this->isMobile);
  60. Configure::write('User.setMobile', $this->setMobile);
  61. }
  62. public function setMobile() {
  63. if ($this->isMobile === null) {
  64. $mobile = $this->isMobile();
  65. $this->isMobile = $mobile;
  66. }
  67. $noMobile = $this->Session->read('User.nomobile');
  68. if (!$this->isMobile && $noMobile === null || $noMobile) {
  69. $this->setMobile = false;
  70. return;
  71. }
  72. $this->setMobile = true;
  73. $this->Controller->viewClass = 'Theme';
  74. $this->Controller->theme = 'Mobile';
  75. //$this->Controller->layoutPath = 'mobile';
  76. }
  77. /**
  78. *
  79. * @return bool $success
  80. */
  81. public function isMobile() {
  82. $isMobile = $this->Session->read('User.mobile');
  83. if ($isMobile !== null) {
  84. return $isMobile;
  85. }
  86. if ($this->settings['engine'] !== 'cake') {
  87. throw new CakeException(__('Engine %s not available', $this->settings['engine']));
  88. //TODO
  89. }
  90. $isMobile = (int)$this->detect();
  91. $this->Session->write('User.mobile', $isMobile);
  92. return $isMobile;
  93. }
  94. public function detect() {
  95. $this->Controller->request->addDetector('mobile', array('options' => array('OMNIA7')));
  96. return $this->Controller->request->is('mobile');
  97. }
  98. public function detectByTools() {
  99. $isMobile = $this->Session->read('Session.mobile');
  100. if ($isMobile !== null) {
  101. return $isMobile;
  102. }
  103. App::uses('UserAgentLib', 'Tools.Lib');
  104. $UserAgentLib = new UserAgentLib();
  105. $mobile = (int)$UserAgentLib->isMobile();
  106. $this->Session->write('Session.mobile', $mobile);
  107. return $mobile;
  108. }
  109. public function detectByWurfl() {
  110. App::import('Vendor', 'WURFL', array('file' => 'WURFLManagerProvider.php'));
  111. $wurflConfigFile = APP . 'Config' . DS . 'wurfl ' . DS . 'config.xml';
  112. $wurflManager = WURFL_WURFLManagerProvider::getWURFLManager($wurflConfigFile);
  113. $requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
  114. if ($requestingDevice->getCapability('is_wireless_device') == 'true') {
  115. return true;
  116. }
  117. return false;
  118. }
  119. }