public function testRemoveRecord() { $idLess1 = $this->object->find('string', 'idLess1'); $countBefore = $this->object->count(); $this->object->removeRecord($idLess1); $this->assertEquals($countBefore - 1, $this->object->count(), 'recordSet count should decrease by 1'); foreach ($this->object as $record) { $this->assertFalse($record === $idLess1, 'idLess1 is still in set'); } }
/** * get grants for records * * @param Tinebase_Record_RecordSet $records */ public function getGrantsForRecords(Tinebase_Record_RecordSet $records) { $recordIds = $records->getArrayOfIds(); if (empty($recordIds)) { return; } $select = $this->_getAclSelectByRecordIds($recordIds)->group(array('record_id', 'account_type', 'account_id')); Tinebase_Backend_Sql_Abstract::traitGroup($select); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $select); } $stmt = $this->_db->query($select); $grantsData = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' grantsData: ' . print_r($grantsData, true)); } foreach ($grantsData as $grantData) { $givenGrants = explode(',', $grantData['account_grants']); foreach ($givenGrants as $grant) { $grantData[$grant] = TRUE; } $recordGrant = new $this->_modelName($grantData, true); unset($recordGrant->account_grant); $record = $records->getById($recordGrant->record_id); $records->removeRecord($record); if (!$record->grants instanceof Tinebase_Record_RecordSet) { $record->grants = new Tinebase_Record_RecordSet($this->_modelName); } $record->grants->addRecord($recordGrant); // NOTICE: this is strange - we have to remove the record and add it // again to make sure that grants are updated ... // maybe we should add a "replace" method? $records->addRecord($record); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Records with grants: ' . print_r($records->toArray(), true)); } }
/** * merges Recurrences of given events into the given event set * * @param Tinebase_Record_RecordSet $_events * @param Tinebase_DateTime $_from * @param Tinebase_DateTime $_until * @return void */ public static function mergeRecurrenceSet($_events, $_from, $_until) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . " from: {$_from} until: {$_until}"); } //compute recurset $candidates = $_events->filter('rrule', "/^FREQ.*/", TRUE); foreach ($candidates as $candidate) { try { $exceptions = $_events->filter('recurid', "/^{$candidate->uid}-.*/", TRUE); $recurSet = Calendar_Model_Rrule::computeRecurrenceSet($candidate, $exceptions, $_from, $_until); foreach ($recurSet as $event) { $_events->addRecord($event); } // check if candidate/baseEvent has an exception itself -> in this case remove baseEvent from set if (is_array($candidate->exdate) && in_array($candidate->dtstart, $candidate->exdate)) { $_events->removeRecord($candidate); } } catch (Exception $e) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " Could not compute recurSet of event: {$candidate->getId()}"); } Tinebase_Exception::log($e); continue; } } }
/** * add default preferences to and remove undefined preferences from record set * * @param Tinebase_Record_RecordSet $_prefs * @param Tinebase_Model_Filter_FilterGroup $_filter */ protected function _addDefaultAndRemoveUndefinedPrefs(Tinebase_Record_RecordSet $_prefs, Tinebase_Model_Filter_FilterGroup $_filter) { $allAppPrefs = $this->getAllApplicationPreferences(); // add default prefs if not already in array (only if no name or type filters are set) if (!$_filter->isFilterSet('name') && !$_filter->isFilterSet('type')) { $missingDefaultPrefs = array_diff($allAppPrefs, $_prefs->name); foreach ($missingDefaultPrefs as $prefName) { $_prefs->addRecord($this->getApplicationPreferenceDefaults($prefName)); } } // remove all prefs that are not defined $undefinedPrefs = array_diff($_prefs->name, $allAppPrefs); if (count($undefinedPrefs) > 0) { $_prefs->addIndices(array('name')); foreach ($undefinedPrefs as $undefinedPrefName) { $record = $_prefs->find('name', $undefinedPrefName); $_prefs->removeRecord($record); if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Removed undefined preference from result: ' . $undefinedPrefName); } } } }
/** * resolves group members and adds/removes them if nesesary * * NOTE: If a user is listed as user and as groupmember, we supress the groupmember * * NOTE: The role to assign to a new group member is not always clear, as multiple groups * might be the 'source' of the group member. To deal with this, we take the role of * the first group when we add new group members * * @param Tinebase_Record_RecordSet $_attendee * @return void */ public static function resolveGroupMembers($_attendee) { if (!$_attendee instanceof Tinebase_Record_RecordSet) { return; } $groupAttendee = $_attendee->filter('user_type', Calendar_Model_Attender::USERTYPE_GROUP); $allCurrGroupMembers = $_attendee->filter('user_type', Calendar_Model_Attender::USERTYPE_GROUPMEMBER); $allCurrGroupMembersContactIds = $allCurrGroupMembers->user_id; $allGroupMembersContactIds = array(); foreach ($groupAttendee as $groupAttender) { #$groupAttenderMemberIds = Tinebase_Group::getInstance()->getGroupMembers($groupAttender->user_id); #$groupAttenderContactIds = Tinebase_User::getInstance()->getMultiple($groupAttenderMemberIds)->contact_id; #$allGroupMembersContactIds = array_merge($allGroupMembersContactIds, $groupAttenderContactIds); $listId = null; if ($groupAttender->user_id instanceof Addressbook_Model_List) { $listId = $groupAttender->user_id->getId(); } else { if ($groupAttender->user_id !== NULL) { $group = Tinebase_Group::getInstance()->getGroupById($groupAttender->user_id); if (!empty($group->list_id)) { $listId = $group->list_id; } } else { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Group attender ID missing'); } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($groupAttender->toArray(), TRUE)); } } } if ($listId !== null) { $groupAttenderContactIds = Addressbook_Controller_List::getInstance()->get($listId)->members; $allGroupMembersContactIds = array_merge($allGroupMembersContactIds, $groupAttenderContactIds); $toAdd = array_diff($groupAttenderContactIds, $allCurrGroupMembersContactIds); foreach ($toAdd as $userId) { $_attendee->addRecord(new Calendar_Model_Attender(array('user_type' => Calendar_Model_Attender::USERTYPE_GROUPMEMBER, 'user_id' => $userId, 'role' => $groupAttender->role))); } } } $toDel = array_diff($allCurrGroupMembersContactIds, $allGroupMembersContactIds); foreach ($toDel as $idx => $contactId) { $attender = $allCurrGroupMembers->find('user_id', $contactId); $_attendee->removeRecord($attender); } // calculate double members (groupmember + user) $groupmembers = $_attendee->filter('user_type', Calendar_Model_Attender::USERTYPE_GROUPMEMBER); $users = $_attendee->filter('user_type', Calendar_Model_Attender::USERTYPE_USER); $doublicates = array_intersect($users->user_id, $groupmembers->user_id); foreach ($doublicates as $user_id) { $attender = $groupmembers->find('user_id', $user_id); $_attendee->removeRecord($attender); } }
/** * get grants assigned to given account of multiple records * * @param Tinebase_Record_RecordSet $_records records to get the grants for * @param string|Tinebase_Model_User $_accountId the account to get the grants for * @param string $_containerProperty container property * @param string $_grantModel * @throws Tinebase_Exception_NotFound */ public function getGrantsOfRecords(Tinebase_Record_RecordSet $_records, $_accountId, $_containerProperty = 'container_id', $_grantModel = 'Tinebase_Model_Grants') { // get container ids $containers = array(); foreach ($_records as $record) { if (isset($record[$_containerProperty]) && !isset($containers[Tinebase_Model_Container::convertContainerIdToInt($record[$_containerProperty])])) { $containers[Tinebase_Model_Container::convertContainerIdToInt($record[$_containerProperty])] = array(); } } if (empty($containers)) { return; } $accountId = Tinebase_Model_User::convertUserIdToInt($_accountId); $select = $this->_getSelect(array('container.id', 'container.name'), TRUE)->where("{$this->_db->quoteIdentifier('container.id')} IN (?)", array_keys($containers))->join(array('container_acl' => SQL_TABLE_PREFIX . 'container_acl'), "{$this->_db->quoteIdentifier('container_acl.container_id')} = {$this->_db->quoteIdentifier('container.id')}", array('container_id', 'account_grants' => Tinebase_Backend_Sql_Command::getAggregateFunction($this->_db, $this->_db->quoteIdentifier('container_acl.account_grant'))))->group(array('container.id', 'container.name', 'container_acl.account_type', 'container_acl.container_id')); $this->addGrantsSql($select, $accountId, '*'); $stmt = $this->_db->query($select); $arr = $stmt->fetchAll(Zend_Db::FETCH_ASSOC); // check array for duplicate entries of container_id $rows = array(); $last_arr = array(); foreach ($arr as $row) { if ($last_arr['container_id'] === $row['container_id']) { $row['account_grants'] = $last_arr['account_grants'] . ',' . $row['account_grants']; end($rows); $rows[key($rows)] = $row; } else { array_push($rows, $row); } $last_arr = $row; } // add results to container ids and get grants array foreach ($rows as $row) { // NOTE id is non-ambiguous $row['id'] = $row['container_id']; $grantsArray = array_unique(explode(',', $row['account_grants'])); $row['account_grants'] = $this->_getGrantsFromArray($grantsArray, $accountId, $_grantModel)->toArray(); $containers[$row['id']] = new Tinebase_Model_Container($row, TRUE); } // add container & grants to records foreach ($_records as &$record) { try { if (!isset($record->{$_containerProperty})) { continue; } $containerId = $record[$_containerProperty]; if (!is_array($containerId) && !$containerId instanceof Tinebase_Record_Abstract && !empty($containers[$containerId])) { $record[$_containerProperty] = $containers[$containerId]; $record[$_containerProperty]['path'] = $containers[$containerId]->getPath(); } } catch (Exception $e) { // if path is not determinable, skip this container $_records->removeRecord($record); } } }
/** * cleanup search results (freebusy) * * @param Tinebase_Record_RecordSet $_events * @param string $_action */ protected function _freeBusyCleanup(Tinebase_Record_RecordSet $_events, $_action) { foreach ($_events as $event) { $doFreeBusyCleanup = $event->doFreeBusyCleanup(); if ($doFreeBusyCleanup && $_action !== 'get') { $_events->removeRecord($event); } } }