/** * initialize vcard object * * @param Tinebase_Record_Abstract $record * @return \Sabre\VObject\Component */ protected function _fromTine20ModelRequiredFields(Tinebase_Record_Abstract $record) { /** @var Addressbook_Model_Contact $record */ $version = Tinebase_Application::getInstance()->getApplicationByName('Addressbook')->version; $card = new \Sabre\VObject\Component\VCard(array('VERSION' => '3.0', 'FN' => $record->n_fileas, 'N' => array($record->n_family, $record->n_given, $record->n_middle, $record->n_prefix, $record->n_suffix), 'PRODID' => "-//tine20.com//Tine 2.0 Addressbook V{$version}//EN", 'UID' => $record->getId(), 'ORG' => array($record->org_name, $record->org_unit), 'TITLE' => $record->title)); return $card; }
/** * converts Addressbook_Model_Contact to vcard * * @param Addressbook_Model_Contact $_record * @return string */ public function fromTine20Model(Tinebase_Record_Abstract $_record) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' contact ' . print_r($_record->toArray(), true)); } $card = new Sabre_VObject_Component('VCARD'); // required vcard fields $card->add(new Sabre_VObject_Property('VERSION', '3.0')); $card->add(new Sabre_VObject_Property('FN', $_record->n_fileas)); $card->add(new Sabre_VObject_Element_MultiValue('N', array($_record->n_family, $_record->n_given))); $card->add(new Sabre_VObject_Property('PRODID', '-//tine20.org//Tine 2.0//EN')); $card->add(new Sabre_VObject_Property('UID', $_record->getId())); // optional fields $card->add(new Sabre_VObject_Element_MultiValue('ORG', array($_record->org_name, $_record->org_unit))); $card->add(new Sabre_VObject_Property('TITLE', $_record->title)); $tel = new Sabre_VObject_Property('TEL', $_record->tel_work); $tel->add('TYPE', 'WORK'); $card->add($tel); $tel = new Sabre_VObject_Property('TEL', $_record->tel_home); $tel->add('TYPE', 'HOME'); $card->add($tel); $tel = new Sabre_VObject_Property('TEL', $_record->tel_cell); $tel->add('TYPE', 'CELL'); $card->add($tel); $tel = new Sabre_VObject_Property('TEL', $_record->tel_pager); $tel->add('TYPE', 'PAGER'); $card->add($tel); $tel = new Sabre_VObject_Property('TEL', $_record->tel_fax); $tel->add('TYPE', 'FAX'); $card->add($tel); #$tel = new Sabre_VObject_Property('TEL', $_record->tel_fax_home); #$tel->add('TYPE', 'FAX'); #$tel->add('TYPE', 'HOME'); #$card->add($tel); $adr = new Sabre_VObject_Element_MultiValue('ADR', array(null, $_record->adr_one_street2, $_record->adr_one_street, $_record->adr_one_locality, $_record->adr_one_region, $_record->adr_one_postalcode, $_record->adr_one_countryname)); $adr->add('TYPE', 'WORK'); $card->add($adr); $adr = new Sabre_VObject_Element_MultiValue('ADR', array(null, $_record->adr_two_street2, $_record->adr_two_street, $_record->adr_two_locality, $_record->adr_two_region, $_record->adr_two_postalcode, $_record->adr_two_countryname)); $adr->add('TYPE', 'HOME'); $card->add($adr); $card->add(new Sabre_VObject_Property('EMAIL;TYPE=work', $_record->email)); $card->add(new Sabre_VObject_Property('EMAIL;TYPE=home', $_record->email_home)); $card->add(new Sabre_VObject_Property('URL;TYPE=work', $_record->url)); $card->add(new Sabre_VObject_Property('URL;TYPE=home', $_record->url_home)); $card->add(new Sabre_VObject_Property('NOTE', $_record->note)); if (!empty($_record->jpegphoto)) { try { $image = Tinebase_Controller::getInstance()->getImage('Addressbook', $_record->getId()); $jpegData = $image->getBlob('image/jpeg'); $photo = new Sabre_VObject_Property('PHOTO', $jpegData); $photo->add('ENCODING', 'b'); $photo->add('TYPE', 'JPEG'); $card->add($photo); } catch (Exception $e) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . " Image for contact {$_record->getId()} not found or invalid"); } } if (isset($_record->tags) && count($_record->tags) > 0) { $card->add(new Sabre_VObject_Property('CATEGORIES', Sabre_VObject_Element_List((array) $_record->tags->name))); } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' card ' . $card->serialize()); } return $card; }
/** * update foreign key values * * @param string $_mode create|update * @param Tinebase_Record_Abstract $_record * * @todo support update mode */ protected function _updateForeignKeys($_mode, Tinebase_Record_Abstract $_record) { if ($_mode == 'create') { foreach ($this->_foreignTables as $key => $foreign) { if (!isset($_record->{$key}) || empty($_record->{$key})) { continue; } //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $_field . ': ' . print_r($_record->{$_field}, TRUE)); foreach ($_record->{$key} as $data) { if ($key == 'flags') { $data = array('flag' => $data, 'folder_id' => $_record->folder_id); } $data['message_id'] = $_record->getId(); $this->_db->insert($this->_tablePrefix . $foreign['table'], $data); } } } }
/** * iterate relations * * @param Tinebase_Record_Abstract $currentRecord * @return array */ protected function _iterateRelations($currentRecord) { if (!$currentRecord->relations || get_class($currentRecord->relations) != 'Tinebase_Record_RecordSet') { $currentRecord->relations = new Tinebase_Record_RecordSet('Tinebase_Model_Relation'); } $be = new Tinebase_Relation_Backend_Sql(); // handle relations to remove if ($this->_removeRelations) { if ($currentRecord->relations->count()) { foreach ($this->_removeRelations as $remRelation) { $removeRelations = $currentRecord->relations->filter('type', $remRelation['type'])->filter('related_model', $remRelation['related_model']); $currentRecord->relations->removeRecords($removeRelations); } } } // handle new relations if ($this->_newRelations) { $removeRelations = NULL; foreach ($this->_newRelations as $newRelation) { $removeRelations = $currentRecord->relations->filter('type', $newRelation['type'])->filter('related_model', $newRelation['related_model']); $already = $removeRelations->filter('related_id', $newRelation['related_id']); if ($already->count() > 0) { $removeRelations = NULL; } else { $newRelation['own_id'] = $currentRecord->getId(); $rel = new Tinebase_Model_Relation(); $rel->setFromArray($newRelation); if ($removeRelations) { $currentRecord->relations->removeRecords($removeRelations); } $currentRecord->relations->addRecord($rel); } } } return $currentRecord->relations->toArray(); }
/** * converts a int, string or Tinebase_Record_Interface to a id * * @param int|string|Tinebase_Record_Abstract $_id the id to convert * @param string $_modelName * @return int|string * @throws Tinebase_Exception_InvalidArgument */ public static function convertId($_id, $_modelName = 'Tinebase_Record_Abstract') { if ($_id instanceof $_modelName) { /** @var Tinebase_Record_Interface $_id */ if (!$_id->getId()) { throw new Tinebase_Exception_InvalidArgument('No id set!'); } $id = $_id->getId(); } elseif (is_array($_id)) { throw new Tinebase_Exception_InvalidArgument('Id can not be an array!'); } else { $id = $_id; } if ($id === 0) { throw new Tinebase_Exception_InvalidArgument($_modelName . '.id can not be 0!'); } return $id; }
/** * Gets tags of a given record where user has the required right to * The tags are stored in the records $_tagsProperty. * * @param Tinebase_Record_Abstract $_record the record object * @param string $_tagsProperty the property in the record where the tags are in (defaults: 'tags') * @param string $_right the required right current user must have on the tags * @return Tinebase_Record_RecordSet tags of record */ public function getTagsOfRecord($_record, $_tagsProperty = 'tags', $_right = Tinebase_Model_TagRight::VIEW_RIGHT) { $recordId = $_record->getId(); $tags = new Tinebase_Record_RecordSet('Tinebase_Model_Tag'); if (!empty($recordId)) { $select = $this->_getSelect($recordId, Tinebase_Application::getInstance()->getApplicationByName($_record->getApplication())->getId()); Tinebase_Model_TagRight::applyAclSql($select, $_right, $this->_db->quoteIdentifier('tagging.tag_id')); Tinebase_Backend_Sql_Abstract::traitGroup($select); foreach ($this->_db->fetchAssoc($select) as $tagArray) { $tags->addRecord(new Tinebase_Model_Tag($tagArray, true)); } } $_record[$_tagsProperty] = $tags; return $tags; }
/** * set grants of record * * @param Tinebase_Record_Abstract $record * @param $boolean $addDuringSetup -> let admin group have all rights instead of user * @return Tinebase_Record_RecordSet of record grants * * @todo improve algorithm: only update/insert/delete changed grants */ public function setGrants($record, $addDuringSetup = false) { $recordId = $record->getId(); if (empty($recordId)) { throw new Timetracker_Exception_UnexpectedValue('record id required to set grants'); } if (!$this->_validateGrants($record)) { $this->_setDefaultGrants($record, $addDuringSetup); } try { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Setting grants for record ' . $recordId); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Grants: ' . print_r($record->grants->toArray(), true)); } $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb()); $this->_grantsBackend->deleteByProperty($recordId, 'record_id'); foreach ($record->grants as $newGrant) { foreach (call_user_func($this->_grantsModel . '::getAllGrants') as $grant) { if ($newGrant->{$grant}) { $newGrant->account_grant = $grant; $newGrant->record_id = $recordId; $this->_grantsBackend->create($newGrant); } } } Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId); } catch (Exception $e) { Tinebase_Exception::log($e); Tinebase_TransactionManager::getInstance()->rollBack(); throw new Tinebase_Exception_Backend($e->getMessage()); } return $record->grants; }
protected function _importRecord(Tinebase_Record_Abstract $_record) { if ($_record->getId()) { return $this->_controllerToImport->update($_record, false); } else { return $this->_controllerToImport->create($_record, false); } }
/** * * @param Tinebase_Record_Abstract $record * @param array $members */ protected function _handleGroupMemberShip($record, $members) { $be = new Tinebase_Group_Sql(); $group = $be->getGroupByName($record->name); $oldMembers = $be->getGroupMembers($group->getId()); $newMembers = $this->_resolveUsers($members); foreach ($oldMembers as $oldMember) { if (!in_array($oldMember, $newMembers)) { $be->removeGroupMember($record->getId(), $oldMember); } } foreach ($newMembers as $newMember) { if (!in_array($newMember, $oldMembers)) { $be->addGroupMember($record->getId(), $newMember); } } }
/** * add new system note * * @param Tinebase_Record_Abstract|string $_record * @param string|Tinebase_Mode_User $_userId * @param string $_type (created|changed) * @param Tinebase_Record_RecordSet|string $_mods (Tinebase_Model_ModificationLog) * @param string $_backend backend of record * @return Tinebase_Model_Note|boolean * * @todo get field translations from application? * @todo attach modlog record (id) to note instead of saving an ugly string */ public function addSystemNote($_record, $_userId = NULL, $_type = Tinebase_Model_Note::SYSTEM_NOTE_NAME_CREATED, $_mods = NULL, $_backend = 'Sql', $_modelName = NULL) { if (empty($_mods) && $_type === Tinebase_Model_Note::SYSTEM_NOTE_NAME_CHANGED) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Nothing changed -> do not add "changed" note.'); return FALSE; } $id = $_record instanceof Tinebase_Record_Abstract ? $_record->getId() : $_record; $modelName = $_modelName !== NULL ? $_modelName : ($_record instanceof Tinebase_Record_Abstract ? get_class($_record) : 'unknown'); if ($_userId === NULL) { $_userId = Tinebase_Core::getUser(); } $user = $_userId instanceof Tinebase_Model_User ? $_userId : Tinebase_User::getInstance()->getUserById($_userId); $translate = Tinebase_Translation::getTranslation('Tinebase'); $noteText = $translate->_($_type) . ' ' . $translate->_('by') . ' ' . $user->accountDisplayName; if ($_mods !== NULL) { if ($_mods instanceof Tinebase_Record_RecordSet && count($_mods) > 0) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' mods to log: ' . print_r($_mods->toArray(), TRUE)); } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Adding "' . $_type . '" system note note to record (id ' . $id . ')'); } $noteText .= ' | ' . $translate->_('Changed fields:'); foreach ($_mods as $mod) { $noteText .= ' ' . $translate->_($mod->modified_attribute) . ' (' . $this->_getSystemNoteChangeText($mod) . ')'; } } else { if (is_string($_mods)) { $noteText = $_mods; } } } $noteType = $this->getNoteTypeByName($_type); $note = new Tinebase_Model_Note(array('note_type_id' => $noteType->getId(), 'note' => substr($noteText, 0, self::MAX_NOTE_LENGTH), 'record_model' => $modelName, 'record_backend' => ucfirst(strtolower($_backend)), 'record_id' => $id)); return $this->addNote($note); }
/** * add new system note * * @param Tinebase_Record_Abstract|string $_record * @param string $_userId * @param string $_type (created|changed) * @param Tinebase_Record_RecordSet RecordSet $_mods (Tinebase_Model_ModificationLog) * @param string $_backend backend of record * @return Tinebase_Model_Note|boolean * * @todo get field translations from application? * @todo attach modlog record (id) to note instead of saving an ugly string */ public function addSystemNote($_record, $_userId, $_type, $_mods = NULL, $_backend = 'Sql', $_modelName = NULL) { $id = $_record instanceof Tinebase_Record_Abstract ? $_record->getId() : $_record; $modelName = $_modelName !== NULL ? $_modelName : ($_record instanceof Tinebase_Record_Abstract ? get_class($_record) : 'unknown'); $translate = Tinebase_Translation::getTranslation('Tinebase'); $backend = ucfirst(strtolower($_backend)); $noteType = $this->getNoteTypeByName($_type); $user = Tinebase_User::getInstance()->getUserById($_userId); $noteText = $translate->_($_type) . ' ' . $translate->_('by') . ' ' . $user->accountDisplayName; if ($_mods !== NULL && count($_mods) > 0) { if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' mods to log: ' . print_r($_mods->toArray(), TRUE)); } Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Adding "' . $_type . '" system note note to record.'); $noteText .= ' | ' . $translate->_('Changed fields:'); foreach ($_mods as $mod) { $noteText .= ' ' . $translate->_($mod->modified_attribute) . ' (' . $mod->old_value . ' -> ' . $mod->new_value . ')'; } } else { if ($_type === Tinebase_Model_Note::SYSTEM_NOTE_NAME_CHANGED) { // nothing changed -> don't add note Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Nothing changed -> don\'t add "changed" note.'); return FALSE; } } $note = new Tinebase_Model_Note(array('note_type_id' => $noteType->getId(), 'note' => $noteText, 'record_model' => $modelName, 'record_backend' => $backend, 'record_id' => $id)); return $this->addNote($note); }
/** * get path for record attachments * * @param Tinebase_Record_Abstract $record * @param boolean $createDirIfNotExists * @throws Tinebase_Exception_InvalidArgument * @return string */ public function getRecordAttachmentPath(Tinebase_Record_Abstract $record, $createDirIfNotExists = FALSE) { if (!$record->getId()) { throw new Tinebase_Exception_InvalidArgument('record needs an identifier'); } $parentPath = $this->_fsController->getApplicationBasePath($record->getApplication(), Tinebase_FileSystem::FOLDER_TYPE_RECORDS); $recordPath = $parentPath . '/' . get_class($record) . '/' . $record->getId(); if ($createDirIfNotExists && !$this->_fsController->fileExists($recordPath)) { $this->_fsController->mkdir($recordPath); } return $recordPath; }
/** * update foreign key values * * @param string $_mode create|update * @param Tinebase_Record_Abstract $_record */ protected function _updateForeignKeys($_mode, Tinebase_Record_Abstract $_record) { if (!empty($this->_foreignTables)) { foreach ($this->_foreignTables as $modelName => $join) { if (!(isset($join['field']) || array_key_exists('field', $join))) { continue; } $idsToAdd = array(); $idsToRemove = array(); if (!empty($_record->{$modelName})) { $idsToAdd = $this->_getIdsFromMixed($_record->{$modelName}); } $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb()); if ($_mode == 'update') { $select = $this->_db->select(); $select->from(array($join['table'] => $this->_tablePrefix . $join['table']), array($join['field']))->where($this->_db->quoteIdentifier($join['table'] . '.' . $join['joinOn']) . ' = ?', $_record->getId()); Tinebase_Backend_Sql_Abstract::traitGroup($select); $stmt = $this->_db->query($select); $currentIds = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); $stmt->closeCursor(); $idsToRemove = array_diff($currentIds, $idsToAdd); $idsToAdd = array_diff($idsToAdd, $currentIds); } if (!empty($idsToRemove)) { $where = '(' . $this->_db->quoteInto($this->_db->quoteIdentifier($this->_tablePrefix . $join['table'] . '.' . $join['joinOn']) . ' = ?', $_record->getId()) . ' AND ' . $this->_db->quoteInto($this->_db->quoteIdentifier($this->_tablePrefix . $join['table'] . '.' . $join['field']) . ' IN (?)', $idsToRemove) . ')'; $this->_db->delete($this->_tablePrefix . $join['table'], $where); } foreach ($idsToAdd as $id) { $recordArray = array($join['joinOn'] => $_record->getId(), $join['field'] => $id); $this->_db->insert($this->_tablePrefix . $join['table'], $recordArray); } Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId); } } }