Calendar - AttenderFilter - handle attender->user_id is object
authorPaul Mehrer <p.mehrer@metaways.de>
Fri, 23 Jun 2017 13:52:55 +0000 (15:52 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Mon, 26 Jun 2017 10:17:04 +0000 (12:17 +0200)
attender->user_id for user_type = group maybe an object. Resolve with getId

Change-Id: Ia7b4bfb36958300897b18938ff4b08cb9777bcd0
Reviewed-on: http://gerrit.tine20.com/customers/4941
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tests/tine20/Calendar/Controller/EventTests.php
tine20/Calendar/Controller/Event.php
tine20/Calendar/Frontend/Json.php
tine20/Calendar/Model/Attender.php
tine20/Calendar/Model/AttenderFilter.php

index cf9ece8..2eb5f51 100644 (file)
@@ -353,6 +353,39 @@ class Calendar_Controller_EventTests extends Calendar_TestCase
         return $persistentEvent;
     }
 
+    /**
+     * test get free busy info with single event
+     *
+     * @return Calendar_Model_Event
+     */
+    public function testGetFreeBusyInfoWithGroup()
+    {
+        $event = $this->_getEvent();
+        $attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
+            array(
+                'user_id' => Tinebase_Group::getInstance()->getDefaultGroup(),
+                'user_type' => Calendar_Model_Attender::USERTYPE_GROUP,
+                'role' => Calendar_Model_Attender::ROLE_OPTIONAL,
+            ),
+        ));
+        $event->attendee = clone $attendee;
+        $persistentEvent = $this->_controller->create($event);
+
+        $period = new Calendar_Model_EventFilter(array(array(
+            'field'     => 'period',
+            'operator'  => 'within',
+            'value'     => array(
+                'from'      => $persistentEvent->dtstart,
+                'until'     => $persistentEvent->dtend
+            ),
+        )));
+        $fbinfo = $this->_controller->getFreeBusyInfo($period, $attendee);
+
+        $this->assertGreaterThanOrEqual(1, count($fbinfo));
+
+        return $persistentEvent;
+    }
+
     public function testSearchFreeTime()
     {
         $event = $this->_getEvent();
index dd2fd7b..6543a81 100644 (file)
@@ -365,8 +365,8 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
      * @todo merge overlapping events to one freebusy entry
      * 
      * @param  Calendar_Model_EventFilter                           $_periods
-     * @param  Tinebase_Record_RecordSet of Calendar_Model_Attender $_attendee
-     * @param  array of UIDs                                        $_ignoreUIDs
+     * @param  Tinebase_Record_RecordSet                            $_attendee
+     * @param  array                                                $_ignoreUIDs
      * @return Tinebase_Record_RecordSet of Calendar_Model_FreeBusy
      */
     public function getFreeBusyInfo($_periods, $_attendee, $_ignoreUIDs = array())
@@ -377,9 +377,26 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
         $attendee = clone $_attendee;
         $groupmembers = $attendee->filter('user_type', Calendar_Model_Attender::USERTYPE_GROUPMEMBER);
         $groupmembers->user_type = Calendar_Model_Attender::USERTYPE_USER;
+        /*$groups = $attendee->filter('user_type', Calendar_Model_Attender::USERTYPE_GROUP);
+        $attendee->removeRecords($groups);
+        /** @var Calendar_Model_Attender $group *
+        foreach($groups as $group) {
+            $group = Tinebase_Group::getInstance()->getGroupById($group->user_id);
+
+            // fetch list only if list_id is not NULL, otherwise we get back an empty list object
+            if (!empty($group->list_id)) {
+                $contactList = Addressbook_Controller_List::getInstance()->get($group->list_id);
+                foreach ($contactList->members as $member) {
+                    $attendee->addRecord(new Calendar_Model_Attender(array(
+                        'user_id' => $member,
+                        'user_type' => Calendar_Model_Attender::USERTYPE_USER
+                    ), true));
+                }
+            }
+        }*/
         
         $conflictCriteria = new Calendar_Model_EventFilter(array(
-            array('field' => 'attender', 'operator' => 'in',     'value' => $_attendee),
+            array('field' => 'attender', 'operator' => 'in',     'value' => $attendee),
             array('field' => 'transp',   'operator' => 'equals', 'value' => Calendar_Model_Event::TRANSP_OPAQUE)
         ));
 
@@ -388,10 +405,12 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
         // create a typemap
         $typeMap = array();
         foreach ($attendee as $attender) {
-            if (! (isset($typeMap[$attender['user_type']]) || array_key_exists($attender['user_type'], $typeMap))) {
+            if (! isset($typeMap[$attender['user_type']])) {
                 $typeMap[$attender['user_type']] = array();
             }
-            
+            if (is_object($attender['user_id'])) {
+                $attender['user_id'] = $attender['user_id']->getId();
+            }
             $typeMap[$attender['user_type']][$attender['user_id']] = array();
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' ' . __LINE__
index b93fe60..626bd35 100644 (file)
@@ -594,7 +594,7 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
             foreach ($result as $type => $res) {
                 foreach ($res['results'] as $r) {
                     $attendee[] = array(
-                        'user_id' => $r['id'],
+                        'user_id' => $type === 'group' ? $r['group_id'] : $r['id'],
                         'user_type' => $type
                     );
                 }
index 26942f3..d7e6701 100644 (file)
@@ -13,6 +13,7 @@
  * @property Tinebase_DateTime alarm_ack_time
  * @property Tinebase_DateTime alarm_snooze_time
  * @property string transp
+ * @property string user_id
  */
 class Calendar_Model_Attender extends Tinebase_Record_Abstract
 {
index ac6e988..9f2934e 100644 (file)
@@ -146,6 +146,12 @@ class Calendar_Model_AttenderFilter extends Tinebase_Model_Filter_Abstract
                     }
                 }
             } else {
+                if (is_object($attenderValue->user_id)) {
+                    $attenderValue = array(
+                        'user_type' => $attenderValue->user_type,
+                        'user_id'   => $attenderValue->user_id->getId()
+                    );
+                }
                 $attendee = array($attenderValue);
             }