Ejemplo n.º 1
0
 /**
  * moves emails from folder to folder
  * @param string $fromIe I-E id
  * @param string $fromFolder IMAP path to folder in which the email lives
  * @param string $toIe I-E id
  * @param string $toFolder
  * @param string $uids UIDs of emails to move, either Sugar GUIDS or IMAP
  * UIDs
  * @param bool $copy Default false
  * @return bool True on successful execution
  */
 function moveEmails($fromIe, $fromFolder, $toIe, $toFolder, $uids, $copy = false)
 {
     global $app_strings;
     global $current_user;
     // same I-E server
     if ($fromIe == $toIe) {
         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() moving email from I-E to I-E");
         //$exDestFolder = explode("::", $toFolder);
         //preserve $this->mailbox
         if (isset($this->mailbox)) {
             $oldMailbox = $this->mailbox;
         }
         $this->retrieve($fromIe);
         $this->mailbox = $fromFolder;
         $this->connectMailserver();
         $exUids = explode('::;::', $uids);
         $uids = implode(",", $exUids);
         // imap_mail_move accepts comma-delimited lists of UIDs
         if ($copy) {
             if (imap_mail_copy($this->conn, $uids, $toFolder, CP_UID)) {
                 $this->mailbox = $toFolder;
                 $this->connectMailserver();
                 $newOverviews = imap_fetch_overview($this->conn, $uids, FT_UID);
                 $this->updateOverviewCacheFile($newOverviews, 'append');
                 if (isset($oldMailbox)) {
                     $this->mailbox = $oldMailbox;
                 }
                 return true;
             } else {
                 $GLOBALS['log']->debug("INBOUNDEMAIL: could not imap_mail_copy() [ {$uids} ] to folder [ {$toFolder} ] from folder [ {$fromFolder} ]");
             }
         } else {
             if (imap_mail_move($this->conn, $uids, $toFolder, CP_UID)) {
                 $GLOBALS['log']->info("INBOUNDEMAIL: imap_mail_move() [ {$uids} ] to folder [ {$toFolder} ] from folder [ {$fromFolder} ]");
                 imap_expunge($this->conn);
                 // hard deletes moved messages
                 // update cache on fromFolder
                 $newOverviews = $this->getOverviewsFromCacheFile($uids, $fromFolder, true);
                 $this->deleteCachedMessages($uids, $fromFolder);
                 // update cache on toFolder
                 $this->checkEmailOneMailbox($toFolder, true, true);
                 if (isset($oldMailbox)) {
                     $this->mailbox = $oldMailbox;
                 }
                 return true;
             } else {
                 $GLOBALS['log']->debug("INBOUNDEMAIL: could not imap_mail_move() [ {$uids} ] to folder [ {$toFolder} ] from folder [ {$fromFolder} ]");
             }
         }
     } elseif ($toIe == 'folder' && $fromFolder == 'sugar::Emails') {
         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() moving email from SugarFolder to SugarFolder");
         // move from sugar folder to sugar folder
         require_once "include/SugarFolders/SugarFolders.php";
         $sugarFolder = new SugarFolder();
         $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
         foreach ($exUids as $id) {
             if ($copy) {
                 $sugarFolder->copyBean($fromIe, $toFolder, $id, "Emails");
             } else {
                 $fromSugarFolder = new SugarFolder();
                 $fromSugarFolder->retrieve($fromIe);
                 $toSugarFolder = new SugarFolder();
                 $toSugarFolder->retrieve($toFolder);
                 $email = new Email();
                 $email->retrieve($id);
                 $email->status = 'unread';
                 // when you move from My Emails to Group Folder, Assign To field for the Email should become null
                 if ($fromSugarFolder->is_dynamic && $toSugarFolder->is_group) {
                     $email->assigned_user_id = "";
                     $email->save();
                     if (!$toSugarFolder->checkEmailExistForFolder($id)) {
                         $fromSugarFolder->deleteEmailFromAllFolder($id);
                         $toSugarFolder->addBean($email);
                     }
                 } elseif ($fromSugarFolder->is_group && $toSugarFolder->is_dynamic) {
                     $fromSugarFolder->deleteEmailFromAllFolder($id);
                     $email->assigned_user_id = $current_user->id;
                     $email->save();
                 } else {
                     // If you are moving something from personal folder then delete an entry from all folder
                     if (!$fromSugarFolder->is_dynamic && !$fromSugarFolder->is_group) {
                         $fromSugarFolder->deleteEmailFromAllFolder($id);
                     }
                     // if
                     if ($fromSugarFolder->is_dynamic && !$toSugarFolder->is_dynamic && !$toSugarFolder->is_group) {
                         $email->assigned_user_id = "";
                         $toSugarFolder->addBean($email);
                     }
                     // if
                     if (!$toSugarFolder->checkEmailExistForFolder($id)) {
                         if (!$toSugarFolder->is_dynamic) {
                             $fromSugarFolder->deleteEmailFromAllFolder($id);
                             $toSugarFolder->addBean($email);
                         } else {
                             $fromSugarFolder->deleteEmailFromAllFolder($id);
                             $email->assigned_user_id = $current_user->id;
                         }
                     } else {
                         $sugarFolder->move($fromIe, $toFolder, $id);
                     }
                     // else
                     $email->save();
                 }
                 // else
             }
         }
         return true;
     } elseif ($toIe == 'folder') {
         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() moving email from I-E to SugarFolder");
         // move to Sugar folder
         require_once "include/SugarFolders/SugarFolders.php";
         $sugarFolder = new SugarFolder();
         $sugarFolder->retrieve($toFolder);
         //Show the import form if we don't have the required info
         if (!isset($_REQUEST['delete'])) {
             $json = getJSONobj();
             if ($sugarFolder->is_group) {
                 $_REQUEST['showTeam'] = false;
                 $_REQUEST['showAssignTo'] = false;
             }
             $ret = $this->email->et->getImportForm($_REQUEST, $this->email);
             $ret['move'] = true;
             $ret['srcFolder'] = $fromFolder;
             $ret['srcIeId'] = $fromIe;
             $ret['dstFolder'] = $toFolder;
             $ret['dstIeId'] = $toIe;
             $out = trim($json->encode($ret, false));
             echo $out;
             return true;
         }
         // import to Sugar
         $this->retrieve($fromIe);
         $this->mailbox = $fromFolder;
         $this->connectMailserver();
         // If its a group folder the team should be of the folder team
         if ($sugarFolder->is_group) {
             $_REQUEST['team_id'] = $sugarFolder->team_id;
             $_REQUEST['team_set_id'] = $sugarFolder->team_set_id;
         } else {
             // TODO - set team_id, team_set for new UI
         }
         // else
         $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
         if (!empty($sugarFolder->id)) {
             $count = 1;
             $return = array();
             $json = getJSONobj();
             foreach ($exUids as $k => $uid) {
                 $msgNo = $uid;
                 if ($this->isPop3Protocol()) {
                     $msgNo = $this->getCorrectMessageNoForPop3($uid);
                 } else {
                     $msgNo = imap_msgno($this->conn, $uid);
                 }
                 if (!empty($msgNo)) {
                     $importStatus = $this->importOneEmail($msgNo, $uid);
                     // add to folder
                     if ($importStatus) {
                         $sugarFolder->addBean($this->email);
                         if (!$copy && isset($_REQUEST['delete']) && $_REQUEST['delete'] == "true" && $importStatus) {
                             $GLOBALS['log']->error("********* delete from mailserver [ {explode(", ", {$uids})} ]");
                             // delete from mailserver
                             $this->deleteMessageOnMailServer($uid);
                             $this->deleteMessageFromCache($uid);
                         }
                         // if
                     }
                     $return[] = $app_strings['LBL_EMAIL_MESSAGE_NO'] . " " . $count . ", " . $app_strings['LBL_STATUS'] . " " . ($importStatus ? $app_strings['LBL_EMAIL_IMPORT_SUCCESS'] : $app_strings['LBL_EMAIL_IMPORT_FAIL']);
                     $count++;
                 }
                 // if
             }
             // foreach
             echo $json->encode($return);
             return true;
         } else {
             $GLOBALS['log']->error("********* SUGARFOLDER - failed to retrieve folder ID [ {$toFolder} ]");
         }
     } else {
         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() called with no passing criteria");
     }
     return false;
 }