Fix some missing updates (#1373)
[openemr.git] / portal / lib / portal_mail.inc
blob46f49679bb913d716548b21475e846bc727a9ce9
1 <?php
2 /**
3  * This file contains functions for handling on-site portal mail.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * @package OpenEMR
19  * @author Derived from existing pnotes library
20  * @author Jerry Padgett <sjpadgett@gmail.com>
21  * @link http://www.open-emr.org
22  */
24 function addPortalMailboxMail(
25     $owner,
26     $newtext,
27     $authorized = '0',
28     $activity = '1',
29     $title = 'Unassigned',
30     $assigned_to = '',
31     $datetime = '',
32     $message_status = "New",
33     $master_note = '0',
34     $sid = '',
35     $sn = '',
36     $rid = '',
37     $rn = '',
38     $replyid = 0
39 ) {
41     if (empty($datetime)) {
42         $datetime = date('Y-m-d H:i:s');
43     }
45     $user = $_SESSION['portal_username'] ? $_SESSION['portal_username'] : $_SESSION['authUser'];
46     // make inactive if set as Done
47     if ($message_status == "Done") {
48         $activity = 0;
49     }
51     $body = $newtext;
52     if ($master_note == '0') {
53         $n = sqlQueryNoLog("SELECT MAX(id) as newid from onsite_mail");
54         $master_note = $n['newid']+1;
55     }
57     if ($replyid) {
58         if ($owner != $sid) {
59             $hold = $master_note;
60             $master_note = $replyid;
61             $replyid = $hold;
62         } else {
63             $replyid = $master_note;
64         }
65     } else if ($owner != $sid) {
66         $replyid = $master_note-1;
67     } else {
68         $replyid = $master_note;
69     }
71     return sqlInsert(
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)
75     );
78 function getPortalPatientDeleted($owner = '', $limit = '', $offset = 0, $search = '')
80     if ($limit) {
81         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
82     }
84     $sql = "
85         SELECT
86         p.id,
87         p.date,
88         p.owner,
89         p.user,
90         p.title,
91         p.body AS body,
92         p.message_status,
93         'Message' as `type`,
94         p.sender_id,
95         p.sender_name,
96         p.recipient_id,
97         p.recipient_name,
98         p.mail_chain,
99         p.reply_mail_chain
100         FROM
101         onsite_mail AS p
102         WHERE p.deleted != 0 AND p.owner = ? AND p.recipient_id = ?
103         $search
104         ORDER BY `date` asc
105         $limit
106         ";
107     $all = $row = array();
108     $data = array($owner,$owner);
109     if ($search) {
110         $data = array($owner,$owner,$owner);
111     }
113     $res = sqlStatement($sql, $data);
114     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
115         $all[$iter] = $row;
116     }
118     return $all;
121 function getPortalPatientNotes($owner = '', $limit = '', $offset = 0, $search = '')
123     if ($limit) {
124         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
125     }
127     $sql = "
128         SELECT
129         p.id,
130         p.date,
131         p.owner,
132         p.user,
133         p.title,
134         p.body AS body,
135         p.message_status,
136         'Message' as `type`,
137         p.sender_id,
138         p.sender_name,
139         p.recipient_id,
140         p.recipient_name,
141         p.mail_chain,
142         p.reply_mail_chain
143         FROM
144         onsite_mail AS p
145         WHERE p.deleted != 1 AND p.owner = ? AND p.recipient_id = ? 
146         $search
147         ORDER BY `date` asc
148         $limit
149         ";
150     $all = $row = array();
151     $data = array($owner,$owner);
152     if ($search) {
153         $data = array($owner,$owner,$owner);
154     }
156     $res = sqlStatement($sql, $data);
157     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
158         $all[$iter] = $row;
159     }
161     return $all;
164 function getPortalPatientNotifications($owner = '', $limit = '', $offset = 0, $search = '')
166     if ($limit) {
167         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
168     }
170     $sql = "
171         SELECT
172         pr.id,
173         date_created AS `date`,
174         'Patient Reminders' AS `user`,
175         due_status AS title,
176         CONCAT(lo.title, ':', lo2.title) AS body,
177         '' as message_status,
178         'Notification' as `type`
179         FROM
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'
187         WHERE pid = ?
188         AND active = 1
189         AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
190         $search
191         ORDER BY `date` asc
192         $limit
193         ";
194     $all = $row = array();
195     $res = sqlStatement($sql, array($owner));
196     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
197         $all[$iter] = $row;
198     }
200     return $all;
203 function getPortalPatientSentNotes($owner = '', $limit = '', $offset = 0, $search = '')
205     if ($limit) {
206         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
207     }
209     $sql = "
210         SELECT
211         p.id,
212         p.date,
213         p.assigned_to,
214         p.title,
215         p.body,
216         p.activity,
217         p.message_status,
218         'Message' as `type`,
219         p.mail_chain,
220         p.reply_mail_chain,
221         p.owner,
222         p.sender_id,
223         p.sender_name,
224         p.recipient_id,
225         p.recipient_name
226         FROM
227         onsite_mail AS p
228         WHERE p.sender_id = ?
229         AND p.deleted != 1
230         AND p.owner = ?
231         AND p.message_status != 'Done'
232         $search
233         ORDER BY `date` asc
234         $limit
235         ";
236     $all = $row = array();
237     $res = sqlStatement($sql, array($owner,$owner));
238     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
239         $all[$iter] = $row;
240     }
242     return $all;
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));
251     } else {
252         sqlStatement("update onsite_mail set message_status = ?, activity = '1' where id = ?", array($message_status, $id));
253     }
256 function getMails($owner, $dotype, $nsrch, $nfsrch)
258     if ($owner) {
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);
264             } else {
265                 $result_notes = getPortalPatientNotes($owner);
266                 $result_notifications = getPortalPatientNotifications($owner);
267                 $result = array_merge((array)$result_notes, (array)$result_notifications);
268                 //$result = $result_notes;
269             }
271             return $result;
272         } elseif ($dotype == "sent") {
273             if ($nsrch) {
274                 $result_sent_notes = getPortalPatientSentNotes($owner, '', '0', $nsrch);
275             } else {
276                 $result_sent_notes = getPortalPatientSentNotes($owner);
277             }
279             return $result_sent_notes;
280         } elseif ($dotype == "all") {
281             $result = array();
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);
285             return $result;
286         } elseif ($dotype == "deleted") {
287             $result = array();
288             $result = getPortalPatientDeleted($owner, '', '0', "OR (p.deleted = 1 AND (p.owner = ?)) ");
289             return $result;
290         }
291     } else {
292         return 'failed';
293     }
296 function getMailDetails($id)
298     if ($owner) {
299         $result = getMailById($id);
300         if ($result['owner'] == $owner && $result['message_status'] == 'New') {
301             updatePortalMailMessageStatus($id, 'Read');
302         }
304         return $result;
305     } else {
306         return 'failed';
307     }
310 function sendMail($owner, $note, $title = 'Unassigned', $to, $noteid, $sid, $sn, $rid, $rn, $status = 'New', $replyid = '')
312     if ($owner) {
313         addPortalMailboxMail($owner, $note, '1', '1', $title, $to, '', $status, $noteid, $sid, $sn, $rid, $rn, $replyid);
314         return 1;
315     } else {
316         return 'failed';
317     }
320 function updateStatus($id, $status)
322         updatePortalMailMessageStatus($id, $status);
323         return 1;