1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37: Yii::import ('application.components.behaviors.*');
38: Yii::import ('application.modules.mobile.components.actions.*');
39: Yii::import ('application.modules.mobile.*');
40: Yii::import ('application.modules.mobile.models.*');
41:
42: class X2MobileControllerBehavior extends X2ControllerBehavior {
43:
44: 45: 46:
47: public $dataUrl;
48: public $pageId;
49: public $pageClass;
50: public ;
51:
52: public $pathAliasBase = 'application.modules.mobile.';
53:
54: 55: 56:
57: public $includeActions = true;
58:
59: public function hasMobileAction ($action) {
60: $actions = $this->owner->actions ();
61: return isset ($actions[$action]);
62: }
63:
64: private $_assetsUrl;
65: public function getAssetsUrl () {
66: if (!isset ($this->_assetsUrl)) {
67: if (isset ($this->owner->module)) {
68: $this->_assetsUrl = $this->owner->module->assetsUrl;
69: } else {
70: $this->_assetsUrl = Yii::app()->getAssetManager()->publish (
71: Yii::getPathOfAlias($this->pathAliasBase.'assets'), false, -1, true);
72: }
73: }
74: return $this->_assetsUrl;
75: }
76:
77: public function setAssetsUrl ($assetsUrl) {
78: $this->_assetsUrl = $assetsUrl;
79: }
80:
81: public function actions () {
82: if ($this->owner instanceof MobileController || !$this->includeActions) return array ();
83: return array (
84: 'mobileIndex' => array (
85: 'class' => 'MobileIndexAction'
86: ),
87: 'mobileView' => array (
88: 'class' => 'MobileViewAction'
89: ),
90: 'mobileCreate' => array (
91: 'class' => 'MobileCreateAction'
92: ),
93: 'mobileUpdate' => array (
94: 'class' => 'MobileUpdateAction'
95: ),
96: 'mobileDelete' => array (
97: 'class' => 'MobileDeleteAction'
98: ),
99: );
100: }
101:
102: public function beforeAction ($action) {
103: if (!($this->owner instanceof MobileController) &&
104: !in_array ($this->owner->action->getId (), array_keys ($this->actions ()))) {
105:
106: return true;
107: }
108:
109: Yii::app()->user->loginUrl = array ('/mobile/login');
110:
111: Yii::app()->params->isMobileApp = true;
112:
113:
114: if (!preg_match (
115: '/\/mobileView$/',
116: Yii::app()->params->profile->asa ('X2LinkableBehavior')->viewRoute)) {
117:
118: Yii::app()->params->profile->asa ('X2LinkableBehavior')->viewRoute .= '/mobileView';
119: }
120:
121: $this->dataUrl = $this->owner->createAbsoluteUrl ($action->getId ());
122: $this->pageId = lcfirst (preg_replace ('/Controller$/', '', get_class ($this->owner))).'-'.
123: $action->getId ();
124:
125: $cookie = new CHttpCookie('isMobileApp', 'true');
126: $cookie->expire = 2147483647;
127: Yii::app()->request->cookies['isMobileApp'] = $cookie;
128:
129:
130:
131:
132: if (!($this->owner instanceof MobileController)) {
133: $this->owner->layout = $this->pathAliasBase.'views.layouts.main';
134: if ($this->owner->module) {
135:
136: $this->owner->setAssetsUrl (Yii::app()->getAssetManager()->publish (
137: Yii::getPathOfAlias($this->pathAliasBase.'assets'), false, -1, true));
138: $this->owner->module->assetsUrl = $this->owner->assetsUrl;
139: Yii::app()->clientScript->packages = MobileModule::getPackages (
140: $this->owner->module->assetsUrl);
141: } else {
142: Yii::app()->clientScript->packages = MobileModule::getPackages (
143: $this->owner->assetsUrl);
144: }
145: }
146:
147: return true;
148: }
149:
150: public function includeDefaultJsAssets () {
151: return !$this->owner->isAjaxRequest () || isset ($_GET['includeX2TouchJsAssets']);
152: }
153:
154: public function includeDefaultCssAssets () {
155: return !$this->owner->isAjaxRequest () || isset ($_GET['includeX2TouchCssAssets']);
156: }
157:
158: 159: 160: 161:
162: public function onPageLoad ($js, $scriptName=null) {
163: static $i=0;
164: $scriptName = !isset ($scriptName) ?
165: 'X2MobileControllerBehavior.onPageLoad.'.$i : $scriptName;
166: if ($this->owner->isAjaxRequest ()) {
167: Yii::app()->clientScript->registerScript($scriptName,
168: "$(function () { $js });", CClientScript::POS_END);
169: } else {
170: Yii::app()->clientScript->registerScript($scriptName,
171: "$(document).on ('pagecontainercreate', function () { $js });", CClientScript::POS_END);
172: }
173: $i++;
174: }
175:
176: 177: 178:
179: public function getUniquePageIdSuffix () {
180: if (isset ($_SESSION['X2MobileControllerBehavior.random'])) {
181: $oldVal = $_SESSION['X2MobileControllerBehavior.random'];
182: while (($suffix = (mt_rand ().preg_replace ('/\./', '_', microtime (true)))) ===
183: $oldVal) {};
184:
185: $_SESSION['X2MobileControllerBehavior.random'] = $suffix;
186: } else {
187: $suffix = mt_rand ().preg_replace ('/\./', '_', microtime (true));
188: }
189: return $suffix;
190: }
191: }
192:
193: ?>
194: