9d226a44c833290e34c2b81d10df6c814776ffb3
[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      * @var string application
102      */
103     protected $_application = 'Calendar';
104         
105     /**
106      * get all possible application prefs
107      *
108      * @return  array   all application prefs
109      */
110     public function getAllApplicationPreferences()
111     {
112         $cropDays = Calendar_Config::getInstance()->get(Calendar_Config::CROP_DAYS_VIEW);
113         
114         $allPrefs = array(
115             self::DAYSVIEW_STARTTIME,
116             self::DAYSVIEW_ENDTIME,
117             self::DAYSVIEW_TIME_VISIBLE,
118             self::DEFAULTCALENDAR,
119             self::DEFAULTPERSISTENTFILTER,
120             self::NOTIFICATION_LEVEL,
121             self::SEND_NOTIFICATION_OF_OWN_ACTIONS,
122             self::SEND_ALARM_NOTIFICATIONS,
123             self::DEFAULTALARM_ENABLED,
124             self::DEFAULTALARM_MINUTESBEFORE,
125             self::DEFAULTATTENDEE_STRATEGY,
126             self::DEFAULT_TIMEINCREMENT,
127             self::DEFAULTATTENDEE_STRATEGY,
128             self::DEFAULT_EVENTS_RRIVATE,
129             self::FIRSTDAYOFWEEK,
130             self::DEFAULT_CALENDAR_STRATEGY
131         );
132         
133         if ($cropDays) {
134             array_unshift($allPrefs, self::DAYSVIEW_DEFAULT_STARTTIME);
135         }
136             
137         return $allPrefs;
138     }
139     
140     /**
141      * get translated right descriptions
142      * 
143      * @return  array with translated descriptions for this applications preferences
144      */
145     public function getTranslatedPreferences()
146     {
147         $translate = Tinebase_Translation::getTranslation($this->_application);
148
149         $prefDescriptions = array(
150             self::DAYSVIEW_STARTTIME => array(
151                 'label'         => $translate->_('Start Time'),
152                 'description'   => $translate->_('Position on the left time axis, day and week view should start with'),
153             ),
154             self::DAYSVIEW_ENDTIME => array(
155                 'label'         => $translate->_('End Time'),
156                 'description'   => $translate->_('Position on the left time axis, day and week view should end with'),
157             ),
158             self::DAYSVIEW_DEFAULT_STARTTIME => array(
159                 'label'         => $translate->_('Default Start Time'),
160                 'description'   => $translate->_('Scroll position on the left time axis, day and week view should start with'),
161             ),
162             self::DAYSVIEW_TIME_VISIBLE => array(
163                 'label'         => $translate->_('Visible Time'),
164                 'description'   => $translate->_('Time to be displayed at once'),
165             ),
166             self::DEFAULTCALENDAR  => array(
167                 'label'         => $translate->_('Default Calendar'),
168                 'description'   => $translate->_('The default calendar for invitations and new events'),
169             ),
170             self::DEFAULTPERSISTENTFILTER  => array(
171                 'label'         => $translate->_('Default Favorite'),
172                 'description'   => $translate->_('The default favorite which is loaded on calendar startup'),
173             ),
174             self::NOTIFICATION_LEVEL => array(
175                 'label'         => $translate->_('Get Notification Emails'),
176                 '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"'),
177             ),
178             self::SEND_NOTIFICATION_OF_OWN_ACTIONS => array(
179                 'label'         => $translate->_('Send Notifications Emails of own Actions'),
180                 'description'   => $translate->_('Get notifications emails for actions you did yourself'),
181             ),
182             self::SEND_ALARM_NOTIFICATIONS => array(
183                 'label'         => $translate->_('Send Alarm Notifications Emails'),
184                 'description'   => $translate->_('Get event alarms via email'),
185             ),
186             self::DEFAULTALARM_ENABLED => array(
187                 'label'         => $translate->_('Enable Standard Alarm'),
188                 'description'   => $translate->_('New events get a standard alarm as defined below'),
189             ),
190             self::DEFAULTALARM_MINUTESBEFORE => array(
191                 'label'         => $translate->_('Standard Alarm Time'),
192                 'description'   => $translate->_('Minutes before the event starts'),
193             ),
194             self::DEFAULTATTENDEE_STRATEGY => array(
195                     'label'         => $translate->_('Default Attendee Strategy'),
196                     'description'   => $translate->_('Default Attendee Strategy for new events'),
197             ),
198             self::DEFAULT_TIMEINCREMENT => array(
199                 'label'         => $translate->_('Time Increment'),
200                 'description'   => $translate->_('Increment of event time steps'),
201             ),
202             self::DEFAULT_EVENTS_RRIVATE => array(
203                 'label'         => $translate->_('Default set Events to privat'),
204                 'description'   => $translate->_('If enabled every created event is always privat'),
205             ),
206             self::FIRSTDAYOFWEEK => array(
207                 'label'         => $translate->_('First Day of Week'),
208                 'description'   => $translate->_('On what day the week should be starting'),
209             ),
210             self::DEFAULT_CALENDAR_STRATEGY => array(
211                 'label'         => $translate->_('Default calendar strategy'),
212                 'description'   => $translate->_('The calendar for new events if no container is selected'),
213             ),
214         );
215         
216         return $prefDescriptions;
217     }
218     
219     /**
220      * Creates XML Data for a combobox
221      *
222      * Hours: 0 to 24
223      *
224      * @param string $default
225      * @return string
226      */
227     protected function _createTimespanDataXML($start=0, $end=24)
228     {
229         $doc = new DomDocument('1.0');
230         $options = $doc->createElement('options');
231         $doc->appendChild($options);
232         
233         $time = new Tinebase_DateTime('@0');
234         for ($i=$start; $i<=$end; $i++) {
235             $time->setHour($i);
236             $timeString = $time->format('H:i');
237             if ($i == $end && $timeString == '00:00') {
238                 $timeString = '24:00';
239             }
240             $value  = $doc->createElement('value');
241             $value->appendChild($doc->createTextNode($timeString));
242             $label  = $doc->createElement('label');
243             $label->appendChild($doc->createTextNode($timeString)); // @todo l10n
244             
245             $option = $doc->createElement('option');
246             $option->appendChild($value);
247             $option->appendChild($label);
248             $options->appendChild($option);
249         }
250         
251         return $doc->saveXML();
252     }
253
254     /**
255      * get preference defaults if no default is found in the database
256      *
257      * @param string $_preferenceName
258      * @param string|Tinebase_Model_User $_accountId
259      * @param string $_accountType
260      * @return Tinebase_Model_Preference
261      */
262     public function getApplicationPreferenceDefaults($_preferenceName, $_accountId = NULL, $_accountType = Tinebase_Acl_Rights::ACCOUNT_TYPE_USER)
263     {
264         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ 
265             . ' Get default value for ' . $_preferenceName . ' of account id '. $_accountId);
266         
267         $preference = $this->_getDefaultBasePreference($_preferenceName);
268         
269         switch ($_preferenceName) {
270             case self::DAYSVIEW_STARTTIME:
271                 $preference->value      = '08:00';
272                 $preference->options = $this->_createTimespanDataXML(0, 23);
273                 break;
274             case self::DAYSVIEW_ENDTIME:
275                 $preference->value      = '18:00';
276                 $preference->options = $this->_createTimespanDataXML(1, 24);
277                 break;
278             case self::DAYSVIEW_DEFAULT_STARTTIME:
279                 $preference->value      = '08:00';
280                 $preference->options = $this->_createTimespanDataXML(0, 23);
281                 break;
282             case self::DAYSVIEW_TIME_VISIBLE:
283                 $preference->value      = '10:00';
284                 $preference->options = $this->_createTimespanDataXML(1, 24);
285                 break;
286             case self::DEFAULTCALENDAR:
287                 $this->_getDefaultContainerPreferenceDefaults($preference, $_accountId);
288                 break;
289             case self::DEFAULTPERSISTENTFILTER:
290                 $preference->value          = Tinebase_PersistentFilter::getPreferenceValues('Calendar', $_accountId, "All my events");
291                 break;
292             case self::NOTIFICATION_LEVEL:
293                 $translate = Tinebase_Translation::getTranslation($this->_application);
294                 // need to put the translations strings here because they were not found in the xml below :/
295                 // _('Never') _('On invitation and cancellation only') _('On time changes') _('On all updates but attendee responses') _('On attendee responses too')
296                 $preference->value      = Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_EVENT_RESCHEDULE;
297                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
298                     <options>
299                         <option>
300                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_NONE . '</value>
301                             <label>'. $translate->_('Never') . '</label>
302                         </option>
303                         <option>
304                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_INVITE_CANCEL . '</value>
305                             <label>'. $translate->_('On invitation and cancellation only') . '</label>
306                         </option>
307                         <option>
308                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_EVENT_RESCHEDULE . '</value>
309                             <label>'. $translate->_('On time changes') . '</label>
310                         </option>
311                         <option>
312                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_EVENT_UPDATE . '</value>
313                             <label>'. $translate->_('On all updates but attendee responses') . '</label>
314                         </option>
315                         <option>
316                             <value>'. Calendar_Controller_EventNotifications::NOTIFICATION_LEVEL_ATTENDEE_STATUS_UPDATE . '</value>
317                             <label>'. $translate->_('On attendee responses too') . '</label>
318                         </option>
319                     </options>';
320                 break;
321             case self::SEND_NOTIFICATION_OF_OWN_ACTIONS:
322                 $preference->value      = 0;
323                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
324                     <options>
325                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
326                     </options>';
327                 break;
328             case self::SEND_ALARM_NOTIFICATIONS:
329                 $preference->value      = 1;
330                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
331                     <options>
332                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
333                     </options>';
334                 break;
335             case self::DEFAULTALARM_ENABLED:
336                 $preference->value      = 0;
337                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
338                     <options>
339                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
340                     </options>';
341                 break;
342             case self::DEFAULTALARM_MINUTESBEFORE:
343                 $preference->value      = 15;
344                 $preference->options    = '';
345                 break;
346             case self::DEFAULTATTENDEE_STRATEGY:
347                 $translate = Tinebase_Translation::getTranslation($this->_application);
348                 $preference->value      = 'me';
349                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
350                     <options>
351                         <option>
352                             <label>' . $translate->_('Me') . '</label>
353                             <value>me</value>
354                         </option>
355                         <option>
356                             <label>' . $translate->_('Intelligent') . '</label>
357                             <value>intelligent</value>
358                         </option>
359                         <option>
360                             <label>' . $translate->_('Calendar owner') . '</label>
361                             <value>calendarOwner</value>
362                         </option>
363                         <option>
364                             <label>' . $translate->_('Filtered attendee') . '</label>
365                             <value>filteredAttendee</value>
366                         </option>
367                         <option>
368                             <label>' . $translate->_('None') . '</label>
369                             <value>none</value>
370                         </option>
371                     </options>';
372                 break;
373             case self::DEFAULT_TIMEINCREMENT:
374                 $preference->value      = 15;
375                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
376                     <options>
377                         <option>
378                             <label>5</label>
379                             <value>5</value>
380                         </option>
381                         <option>
382                             <label>10</label>
383                             <value>10</value>
384                         </option>
385                         <option>
386                             <label>15</label>
387                             <value>15</value>
388                         </option>
389                         <option>
390                             <label>20</label>
391                             <value>20</value>
392                         </option>
393                         <option>
394                             <label>30</label>
395                             <value>30</value>
396                         </option>
397                         <option>
398                             <label>60</label>
399                             <value>60</value>
400                         </option>
401                     </options>';
402                 break;
403             case self::DEFAULT_EVENTS_RRIVATE:
404                 $preference->value      = 0;
405                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
406                     <options>
407                         <special>' . Tinebase_Preference_Abstract::YES_NO_OPTIONS . '</special>
408                     </options>';
409                 break;
410             case self::FIRSTDAYOFWEEK:
411                 $preference->value = 1;
412                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
413                     <options>
414                         <option>
415                             <label>Sunday</label>
416                             <value>0</value>
417                         </option>
418                         <option>
419                             <label>Monday</label>
420                             <value>1</value>
421                         </option>
422                     </options>';
423                 break;
424             case self::DEFAULT_CALENDAR_STRATEGY:
425                 $translate = Tinebase_Translation::getTranslation($this->_application);
426                 $preference->value = 'default';
427                 $preference->options    = '<?xml version="1.0" encoding="UTF-8"?>
428                     <options>
429                         <option>
430                             <label>' . $translate->_('Default Calendar') . '</label>
431                             <value>default</value>
432                         </option>
433                         <option>
434                             <label>' . $translate->_('None') . '</label>
435                             <value>none</value>
436                         </option>
437                     </options>';
438                 break;
439             default:
440                 throw new Tinebase_Exception_NotFound('Default preference with name ' . $_preferenceName . ' not found.');
441         }
442         
443         return $preference;
444     }
445 }