/**
  * repair missing displaycontainer_id
  */
 public function update_7()
 {
     $allUser = $this->_db->query("SELECT " . $this->_db->quoteIdentifier('id') . "," . $this->_db->quoteIdentifier('contact_id') . " FROM " . $this->_db->quoteIdentifier(SQL_TABLE_PREFIX . "accounts") . " WHERE " . $this->_db->quoteIdentifier("contact_id") . " IS NOT NULL")->fetchAll(Zend_Db::FETCH_ASSOC);
     $contactUserMap = array();
     foreach ($allUser as $id => $user) {
         $contactUserMap[$user['contact_id']] = $user['id'];
     }
     // find all user/groupmember attendees with missing displaycontainer
     $attendees = $this->_db->query("SELECT DISTINCT" . $this->_db->quoteIdentifier('user_type') . "," . $this->_db->quoteIdentifier('user_id') . " FROM " . $this->_db->quoteIdentifier(SQL_TABLE_PREFIX . "cal_attendee") . " WHERE " . $this->_db->quoteIdentifier("displaycontainer_id") . " IS  NULL" . "  AND " . $this->_db->quoteIdentifier("user_type") . $this->_db->quoteInto(" IN (?)", array('user', 'groupmemeber')) . "  AND " . $this->_db->quoteIdentifier("user_id") . $this->_db->quoteInto(" IN (?)", array_keys($contactUserMap)))->fetchAll(Zend_Db::FETCH_ASSOC);
     // find all user/groupmember attendees with missing displaycontainer
     $attendees = array_merge($attendees, $this->_db->query("SELECT DISTINCT" . $this->_db->quoteIdentifier('user_type') . "," . $this->_db->quoteIdentifier('user_id') . " FROM " . $this->_db->quoteIdentifier(SQL_TABLE_PREFIX . "cal_attendee") . " WHERE " . $this->_db->quoteIdentifier("displaycontainer_id") . " IS  NULL" . "  AND " . $this->_db->quoteIdentifier("user_type") . $this->_db->quoteInto(" IN (?)", array('resource')))->fetchAll(Zend_Db::FETCH_ASSOC));
     $resources = $this->_db->query("SELECT " . $this->_db->quoteIdentifier('id') . "," . $this->_db->quoteIdentifier('container_id') . " FROM " . $this->_db->quoteIdentifier(SQL_TABLE_PREFIX . "cal_resources"))->fetchAll(Zend_Db::FETCH_ASSOC);
     $resourceContainerMap = array();
     foreach ($resources as $resource) {
         $resourceContainerMap[$resource['id']] = $resource['container_id'];
     }
     foreach ($attendees as $attendee) {
         //find out displaycontainer
         if ($attendee['user_type'] != 'resource') {
             $userAccountId = $contactUserMap[$attendee['user_id']];
             try {
                 $attendee['displaycontainerId'] = Calendar_Controller_Event::getDefaultDisplayContainerId($userAccountId);
             } catch (Tinebase_Exception_NotFound $tenf) {
                 Setup_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . " Could not find user with id " . $attendee['user_id']);
                 continue;
             }
         } else {
             $attendee['displaycontainerId'] = $resourceContainerMap[$attendee['user_id']];
         }
         // update displaycontainer
         $this->_db->query("UPDATE" . $this->_db->quoteIdentifier(SQL_TABLE_PREFIX . "cal_attendee") . " SET " . $this->_db->quoteIdentifier("displaycontainer_id") . " = " . $this->_db->quote($attendee['displaycontainerId']) . " WHERE " . $this->_db->quoteIdentifier("user_type") . " = " . $this->_db->quote($attendee['user_type']) . "  AND " . $this->_db->quoteIdentifier("user_id") . " = " . $this->_db->quote($attendee['user_id']));
     }
     $this->setApplicationVersion('Calendar', '8.8');
 }
 /**
  * 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));
         }
     }
 }