db = DBManagerFactory::getInstance(); $this->id = null; $this->type = "inbound"; } /** * @param int|string $folderId - (should be string, int type is legacy) * @return null|string (folder ID) * @throws SuiteException */ public function retrieve($folderId = -1) { $isValidator = new SuiteValidator(); if ($isValidator->isValidId($folderId)) { $result = $this->db->query("SELECT * FROM folders WHERE id='" . $folderId . "'"); $row = $this->db->fetchByAssoc($result); // get the root of the tree // is the id of the root node is the same as the inbound email id if (empty($row['parent_folder'])) { // root node (inbound) $this->id = $row['id']; $this->type = $row['folder_type']; $this->mailbox = 'INBOX'; // Top level IMAP folder } else { // child node $this->id = $row['parent_folder']; $this->type = $row['folder_type']; $this->mailbox = $row['name']; } } else { throw new SuiteException("Invalid or empty Email Folder ID"); } return $this->id; } /** * @param array $request * @return Folder * @throws SuiteException */ public function retrieveFromRequest($request) { if (isset($request['folders_id']) && !empty($request['folders_id'])) { $foldersId = $request['folders_id']; $this->retrieve($foldersId); $_SESSION['CURRENT_IMAP_MAILBOX_ID'] = $request['folders_id']; } else { $GLOBALS['log']->warn("Empty or undefined Email Folder ID"); } return $this; } /** * @param array|null $request * @throws SuiteException */ public function loadMailboxFolder(?array $request): void { global $current_user; $inboundEmailID = $current_user->getPreference('defaultIEAccount', 'Emails'); $folderId = ''; if (isset($request['folders_id']) && !empty($request['folders_id'])) { $folderId = $request['folders_id']; } elseif (!empty($_SESSION['CURRENT_IMAP_MAILBOX_ID'])) { $folderId = $_SESSION['CURRENT_IMAP_MAILBOX_ID']; } elseif (!empty($inboundEmailID)) { $folderId = $inboundEmailID; } if (!$this->isSelectedForDisplay($folderId)) { $folderId = $this->getFirstDisplayFolder(); } if (!empty($folderId)) { $this->retrieve($folderId); $_SESSION['CURRENT_IMAP_MAILBOX_ID'] = $folderId; return; } $_SESSION['CURRENT_IMAP_MAILBOX_ID'] = ''; $GLOBALS['log']->warn("Empty or undefined Email Folder ID"); } /** * @return string */ public function getType() { return $this->type; } /** * @return null|string */ public function getId() { return $this->id; } /** * @return string */ public function getMailbox() { return $this->mailbox; } /** * Check if folder is to display * @param $folderId * @return bool */ public function isSelectedForDisplay($folderId): bool { return (new SugarFolder())->isToDisplay($folderId); } /** * @return mixed|string */ protected function getFirstDisplayFolder(): ?string { $folder = new SugarFolder(); $folder = $folder->getFirstDisplayFolders(); if ($folder === null) { return null; } return $folder['id'] ?? ''; } }