Various changes and fixes (#7424)
[openemr.git] / interface / main / calendar / modules / PostCalendar / pnuser.php
blobc79ae239c98bd89150a3dd0a44dd02c0e6a0c3bf
1 <?php
3 @define('__POSTCALENDAR__', 'PostCalendar');
4 /**
5 * $Id$
7 * PostCalendar::PostNuke Events Calendar Module
8 * Copyright (C) 2002 The PostCalendar Team
9 * http://postcalendar.tv
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * To read the license please read the docs/license.txt or visit
26 * http://www.gnu.org/copyleft/gpl.html
30 //=========================================================================
31 // Load the API Functions and Language defines
32 //=========================================================================
33 pnModAPILoad(__POSTCALENDAR__, 'user');
35 // Added to improve security and standardization of input data to be used in
36 // database insertion.
38 /**
39 * view items
40 * This is a standard function to provide an overview of all of the items
41 * available from the module.
43 function postcalendar_user_view()
45 // get the vars that were passed in
46 list($Date,
47 $print,
48 $viewtype,
49 $jumpday,
50 $jumpmonth,
51 $jumpyear) = pnVarCleanFromInput(
52 'Date',
53 'print',
54 'viewtype',
55 'jumpday',
56 'jumpmonth',
57 'jumpyear'
60 $Date = postcalendar_getDate();
62 if (!isset($viewtype)) {
63 $viewtype = _SETTING_DEFAULT_VIEW;
66 // added to allow the view & providers to remain as the user last saw it -- JRM
67 if ($_SESSION['viewtype']) {
68 $viewtype = $_SESSION['viewtype'];
71 if (!empty($_SESSION['pc_username'])) {
72 $pc_username = $_SESSION['pc_username'];
75 return postcalendar_user_display(array('viewtype' => $viewtype,'Date' => $Date,'print' => $print)) . postcalendar_footer();
78 /**
79 * display item
80 * This is a standard function to provide detailed information on a single item
81 * available from the module.
83 function postcalendar_user_display($args)
85 list($eid, $viewtype, $tplview,
86 $pc_username, $Date, $print, $category, $topic, $pc_facility) = pnVarCleanFromInput(
87 'eid',
88 'viewtype',
89 'tplview',
90 'pc_username',
91 'Date',
92 'print',
93 'pc_category',
94 'pc_topic',
95 'pc_facility'
97 // added to allow the view & providers to remain as the user last saw it -- JRM
98 if ($_SESSION['viewtype']) {
99 $viewtype = $_SESSION['viewtype'];
102 extract($args);
103 if (empty($Date) && empty($viewtype)) {
104 return false;
107 $cacheid = md5(strtotime("now"));
109 $out = "\n\n<!-- START user_display -->\n\n";
110 $out .= pnModAPIFunc('PostCalendar', 'user', 'buildView', array('Date' => $Date,
111 'viewtype' => $viewtype,
112 'cacheid' => $cacheid));
113 $out .= "\n\n<!-- END user_display -->\n\n";
115 // Return the output that has been generated by this function
116 return $out;
120 * search events
122 function postcalendar_user_search()
124 $tpl = new pcSmarty();
125 $k = isset($_REQUEST['pc_keywords']) ? pnVarCleanFromInput($_REQUEST['pc_keywords']) : '';
126 $k_andor = pnVarCleanFromInput('pc_keywords_andor');
127 $pc_category = pnVarCleanFromInput('pc_category');
128 $pc_facility = pnVarCleanFromInput('pc_facility');
129 $pc_topic = pnVarCleanFromInput('pc_topic');
130 $submit = pnVarCleanFromInput('submit');
131 $event_dur_hours = pnVarCleanFromInput('event_dur_hours');
132 $event_dur_minutes = pnVarCleanFromInput('event_dur_minutes');
133 $start = pnVarCleanFromInput('start');
134 $end = pnVarCleanFromInput('end');
136 // get list of categories for the user to choose from
137 $categories = postcalendar_userapi_getCategories();
138 $cat_options = '';
139 foreach ($categories as $category) {
140 $selected = "";
141 if ($pc_category == $category['id']) {
142 $selected = " SELECTED ";
145 //modified 8/09 by BM to allow translation if applicable
146 $cat_options .= "<option value=\"" . attr($category['id']) . "\" $selected>" . text(xl_appt_category($category['name'])) . "</option>";
149 $tpl->assign_by_ref('CATEGORY_OPTIONS', $cat_options);
151 $tpl->assign('event_dur_hours', $event_dur_hours);
152 $tpl->assign('event_dur_minutes', $event_dur_minutes);
154 // create default start and end dates for the search form
155 if (isset($start) && $start != "") {
156 $tpl->assign('DATE_START', $start);
157 } else {
158 $tpl->assign('DATE_START', date("m/d/Y"));
161 if (isset($end) && $end != "") {
162 $tpl->assign('DATE_END', $end);
163 } else {
164 $tpl->assign('DATE_END', date("m/d/Y", strtotime("+7 Days", time())));
167 // then override the setting if we have a value from the submitted form
168 $ProviderID = pnVarCleanFromInput("provider_id");
169 if (is_numeric($ProviderID)) {
170 $tpl->assign('ProviderID', $ProviderID);
171 } elseif ($ProviderID == "_ALL_") {
172 // do nothing
173 } else {
174 $tpl->assign('ProviderID', "");
177 $provinfo = getProviderInfo();
178 $tpl->assign('providers', $provinfo);
179 // build a list of provider-options for the select box on the input form -- JRM
180 $provider_options = "<option value='_ALL_' ";
181 if ($ProviderID == "_ALL_") {
182 $provider_options .= " SELECTED ";
185 $provider_options .= ">" . xlt('All Providers') . "</option>";
186 foreach ($provinfo as $provider) {
187 $selected = "";
188 // if we don't have a ProviderID chosen, pick the first one from the
189 // pc_username Session variable
190 if ($ProviderID == "") {
191 // that variable stores the 'username' and not the numeric 'id'
192 if ($_SESSION['pc_username'][0] == $provider['username']) {
193 $selected = " SELECTED ";
195 } elseif ($ProviderID == $provider['id']) {
196 $selected = " SELECTED ";
199 $provider_options .= "<option value=\"" . attr($provider['id']) . "\" " . $selected . ">";
200 $provider_options .= text($provider['lname']) . ", " . text($provider['fname']) . "</option>";
203 $tpl->assign_by_ref('PROVIDER_OPTIONS', $provider_options);
205 // build a list of facility options for the select box on the input form -- JRM
206 $facilities = getFacilities();
207 $fac_options = "<option value=''>" . xlt('All Facilities') . "</option>";
208 foreach ($facilities as $facility) {
209 $selected = "";
210 if ($facility['id'] == $pc_facility) {
211 $selected = " SELECTED ";
214 $fac_options .= "<option value=\"" . attr($facility['id']) . "\" " . $selected . ">";
215 $fac_options .= text($facility['name']) . "</option>";
218 $tpl->assign_by_ref('FACILITY_OPTIONS', $fac_options);
220 $PatientID = pnVarCleanFromInput("patient_id");
221 // limit the number of results returned by getPatientPID
222 // this helps to prevent the server from stalling on a request with
223 // no PID and thousands of PIDs in the database -- JRM
224 // the function getPatientPID($pid, $given, $orderby, $limit, $start) <-- defined in library/patient.inc.php
225 $plistlimit = 500;
226 if (is_numeric($PatientID)) {
227 $tpl->assign('PatientList', getPatientPID(array('pid' => $PatientID, 'limit' => $plistlimit)));
228 } else {
229 $tpl->assign('PatientList', getPatientPID(array('limit' => $plistlimit)));
232 $event_endday = pnVarCleanFromInput("event_endday");
233 $event_endmonth = pnVarCleanFromInput("event_endmonth");
234 $event_endyear = pnVarCleanFromInput("event_endyear");
236 $event_startday = pnVarCleanFromInput("event_startday");
237 $event_startmonth = pnVarCleanFromInput("event_startmonth");
238 $event_startyear = pnVarCleanFromInput("event_startyear");
239 if ($event_startday > $event_endday) {
240 $event_endday = $event_startday;
243 if ($event_startmonth > $event_endmonth) {
244 $event_endmonth = $event_startmonth;
247 if ($event_startyear > $event_endyear) {
248 $event_endyear = $event_startyear;
251 $tpl->assign('patient_id', $PatientID);
252 $tpl->assign('provider_id', $ProviderID);
253 $tpl->assign("event_category", pnVarCleanFromInput("event_category"));
254 $tpl->assign("event_subject", pnVarCleanFromInput("event_subject"));
255 $output = new pnHTML();
256 $output->SetOutputMode(_PNH_RETURNOUTPUT);
257 if (_SETTING_USE_INT_DATES) {
258 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildDaySelect', array('pc_day' => $day,'selected' => $event_startday));
259 $formdata = $output->FormSelectMultiple('event_startday', $sel_data);
260 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildMonthSelect', array('pc_month' => $month,'selected' => $event_startmonth));
261 $formdata .= $output->FormSelectMultiple('event_startmonth', $sel_data);
262 } else {
263 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildMonthSelect', array('pc_month' => ($month ?? null),'selected' => $event_startmonth));
264 $formdata = $output->FormSelectMultiple('event_startmonth', $sel_data);
265 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildDaySelect', array('pc_day' => ($day ?? null),'selected' => $event_startday));
266 $formdata .= $output->FormSelectMultiple('event_startday', $sel_data);
269 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildYearSelect', array('pc_year' => ($year ?? null),'selected' => $event_startyear));
270 $formdata .= $output->FormSelectMultiple('event_startyear', $sel_data);
271 $output->SetOutputMode(_PNH_KEEPOUTPUT);
272 $tpl->assign('SelectDateTimeStart', $formdata);
273 $output->SetOutputMode(_PNH_RETURNOUTPUT);
274 if (_SETTING_USE_INT_DATES) {
275 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildDaySelect', array('pc_day' => $day,'selected' => $event_endday));
276 $formdata = $output->FormSelectMultiple('event_endday', $sel_data);
277 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildMonthSelect', array('pc_month' => $month,'selected' => $event_endmonth));
278 $formdata .= $output->FormSelectMultiple('event_endmonth', $sel_data);
279 } else {
280 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildMonthSelect', array('pc_month' => ($month ?? null),'selected' => $event_endmonth));
281 $formdata = $output->FormSelectMultiple('event_endmonth', $sel_data);
282 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildDaySelect', array('pc_day' => ($day ?? null),'selected' => $event_endday ));
283 $formdata .= $output->FormSelectMultiple('event_endday', $sel_data);
286 $sel_data = pnModAPIFunc(__POSTCALENDAR__, 'user', 'buildYearSelect', array('pc_year' => ($year ?? null),'selected' => $event_endyear));
287 $formdata .= $output->FormSelectMultiple('event_endyear', $sel_data);
288 $output->SetOutputMode(_PNH_KEEPOUTPUT);
289 $tpl->assign('SelectDateTimeEnd', $formdata);
290 $output = null;
291 if (_SETTING_DISPLAY_TOPICS) {
292 $topics = postcalendar_userapi_getTopics();
293 $top_options = '';
294 foreach ($topics as $topic) {
295 $top_options .= "<option value=\"" . attr($topic['id']) . "\">" . text($topic['text']) . "</option>";
298 $tpl->assign_by_ref('TOPIC_OPTIONS', $top_options);
301 //=================================================================
302 // Find out what Template we're using
303 //=================================================================
304 $template_name = _SETTING_TEMPLATE;
305 if (!isset($template_name)) {
306 $template_name = 'default';
309 //=================================================================
310 // Output the search form
311 //=================================================================
312 $tpl->assign('FORM_ACTION', pnModURL(__POSTCALENDAR__, 'user', 'search'));
313 //=================================================================
314 // Perform the search if we have data
315 //=================================================================
316 if (!empty($submit) && strtolower($submit) == "find first") {
317 // not sure how we get here...
318 $searchargs = array();
319 $searchargs['start'] = pnVarCleanFromInput("event_startmonth") . "/" . pnVarCleanFromInput("event_startday") . "/" . pnVarCleanFromInput("event_startyear");
320 $searchargs['end'] = pnVarCleanFromInput("event_endmonth") . "/" . pnVarCleanFromInput("event_endday") . "/" . pnVarCleanFromInput("event_endyear");
321 $searchargs['provider_id'] = pnVarCleanFromInput("provider_id");
322 $searchargs['faFlag'] = true;
323 //print_r($searchargs);
324 //echo "<br />";
325 //set defaults to current week if empty
326 if ($searchargs['start'] == "//") {
327 $searchargs['start'] = date("m/d/Y");
330 if ($searchargs['end'] == "//") {
331 $searchargs['end'] = date("m/d/Y", strtotime("+7 Days", strtotime($searchargs['start'])));
334 //print_r($searchargs);
335 $eventsByDate =& postcalendar_userapi_pcGetEvents($searchargs);
336 //print_r($eventsByDate);
337 $found = findFirstAvailable($eventsByDate);
338 $tpl->assign('available_times', $found);
339 //print_r($_POST);
341 $tpl->assign('SEARCH_PERFORMED', true);
342 $tpl->assign('A_EVENTS', $eventsByDate);
345 if (!empty($submit) && strtolower($submit) == "listapps") {
346 // not sure how we get here...
347 $searchargs = array();
348 $searchargs['start'] = date("m/d/Y");
349 $searchargs['end'] = date("m/d/Y", strtotime("+1 year", strtotime($searchargs['start'])));
350 $searchargs['patient_id'] = pnVarCleanFromInput("patient_id");
351 $searchargs['listappsFlag'] = true;
353 $sqlKeywords .= "(a.pc_pid = '" . add_escape_custom(pnVarCleanFromInput("patient_id")) . "' )";
355 $searchargs['s_keywords'] = $sqlKeywords;
356 //print_r($searchargs);
357 $eventsByDate =& postcalendar_userapi_pcGetEvents($searchargs);
358 //print_r($eventsByDate);
359 $tpl->assign('appointments', $eventsByDate);
360 //print_r($_POST);
362 $tpl->assign('SEARCH_PERFORMED', true);
363 $tpl->assign('A_EVENTS', $eventsByDate);
364 } elseif (!empty($submit)) {
365 // we get here by searching via the PostCalendar search
366 $sqlKeywords = '';
367 $keywords = explode(' ', $k);
368 // build our search query
369 foreach ($keywords as $word) {
370 if (!empty($sqlKeywords)) {
371 $sqlKeywords .= " $k_andor ";
374 $sqlKeywords .= '(';
375 $sqlKeywords .= "pd.lname LIKE '%" . add_escape_custom($word) . "%' OR ";
376 $sqlKeywords .= "pd.fname LIKE '%" . add_escape_custom($word) . "%' OR ";
377 $sqlKeywords .= "u.lname LIKE '%" . add_escape_custom($word) . "%' OR ";
378 $sqlKeywords .= "u.fname LIKE '%" . add_escape_custom($word) . "%' OR ";
379 $sqlKeywords .= "a.pc_title LIKE '%" . add_escape_custom($word) . "%' OR ";
380 $sqlKeywords .= "a.pc_hometext LIKE '%" . add_escape_custom($word) . "%' OR ";
381 $sqlKeywords .= "a.pc_location LIKE '%" . add_escape_custom($word) . "%'";
382 $sqlKeywords .= ') ';
386 if (!empty($pc_category)) {
387 $s_category = "a.pc_catid = '" . add_escape_custom($pc_category) . "'";
390 if (!empty($pc_topic)) {
391 $s_topic = "a.pc_topic = '" . add_escape_custom($pc_topic) . "'";
394 $searchargs = array();
395 if (!empty($sqlKeywords)) {
396 $searchargs['s_keywords'] = $sqlKeywords;
399 if (!empty($s_category)) {
400 $searchargs['s_category'] = $s_category;
403 if (!empty($s_topic)) {
404 $searchargs['s_topic'] = $s_topic;
407 // some new search parameters introduced in the ajax_search form... JRM March 2008
409 // the ajax_search form has form parameters for 'start' and 'end' already built in
410 // so use them if available
411 $tmpDate = pnVarCleanFromInput("start");
412 if (isset($tmpDate) && $tmpDate != "") {
413 $searchargs['start'] = pnVarCleanFromInput("start");
414 } else {
415 $searchargs['start'] = "//";
418 $tmpDate = pnVarCleanFromInput("end");
419 if (isset($tmpDate) && $tmpDate != "") {
420 $searchargs['end'] = pnVarCleanFromInput("end");
421 } else {
422 $searchargs['end'] = "//";
425 // we can limit our search by provider -- JRM March 2008
426 if (isset($ProviderID) && $ProviderID != "") { // && $ProviderID != "_ALL_") {
427 $searchargs['provider_id'] = array();
428 array_push($searchargs['provider_id'], $ProviderID);
431 $eventsByDate =& postcalendar_userapi_pcGetEvents($searchargs);
433 // we can limit our search by facility -- JRM March 2008
434 if (isset($pc_facility) && $pc_facility != "") {
435 $searchargs['pc_facility'] = $pc_facility;
438 //print_r($eventsByDate);
439 $tpl->assign('SEARCH_PERFORMED', true);
440 $tpl->assign('A_EVENTS', $eventsByDate);
443 $tpl->caching = false;
444 $tpl->assign('STYLE', $GLOBALS['style']);
445 $pageSetup =& pnModAPIFunc(__POSTCALENDAR__, 'user', 'pageSetup');
446 $return = $pageSetup . $tpl->fetch($template_name . '/user/ajax_search.html');
447 return $return;