0011984: printing events with tags is broken for daysview
[tine20] / tine20 / Tinebase / Preference.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Tinebase
6  * @subpackage  Preference
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schuele <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2009-2010 Metaways Infosystems GmbH (http://www.metaways.de)
10  * 
11  */
12
13
14 /**
15  * backend for tinebase preferences
16  *
17  * @package     Tinebase
18  * @subpackage  Preference
19  */
20 class Tinebase_Preference extends Tinebase_Preference_Abstract
21 {
22     /**************************** application preferences/settings *****************/
23     
24     /**
25      * page size in grids
26      * 
27      */
28     const PAGE_SIZE = 'pageSize';
29     
30     /**
31      * strip rows in grids
32      * 
33      */
34     const GRID_STRIPE_ROWS = 'gridStripeRows';
35     
36     /**
37      * show load mask in grids
38      * 
39      */
40     const GRID_LOAD_MASK = 'gridLoadMask';
41     
42     /**
43      * order of buttons in dialogs
44      * 
45      */
46     const DIALOG_BUTTONS_ORDER_STYLE = 'dialogButtonsOrderStyle';
47     
48     /**
49      * auto search on filter change
50      * 
51      */
52     const FILTER_CHANGE_AUTO_SEARCH = 'filterChangeAutoSearch';
53    
54     /**
55      *  timezone pref const
56      *
57      */
58     const TIMEZONE = 'timezone';
59
60     /**
61      * locale pref const
62      *
63      */
64     const LOCALE = 'locale';
65     
66     /**
67      * default application
68      *
69      */
70     const DEFAULT_APP = 'defaultapp';
71
72     /**
73      * preferred window type
74      *
75      */
76     const WINDOW_TYPE = 'windowtype';
77     
78     /**
79      * show logout confirmation
80      *
81      */
82     const CONFIRM_LOGOUT = 'confirmLogout';
83
84     /**
85      * advanced search through relations and so on
86      */
87     const ADVANCED_SEARCH = 'advancedSearch';
88     
89     /**************************** public functions *********************************/
90     
91     /**
92      * get all possible application prefs
93      *
94      * @return  array   all application prefs
95      */
96     public function getAllApplicationPreferences()
97     {
98         $allPrefs = ($this->_application == 'Tinebase') 
99             ? array(
100                 self::TIMEZONE,
101                 self::LOCALE,
102                 self::DEFAULT_APP,
103                 self::WINDOW_TYPE,
104                 self::CONFIRM_LOGOUT,
105                 self::PAGE_SIZE,
106                 self::GRID_STRIPE_ROWS,
107                 self::GRID_LOAD_MASK,
108                 self::DIALOG_BUTTONS_ORDER_STYLE,
109                 self::FILTER_CHANGE_AUTO_SEARCH,
110                 self::ADVANCED_SEARCH
111             )
112             : array();
113             
114         return $allPrefs;
115     }
116     
117     /**
118      * get translated right descriptions
119      * 
120      * @return  array with translated descriptions for this applications preferences
121      */
122     public function getTranslatedPreferences()
123     {
124         $translate = Tinebase_Translation::getTranslation($this->_application);
125
126         $prefDescriptions = array(
127             self::PAGE_SIZE  => array(
128                 'label'         => $translate->_('Page size'),
129                 'description'   => $translate->_('Page size in grids'),
130             ),
131             self::GRID_STRIPE_ROWS  => array(
132                 'label'         => $translate->_('Grid stripe rows'),
133                 'description'   => $translate->_('Stripe rows in grids'),
134             ),
135             self::GRID_LOAD_MASK  => array(
136                 'label'         => $translate->_('Grid load mask'),
137                 'description'   => $translate->_('Show load mask in grids'),
138             ),
139             self::DIALOG_BUTTONS_ORDER_STYLE  => array(
140                 'label'         => $translate->_('Dialog buttons order style'),
141                 'description'   => $translate->_('Dialog buttons order style (Windows / Mac)'),
142             ),
143             self::FILTER_CHANGE_AUTO_SEARCH  => array(
144                 'label'         => $translate->_('Auto search on filter change'),
145                 'description'   => $translate->_('Perform auto search when filter is changed'),
146             ),
147             self::TIMEZONE  => array(
148                 'label'         => $translate->_('Timezone'),
149                 'description'   => $translate->_('The timezone in which dates are shown in Tine 2.0.'),
150             ),
151             self::LOCALE  => array(
152                 'label'         => $translate->_('Language'),
153                 'description'   => $translate->_('The language of the Tine 2.0 GUI.'),
154             ),
155             self::DEFAULT_APP  => array(
156                 'label'         => $translate->_('Default Application'),
157                 'description'   => $translate->_('The default application to show after login.'),
158             ),
159             self::WINDOW_TYPE  => array(
160                 'label'         => $translate->_('Window Type'),
161                 'description'   => $translate->_('You can choose between modal windows or normal browser popup windows.'),
162             ),
163             self::CONFIRM_LOGOUT  => array(
164                 'label'         => $translate->_('Confirm Logout'),
165                 'description'   => $translate->_('Show confirmation dialog on logout.'),
166             ),
167             self::ADVANCED_SEARCH => array(
168                 'label'         => $translate->_('Enable advanced search'),
169                 'description'   => $translate->_('If enabled quickfilter searches through relations as well.')
170             )
171         );
172         
173         return $prefDescriptions;
174     }
175     
176     /**
177      * get preference defaults if no default is found in the database
178      *
179      * @param string $_preferenceName
180      * @return Tinebase_Model_Preference
181      */
182     public function getApplicationPreferenceDefaults($_preferenceName, $_accountId=NULL, $_accountType=Tinebase_Acl_Rights::ACCOUNT_TYPE_USER)
183     {
184         $preference = $this->_getDefaultBasePreference($_preferenceName);
185         
186         switch($_preferenceName) {
187             case self::PAGE_SIZE:
188                 $preference->value  = 50; 
189                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
190                     <options>
191                         <option>
192                             <label>15</label>
193                             <value>15</value>
194                         </option>
195                         <option>
196                             <label>30</label>
197                             <value>30</value>
198                         </option>
199                         <option>
200                             <label>50</label>
201                             <value>50</value>
202                         </option>
203                         <option>
204                             <label>100</label>
205                             <value>100</value>
206                         </option>
207                     </options>';
208                 $preference->personal_only = FALSE;
209                 break;
210
211             case self::GRID_STRIPE_ROWS:
212                 $preference->value      = 0;
213                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
214                     <options>
215                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
216                     </options>';
217                   $preference->personal_only = FALSE;
218                 break;
219             case self::GRID_LOAD_MASK:
220                 $preference->value      = 0;
221                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
222                     <options>
223                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
224                     </options>';
225                   $preference->personal_only = FALSE;
226                 break;
227
228             case self::DIALOG_BUTTONS_ORDER_STYLE:
229                 $preference->value  = 'Macintosh'; 
230                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
231                     <options>
232                         <option>
233                             <label>Windows</label>
234                             <value>Windows</value>
235                         </option>
236                         <option>
237                             <label>Macintosh</label>
238                             <value>Macintosh</value>
239                         </option>
240                     </options>';
241                 $preference->personal_only = FALSE;
242                 break;
243             case self::FILTER_CHANGE_AUTO_SEARCH:
244                 $preference->value      = 1;
245                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
246                     <options>
247                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
248                     </options>';
249                 $preference->personal_only = FALSE;
250                 break;
251             case self::TIMEZONE:
252                 $preference->value      = 'Europe/Berlin';
253                 break;
254             case self::LOCALE:
255                 $preference->value      = 'auto';
256                 break;
257             case self::DEFAULT_APP:
258                 $preference->value      = 'Addressbook';
259                 break;
260             case self::WINDOW_TYPE:
261                 $preference->value      = 'Browser';
262                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
263                     <options>
264                         <option>
265                             <label>Browser windows</label>
266                             <value>Browser</value>
267                         </option>
268                     </options>';
269                 break;
270             case self::CONFIRM_LOGOUT:
271                 $preference->value      = 1;
272                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
273                     <options>
274                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
275                     </options>';
276                 break;
277             case self::ADVANCED_SEARCH:
278                 $preference->value      = 0;
279                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
280                     <options>
281                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
282                     </options>';
283                 break;
284             default:
285                 throw new Tinebase_Exception_NotFound('Default preference with name ' . $_preferenceName . ' not found.');
286         }
287         
288         return $preference;
289     }
290     
291     /**
292      * overwrite this to add more special options for other apps
293      *
294      * - result array has to have the following format:
295      *  array(
296      *      array('value1', 'label1'),
297      *      array('value2', 'label2'),
298      *      ...
299      *  )
300      *
301      * @param string $_value
302      * @return array
303      *
304      * @todo add application title translations?
305      */
306     protected function _getSpecialOptions($_value)
307     {
308         $result = array();
309
310         switch ($_value) {
311
312             case Tinebase_Preference::TIMEZONE:
313                 $locale =  Tinebase_Core::get(Tinebase_Core::LOCALE);
314
315                 $availableTimezonesTranslations = Zend_Locale::getTranslationList('citytotimezone', $locale);
316                 $availableTimezones = DateTimeZone::listIdentifiers();
317                 foreach ($availableTimezones as $timezone) {
318                     $result[] = array($timezone, $timezone);
319                 }
320                 break;
321
322             case Tinebase_Preference::LOCALE:
323                 $availableTranslations = Tinebase_Translation::getAvailableTranslations();
324                 foreach ($availableTranslations as $lang) {
325                     $region = (!empty($lang['region'])) ? ' / ' . $lang['region'] : '';
326                     $result[] = array($lang['locale'], $lang['language'] . $region);
327                 }
328                 break;
329
330             case Tinebase_Preference::DEFAULT_APP:
331                 $applications = Tinebase_Application::getInstance()->getApplications();
332                 foreach ($applications as $app) {
333                     if (
334                     $app->status == 'enabled'
335                     && $app->name != 'Tinebase'
336                     && Tinebase_Core::getUser()->hasRight($app->name, Tinebase_Acl_Rights_Abstract::RUN)
337                     ) {
338                         $result[] = array($app->name, $app->name);
339                     }
340                 }
341                 break;
342
343             default:
344                 $result = parent::_getSpecialOptions($_value);
345                 break;
346         }
347
348         return $result;
349     }
350     
351     /**
352      * do some call json functions if preferences name match
353      * - every app should define its own special handlers
354      *
355      * @param Tinebase_Frontend_Json_Abstract $_jsonFrontend
356      * @param string $name
357      * @param string $value
358      * @param string $appName
359      */
360     public function doSpecialJsonFrontendActions(Tinebase_Frontend_Json_Abstract $_jsonFrontend, $name, $value, $appName)
361     {
362         if ($appName == $this->_application) {
363             // get default prefs if value = use default
364             if ($value == Tinebase_Model_Preference::DEFAULT_VALUE) {
365                 $value = $this->{$name};
366                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Using default (' . $value . ') for ' . $name);
367             }
368             
369             $session = Tinebase_Core::get(Tinebase_Session::SESSION);
370             
371             switch ($name) {
372                 case Tinebase_Preference::LOCALE:
373                     unset($session->userLocale);
374                     $_jsonFrontend->setLocale($value, FALSE, TRUE);
375                     break;
376                 case Tinebase_Preference::TIMEZONE:
377                     unset($session->timezone);
378                     $_jsonFrontend->setTimezone($value, FALSE);
379                     break;
380             }
381         }
382     }
383 }