improve default attendee for CalDAV/ActiveSync
[tine20] / tine20 / Calendar / Frontend / WebDAV / Event.php
index 73c067c..2aaf705 100644 (file)
@@ -60,10 +60,7 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
         
         list($backend, $version) = Calendar_Convert_Event_VCalendar_Factory::parseUserAgent($_SERVER['HTTP_USER_AGENT']);
         
-        $this->_eventFilter = new Calendar_Model_EventFilter(array(
-            array('field' => 'container_id', 'operator' => 'equals', 'value' => $this->_container->getId())
-        ));
-        $this->_assertEventFilter();
+        $this->_assertEventFacadeParams($this->_container);
         
         $this->_converter = Calendar_Convert_Event_VCalendar_Factory::factory($backend, $version);
     }
@@ -140,10 +137,7 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
             . " Event to create: " . print_r($event->toArray(), TRUE));
         
-        $filter =  new Calendar_Model_EventFilter(array(
-            array('field' => 'container_id', 'operator' => 'equals', 'value' => $container->getId())
-        ));
-        Calendar_Controller_MSEventFacade::getInstance()->setEventFilter($filter);
+        self::_assertEventFacadeParams($container);
         
         // check if there is already an existing event with this ID
         // this can happen when the invitation email is faster then the caldav update or
@@ -202,7 +196,7 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
         sleep(1);
         
         // (re) fetch event as tree move does not refresh src node before delete
-        $this->_assertEventFilter();
+        $this->_assertEventFacadeParams($this->_container);
         $event = Calendar_Controller_MSEventFacade::getInstance()->get($this->_event);
         
         // disallow event cleanup in the past
@@ -379,7 +373,7 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
      */
     public function put($cardData) 
     {
-        $this->_assertEventFilter();
+        $this->_assertEventFacadeParams($this->_container);
         if (get_class($this->_converter) == 'Calendar_Convert_Event_VCalendar_Generic') {
             if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) 
                 Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . " update by generic client not allowed. See Calendar_Convert_Event_VCalendar_Factory for supported clients.");
@@ -487,14 +481,32 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
     }
     
     /**
-     * asserts correct event filter in MSEventFacade
+     * asserts correct event filter and calendar user in MSEventFacade
      * 
      * NOTE: this is nessesary as MSEventFacade is a singleton and in some operations (e.g. move) there are 
      *       multiple instances of self
      */
-    protected function _assertEventFilter()
+    protected static function _assertEventFacadeParams($container)
     {
-        Calendar_Controller_MSEventFacade::getInstance()->setEventFilter(clone $this->_eventFilter);
+        $eventFilter = new Calendar_Model_EventFilter(array(
+                array('field' => 'container_id', 'operator' => 'equals', 'value' => $container->getId())
+        ));
+        
+        try {
+            $calendarUserId = $container->type == Tinebase_Model_Container::TYPE_PERSONAL ?
+            Addressbook_Controller_Contact::getInstance()->getContactByUserId($container->getOwner(), true)->getId() :
+            Tinebase_Core::getUser()->contact_id;
+        } catch (Exception $e) {
+            $calendarUserId = Tinebase_Core::getUser()->contact_id;
+        }
+        
+        $calendarUser = new Calendar_Model_Attender(array(
+            'user_type' => Calendar_Model_Attender::USERTYPE_USER,
+            'user_id'   => $calendarUserId,
+        ));
+        
+        Calendar_Controller_MSEventFacade::getInstance()->setEventFilter($eventFilter);
+        Calendar_Controller_MSEventFacade::getInstance()->setCalendarUser($calendarUser);
     }
     
     /**
@@ -518,7 +530,7 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
     public function getRecord()
     {
         if (! $this->_event instanceof Calendar_Model_Event) {
-            $this->_assertEventFilter();
+            $this->_assertEventFacadeParams($this->_container);
             $this->_event = Calendar_Controller_MSEventFacade::getInstance()->get($this->_event);
             
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " " . print_r($this->_event->toArray(), true));