Calendar Frontend Json - searchAttendee / getFreeBusyInfo use event
authorPaul Mehrer <p.mehrer@metaways.de>
Fri, 16 Jun 2017 10:22:16 +0000 (12:22 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Wed, 21 Jun 2017 05:50:37 +0000 (07:50 +0200)
Calendar - searchFreeTime - fix timezone issue

instead of periods (so the event can carry a rrule, that will in turn be
resprected when calculating the periods)

Change-Id: I0076f601b730a85a750bfef467b54e586c59a816
Reviewed-on: http://gerrit.tine20.com/customers/4884
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
tests/tine20/Calendar/JsonTests.php
tine20/Calendar/Controller/Event.php
tine20/Calendar/Frontend/Json.php

index ec6e2fc..cf9ece8 100644 (file)
@@ -361,7 +361,6 @@ class Calendar_Controller_EventTests extends Calendar_TestCase
             array('user_id' => $this->_getPersonasContacts('pwulf')->getId(), 'user_type' => Calendar_Model_Attender::USERTYPE_USER)
         ));
         $event->rrule = 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,FR';
-        $event->originator_tz = $event->dtstart->getTimezone()->getName();
 
         $options = array(
             'constraints' => array(array(
index 17d4fa1..354bee0 100644 (file)
@@ -1996,24 +1996,16 @@ class Calendar_JsonTests extends Calendar_TestCase
             array('user_id' => $this->_getPersonasContacts('pwulf')->getId())
         ));
         $persistentEvent = $this->_eventController->create($event);
+        $persistentEvent->setTimezone(Tinebase_Core::getUserTimezone());
 
-        $period = array(array(
-            'field'     => 'period',
-            'operator'  => 'within',
-            'value'     => array(
-                'from'      => $persistentEvent->dtstart,
-                'until'     => $persistentEvent->dtend
-            ),
-        ));
-
-        $fbinfo = $this->_uit->getFreeBusyInfo($persistentEvent->attendee->toArray(), $period);
+        $fbinfo = $this->_uit->getFreeBusyInfo($persistentEvent->attendee->toArray(), $persistentEvent->toArray());
         $this->assertEquals(2, count($fbinfo));
 
-        $fbinfo = $this->_uit->getFreeBusyInfo($persistentEvent->attendee->toArray(), $period, array($persistentEvent->uid));
+        $fbinfo = $this->_uit->getFreeBusyInfo($persistentEvent->attendee->toArray(), $persistentEvent->toArray(), array($persistentEvent->uid));
         $this->assertEquals(0, count($fbinfo));
     }
 
-    public function testSearchAttendee()
+    public function testSearchAttenders()
     {
         $event = $this->_getEvent();
         $event->attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(
@@ -2021,20 +2013,12 @@ class Calendar_JsonTests extends Calendar_TestCase
             array('user_id' => $this->_getPersonasContacts('pwulf')->getId())
         ));
         $persistentEvent = $this->_eventController->create($event);
-
-        $period = array(array(
-            'field'     => 'period',
-            'operator'  => 'within',
-            'value'     => array(
-                'from'      => $persistentEvent->dtstart,
-                'until'     => $persistentEvent->dtend
-            ),
-        ));
+        $persistentEvent->setTimezone(Tinebase_Core::getUserTimezone());
 
         $filter = array(array('field' => 'query', 'operator' => 'contains', 'value' => 'l'));
         $paging = array('sort' => 'name', 'dir' => 'ASC', 'start' => 0, 'limit' => 50);
 
-        $result = $this->_uit->searchAttendee($filter, $paging, $period, array());
+        $result = $this->_uit->searchAttenders($filter, $paging, $persistentEvent->toArray(), array());
         $this->assertTrue(
             isset($result[Calendar_Model_Attender::USERTYPE_USER]) &&
             count($result[Calendar_Model_Attender::USERTYPE_USER]) === 3 &&
@@ -2045,7 +2029,7 @@ class Calendar_JsonTests extends Calendar_TestCase
             count($result['freeBusyInfo']) === 2, print_r($result, true));
 
         $filter[] = array('field' => 'type', 'value' => array(Calendar_Model_Attender::USERTYPE_RESOURCE));
