0013214: allow to set fixed calendars as user preference
[tine20] / tine20 / Calendar / Preference.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Calendar
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @author      Cornelius Weiss <c.weiss@metaways.de>
8  * @copyright   Copyright (c) 2009-2014 Metaways Infosystems GmbH (http://www.metaways.de)
9  */
10
11
12 /**
13  * backend for Calendar preferences
14  *
15  * @package     Calendar
16  */
17 class Calendar_Preference extends Tinebase_Preference_Abstract
18 {
19     /**
20      * where daysview should be scrolled to
21      */
22     const DAYSVIEW_STARTTIME = 'daysviewstarttime';
23     
24     /**
25      * where daysvoew should be scrolled maximum up to
26      */
27     const DAYSVIEW_ENDTIME = 'daysviewendtime';
28     
29     /**
30      * where daysview should be scrolled to
31      */
32     const DAYSVIEW_DEFAULT_STARTTIME = 'daysviewdefaultstarttime';
33
34     /**
35      * time in minutes to be visible in dayviews at once
36      */
37     const DAYSVIEW_TIME_VISIBLE = 'daysviewtimevisible';
38
39     /**
40      * default calendar all newly created/invited events are placed in
41      */
42     const DEFAULTCALENDAR = 'defaultCalendar';
43
44     /**
45      * have name of default favorite an a central palce
46      * _("All my events")
47      */
48     const DEFAULTPERSISTENTFILTER_NAME = "All my events";
49     
50     /**
51      * general notification level
52      */
53     const NOTIFICATION_LEVEL = 'notificationLevel';
54     
55     /**
56      * send notifications of own updates
57      */
58     const SEND_NOTIFICATION_OF_OWN_ACTIONS = 'sendnotificationsofownactions';
59
60     /**
61      * send alarm notifications
62      */
63     const SEND_ALARM_NOTIFICATIONS = 'sendalarmnotifications';
64     
65     /**
66      * enable default alarm 
67      */
68     const DEFAULTALARM_ENABLED = 'defaultalarmenabled';
69     
70     /**
71      * default alarm time in minutes before
72      */
73     const DEFAULTALARM_MINUTESBEFORE = 'defaultalarmminutesbefore';
74     
75     /**
76      * default alarm time in minutes before
77      */
78     const DEFAULTATTENDEE_STRATEGY = 'defaultAttendeeStrategy';
79
80     /**
81      * default set events to privat
82      */
83     const DEFAULT_EVENTS_RRIVATE = 'defaultSetEventsToPrivat';
84     
85     /**
86      * timeIncrement
87      */
88     const DEFAULT_TIMEINCREMENT = 'timeIncrement';
89     
90     /**
91      * firstdayofweek
92      */
93     const FIRSTDAYOFWEEK = 'firstdayofweek';
94
95     /**
96      * default calendar strategy
97      */
98     const DEFAULT_CALENDAR_STRATEGY = 'defaultCalendarStrategy';
99
100     /**
101      * fixedCalendars
102      */
103     const FIXED_CALENDARS = 'fixedCalendars';
104
105     /**
106      * @var string application
107      */
108     protected $_application = 'Calendar';
109         
110     /**
111      * get all possible application prefs
112      *
113      * @return  array   all application prefs
114      */
115     public function getAllApplicationPreferences()
116     {
117         $cropDays = Calendar_Config::getInstance()->get(Calendar_Config::CROP_DAYS_VIEW);
118         
119         $allPrefs = array(
120             self::DAYSVIEW_STARTTIME,
121             self::DAYSVIEW_ENDTIME,
122             self::DAYSVIEW_TIME_VISIBLE,
123             self::DEFAULTCALENDAR,
124             self::DEFAULTPERSISTENTFILTER,
125             self::NOTIFICATION_LEVEL,
126             self::SEND_NOTIFICATION_OF_OWN_ACTIONS,
127             self::SEND_ALARM_NOTIFICATIONS,
128             self::DEFAULTALARM_ENABLED,
129             self::DEFAULTALARM_MINUTESBEFORE,
130             self::DEFAULTATTENDEE_STRATEGY,
131             self::DEFAULT_TIMEINCREMENT,
132             self::DEFAULTATTENDEE_STRATEGY,
133             self::DEFAULT_EVENTS_RRIVATE,
134             self::FIRSTDAYOFWEEK,
135             self::DEFAULT_CALENDAR_STRATEGY,
136             self::FIXED_CALENDARS,
137         );
138         
139         if ($cropDays) {
140             array_unshift($allPrefs, self::DAYSVIEW_DEFAULT_STARTTIME);
141         }
142             
143         return $allPrefs;
144     }
145     
146     /**
147      * get translated right descriptions
148      * 
149      * @return  array with translated descriptions for this applications preferences
150      */
151     public function getTranslatedPreferences()
152     {
153         $translate = Tinebase_Translation::getTranslation($this->_application);
154
155         $prefDescriptions = array(
156             self::DAYSVIEW_STARTTIME => array(
157                 'label'         => $translate->_('Start Time'),
158                 'description'   => $translate->_('Position on the left time axis, day and week view should start with'),
159             ),
160             self::DAYSVIEW_ENDTIME => array(
161                 'label'         => $translate->_('End Time'),
162                 'description'   => $translate->_('Position on the left time axis, day and week view should end with'),
163             ),
164             self::DAYSVIEW_DEFAULT_STARTTIME => array(
165                 'label'         => $translate->_('Default Start Time'),
166                 'description'   => $translate->_('Scroll position on the left time axis, day and week view should start with'),
167             ),
168             self::DAYSVIEW_TIME_VISIBLE => array(
169                 'label'         => $translate->_('Visible Time'),
170                 'description'   => $translate->_('Time to be displayed at once'),
171             ),
172             self::DEFAULTCALENDAR  => array(
173                 'label'         => $translate->_('Default Calendar'),
174                 'description'   => $translate->_('The default calendar for invitations and new events'),
175             ),
176             self::DEFAULTPERSISTENTFILTER  => array(
177                 'label'         => $translate->_('Default Favorite'),
178                 'description'   => $translate->_('The default favorite which is loaded on calendar startup'),
179             ),
180             self::NOTIFICATION_LEVEL => array(
181                 'label'         => $translate->_('Get Notification Emails'),
182                 'description'   => $translate->_('The level of actions you want to be notified about. Please note that organizers will get notifications for all updates including attendee answers unless this preference is set to "Never"'),
183             ),
184             self::SEND_NOTIFICATION_OF_OWN_ACTIONS => array(
185                 'label'         => $translate->_('Send Notifications Emails of own Actions'),
186                 'description'   => $translate->_('Get notifications emails for actions you did yourself'),
187             ),
188             self::SEND_ALARM_NOTIFICATIONS => array(
189                 'label'         => $translate->_('Send Alarm Notifications Emails'),
190                 'description'   => $translate->_('Get event alarms via email'),
191             ),
192             self::DEFAULTALARM_ENABLED => array(
193                 'label'         => $translate->_('Enable Standard Alarm'),
194                 'description'   => $translate->_('New events get a standard alarm as defined below'),
195             ),
196             self::DEFAULTALARM_MINUTESBEFORE => array(
197                 'label'         => $translate->_('Standard Alarm Time'),
198                 'description'   => $translate->_('Minutes before the event starts'),
199             ),
200             self::DEFAULTATTENDEE_STRATEGY => array(
201                     'label'         => $translate->_('Default Attendee Strategy'),
202                     'description'   => $translate->_('Default Attendee Strategy for new events'),
203             ),
204             self::DEFAULT_TIMEINCREMENT => array(
205                 'label'         => $translate->_('Time Increment'),
206                 'description'   => $translate->_('Increment of event time steps'),
207             ),
208             self::DEFAULT_EVENTS_RRIVATE => array(
209                 'label'         => $translate->_('Default set Events to privat'),
210                 'description'   => $translate->_('If enabled every created event is always privat'),
211             ),
212             self::FIRSTDAYOFWEEK => array(
213                 'label'         => $translate->_('First Day of Week'),
214                 'description'   => $translate->_('On what day the week should be starting'),
215             ),
216             self::DEFAULT_CALENDAR_STRATEGY => array(
217                 'label'         => $translate->_('Default calendar strategy'),
218                 'description'   => $translate->_('The calendar for new events if no container is selected'),
219             ),
220             self::FIXED_CALENDARS => array(
221                 'label'         => $translate->_('Fixed Calendars'),
222                 'description'   => $translate->_('Calendars always selected regardless of all filter parameters.'),
223             ),
224         );
225         
226         return $prefDescriptions;
227     }
228     
229     /**
230      * Creates XML Data for a combobox
231      *
232      * Hours: 0 to 24
233      *
234      * @param string $default
235      * @return string
236      */
237     protected function _createTimespanDataXML($start=0, $end=24)
238     {
239         $doc = new DomDocument('1.0');
240         $options = $doc->createElement('options');
241         $doc->appendChild($options);
242         
243         $time = new Tinebase_DateTime('@0');
244         for ($i=$start; $i<=$end; $i++) {
245             $time->setHour($i);
246             $timeString = $time->format('H:i');
247             if ($i == $end && $timeString == '00:00') {
248                 $timeString = '24:00';
249             }
250             $value  = $doc->createElement('value');
251             $value->appendChild($doc->createTextNode($timeString));
252             $label  = $doc->createElement('label');
253             $label->appendChild($doc->createTextNode($timeString)); // @todo l10n
254             
255             $option = $doc->createElement('option');
256             $option->appendChild($value);
257             $option->appendChild($label);
258             $options->appendChild($option);
259         }
260         
261         return $doc->saveXML();
262     }
263
264     /**
265      * get preference defaults if no default is found in the database
266      *
267      * @param string $_preferenceName
268      * @param string|Tinebase_Model_User $_accountId
269      * @param string $_accountType
270      * @return Tinebase_Model_Preference
271      */
272     public function getApplicationPreferenceDefaults($_preferenceName, $_accountId = NULL, $_accountType = Tinebase_Acl_Rights::ACCOUNT_TYPE_USER)
273     {
274         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
275             . ' Get default value for ' . $_preferenceName . ' of account id '. $_accountId);
276         
277         $preference = $this->_getDefaultBasePreference($_preferenceName);
278         
279         switch ($_preferenceName) {
280             case self::DAYSVIEW_STARTTIME:
281                 $preference->value      = '08:00';
282                 $preference->options = $this->_createTimespanDataXML(0, 23);
283                 break;
284             case self::DAYSVIEW_ENDTIME:
285                 $preference->value      = '18:00';
286                 $preference->options = $this->_createTimespanDataXML(1, 24);
287                 break;
288             case self::DAYSVIEW_DEFAULT_STARTTIME:
289                 $preference->value      = '08:00';
290                 $preference->options = $this->_createTimespanDataXML(0, 23);
291                 break;
292             case self::DAYSVIEW_TIME_VISIBLE:
293                 $preference->value      = '10:00';
294                 $preference->options = $this->_createTimespanDataXML(1, 24);
295                 break;
296             case self::DEFAULTCALENDAR:
297                 $this->_getDefaultContainerPreferenceDefaults($preference, $_accountId);
298                 break;
299             case self::DEFAULTPERSISTENTFILTER:
300                 $preference->value          = Tinebase_PersistentFilter::getPreferenceValues('Calendar', $_accountId, "All my events");
301                 break;
302             case self::NOTIFICATION_LEVEL:
303                 $translate = Tinebase_Translation::getTranslation($this->_application);
304                 // need to put the translations strings here because they were not found in the xml below :/
305                 // _('Never') _('On invitation and cancellation only') _('On time changes') _('On all updates but attendee responses') _('On attendee responses too')
306                 $preference->value      = Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_EVENT_RESCHEDULE;
307                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
308                     <options>
309                         <option>
310                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_NONE . '</value>
311                             <label>'. $translate->_('Never') . '</label>
312                         </option>
313                         <option>
314                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_INVITE_CANCEL . '</value>
315                             <label>'. $translate->_('On invitation and cancellation only') . '</label>
316                         </option>
317                         <option>
318                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_EVENT_RESCHEDULE . '</value>
319                             <label>'. $translate->_('On time changes') . '</label>
320                         </option>
321                         <option>
322                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_EVENT_UPDATE . '</value>
323                             <label>'. $translate->_('On all updates but attendee responses') . '</label>
324                         </option>
325                         <option>
326                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_ATTENDEE_STATUS_UPDATE . '</value>
327                             <label>'. $translate->_('On attendee responses too') . '</label>
328                         </option>
329                     </options>';
330                 break;
331             case self::SEND_NOTIFICATION_OF_OWN_ACTIONS:
332                 $preference->value      = 0;
333                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
334                     <options>
335                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
336                     </options>';
337                 break;
338             case self::SEND_ALARM_NOTIFICATIONS:
339                 $preference->value      = 1;
340                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
341                     <options>
342                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
343                     </options>';
344                 break;
345             case self::DEFAULTALARM_ENABLED:
346                 $preference->value      = 0;
347                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
348                     <options>
349                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
350                     </options>';
351                 break;
352             case self::DEFAULTALARM_MINUTESBEFORE:
353                 $preference->value      = 15;
354                 $preference->options    = '';
355                 break;
356             case self::DEFAULTATTENDEE_STRATEGY:
357                 $translate = Tinebase_Translation::getTranslation($this->_application);
358                 $preference->value      = 'me';
359                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
360                     <options>
361                         <option>
362                             <label>' . $translate->_('Me') . '</label>
363                             <value>me</value>
364                         </option>
365                         <option>
366                             <label>' . $translate->_('Intelligent') . '</label>
367                             <value>intelligent</value>
368                         </option>
369                         <option>
370                             <label>' . $translate->_('Calendar owner') . '</label>
371                             <value>calendarOwner</value>
372                         </option>
373                         <option>
374                             <label>' . $translate->_('Filtered attendee') . '</label>
375                             <value>filteredAttendee</value>
376                         </option>
377                         <option>
378                             <label>' . $translate->_('None') . '</label>
379                             <value>none</value>
380                         </option>
381                     </options>';
382                 break;
383             case self::DEFAULT_TIMEINCREMENT:
384                 $preference->value      = 15;
385                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
386                     <options>
387                         <option>
388                             <label>5</label>
389                             <value>5</value>
390                         </option>
391                         <option>
392                             <label>10</label>
393                             <value>10</value>
394                         </option>
395                         <option>
396                             <label>15</label>
397                             <value>15</value>
398                         </option>
399                         <option>
400                             <label>20</label>
401                             <value>20</value>
402                         </option>
403                         <option>
404                             <label>30</label>
405                             <value>30</value>
406                         </option>
407                         <option>
408                             <label>60</label>
409                             <value>60</value>
410                         </option>
411                     </options>';
412                 break;
413             case self::DEFAULT_EVENTS_RRIVATE:
414                 $preference->value      = 0;
415                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
416                     <options>
417                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
418                     </options>';
419                 break;
420             case self::FIRSTDAYOFWEEK:
421                 $preference->value = 1;
422                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
423                     <options>
424                         <option>
425                             <label>Sunday</label>
426                             <value>0</value>
427                         </option>
428                         <option>
429                             <label>Monday</label>
430                             <value>1</value>
431                         </option>
432                     </options>';
433                 break;
434             case self::DEFAULT_CALENDAR_STRATEGY:
435                 $translate = Tinebase_Translation::getTranslation($this->_application);
436                 $preference->value = 'default';
437                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
438                     <options>
439                         <option>
440                             <label>' . $translate->_('Default Calendar') . '</label>
441                             <value>default</value>
442                         </option>
443                         <option>
444                             <label>' . $translate->_('None') . '</label>
445                             <value>none</value>
446                         </option>
447                     </options>';
448                 break;
449             case self::FIXED_CALENDARS:
450                 $preference->value = array();
451                 // TODO set better (?) options / maybe this could be removed
452                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
453                     <options>
454                         <special>' . Tinebase_Preference_Abstract::DEFAULTCONTAINER_OPTIONS . '</special>
455                     </options>';
456
457                 $preference->uiconfig = array(
458                     'xtype'       => 'containerspicker',
459                     'appName'     => 'Calendar',
460                     'model'       => 'Event',
461                 );
462                 $preference->personal_only = true;
463                 break;
464             default:
465                 throw new Tinebase_Exception_NotFound('Default preference with name ' . $_preferenceName . ' not found.');
466         }
467         
468         return $preference;
469     }
470
471     /**
472      * overwrite this in concrete classes if needed
473      *
474      * @param $_preferenceName
475      * @return array
476      */
477     public function _getPrefRecordConfig($_preferenceName)
478     {
479         switch ($_preferenceName) {
480             case self::FIXED_CALENDARS:
481                 return array(
482                     'modelName'      => 'Tinebase_Model_Container',
483                 );
484                 break;
485             default:
486                 return parent::_getPrefRecordConfig($_preferenceName);
487         }
488     }
489 }