/**
  * resolves an array with usernames to an array of user ids
  * 
  * @param array $users
  */
 protected function _resolveUsers($users)
 {
     if (!$this->_userRecords) {
         $this->_userRecords = new Tinebase_Record_RecordSet('Tinebase_Model_User');
     }
     $resolved = array();
     if (is_array($users) && !empty($users)) {
         foreach ($users as $userName) {
             $user = $this->_userRecords->filter('name', $userName)->getFirstRecord();
             if (!$user) {
                 try {
                     $user = Tinebase_User::getInstance()->getUserByLoginName($userName);
                 } catch (Tinebase_Exception_NotFound $tenf) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Skipping user ' . $userName);
                     }
                     Tinebase_Exception::log($tenf);
                     continue;
                 }
                 $this->_userRecords->addRecord($user);
             }
             $resolved[] = $user->getId();
         }
     }
     return $resolved;
 }
 /**
  * get special field value
  *
  * @param Tinebase_Record_Interface $_record
  * @param array $_param
  * @param string $_key
  * @param string $_cellType
  * @return string
  */
 protected function _getSpecialFieldValue(Tinebase_Record_Interface $_record, $_param, $_key = NULL, &$_cellType = NULL)
 {
     $supplierId = $_record->getId();
     if (!isset($this->_supplierAddresses[$supplierId])) {
         $all = $this->_addresses->filter('customer_id', $supplierId);
         $this->_addresses->removeRecords($all);
         $this->_supplierAddresses[$supplierId] = array('postal' => $all->filter('type', 'postal')->getFirstRecord(), 'billing' => array('records' => $all->filter('type', 'billing'), 'index' => 0), 'delivery' => array('records' => $all->filter('type', 'delivery'), 'index' => 0));
     }
     switch ($_param['type']) {
         case 'postal':
             $address = $this->_supplierAddresses[$supplierId]['postal'];
             break;
         default:
             if (isset($this->_supplierAddresses[$supplierId][$_param['type']]['records'])) {
                 $address = $this->_supplierAddresses[$supplierId][$_param['type']]['records']->getByIndex($this->_supplierAddresses[$supplierId][$_param['type']]['index']);
                 $this->_supplierAddresses[$supplierId][$_param['type']]['index']++;
             }
     }
     return $address ? $this->_renderAddress($address, $_param['type']) : '';
 }
 /**
  * 
  * @param array $recordData
  * @return Tinebase_Record_RecordSet
  */
 protected function _createTimesheets($recordData = NULL)
 {
     $this->_timesheetController = Timetracker_Controller_Timesheet::getInstance();
     if (!$this->_timesheetRecords) {
         $this->_timesheetRecords = new Tinebase_Record_RecordSet('Timetracker_Model_Timesheet');
     }
     if (!$recordData) {
         if (!$this->_timeaccountRecords) {
             $this->_createTimeaccounts();
         }
         $tsDate = clone $this->_referenceDate;
         $tsDate->addMonth(4)->addDay(5);
         // this is a ts on 20xx-05-06
         $timesheet = new Timetracker_Model_Timesheet(array('account_id' => Tinebase_Core::getUser()->getId(), 'timeaccount_id' => $this->_timeaccountRecords->filter('title', 'TA-for-Customer3')->getFirstRecord()->getId(), 'start_date' => $tsDate, 'duration' => 105, 'description' => 'ts from ' . (string) $tsDate));
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         // this is a ts on 20xx-05-07
         $timesheet->id = NULL;
         $timesheet->start_date = $tsDate->addDay(1);
         $timesheet->description = 'ts from ' . (string) $tsDate;
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         // this is a ts on 20xx-09-07
         $timesheet->id = NULL;
         $timesheet->start_date = $tsDate->addMonth(4);
         $timesheet->description = 'ts from ' . (string) $tsDate;
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         // this is a ts on 20xx-09-08
         $timesheet->id = NULL;
         $timesheet->start_date = $tsDate->addDay(1);
         $timesheet->description = 'ts from ' . (string) $tsDate;
         $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
     } else {
         foreach ($recordData as $tsData) {
             $timesheet = new Timetracker_Model_Timesheet($tsData);
             $this->_timesheetRecords->addRecord($this->_timesheetController->create($timesheet));
         }
     }
     return $this->_timesheetRecords;
 }
 /**
  * sort folder record set
  * - begin with INBOX + other standard/system folders, add other folders
  *
  * @param Tinebase_Record_RecordSet $_folders
  * @param string $_parentFolder
  * @return Tinebase_Record_RecordSet
  */
 protected function _sortFolders(Tinebase_Record_RecordSet $_folders, $_parentFolder)
 {
     $sortedFolders = new Tinebase_Record_RecordSet('Felamimail_Model_Folder');
     $_folders->sort('localname', 'ASC', 'natcasesort');
     $_folders->addIndices(array('globalname'));
     Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Sorting subfolders of "' . $_parentFolder . '".');
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_folders->globalname, TRUE));
     }
     foreach ($this->_systemFolders as $systemFolderName) {
         $folders = $_folders->filter('globalname', '@^' . $systemFolderName . '$@i', TRUE);
         //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $systemFolderName . ' => ' . print_r($folders->toArray(), TRUE));
         if (count($folders) > 0) {
             $sortedFolders->addRecord($folders->getFirstRecord());
         }
     }
     foreach ($_folders as $folder) {
         if (!in_array(strtolower($folder->globalname), $this->_systemFolders)) {
             $sortedFolders->addRecord($folder);
         }
     }
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($sortedFolders->globalname, TRUE));
     return $sortedFolders;
 }
 /**
  * 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;
         }
     }
 }
Exemple #6
0
 /**
  * get default preference (from recordset, db or app defaults)
  * 
  * @param string $_preferenceName
  * @param Tinebase_Record_RecordSet $_preferences
  */
 protected function _getDefaultPreference($_preferenceName, $_preferences = NULL)
 {
     if ($_preferences !== NULL) {
         $defaults = $_preferences->filter('type', Tinebase_Model_Preference::TYPE_ADMIN);
     } else {
         $defaults = $this->search(new Tinebase_Model_PreferenceFilter(array(array('field' => 'type', 'operator' => 'equals', 'value' => Tinebase_Model_Preference::TYPE_ADMIN), array('field' => 'name', 'operator' => 'equals', 'value' => $_preferenceName), array('field' => 'account_id', 'operator' => 'equals', 'value' => 0), array('field' => 'application_id', 'operator' => 'equals', 'value' => Tinebase_Application::getInstance()->getApplicationByName($this->_application)->getId()))));
     }
     if (count($defaults) > 0) {
         $defaultPref = $defaults->getFirstRecord();
     } else {
         $defaultPref = $this->getApplicationPreferenceDefaults($_preferenceName);
     }
     return $defaultPref;
 }
 /**
  * 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);
     }
 }
 /**
  * move messages from one folder to another
  * 
  * @param Tinebase_Record_RecordSet $_messages
  * @param string $_folderId
  * @param Expressomail_Model_Folder|string $_targetFolder
  * @return boolean did we move messages?
  */
 protected function _moveMessagesByFolder(Tinebase_Record_RecordSet $_messages, $_folderId, $_targetFolder)
 {
     $messagesInFolder = $_messages->filter('folder_id', $_folderId);
     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Moving messages: ' . print_r($messagesInFolder->getArrayOfIds(), TRUE));
     }
     $result = TRUE;
     if ($_targetFolder === Expressomail_Model_Folder::FOLDER_TRASH) {
         $result = $this->_moveMessagesToTrash($messagesInFolder, $_folderId);
     } else {
         if ($_folderId === $_targetFolder->getId()) {
             // no need to move
             $result = FALSE;
         } else {
             if ($messagesInFolder->getFirstRecord()->account_id == $_targetFolder->account_id) {
                 $this->_moveMessagesInFolderOnSameAccount($messagesInFolder, $_targetFolder);
             } else {
                 $this->_moveMessagesToAnotherAccount($messagesInFolder, $_targetFolder);
             }
         }
     }
     if (!$result) {
         $_messages->removeRecords($messagesInFolder);
     }
     return $result;
 }
 /**
  * returns the employee for the current account
  */
 protected function _getCurrentUsersEmployee()
 {
     return $this->_employees->filter('account_id', Tinebase_Core::getUser()->accountId)->getFirstRecord();
 }
 /**
  * updates installed applications. does nothing if no applications are installed
  *
  * @param Tinebase_Record_RecordSet $_applications
  * @return  array   messages
  */
 public function updateApplications(Tinebase_Record_RecordSet $_applications)
 {
     $this->_updatedApplications = 0;
     $smallestMajorVersion = NULL;
     $biggestMajorVersion = NULL;
     //find smallest major version
     foreach ($_applications as $application) {
         if ($smallestMajorVersion === NULL || $application->getMajorVersion() < $smallestMajorVersion) {
             $smallestMajorVersion = $application->getMajorVersion();
         }
         if ($biggestMajorVersion === NULL || $application->getMajorVersion() > $biggestMajorVersion) {
             $biggestMajorVersion = $application->getMajorVersion();
         }
     }
     $messages = array();
     // update tinebase first (to biggest major version)
     $tinebase = $_applications->filter('name', 'Tinebase')->getFirstRecord();
     if (!empty($tinebase)) {
         unset($_applications[$_applications->getIndexById($tinebase->getId())]);
         list($major, $minor) = explode('.', $this->getSetupXml('Tinebase')->version[0]);
         Setup_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Updating Tinebase to version ' . $major . '.' . $minor);
         for ($majorVersion = $tinebase->getMajorVersion(); $majorVersion <= $major; $majorVersion++) {
             $messages += $this->updateApplication($tinebase, $majorVersion);
         }
     }
     // update the rest
     for ($majorVersion = $smallestMajorVersion; $majorVersion <= $biggestMajorVersion; $majorVersion++) {
         foreach ($_applications as $application) {
             if ($application->getMajorVersion() <= $majorVersion) {
                 $messages += $this->updateApplication($application, $majorVersion);
             }
         }
     }
     return array('messages' => $messages, 'updated' => $this->_updatedApplications);
 }
 public function testRegexpFilter()
 {
     $recordSet = new Tinebase_Record_RecordSet('Tinebase_Record_DummyRecord');
     $recordSet->addRecord(new Tinebase_Record_DummyRecord(array('id' => '100', 'string' => 'bommel-1'), true));
     $recordSet->addRecord(new Tinebase_Record_DummyRecord(array('id' => '200', 'string' => 'super-1'), true));
     $recordSet->addRecord(new Tinebase_Record_DummyRecord(array('id' => '300', 'string' => 'bommel-2'), true));
     $filterResultWOIndices = $recordSet->filter('string', '/^bommel.*/', TRUE);
     $this->assertEquals(2, count($filterResultWOIndices));
     $this->assertEquals(array(100, 300), $filterResultWOIndices->getArrayOfIds());
     $recordSet->addIndices(array('string'));
     $filterResultWIndices = $recordSet->filter('string', '/^bommel.*/', TRUE);
     $this->assertEquals(count($filterResultWOIndices), count($filterResultWIndices));
     $this->assertEquals(array(100, 300), $filterResultWIndices->getArrayOfIds());
 }
 /**
  * assert status authkey with editGrant
  * assert stauts can be set with editGrant
  * assert stauts can't be set without editGrant
  */
 public function testResourceAttendeeGrants()
 {
     $editableResoureData = $this->testSaveResource();
     $nonEditableResoureData = $this->testSaveResource(array('readGrant'));
     $event = $this->_getEvent(TRUE);
     $event->attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', array(array('user_type' => Calendar_Model_Attender::USERTYPE_RESOURCE, 'user_id' => $editableResoureData['id'], 'status' => Calendar_Model_Attender::STATUS_ACCEPTED), array('user_type' => Calendar_Model_Attender::USERTYPE_RESOURCE, 'user_id' => $nonEditableResoureData['id'], 'status' => Calendar_Model_Attender::STATUS_ACCEPTED)));
     $persistentEventData = $this->_uit->saveEvent($event->toArray());
     $attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', $persistentEventData['attendee']);
     $this->assertEquals(1, count($attendee->filter('status', Calendar_Model_Attender::STATUS_ACCEPTED)), 'one accepted');
     $this->assertEquals(1, count($attendee->filter('status', Calendar_Model_Attender::STATUS_NEEDSACTION)), 'one needs action');
     $this->assertEquals(1, count($attendee->filter('status_authkey', '/[a-z0-9]+/', TRUE)), 'one has authkey');
     $attendee->status = Calendar_Model_Attender::STATUS_TENTATIVE;
     $persistentEventData['attendee'] = $attendee->toArray();
     $updatedEventData = $this->_uit->saveEvent($persistentEventData);
     $attendee = new Tinebase_Record_RecordSet('Calendar_Model_Attender', $updatedEventData['attendee']);
     $this->assertEquals(1, count($attendee->filter('status', Calendar_Model_Attender::STATUS_TENTATIVE)), 'one tentative');
 }
 /**
  * add multiple modification system nodes
  * 
  * @param Tinebase_Record_RecordSet $_mods
  * @param string $_userId
  * @param string $modelName
  */
 public function addMultipleModificationSystemNotes($_mods, $_userId, $modelName = null)
 {
     $_mods->addIndices(array('record_id'));
     foreach ($_mods->record_id as $recordId) {
         $modsOfRecord = $_mods->filter('record_id', $recordId);
         $this->addSystemNote($recordId, $_userId, Tinebase_Model_Note::SYSTEM_NOTE_NAME_CHANGED, $modsOfRecord, 'Sql', $modelName);
     }
 }