/**
  * update to 8.1
  * - move ack & snooze time from attendee to alarm
  */
 public function update_0()
 {
     // find all events with ack or snooze times set
     $eventIds = $this->_db->query("SELECT DISTINCT " . $this->_db->quoteIdentifier('cal_event_id') . " FROM " . $this->_db->quoteIdentifier(SQL_TABLE_PREFIX . "cal_attendee") . " WHERE " . $this->_db->quoteIdentifier("alarm_ack_time") . " IS NOT NULL OR " . $this->_db->quoteIdentifier("alarm_snooze_time") . " IS NOT NULL")->fetchAll(Zend_Db::FETCH_ASSOC);
     $attendeeBE = new Calendar_Backend_Sql_Attendee();
     $alarmBE = Tinebase_Alarm::getInstance();
     foreach ($eventIds as $eventId) {
         $eventId = $eventId['cal_event_id'];
         $attendeeFilter = new Tinebase_Model_Filter_FilterGroup();
         $attendeeFilter->addFilter(new Tinebase_Model_Filter_Text('cal_event_id', 'equals', $eventId));
         $attendees = $attendeeBE->search($attendeeFilter);
         $alarms = $alarmBE->search(new Tinebase_Model_AlarmFilter(array(array('field' => 'model', 'operator' => 'equals', 'value' => 'Calendar_Model_Event'), array('field' => 'record_id', 'operator' => 'equals', 'value' => $eventId))));
         foreach ($alarms as $alarm) {
             foreach ($attendees as $attendee) {
                 if ($attendee->alarm_ack_time instanceof Tinebase_DateTime) {
                     $alarm->setOption("acknowledged-{$attendee->user_id}", $attendee->alarm_ack_time->format(Tinebase_Record_Abstract::ISO8601LONG));
                 }
                 if ($attendee->alarm_snooze_time instanceof Tinebase_DateTime) {
                     $alarm->setOption("snoozed-{$attendee->user_id}", $attendee->alarm_snooze_time->format(Tinebase_Record_Abstract::ISO8601LONG));
                 }
             }
             $alarmBE->update($alarm);
         }
     }
     // delte ack & snooze from attendee
     $this->_backend->dropCol('cal_attendee', 'alarm_ack_time');
     $this->_backend->dropCol('cal_attendee', 'alarm_snooze_time');
     $this->setTableVersion('cal_attendee', 5);
     $this->setApplicationVersion('Calendar', '8.1');
 }
 /**
  * repair dangling attendee records (no displaycontainer_id)
  *
  * @see https://forge.tine20.org/mantisbt/view.php?id=8172
  */
 public function repairDanglingDisplaycontainerEvents()
 {
     $filter = new Tinebase_Model_Filter_FilterGroup();
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'user_type', 'operator' => 'in', 'value' => array(Calendar_Model_Attender::USERTYPE_USER, Calendar_Model_Attender::USERTYPE_GROUPMEMBER, Calendar_Model_Attender::USERTYPE_RESOURCE))));
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'displaycontainer_id', 'operator' => 'isnull', 'value' => null)));
     $danglingAttendee = $this->_attendeeBackend->search($filter);
     $danglingContactAttendee = $danglingAttendee->filter('user_type', '/' . Calendar_Model_Attender::USERTYPE_USER . '|' . Calendar_Model_Attender::USERTYPE_GROUPMEMBER . '/', TRUE);
     $danglingContactIds = array_unique($danglingContactAttendee->user_id);
     $danglingContacts = Addressbook_Controller_Contact::getInstance()->getMultiple($danglingContactIds, TRUE);
     $danglingResourceAttendee = $danglingAttendee->filter('user_type', Calendar_Model_Attender::USERTYPE_RESOURCE);
     $danglingResourceIds = array_unique($danglingResourceAttendee->user_id);
     Calendar_Controller_Resource::getInstance()->doContainerACLChecks(false);
     $danglingResources = Calendar_Controller_Resource::getInstance()->getMultiple($danglingResourceIds, TRUE);
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Processing ' . count($danglingContactIds) . ' dangling contact ids...');
     }
     foreach ($danglingContactIds as $danglingContactId) {
         $danglingContact = $danglingContacts->getById($danglingContactId);
         if ($danglingContact && $danglingContact->account_id) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Get default display container for account ' . $danglingContact->account_id);
             }
             if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
                 Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($danglingContact->toArray(), true));
             }
             $displayCalId = Calendar_Controller_Event::getDefaultDisplayContainerId($danglingContact->account_id);
             if ($displayCalId) {
                 // finaly repair attendee records
                 $attendeeRecords = $danglingContactAttendee->filter('user_id', $danglingContactId);
                 $this->_attendeeBackend->updateMultiple($attendeeRecords->getId(), array('displaycontainer_id' => $displayCalId));
                 Tinebase_Core::getLogger()->NOTICE(__METHOD__ . '::' . __LINE__ . " repaired the following contact attendee " . print_r($attendeeRecords->toArray(), TRUE));
             }
         }
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Processing ' . count($danglingResourceIds) . ' dangling resource ids...');
     }
     foreach ($danglingResourceIds as $danglingResourceId) {
         $resource = $danglingResources->getById($danglingResourceId);
         if ($resource && $resource->container_id) {
             $displayCalId = $resource->container_id;
             $attendeeRecords = $danglingResourceAttendee->filter('user_id', $danglingResourceId);
             $this->_attendeeBackend->updateMultiple($attendeeRecords->getId(), array('displaycontainer_id' => $displayCalId));
             Tinebase_Core::getLogger()->NOTICE(__METHOD__ . '::' . __LINE__ . " repaired the following resource attendee " . print_r($attendeeRecords->toArray(), TRUE));
         }
     }
 }