-        $result = $this->_uit->searchAttendee($filter, $paging, $period, array());
+        $result = $this->_uit->searchAttenders($filter, $paging, $persistentEvent->toArray(), array());
         $this->assertTrue(
             !isset($result[Calendar_Model_Attender::USERTYPE_USER]) &&
             !isset($result[Calendar_Model_Attender::USERTYPE_GROUP]) &&
index b49a64e..6ad49c1 100644 (file)
@@ -576,7 +576,6 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
      * @param Calendar_Model_Event $_event with
      *   attendee to find free timeslot for
      *   dtstart, dtend -> to calculate duration
-     *   originator_tz needs to be set!
      *   rrule optional
      * @param array $_options
      *  'from'         datetime (optional, defaults event->dtstart) from where to start searching
@@ -603,16 +602,15 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
             throw new Tinebase_Exception_UnexpectedValue('attendee needs to be set and contain at least one attendee');
         }
 
-        $_event->setTimezone($_event->originator_tz);
+        if (empty($_event->originator_tz)) {
+            $_event->originator_tz = Tinebase_Core::getUserTimezone();
+        }
 
         $from = isset($_options['from']) ? ($_options['from'] instanceof Tinebase_DateTime ? $_options['from'] :
             new Tinebase_DateTime($_options['from'])) : clone $_event->dtstart;
         $until = isset($_options['until']) ? ($_options['until'] instanceof Tinebase_DateTime ? $_options['until'] :
             new Tinebase_DateTime($_options['until'])) : $_event->dtend->getClone()->addYear(2);
 
-        $from->setTimezone($_event->originator_tz);
-        $until->setTimezone($_event->originator_tz);
-
         $currentFrom = $from->getClone()->setTime(0, 0, 0);
         $currentUntil = $from->getClone()->addDay(6)->setTime(23, 59, 59);
         if ($currentUntil->isLater($until)) {
@@ -629,9 +627,9 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
                     continue;
                 }
                 $constraint['uid'] = Tinebase_Record_Abstract::generateUID();
-                $event = new Calendar_Model_Event($constraint, true);
+                $event = new Calendar_Model_Event(array(), true);
+                $event->setFromJsonInUsersTimezone($constraint);
                 $event->originator_tz = $_event->originator_tz;
-                $event->setTimezone($_event->originator_tz);
                 $constraints->addRecord($event);
             }
         }
@@ -748,7 +746,7 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
             'uid'           => Tinebase_Record_Abstract::generateUID(),
             'dtstart'       => new Tinebase_DateTime($_startSec),
             'dtend'         => new Tinebase_DateTime($_startSec + $_durationSec),
-            'originator_tz' => $_event->originator_tz
+            'originator_tz' => $_event->originator_tz,
         ), true);
         $result = new Tinebase_Record_RecordSet('Calendar_Model_Event', array($event));
 
index 51b9817..6c4c9e0 100644 (file)
@@ -275,7 +275,6 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
      * @param array $_event
      *   attendee to find free timeslot for
      *   dtstart, dtend -> to calculate duration
-     *   originator_tz needs to be set!
      *   rrule optional
      * @param array $_options
      *  'from'         datetime (optional, defaults event->dtstart) from where to start searching
@@ -289,7 +288,10 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
      */
     public function searchFreeTime($_event, $_options)
     {
-        $records = Calendar_Controller_Event::getInstance()->searchFreeTime(new Calendar_Model_Event($_event, true), $_options);
+        $eventRecord = new Calendar_Model_Event(array(), TRUE);
+        $eventRecord->setFromJsonInUsersTimezone($_event);
+
+        $records = Calendar_Controller_Event::getInstance()->searchFreeTime($eventRecord, $_options);
 
         $records->attendee = array();
         $result = $this->_multipleRecordsToJson($records, null, null);
@@ -493,38 +495,45 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
     }
 
     /**
-     * @param array $_attendee
-     * @param array $_periods
-     * @param array $_ignoreUIDs
+     * @param array $attendee
+     * @param array $event
+     * @param array $ignoreUIDs
      * @return array
      */
