0010464: cli script for repairing attendee
authorPhilipp Schüle <p.schuele@metaways.de>
Thu, 13 Nov 2014 20:52:16 +0000 (21:52 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 14 Nov 2014 10:17:02 +0000 (11:17 +0100)
* adds cli/controller functions and test
* adds isRecurInstance helper to event model

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

Change-Id: I0a2a17fe8673d88162bab63893ceb76fc6fd1787
Reviewed-on: http://gerrit.tine20.com/customers/1356
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/Controller/EventTests.php
tine20/Calendar/Controller/Event.php
tine20/Calendar/Frontend/Cli.php
tine20/Calendar/Model/Event.php

index 4f73f80..e88d4f2 100644 (file)
@@ -1445,4 +1445,20 @@ class Calendar_Controller_EventTests extends Calendar_TestCase
         $event6->dtend->addDay(8);
         $this->_controller->create($event6);
     }
+    
+    public function testRepairAttendee()
+    {
+        $event = $this->_getEvent(true);
+        $event->attendee = null;
+        $persistentEvent = $this->_controller->create($event);
+        
+        $result = $this->_controller->repairAttendee($persistentEvent->container_id, Tinebase_DateTime::now()->subDay(1), Tinebase_DateTime::now());
+        
+        $this->assertEquals(1, $result, 'should repair 1 event');
+        
+        $repairedEvent = $this->_controller->get($persistentEvent->getId());
+        $this->assertEquals(1, count($repairedEvent->attendee));
+        $ownAttender = Calendar_Model_Attender::getOwnAttender($repairedEvent->attendee);
+        $this->assertTrue($ownAttender !== null);
+    }
 }
index 17f7266..1d6a2c5 100644 (file)
@@ -2269,12 +2269,12 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
                 $summaryMatch = $cal2Events->filter('summary', $event->summary);
                 if (count($summaryMatch) > 0) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
-                        . " Found " . count($summaryMatch) . ' events with matching summaries: ' . print_r($summaryMatch->toArray(), true));
+                        . " Found " . count($summaryMatch) . ' events with matching summaries');
                     
                     $dtStartMatch = $cal2Events->filter('dtstart', $event->dtstart);
                     if (count($dtStartMatch) > 0) {
                         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
-                            . " Found " . count($summaryMatch) . ' events with matching dtstarts and summaries: ' . print_r($dtStartMatch->toArray(), true));
+                            . " Found " . count($summaryMatch) . ' events with matching dtstarts and summaries');
                         
                         $matchingEvents->merge($dtStartMatch);
                         // remove from cal1+cal2
@@ -2326,4 +2326,74 @@ class Calendar_Controller_Event extends Tinebase_Controller_Record_Abstract impl
         Calendar_Model_Rrule::mergeAndRemoveNonMatchingRecurrences($events, $filter);
         return $events;
     }
+    
+    /**
+     * add calendar owner as attendee if not already set
+     * 
+     * @param string $calendarId
+     * @param Tinebase_DateTime $from
+     * @param Tinebase_DateTime $until
+     * @param boolean $dry run
+     * 
+     * @return number of updated events
+     */
+    public function repairAttendee($calendarId, $from, $until, $dry = false)
+    {
+        $container = Tinebase_Container::getInstance()->getContainerById($calendarId);
+        if ($container->type !== Tinebase_Model_Container::TYPE_PERSONAL) {
+            throw new Calendar_Exception('Only allowed for personal containers!');
+        }
+        if ($container->owner_id !== Tinebase_Core::getUser()->getId()) {
+            throw new Calendar_Exception('Only allowed for own containers!');
+        }
+        
+        $updateCount = 0;
+        while ($from->isEarlier($until)) {
+            $endWeek = $from->getClone()->addWeek(1);
+            
+            if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
+                    . ' Repairing period ' . $from . ' - ' . $endWeek);
+            
+            
+            // TODO we need to detect events with DECLINED/DELETED attendee
+            $events = $this->_getEventsForPeriodAndCalendar($calendarId, $from, $endWeek);
+            
+            $from->addWeek(1);
+            
+            if (count($events) == 0) {
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                        . ' No events found');
+                continue;
+            }
+            
+            foreach ($events as $event) {
+                // add attendee if not already set
+                if ($event->isRecurInstance()) {
+                    // TODO get base event
+                    if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                            . ' Skip recur instance ' . $event->toShortString());
+                    continue;
+                }
+                
+                $ownAttender = Calendar_Model_Attender::getOwnAttender($event->attendee);
+                if (! $ownAttender) {
+                    if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                            . ' Add missing attender to event ' . $event->toShortString());
+                    
+                    $attender = new Calendar_Model_Attender(array(
+                        'user_type' => Calendar_Model_Attender::USERTYPE_USER,
+                        'user_id'   => Tinebase_Core::getUser()->contact_id,
+                        'status'    => Calendar_Model_Attender::STATUS_ACCEPTED
+                    ));
+                    $event->attendee->addRecord($attender);
+                    if (! $dry) {
+                        $this->update($event);
+                    }
+                    $updateCount++;
+                }
+            }
+        }
+        
+        return $updateCount;
+    }
 }
index 6bfb92e..aad1242 100644 (file)
@@ -381,4 +381,20 @@ class Calendar_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
         }
         echo "-----------------------------\n";
     }
+    
+    public function repairAttendee(Zend_Console_Getopt $_opts)
+    {
+        $args = $this->_parseArgs($_opts, array('cal'));
+        $from = isset($args['from']) ? new Tinebase_DateTime($args['from']) : Tinebase_DateTime::now();
+        $until = isset($args['until']) ? new Tinebase_DateTime($args['until']) : Tinebase_DateTime::now()->addYear(1);
+        $dry = $_opts->d;
+        
+        echo "Repairing Calendar with IDs " . $args['cal'] .  "(" . $from . " - " . $until . ")...\n";
+        if ($dry) {
+            echo "-- DRY RUN --\n";
+        }
+        
+        $result = Calendar_Controller_Event::getInstance()->repairAttendee($args['cal'], $from, $until, $dry);
+        echo "Repaired " . $result . " events\n";
+    }
 }
index a841801..a938810 100644 (file)
@@ -450,6 +450,16 @@ class Calendar_Model_Event extends Tinebase_Record_Abstract
     {
         return !!$this->recurid;
     }
+
+    /**
+     * event is non persistent recur instance
+     * 
+     * @return boolean
+     */
+    public function isRecurInstance()
+    {
+        return (boolean) preg_match('/^fakeid/', $this->getId());
+    }
     
     /**
      * sets recurId of this model
@@ -683,4 +693,9 @@ class Calendar_Model_Event extends Tinebase_Record_Abstract
         
         return $organizer instanceof Addressbook_Model_Contact && ! $organizer->account_id;
     }
+    
+    public function toShortString()
+    {
+        return $this->summary . '(' . $this->dtstart . ' - ' . $this->dtend . ')';
+    }
 }