0010120: shorten long event summaries
[tine20] / tine20 / Calendar / Controller.php
1 <?php
2 /**
3  * Sql Calendar 
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 Metaways Infosystems GmbH (http://www.metaways.de)
9  */
10
11 /**
12  * main controller for Calendar
13  *
14  * @package     Calendar
15  */
16 class Calendar_Controller extends Tinebase_Controller_Event implements Tinebase_Container_Interface
17 {
18     /**
19      * holds the instance of the singleton
20      *
21      * @var Calendar_Controller
22      */
23     private static $_instance = NULL;
24
25     /**
26      * holds the default Model of this application
27      * @var string
28      */
29     protected static $_defaultModel = 'Calendar_Model_Event';
30     
31     /**
32      * don't clone. Use the singleton.
33      *
34      */
35     private function __clone() 
36     {
37     }
38     
39     /**
40      * the singleton pattern
41      *
42      * @return Calendar_Controller
43      */
44     public static function getInstance() 
45     {
46         if (self::$_instance === NULL) {
47             self::$_instance = new Calendar_Controller();
48         }
49         
50         return self::$_instance;
51     }
52
53     /**
54      * event handler function
55      * 
56      * all events get routed through this function
57      *
58      * @param Tinebase_Event_Abstract $_eventObject the eventObject
59      */
60     protected function _handleEvent(Tinebase_Event_Abstract $_eventObject)
61     {
62         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' (' . __LINE__ . ') handle event of type ' . get_class($_eventObject));
63         
64         switch(get_class($_eventObject)) {
65             case 'Admin_Event_AddAccount':
66                 //$this->createPersonalFolder($_eventObject->account);
67                 Tinebase_Core::getPreference('Calendar')->getValueForUser(Calendar_Preference::DEFAULTCALENDAR, $_eventObject->account->getId());
68                 break;
69                 
70             case 'Admin_Event_DeleteAccount':
71                 // not a good idea, as it could be the originaters container for invitations
72                 // we need to move all contained events first
73                 //$this->deletePersonalFolder($_eventObject->account);
74                 break;
75                 
76             case 'Admin_Event_UpdateGroup':
77                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' (' . __LINE__ . ') updated group ' . $_eventObject->group->name);
78                 Tinebase_ActionQueue::getInstance()->queueAction('Calendar.onUpdateGroup', $_eventObject->group->getId());
79                 break;
80             case 'Admin_Event_AddGroupMember':
81                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' (' . __LINE__ . ') add groupmember ' . (string) $_eventObject->userId . ' to group ' . (string) $_eventObject->groupId);
82                 Tinebase_ActionQueue::getInstance()->queueAction('Calendar.onUpdateGroup', $_eventObject->groupId);
83                 break;
84                 
85             case 'Admin_Event_RemoveGroupMember':
86                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' (' . __LINE__ . ') removed groupmember ' . (string) $_eventObject->userId . ' from group ' . (string) $_eventObject->groupId);
87                 Tinebase_ActionQueue::getInstance()->queueAction('Calendar.onUpdateGroup', $_eventObject->groupId);
88                 break;
89                 
90             case 'Tinebase_Event_Container_BeforeCreate':
91                 $this->_handleContainerBeforeCreateEvent($_eventObject);
92                 break;
93         }
94     }
95     
96     /**
97      * creates the initial folder for new accounts
98      *
99      * @param mixed[int|Tinebase_Model_User] $_account   the accountd object
100      * @return Tinebase_Record_RecordSet of subtype Tinebase_Model_Container
101      */
102     public function createPersonalFolder($_account)
103     {
104         $translation = Tinebase_Translation::getTranslation('Calendar');
105         
106         $account = Tinebase_User::getInstance()->getUserById($_account);
107         
108         $newContainer = new Tinebase_Model_Container(array(
109             'name'              => sprintf($translation->_("%s's personal calendar"), $account->accountFullName),
110             'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
111             'owner_id'          => $_account,
112             'backend'           => 'Sql',
113             'color'             => '#FF6600',
114             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId(),
115             'model'             => static::$_defaultModel
116         ));
117         
118         $personalContainer = Tinebase_Container::getInstance()->addContainer($newContainer);
119         $container = new Tinebase_Record_RecordSet('Tinebase_Model_Container', array($personalContainer));
120         
121         return $container;
122     }
123     
124     /**
125      * delete all personal user folders and the contacts associated with these folders
126      *
127      * @param Tinebase_Model_User $_account the accountd object
128      * @todo implement and write test
129      */
130     public function deletePersonalFolder($_account)
131     {
132         
133     }
134     
135     /**
136      * handler for Tinebase_Event_Container_BeforeCreate
137      * - give owner of personal container all grants
138      * - give freebusy grants to anyone for personal container
139      * 
140      * @param Tinebase_Event_Container_BeforeCreate $_eventObject
141      */
142     protected function _handleContainerBeforeCreateEvent(Tinebase_Event_Container_BeforeCreate $_eventObject)
143     {
144         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->INFO(__METHOD__ . ' (' . __LINE__ . ') about to handle Tinebase_Event_Container_BeforeCreate' );
145         
146         if ($_eventObject->container && 
147             $_eventObject->container->type === Tinebase_Model_Container::TYPE_PERSONAL &&
148             $_eventObject->container->application_id === Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId() &&
149             $_eventObject->grants instanceof Tinebase_Record_RecordSet
150             ) {
151             // get owner from initial initial grants
152             $grants = $_eventObject->grants;
153             $grants->removeAll();
154             
155             $grants->addRecord(new Tinebase_Model_Grants(array(
156                 'account_id'     => $_eventObject->accountId,
157                 'account_type'   => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER,
158                 Tinebase_Model_Grants::GRANT_READ      => true,
159                 Tinebase_Model_Grants::GRANT_ADD       => true,
160                 Tinebase_Model_Grants::GRANT_EDIT      => true,
161                 Tinebase_Model_Grants::GRANT_DELETE    => true,
162                 Tinebase_Model_Grants::GRANT_EXPORT    => true,
163                 Tinebase_Model_Grants::GRANT_SYNC      => true,
164                 Tinebase_Model_Grants::GRANT_ADMIN     => true,
165                 Tinebase_Model_Grants::GRANT_FREEBUSY  => true,
166                 Tinebase_Model_Grants::GRANT_PRIVATE   => true,
167             ), TRUE));
168             
169             if (! Tinebase_Config::getInstance()->get(Tinebase_Config::ANYONE_ACCOUNT_DISABLED)) {
170                 $grants->addRecord(new Tinebase_Model_Grants(array(
171                     'account_id'      => '0',
172                     'account_type'    => Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE,
173                     Tinebase_Model_Grants::GRANT_FREEBUSY  => true
174                 ), TRUE));
175             }
176         }
177     }
178     
179     /**
180      * send notifications 
181      * 
182      * @param Calendar_Model_Event       $_event
183      * @param Tinebase_Model_FullAccount $_updater
184      * @param Sting                      $_action
185      * @param Calendar_Model_Event       $_oldEvent
186      * @return void
187      */
188     public function sendEventNotifications($_event, $_updater, $_action, $_oldEvent=NULL)
189     {
190         Calendar_Controller_EventNotifications::getInstance()->doSendNotifications($_event, $_updater, $_action, $_oldEvent);
191     }
192     
193     /**
194      * update group events
195      * 
196      * @param string $_groupId
197      * @return void
198      */
199     public function onUpdateGroup($_groupId)
200     {
201         Calendar_Controller_Event::getInstance()->onUpdateGroup($_groupId);
202     }
203 }