/** * Will save attachments for a draft. * When requested to save/send a message by a client, all existing * attachments/files will be available in the postedAttachments property, * data according to the structure of com.conjoon.cudgets.data.FileRecord. * Important keys are 'orgId', 'metaType', 'key', 'name' * * Warning! since its not guaranteed that the ids in removeAttachments are * ids for attachments that indeed belong to the draft, it's needed * to check whether the current list of attachments holds this id. * * @param Conjoon_Modules_Groupware_Email_Draft $message * @param array $postedAttachments * @param array $removeAttachmentIds * @param array $attachmentMap * * @throws InvalidArgumentException */ public function saveAttachmentsForDraft(Conjoon_Modules_Groupware_Email_Draft $draft, $postedAttachments = array(), $removeAttachmentIds = array(), &$attachmentMap = array()) { if ($draft->getId() <= 0) { throw new InvalidArgumentException("Invalid draft supplied - id was " . $draft->getId()); } /** * @see Conjoon_Modules_Groupware_Email_Attachment_Model_Attachment */ require_once 'Conjoon/Modules/Groupware/Email/Attachment/Model/Attachment.php'; $attachmentModel = new Conjoon_Modules_Groupware_Email_Attachment_Model_Attachment(); // first off, get all the attachments from the draft $draftAttachments = $draft->getAttachments(); $postedEmailAttachmentIds = array(); $existingEmailAttachmentIds = array(); $postedFilesIds = array(); $finalPostedFiles = array(); $finalPostedAttachments = array(); $finalExistingAttachments = array(); //get ids for emailAttachments for ($i = 0, $len = count($postedAttachments); $i < $len; $i++) { if ($postedAttachments[$i]['metaType'] == 'emailAttachment') { $postedEmailAttachmentIds[] = $postedAttachments[$i]['orgId']; $finalPostedAttachments[$postedAttachments[$i]['orgId']] = $postedAttachments[$i]; } else { $postedFilesIds[] = $postedAttachments[$i]['orgId']; $finalPostedFiles[$postedAttachments[$i]['orgId']] = $postedAttachments[$i]; } } for ($i = 0, $len = count($draftAttachments); $i < $len; $i++) { // intersect will be created later $existingEmailAttachmentIds[] = $draftAttachments[$i]->getId(); if (in_array($draftAttachments[$i]->getId(), $removeAttachmentIds)) { continue; } $finalExistingAttachments[$draftAttachments[$i]->getId()] = $draftAttachments[$i]; } // finally create the intersection of all ids that are in the // lists of items to remove and in the list of existing items $removeAttachmentIds = array_values(array_intersect($removeAttachmentIds, $existingEmailAttachmentIds)); // get the ids from the attachments that need to get changed $changeNameIds = array_values(array_intersect($postedEmailAttachmentIds, $existingEmailAttachmentIds)); // get the ids from the attachments that need to get saved, i.e. // when a draft was created with email attachments which currently // beong to another email $copyAttachmentIds = array_values(array_diff($postedEmailAttachmentIds, $existingEmailAttachmentIds)); // take care of copying attachments for ($i = 0, $len = count($copyAttachmentIds); $i < $len; $i++) { $id = $copyAttachmentIds[$i]; $newAttachmentId = $attachmentModel->copyAttachmentForNewItemId($id, $draft->getId(), $finalPostedAttachments[$id]['name']); if ($newAttachmentId > 0) { $attachmentMap[$finalPostedAttachments[$id]['id']] = $newAttachmentId; } } // take care of deleting attachments for ($i = 0, $len = count($removeAttachmentIds); $i < $len; $i++) { $attachmentModel->deleteAttachmentForId($removeAttachmentIds[$i]); } // take care of renaming attachments for ($i = 0, $len = count($changeNameIds); $i < $len; $i++) { $id = $changeNameIds[$i]; if ($finalExistingAttachments[$id]->getFileName() != $finalPostedAttachments[$id]['name']) { $updated = $attachmentModel->updateNameForAttachment($id, $finalPostedAttachments[$id]['name']); if ($updated) { $finalExistingAttachments[$id]->setFileName($finalPostedAttachments[$id]['name']); $attachmentMap[$finalPostedAttachments[$id]['id']] = $id; } } } // copy files to attachments /** * @see Conjoon_Modules_Groupware_Files_File_Facade */ require_once 'Conjoon/Modules/Groupware/Files/File/Facade.php'; $filesFacade = Conjoon_Modules_Groupware_Files_File_Facade::getInstance(); foreach ($finalPostedFiles as $id => $file) { // possible that the file is stored in the file system, so get the content here // and pass as argument $fileData = $filesFacade->getLobContentWithData(array('id' => $file['orgId'], 'key' => $file['key'], 'includeResource' => true)); $newAttachmentId = $attachmentModel->copyFromFilesForItemId($file['key'], $file['orgId'], $draft->getId(), $file['name'], $fileData['resource']); if ($newAttachmentId > 0) { $attachmentMap[$finalPostedFiles[$id]['id']] = $newAttachmentId; } } }
/** * Returns an assoc array with the data of an draft. The returned array * has all properties as according to Conjoon_Modules_Groupware_Email_Draft. * * @param integer $itemId * @param integer $userId * @param string $context The context used to fetch the draft. Important * when dealign with contexts "reply", "reply_all" and "forward". * - context "forward": fields "references" and "in_reply_to" will be set * to an empty string * - context "reply", "reply_all": "in_reply_to" will be set to the message-id * of the email, references will be concatenated with the message-id * * @return array */ public function getDraft($itemId, $userId, $context = '') { $itemId = (int) $itemId; if ($itemId <= 0) { return array(); } $itemModel = new Conjoon_Modules_Groupware_Email_Item_Model_Item(); $row = $itemModel->fetchRow($itemModel->select()->from($itemModel)->where('id = ?', $itemId)); if (!$row) { return array(); } $draft = array('id' => $row->id, 'date' => $row->date, 'subject' => $row->subject, 'from' => $row->from, 'reply_to' => $row->reply_to, 'to' => $row->to, 'cc' => $row->cc, 'bcc' => $row->bcc, 'in_reply_to' => $row->in_reply_to, 'references' => $row->references, 'content_text_plain' => $row->content_text_plain, 'content_text_html' => $row->content_text_html, 'groupware_email_folders_id' => $row->groupware_email_folders_id, 'attachments' => array()); // clear memory unset($row); // set in_reply_to, references according to the context switch ($context) { case Conjoon_Modules_Groupware_Email_Keys::REFERENCE_TYPE_REPLY: case Conjoon_Modules_Groupware_Email_Keys::REFERENCE_TYPE_REPLY_ALL: $inboxModel = new Conjoon_Modules_Groupware_Email_Item_Model_Inbox(); $messageId = $inboxModel->getMessageIdForItem($draft['id']); if ($messageId != "") { $draft['in_reply_to'] = $messageId; $draft['references'] = $draft['references'] != '' ? $draft['references'] . ' ' . $messageId : $messageId; } else { $draft['in_reply_to'] = ''; $draft['references'] = ''; } break; case Conjoon_Modules_Groupware_Email_Keys::REFERENCE_TYPE_FORWARD: $draft['in_reply_to'] = ''; $draft['references'] = ''; case '': case Conjoon_Modules_Groupware_Email_Keys::REFERENCE_TYPE_EDIT: /** * @see Conjoon_Modules_Groupware_Email_Attachment_Model_Attachment */ require_once 'Conjoon/Modules/Groupware/Email/Attachment/Model/Attachment.php'; $attachmentModel = new Conjoon_Modules_Groupware_Email_Attachment_Model_Attachment(); $draft['attachments'] = $attachmentModel->getAttachmentsForItem($draft['id'])->toArray(); break; } // check if the item is available in outbox and get the id of it under which it was // created. Otherwise, get the standard account out of the accounts-table $outboxModel = new Conjoon_Modules_Groupware_Email_Item_Model_Outbox(); $accIdRow = $outboxModel->fetchRow($outboxModel->select()->from($outboxModel, array('groupware_email_accounts_id'))->where('groupware_email_items_id = ? ', $draft['id'])); $accountModel = new Conjoon_Modules_Groupware_Email_Account_Model_Account(); if (!$accIdRow) { $accId = $accountModel->getStandardAccountIdForUser($userId); } else { $accId = $accIdRow->groupware_email_accounts_id; // check if the account still exists $account = $accountModel->getAccount($accId, $userId); if (empty($account)) { $accId = $accountModel->getStandardAccountIdForUser($userId); if ($accId == 0) { return array(); } } } $draft['groupware_email_accounts_id'] = $accId; return $draft; }