0010188: add user contact on the fly if hidden from addressbook
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 29 Aug 2014 09:24:12 +0000 (11:24 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:48 +0000 (11:26 +0200)
... in MSEventFacade, as this requires a valid contact as attender

https://forge.tine20.org/mantisbt/view.php?id=10188

Change-Id: I441f2e03ba747a8a4d24b111afbe2bde8314ebe5
Reviewed-on: http://gerrit.tine20.com/customers/1067
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Calendar/Controller/MSEventFacade.php
tine20/Calendar/Frontend/WebDAV/Event.php

index 52f4312..703d529 100644 (file)
@@ -59,13 +59,14 @@ class Calendar_Controller_MSEventFacade implements Tinebase_Controller_Record_In
      *
      * don't use the constructor. use the singleton 
      */
-    private function __construct() {
+    private function __construct()
+    {
         $this->_eventController = Calendar_Controller_Event::getInstance();
         
         // set default CU
         $this->setCalendarUser(new Calendar_Model_Attender(array(
             'user_type' => Calendar_Model_Attender::USERTYPE_USER,
-            'user_id'   => Tinebase_Core::getUser()->contact_id
+            'user_id'   => self::getCurrentUserContactId()
         )));
     }
 
@@ -91,6 +92,25 @@ class Calendar_Controller_MSEventFacade implements Tinebase_Controller_Record_In
     }
     
     /**
+     * get user contact id
+     * - NOTE: creates a new user contact on the fly if it did not exist before
+     * 
+     * @return string
+     */
+    public static function getCurrentUserContactId()
+    {
+        if (empty(Tinebase_Core::getUser()->contact_id)) {
+            Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+            . ' Creating user contact for ' . Tinebase_Core::getUser()->accountDisplayName . ' on the fly ...');
+            $contact = Admin_Controller_User::getInstance()->createOrUpdateContact(Tinebase_Core::getUser());
+            Tinebase_Core::getUser()->contact_id = $contact->getId();
+            Tinebase_User::getInstance()->updateUserInSqlBackend(Tinebase_Core::getUser());
+        }
+        
+        return Tinebase_Core::getUser()->contact_id;
+    }
+    
+    /**
      * get by id
      *
      * @param string $_id
index 2aaf705..de7b476 100644 (file)
@@ -497,7 +497,7 @@ class Calendar_Frontend_WebDAV_Event extends Sabre\DAV\File implements Sabre\Cal
             Addressbook_Controller_Contact::getInstance()->getContactByUserId($container->getOwner(), true)->getId() :
             Tinebase_Core::getUser()->contact_id;
         } catch (Exception $e) {
-            $calendarUserId = Tinebase_Core::getUser()->contact_id;
+            $calendarUserId = Calendar_Controller_MSEventFacade::getCurrentUserContactId();
         }
         
         $calendarUser = new Calendar_Model_Attender(array(