/** * 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; } } }