3 * This file contains functions for handling on-site portal mail.
5 * LICENSE: This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Derived from existing pnotes library
20 * @author Jerry Padgett <sjpadgett@gmail.com>
21 * @link http://www.open-emr.org
24 function addPortalMailboxMail(
29 $title = 'Unassigned',
32 $message_status = "New",
41 if (empty($datetime)) {
42 $datetime = date('Y-m-d H:i:s');
45 $user = $_SESSION['portal_username'] ? $_SESSION['portal_username'] : $_SESSION['authUser'];
46 // make inactive if set as Done
47 if ($message_status == "Done") {
52 if ($master_note == '0') {
53 $n = sqlQueryNoLog("SELECT MAX(id) as newid from onsite_mail");
54 $master_note = $n['newid']+1;
60 $master_note = $replyid;
63 $replyid = $master_note;
65 } else if ($owner != $sid) {
66 $replyid = $master_note-1;
68 $replyid = $master_note;
72 "INSERT INTO onsite_mail (date, body, owner, user, groupname, " .
73 "authorized, activity, title, assigned_to, message_status, mail_chain, sender_id, sender_name, recipient_id, recipient_name, reply_mail_chain) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)",
74 array($datetime, $body, $owner, $user, 'Default', $authorized, $activity, $title, $assigned_to, $message_status,$master_note,$sid,$sn,$rid,$rn,$replyid)
78 function getPortalPatientDeleted($owner = '', $limit = '', $offset = 0, $search = '')
81 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
102 WHERE p.deleted != 0 AND p.owner = ? AND p.recipient_id = ?
107 $all = $row = array();
108 $data = array($owner,$owner);
110 $data = array($owner,$owner,$owner);
113 $res = sqlStatement($sql, $data);
114 for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
121 function getPortalPatientNotes($owner = '', $limit = '', $offset = 0, $search = '')
124 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
145 WHERE p.deleted != 1 AND p.owner = ? AND p.recipient_id = ?
150 $all = $row = array();
151 $data = array($owner,$owner);
153 $data = array($owner,$owner,$owner);
156 $res = sqlStatement($sql, $data);
157 for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
164 function getPortalPatientNotifications($owner = '', $limit = '', $offset = 0, $search = '')
167 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
173 date_created AS `date`,
174 'Patient Reminders' AS `user`,
176 CONCAT(lo.title, ':', lo2.title) AS body,
177 '' as message_status,
178 'Notification' as `type`
180 patient_reminders AS pr
181 LEFT JOIN list_options AS lo
182 ON lo.option_id = pr.category
183 AND lo.list_id = 'rule_action_category'
184 LEFT JOIN list_options AS lo2
185 ON lo2.option_id = pr.item
186 AND lo2.list_id = 'rule_action'
189 AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
194 $all = $row = array();
195 $res = sqlStatement($sql, array($owner));
196 for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
203 function getPortalPatientSentNotes($owner = '', $limit = '', $offset = 0, $search = '')
206 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
228 WHERE p.sender_id = ?
231 AND p.message_status != 'Done'
236 $all = $row = array();
237 $res = sqlStatement($sql, array($owner,$owner));
238 for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
245 function updatePortalMailMessageStatus($id, $message_status)
247 if ($message_status == "Done") {
248 sqlStatement("update onsite_mail set message_status = ?, activity = '0' where id = ?", array($message_status, $id));
249 } elseif ($message_status == "Delete") {
250 sqlStatement("update onsite_mail set message_status = ?, activity = '1', deleted = '1',delete_date = ? where mail_chain = ? OR id = ?", array($message_status, date('Y-m-d H:i:s'), $id, $id));
252 sqlStatement("update onsite_mail set message_status = ?, activity = '1' where id = ?", array($message_status, $id));
256 function getMails($owner, $dotype, $nsrch, $nfsrch)
259 if ($dotype == "inbox") {
260 if ($nsrch && $nfsrch) {
261 $result_notes = getPortalPatientNotes($owner, '', '0', $nsrch);
262 $result_notifications = getPortalPatientNotifications($owner, '', '0', $nfsrch);
263 $result = array_merge((array)$result_notes, (array)$result_notifications);
265 $result_notes = getPortalPatientNotes($owner);
266 $result_notifications = getPortalPatientNotifications($owner);
267 $result = array_merge((array)$result_notes, (array)$result_notifications);
268 //$result = $result_notes;
272 } elseif ($dotype == "sent") {
274 $result_sent_notes = getPortalPatientSentNotes($owner, '', '0', $nsrch);
276 $result_sent_notes = getPortalPatientSentNotes($owner);
279 return $result_sent_notes;
280 } elseif ($dotype == "all") {
282 $result_notes = getPortalPatientNotes($owner, '', '0', "OR (p.deleted != 1 AND (p.owner = ?)) ");
283 $result_notifications = getPortalPatientNotifications($owner);
284 $result = array_merge((array)$result_notes, (array)$result_notifications);
286 } elseif ($dotype == "deleted") {
288 $result = getPortalPatientDeleted($owner, '', '0', "OR (p.deleted = 1 AND (p.owner = ?)) ");
296 function getMailDetails($id)
299 $result = getMailById($id);
300 if ($result['owner'] == $owner && $result['message_status'] == 'New') {
301 updatePortalMailMessageStatus($id, 'Read');
310 function sendMail($owner, $note, $title = 'Unassigned', $to, $noteid, $sid, $sn, $rid, $rn, $status = 'New', $replyid = '')
313 addPortalMailboxMail($owner, $note, '1', '1', $title, $to, '', $status, $noteid, $sid, $sn, $rid, $rn, $replyid);
320 function updateStatus($id, $status)
322 updatePortalMailMessageStatus($id, $status);