-    public function getFreeBusyInfo($_attendee, $_periods, $_ignoreUIDs = array())
+    public function getFreeBusyInfo($attendee, $event, $ignoreUIDs = array())
     {
-        $attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', $_attendee);
-        $periods = new Calendar_Model_EventFilter($_periods);
+        $attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', $attendee);
+        $calendarController = Calendar_Controller_Event::getInstance();
+        $eventRecord = new Calendar_Model_Event(array(), TRUE);
+        $eventRecord->setFromJsonInUsersTimezone($event);
+
+        $periods = $calendarController->getBlockingPeriods($eventRecord, array(
+            'from'  => $eventRecord->dtstart,
+            'until' => $eventRecord->dtstart->getClone()->addMonth(2)
+        ));
 
-        $fbInfo = Calendar_Controller_Event::getInstance()->getFreeBusyInfo($periods, $attendee, $_ignoreUIDs);
+        $fbInfo = $calendarController->getFreeBusyInfo($periods, $attendee, $ignoreUIDs);
 
         return $fbInfo->toArray();
     }
 
     /**
-     * @param array $_filter
-     * @param array $_paging
-     * @param array $_periods
-     * @param array $_ignoreUIDs
+     * @param array $filter
+     * @param array $paging
+     * @param array $event
+     * @param array $ignoreUIDs
      * @return array
      */
-    public function searchAttendee($_filter, $_paging, $_periods, $_ignoreUIDs)
+    public function searchAttenders($filter, $paging, $event, $ignoreUIDs)
     {
         $filters = array();
-        foreach($_filter as $filter) {
-            switch($filter['field']) {
+        foreach($filter as $f) {
+            switch($f['field']) {
                 case 'query':
-                    $filters['query'] = $filter;
+                    $filters['query'] = $f;
                     break;
                 default:
-                    $filters[$filter['field']] = $filter['value'];
+                    $filters[$f['field']] = $f['value'];
                     break;
             }
         }
@@ -540,7 +549,7 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
             if (isset($filters['userFilter'])) {
                 $contactFilter[] = $filters['userFilter'];
             }
-            $contactPaging = $_paging;
+            $contactPaging = $paging;
             $contactPaging['sort'] = 'type';
             $result[Calendar_Model_Attender::USERTYPE_USER] = $addressBookFE->searchContacts($contactFilter, $contactPaging);
         }
@@ -554,7 +563,7 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
                 $groupFilter[] = $filters['groupFilter'];
             }
             $groupFilter[] = array('field' => 'type', 'operator' => 'contains', 'value' => 'group');
-            $result[Calendar_Model_Attender::USERTYPE_GROUP] = $addressBookFE->searchLists($groupFilter, $_paging);
+            $result[Calendar_Model_Attender::USERTYPE_GROUP] = $addressBookFE->searchLists($groupFilter, $paging);
         }
 
         if (!isset($filters['type']) || in_array(Calendar_Model_Attender::USERTYPE_RESOURCE, $filters['type'])) {
@@ -562,20 +571,24 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
             if (isset($filters['resourceFilter'])) {
                 $resourceFilter[] = $filters['resourceFilter'];
             }
-            $result[Calendar_Model_Attender::USERTYPE_RESOURCE] = $this->searchResources($resourceFilter, $_paging);
+            $result[Calendar_Model_Attender::USERTYPE_RESOURCE] = $this->searchResources($resourceFilter, $paging);
         }
 
-        $attendee = array();
-        foreach ($result as $type => $res) {
-            foreach ($res['results'] as $r) {
-                $attendee[] = array(
-                    'user_id'   => $r['id'],
-                    'user_type' => $type
-                );
+        if (empty($event)) {
+            $result['freeBusyInfo'] = array();
+        } else {
+            $attendee = array();
+            foreach ($result as $type => $res) {
+                foreach ($res['results'] as $r) {
+                    $attendee[] = array(
+                        'user_id' => $r['id'],
+                        'user_type' => $type
+                    );
+                }
             }
-        }
 
-        $result['freeBusyInfo'] = $this->getFreeBusyInfo($attendee, $_periods, $_ignoreUIDs);
+            $result['freeBusyInfo'] = $this->getFreeBusyInfo($attendee, $event, $ignoreUIDs);
+        }
 
         return $result;
     }