1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address contact@x2engine.com.
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * Time Zone information widget.
39: *
40: * A widget that displays time information (i.e. zone, current time there)
41: * specific to the contact being viewed.
42: *
43: * @package application.components
44: */
45: class TimeZone extends X2Widget {
46:
47: //public $visibility;
48:
49: public $model;
50: public $localTime = true;
51:
52: public function init() {
53: parent::init();
54: }
55:
56: public function run() {
57: $tzOffset = null;
58:
59: if($this->localTime) { // local mode, no offset needed
60: $tzOffset = 0;
61:
62: $tz = Yii::app()->params->profile->timeZone;
63: try {
64: $dateTimeZone = new DateTimeZone($tz);
65: } catch (Exception $e) {
66: $dateTimeZone = null;
67: }
68: $localTime = new DateTime();
69: if(@date_timezone_set($localTime,$dateTimeZone)) {
70: $tzOffset = $localTime->getOffset();
71: }
72: } else {
73: if(!isset($this->model))
74: return;
75:
76: $address = '';
77: if(!empty($this->model->city))
78: $address .= $this->model->city.', ';
79: if(!empty($this->model->state))
80: $address .= $this->model->state;
81: if(!empty($this->model->country))
82: $address .= ' '.$this->model->country;
83:
84: $tz = $this->model->timezone;
85:
86: try {
87: $dateTimeZone = new DateTimeZone($tz);
88: } catch (Exception $e) {
89: $dateTimeZone = null;
90: }
91: $contactTime = new DateTime();
92: if(@date_timezone_set($contactTime,$dateTimeZone)) {
93: $tzOffset = $contactTime->getOffset();
94:
95: if(empty($this->model->timezone)) { // if we just looked this timezone up,
96: $this->model->timezone = $tz; // save it
97: $this->model->update(array('timezone'));
98: }
99: } elseif(!empty($this->model->timezone)) {
100: // if the messed up timezone was previously saved, clear it
101:
102: $this->model->timezone = '';
103: $this->model->update(array('timezone'));
104: }
105: }
106:
107: if($tzOffset !== null) {
108: $offsetJs = '';
109:
110: if($this->localTime) {
111:
112: $offset = $tzOffset;
113:
114: $tzString = 'UTC';
115: $tzString .= ($offset > 0)? '+' : '-';
116:
117: $offset = abs($offset);
118:
119: $offsetH = floor($offset/3600);
120: $offset -= $offsetH*3600;
121: $offsetM = floor($offset/60);
122:
123: $tzString .= $offsetH;
124: if($offsetM > 0)
125: $tzString .= ':'.$offsetM;
126:
127: Yii::app()->clientScript->registerScript(
128: 'timezoneClock',
129: 'x2.tzOffset = '.($tzOffset*1000).';
130: x2.tzUtcOffset = " ('.addslashes($tzString).')";',
131: CClientScript::POS_BEGIN);
132:
133: //echo Yii::t('app','Current time in').'<br><b>'.$address.'</b>';
134: }
135: $clockType = Profile::getWidgetSetting('TimeZone','clockType');
136:
137: Yii::app()->clientScript->registerScriptFile(
138: Yii::app()->getBaseUrl().'/js/clockWidget.js');
139: Yii::app()->clientScript->registerCssFile(
140: Yii::app()->theme->baseUrl.'/css/components/clockWidget.css');
141:
142: $this->render('timeZone', array('widgetSettings' => $clockType));
143:
144:
145: } else
146: echo Yii::t('app','Timezone not available');
147: }
148:
149: }
150: