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: * Class to assist the login theme cookies and Javascript.
39: * The login page has both the ability to change the background color and
40: * ability to change overall theme of the page. The cookie saved will be the background color
41: * and the theme name.
42: *
43: * If the user is on default theme and changes the theme to dark theme, The app will change this
44: * users theme to the dark theme.
45: * If the user is on a custom theme and logs in with the default theme, the theme of the app will
46: * not change, to respect the option to have a default login page but a themed app.
47: *
48: * The right most background color option will be the color of the theme background.
49: *
50: * @package application.components
51: * @author Alex Rowe <alex@x2engine.com>
52: */
53: abstract class LoginThemeHelperBase {
54:
55: /**
56: * @var constant name of cookie that saves the current profile theme
57: */
58: const PROFILE_COOKIE = 'profileTheme';
59:
60: /**
61: * @var constant Name of the cookie that is the current theme of the login screen. This gets
62: * set by clicking the dark/light theme toggle
63: */
64: const LOGIN_THEME_COOKIE = 'themeName';
65:
66: /**
67: * @var constant Name of the cookie that defines the login background color
68: */
69: const LOGIN_BACKGROUND_COOKIE = 'loginBackground';
70:
71: /**
72: * @var int length of the cookies set
73: */
74: public static $cookieLength = 1209600; // Two weeks
75:
76: /**
77: * @var string name of the next theme. This will be the dark theme if the current theme is default
78: */
79: public $nextTheme;
80:
81: /**
82: * @var string name of the currently applied theme.
83: */
84: public $currentTheme;
85:
86: /**
87: * @var string color name of the background color currently set.
88: */
89: public $currentBackground;
90:
91: /**
92: * Whether or not user has a dark theme selected
93: */
94: public $usingDarkTheme;
95:
96: /**
97: * The constructor does most of the work. Handles Posting expected on the login screen.
98: */
99: public function __construct() {
100: Yii::app()->clientScript->registerPackage('X2CSS');
101:
102: $loginTheme = ThemeGenerator::$defaultLight;
103: $darkTheme = ThemeGenerator::$defaultDark;
104:
105: // Set the dark theme to be a different than default
106: if (isset ($_COOKIE[self::PROFILE_COOKIE]) &&
107: $_COOKIE[self::PROFILE_COOKIE] != ThemeGenerator::$defaultLight) {
108: $darkTheme = $_COOKIE[self::PROFILE_COOKIE];
109: }
110:
111: // Check if the login theme is set
112: if (isset ($_POST[self::LOGIN_THEME_COOKIE])) {
113:
114: //Set a cookie if a post was mode
115: AuxLib::setCookie(
116: self::LOGIN_THEME_COOKIE, $_POST[self::LOGIN_THEME_COOKIE], self::$cookieLength);
117: $loginTheme = $_POST[self::LOGIN_THEME_COOKIE];
118:
119: } else if (isset ($_COOKIE[self::LOGIN_THEME_COOKIE])) {
120: $loginTheme = $_COOKIE[self::LOGIN_THEME_COOKIE];
121: }
122:
123: // get the button post value; The opposite of what theme is set.
124: $nextTheme = ( $loginTheme == ThemeGenerator::$defaultLight ) ? $darkTheme : ThemeGenerator::$defaultLight;
125: $this->usingDarkTheme = $loginTheme !== ThemeGenerator::$defaultLight;
126:
127: $this->currentTheme = $loginTheme;
128: $this->nextTheme = $nextTheme;
129:
130: $this->currentColor = null;
131: if ( isset($_COOKIE[self::LOGIN_BACKGROUND_COOKIE]) )
132: $this->currentColor = $_COOKIE[self::LOGIN_BACKGROUND_COOKIE];
133:
134: $this->registerJS();
135: }
136:
137: private static $_singleton;
138: public static function singleton () {
139: if (!self::$_singleton)
140: throw new CException ('LoginThemeHelper not yet initialized');
141: return self::$_singleton;
142: }
143:
144: public static function init () {
145: if (self::$_singleton)
146: throw new CException ('LoginThemeHelper can only be initialized once');
147: $calledClass = get_called_class ();
148: self::$_singleton = new $calledClass;
149: }
150:
151: public static function render(){
152: $th = self::$_singleton;
153: ThemeGenerator::renderTheme($th->currentTheme);
154: echo $th->formHtml();
155: }
156:
157: public function formHtml(){
158: $html = '';
159: $html .= CHtml::beginForm('','post', array(
160: 'id'=>'dark-theme-form',
161: ));
162: $html .= CHtml::hiddenField('themeName', $this->nextTheme);
163: $html .= CHtml::endForm();
164: return $html;
165:
166: }
167:
168: /**
169: * Helper action upon login
170: * expects a post of the theme and sets it to be the current theme ONLY if the current theme is not already set.
171: */
172: public static function login() {
173: if (!isset ($_POST[self::LOGIN_THEME_COOKIE])) {
174: return;
175: }
176:
177: $themeName = $_POST[self::LOGIN_THEME_COOKIE];
178: $profile = X2Model::model('Profile')->findByPk(Yii::app()->user->id);
179:
180: if ($profile->theme['themeName'] == '' || $profile->theme['themeName'] == ThemeGenerator::$defaultLight) {
181: $profile->theme = ThemeGenerator::loadDefault($themeName, false);
182: $profile->save();
183: }
184:
185: }
186:
187: /**
188: * Saves a profile Theme to the cookies
189: * @param string $themeName name of the theme to be set.
190: */
191: public static function saveProfileTheme($themeName){
192: //Set a cookie for the profile theme set
193: if( $themeName != ThemeGenerator::$defaultLight) {
194: AuxLib::setCookie(self::PROFILE_COOKIE, $themeName, self::$cookieLength);
195: }
196:
197: // Set a cookie for the login screen
198: if( isset($_COOKIE[self::LOGIN_THEME_COOKIE]) ) {
199: AuxLib::setCookie(self::LOGIN_THEME_COOKIE, $themeName, self::$cookieLength);
200: }
201:
202: }
203:
204: /**
205: * Registers necessary JS and passes is the proper arguments
206: * Checks for POST
207: */
208: public function registerJS() {
209: }
210:
211: }
212:
213: ?>
214: