improved performance of event queries
[openemr.git] / interface / main / calendar / modules / PostCalendar / pnuserapi.php
blob7f0e6e0e0622e0eb771f2b2e0556aef829bc1178
1 <?php
2 @define('__POSTCALENDAR__','PostCalendar');
3 /**
4 * $Id$
6 * PostCalendar::PostNuke Events Calendar Module
7 * Copyright (C) 2002 The PostCalendar Team
8 * http://postcalendar.tv
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * To read the license please read the docs/license.txt or visit
25 * http://www.gnu.org/copyleft/gpl.html
29 //=========================================================================
30 // Require utility classes
31 //=========================================================================
32 $pcModInfo = pnModGetInfo(pnModGetIDFromName(__POSTCALENDAR__));
33 $pcDir = pnVarPrepForOS($pcModInfo['directory']);
34 require_once("modules/$pcDir/common.api.php");
35 unset($pcModInfo,$pcDir);
37 function postcalendar_userapi_getLongDayName($args)
39 extract($args); unset($args);
40 if(!isset($Date)) { return false; }
41 $pc_long_day = array(_CALLONGFIRSTDAY,
42 _CALLONGSECONDDAY,
43 _CALLONGTHIRDDAY,
44 _CALLONGFOURTHDAY,
45 _CALLONGFIFTHDAY,
46 _CALLONGSIXTHDAY,
47 _CALLONGSEVENTHDAY);
48 return $pc_long_day[Date("w",$Date)];
51 /**
52 * postcalendar_userapi_buildView
54 * Builds the month display
55 * @param string $Date mm/dd/yyyy format (we should use timestamps)
56 * @return string generated html output
57 * @access public
59 function postcalendar_userapi_buildView($args)
61 $print = pnVarCleanFromInput('print');
62 $show_days = pnVarCleanFromInput('show_days');
63 extract($args); unset($args);
64 $schedule_start =$GLOBALS[schedule_start];
65 $schedule_end = $GLOBALS[schedule_end];
69 //print "$schedule_start ,,, $schedule_end";
70 $times =array();
72 for($blocknum = ($schedule_start); $blocknum<= ($schedule_end);$blocknum++){
74 if($blocknum >= '12'){
75 $mer ='pm';
77 else{
78 $mer ='am';
80 $minute =array('00');
82 for($minutes =($GLOBALS['calendar_interval']); $minutes <= 60;$minutes +=$GLOBALS['calendar_interval']){
83 if($minutes <= '9'){
84 $under_ten ="0".$minutes;
85 array_push($minute,"$under_ten");
87 else if($minutes >='60') {
88 break;
90 else{
91 array_push($minute,"$minutes");
93 //print_r($minute);
96 foreach($minute as $m ){
97 //print "$blocknum :$m $mer<br />";
98 array_push($times,array("hour"=>$blocknum,"minute"=>$m,"mer"=>$mer));
107 //=================================================================
108 // get the module's information
109 //=================================================================
110 $modinfo = pnModGetInfo(pnModGetIDFromName(__POSTCALENDAR__));
111 $pcDir = $modinfo['directory'];
112 unset($modinfo);
113 //=================================================================
114 // grab the for post variable
115 //=================================================================
117 $pc_username = pnVarCleanFromInput('pc_username');
118 $category = pnVarCleanFromInput('pc_category');
119 $topic = pnVarCleanFromInput('pc_topic');
120 //=================================================================
121 // set the correct date
122 //=================================================================
123 $Date =& postcalendar_getDate();
124 //=================================================================
125 // get the current view
126 //=================================================================
127 if(!isset($viewtype)) { $viewtype = 'month'; }
128 //=================================================================
129 // Find out what Template we're using
130 //=================================================================
131 $template_name = _SETTING_TEMPLATE;
132 if(!isset($template_name)) { $template_name = 'default'; }
133 //=================================================================
134 // Find out what Template View to use
135 //=================================================================
136 $template_view = pnVarCleanFromInput('tplview');
137 if(!isset($template_view)) { $template_view = 'default'; }
138 //=================================================================
139 // See if the template view exists
140 //=================================================================
141 if(!file_exists("modules/$pcDir/pntemplates/$template_name/views/$viewtype/$template_view.html")) {
142 $template_view_load = 'default';
143 } else {
144 $template_view_load = pnVarPrepForOS($template_view);
146 //=================================================================
147 // Grab the current theme information
148 //=================================================================
149 pnThemeLoad(pnUserGetTheme());
150 global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $bgcolor5, $bgcolor6, $textcolor1, $textcolor2;
151 //=================================================================
152 // Insert necessary JavaScript into the page
153 //=================================================================
154 $output = pnModAPIFunc(__POSTCALENDAR__,'user','pageSetup');
155 //=================================================================
156 // Setup Smarty Template Engine
157 //=================================================================
158 $tpl =& new pcSmarty();
160 //if(!$tpl->is_cached("$template_name/views/$viewtype/$template_view_load.html",$cacheid)) {
161 //diable caching completely
162 if (true) {
163 //=================================================================
164 // Let's just finish setting things up
165 //=================================================================
166 $the_year = substr($Date,0,4);
167 $the_month = substr($Date,4,2);
168 $the_day = substr($Date,6,2);
169 $last_day = Date_Calc::daysInMonth($the_month,$the_year);
172 //=================================================================
173 // populate the template object with information for
174 // Month Names, Long Day Names and Short Day Names
175 // as translated in the language files
176 // (may be adding more here soon - based on need)
177 //=================================================================
178 $pc_month_names = array(_CALJAN,_CALFEB,_CALMAR,_CALAPR,_CALMAY,_CALJUN,
179 _CALJUL,_CALAUG,_CALSEP,_CALOCT,_CALNOV,_CALDEC);
181 $pc_short_day_names = array(_CALSUNDAYSHORT, _CALMONDAYSHORT,
182 _CALTUESDAYSHORT, _CALWEDNESDAYSHORT,
183 _CALTHURSDAYSHORT, _CALFRIDAYSHORT,
184 _CALSATURDAYSHORT);
186 $pc_long_day_names = array(_CALSUNDAY, _CALMONDAY,
187 _CALTUESDAY, _CALWEDNESDAY,
188 _CALTHURSDAY, _CALFRIDAY,
189 _CALSATURDAY);
190 //=================================================================
191 // here we need to set up some information for later
192 // variable creation. This helps us establish the correct
193 // date ranges for each view. There may be a better way
194 // to handle all this, but my brain hurts, so your comments
195 // are very appreciated and welcomed.
196 //=================================================================
197 switch (_SETTING_FIRST_DAY_WEEK)
199 case _IS_MONDAY:
200 $pc_array_pos = 1;
201 $first_day = date('w',mktime(0,0,0,$the_month,0,$the_year));
202 $week_day = date('w',mktime(0,0,0,$the_month,$the_day-1,$the_year));
203 $end_dow = date('w',mktime(0,0,0,$the_month,$last_day,$the_year));
204 if($end_dow != 0) {
205 $the_last_day = $last_day+(7-$end_dow);
206 } else {
207 $the_last_day = $last_day;
209 break;
210 case _IS_SATURDAY:
211 $pc_array_pos = 6;
212 $first_day = date('w',mktime(0,0,0,$the_month,2,$the_year));
213 $week_day = date('w',mktime(0,0,0,$the_month,$the_day+1,$the_year));
214 $end_dow = date('w',mktime(0,0,0,$the_month,$last_day,$the_year));
215 if($end_dow == 6) {
216 $the_last_day = $last_day+6;
217 } elseif($end_dow != 5) {
218 $the_last_day = $last_day+(5-$end_dow);
219 } else {
220 $the_last_day = $last_day;
222 break;
223 case _IS_SUNDAY:
224 default:
225 $pc_array_pos = 0;
226 $first_day = date('w',mktime(0,0,0,$the_month,1,$the_year));
227 $week_day = date('w',mktime(0,0,0,$the_month,$the_day,$the_year));
228 $end_dow = date('w',mktime(0,0,0,$the_month,$last_day,$the_year));
229 if($end_dow != 6) {
230 $the_last_day = $last_day+(6-$end_dow);
231 } else {
232 $the_last_day = $last_day;
234 break;
236 // assing the times array to the tpl the times array is for the days schedule
238 $tpl->assign_by_ref("times",$times);
239 // load the table width to the template
240 $tpl->assign("day_td_width",$GLOBALS['day_view_td_width']);
242 //=================================================================
243 // Week View is a bit of a pain in the ass, so we need to
244 // do some extra setup for that view. This section will
245 // find the correct starting and ending dates for a given
246 // seven day period, based on the day of the week the
247 // calendar is setup to run under (Sunday, Saturday, Monday)
248 //=================================================================
249 $first_day_of_week = sprintf('%02d',$the_day-$week_day);
250 $week_first_day = date('m/d/Y',mktime(0,0,0,$the_month,$first_day_of_week,$the_year));
251 list($week_first_day_month, $week_first_day_date, $week_first_day_year) = explode('/',$week_first_day);
252 $week_first_day_month_name = pnModAPIFunc(__POSTCALENDAR__, 'user', 'getmonthname',
253 array('Date'=>mktime(0,0,0,$week_first_day_month,$week_first_day_date,$week_first_day_year)));
254 $week_last_day = date('m/d/Y',mktime(0,0,0,$the_month,$first_day_of_week+6,$the_year));
255 list($week_last_day_month, $week_last_day_date, $week_last_day_year) = explode('/',$week_last_day);
256 $week_last_day_month_name = pnModAPIFunc(__POSTCALENDAR__, 'user', 'getmonthname',
257 array('Date'=>mktime(0,0,0,$week_last_day_month,$week_last_day_date,$week_last_day_year)));
259 //=================================================================
260 // Setup some information so we know the actual month's dates
261 // also get today's date for later use and highlighting
262 //=================================================================
263 $month_view_start = date('Y-m-d',mktime(0,0,0,$the_month,1,$the_year));
264 $month_view_end = date('Y-m-t',mktime(0,0,0,$the_month,1,$the_year));
265 $today_date = postcalendar_today('%Y-%m-%d');
266 //=================================================================
267 // Setup the starting and ending date ranges for pcGetEvents()
268 //=================================================================
269 switch($viewtype) {
270 case 'day' :
271 $starting_date = date('m/d/Y',mktime(0,0,0,$the_month,$the_day,$the_year));
272 $ending_date = date('m/d/Y',mktime(0,0,0,$the_month,$the_day,$the_year));
273 break;
274 case 'week' :
275 $starting_date = "$week_first_day_month/$week_first_day_date/$week_first_day_year";
276 $ending_date = "$week_last_day_month/$week_last_day_date/$week_last_day_year";
277 $calendarView = Date_Calc::getCalendarWeek($week_first_day_date,
278 $week_first_day_month,
279 $week_first_day_year,
280 '%Y-%m-%d');
281 break;
282 case 'month' :
283 $starting_date = date('m/d/Y',mktime(0,0,0,$the_month,1-$first_day,$the_year));
284 $ending_date = date('m/d/Y',mktime(0,0,0,$the_month,$the_last_day,$the_year));
285 $calendarView = Date_Calc::getCalendarMonth($the_month, $the_year, '%Y-%m-%d');
286 break;
287 case 'year' :
288 $starting_date = date('m/d/Y',mktime(0,0,0,1,1,$the_year));
289 $ending_date = date('m/d/Y',mktime(0,0,0,1,1,$the_year+1));
290 $calendarView = Date_Calc::getCalendarYear($the_year, '%Y-%m-%d');
291 break;
293 //=================================================================
294 // Load the events
295 //=================================================================
296 if($viewtype != 'year') {
297 $eventsByDate =& postcalendar_userapi_pcGetEvents(array('start'=>$starting_date,'end'=>$ending_date, 'viewtype' => $viewtype));
299 } else {
300 $eventsByDate = array();
304 //=================================================================
305 // Create and array with the day names in the correct order
306 //=================================================================
307 $daynames = array();
308 $numDays = count($pc_long_day_names);
309 for($i=0; $i < $numDays; $i++)
310 { if($pc_array_pos >= $numDays) {
311 $pc_array_pos = 0;
313 array_push($daynames,$pc_long_day_names[$pc_array_pos]);
314 $pc_array_pos++;
316 unset($numDays);
317 $sdaynames = array();
318 $numDays = count($pc_short_day_names);
319 for($i=0; $i < $numDays; $i++)
320 { if($pc_array_pos >= $numDays) {
321 $pc_array_pos = 0;
323 array_push($sdaynames,$pc_short_day_names[$pc_array_pos]);
324 $pc_array_pos++;
326 unset($numDays);
327 //=================================================================
328 // Prepare some values for the template
329 //=================================================================
330 $prev_month = Date_Calc::beginOfPrevMonth(1,$the_month,$the_year,'%Y%m%d');
331 $next_month = Date_Calc::beginOfNextMonth(1,$the_month,$the_year,'%Y%m%d');
333 $pc_prev = pnModURL(__POSTCALENDAR__,'user','view',
334 array('tplview'=>$template_view,
335 'viewtype'=>'month',
336 'Date'=>$prev_month,
337 'pc_username'=>$pc_username,
338 'pc_category'=>$category,
339 'pc_topic'=>$topic));
341 $pc_next = pnModURL(__POSTCALENDAR__,'user','view',
342 array('tplview'=>$template_view,
343 'viewtype'=>'month',
344 'Date'=>$next_month,
345 'pc_username'=>$pc_username,
346 'pc_category'=>$category,
347 'pc_topic'=>$topic));
349 $prev_day = Date_Calc::prevDay($the_day,$the_month,$the_year,'%Y%m%d');
350 $next_day = Date_Calc::nextDay($the_day,$the_month,$the_year,'%Y%m%d');
351 $pc_prev_day = pnModURL(__POSTCALENDAR__,'user','view',
352 array('tplview'=>$template_view,
353 'viewtype'=>'day',
354 'Date'=>$prev_day,
355 'pc_username'=>$pc_username,
356 'pc_category'=>$category,
357 'pc_topic'=>$topic));
359 $pc_next_day = pnModURL(__POSTCALENDAR__,'user','view',
360 array('tplview'=>$template_view,
361 'viewtype'=>'day',
362 'Date'=>$next_day,
363 'pc_username'=>$pc_username,
364 'pc_category'=>$category,
365 'pc_topic'=>$topic));
367 $prev_week = date('Ymd',mktime(0,0,0,$week_first_day_month,$week_first_day_date-7,$week_first_day_year));
368 $next_week = date('Ymd',mktime(0,0,0,$week_last_day_month,$week_last_day_date+1,$week_last_day_year));
369 $pc_prev_week = pnModURL(__POSTCALENDAR__,'user','view',
370 array('viewtype'=>'week',
371 'Date'=>$prev_week,
372 'pc_username'=>$pc_username,
373 'pc_category'=>$category,
374 'pc_topic'=>$topic));
375 $pc_next_week = pnModURL(__POSTCALENDAR__,'user','view',
376 array('viewtype'=>'week',
377 'Date'=>$next_week,
378 'pc_username'=>$pc_username,
379 'pc_category'=>$category,
380 'pc_topic'=>$topic));
382 $prev_year = date('Ymd',mktime(0,0,0,1,1,$the_year-1));
383 $next_year = date('Ymd',mktime(0,0,0,1,1,$the_year+1));
384 $pc_prev_year = pnModURL(__POSTCALENDAR__,'user','view',
385 array('viewtype'=>'year',
386 'Date'=>$prev_year,
387 'pc_username'=>$pc_username,
388 'pc_category'=>$category,
389 'pc_topic'=>$topic));
390 $pc_next_year = pnModURL(__POSTCALENDAR__,'user','view',
391 array('viewtype'=>'year',
392 'Date'=>$next_year,
393 'pc_username'=>$pc_username,
394 'pc_category'=>$category,
395 'pc_topic'=>$topic));
397 //=================================================================
398 // Populate the template
399 //=================================================================
400 $all_categories = pnModAPIFunc(__POSTCALENDAR__,'user','getCategories');
404 if(isset($calendarView)) {
405 $tpl->assign_by_ref('CAL_FORMAT',$calendarView);
409 if($viewtype == "week") {
410 $last_blocks = array();
411 foreach($eventsByDate as $cdate => $day) {
412 $tblock = array_reverse($day['blocks']);
413 $last_blocks[$cdate] = count($tblock) -1;
414 for ($i=0;$i<count($tblock);$i++) {
415 if (!empty($tblock[$i])) {
416 $last_blocks[$cdate] = count($tblock) - $i;
417 break;
421 $tpl->assign("last_blocks",$last_blocks);
424 $tpl->assign('STYLE',$GLOBALS['style']);
425 $tpl->assign('show_days',$show_days);
426 $provinfo = getProviderInfo();
427 $single = array();
429 //filter the display on the requested username, the provinfo array is used to build columns in the week view.
431 foreach($provinfo as $provider) {
432 if(is_array($pc_username)){
433 foreach($pc_username as $uname){
434 if (!empty($pc_username) && $provider['username'] == $uname) {
436 array_push($single,$provider);
441 else{
442 if (!empty($pc_username) && $provider['username'] == $pc_username) {
443 array_push($single,$provider);
447 if ($single != null) {
448 $provinfo = $single;
451 //$provinfo[count($provinfo) +1] = array("id" => "","lname" => "Other");
452 $tpl->assign_by_ref('providers', $provinfo);
454 if (pnVarCleanFromInput("show_days") != 1) {
455 $tpl->assign('showdaysurl',"index.php?" . $_SERVER['QUERY_STRING'] . "&show_days=1");
459 $tpl->assign('interval', $GLOBALS['calendar_interval']);
460 $tpl->assign_by_ref('VIEW_TYPE',$viewtype);
461 $tpl->assign_by_ref('A_MONTH_NAMES',$pc_month_names);
462 $tpl->assign_by_ref('A_LONG_DAY_NAMES',$pc_long_day_names);
463 $tpl->assign_by_ref('A_SHORT_DAY_NAMES',$pc_short_day_names);
464 $tpl->assign_by_ref('S_LONG_DAY_NAMES',$daynames);
465 $tpl->assign_by_ref('S_SHORT_DAY_NAMES',$sdaynames);
466 $tpl->assign_by_ref('A_EVENTS',$eventsByDate);
467 $tpl->assign_by_ref('A_CATEGORY',$all_categories);
468 $tpl->assign_by_ref('PREV_MONTH_URL',$pc_prev);
469 $tpl->assign_by_ref('NEXT_MONTH_URL',$pc_next);
470 $tpl->assign_by_ref('PREV_DAY_URL',$pc_prev_day);
471 $tpl->assign_by_ref('NEXT_DAY_URL',$pc_next_day);
472 $tpl->assign_by_ref('PREV_WEEK_URL',$pc_prev_week);
473 $tpl->assign_by_ref('NEXT_WEEK_URL',$pc_next_week);
474 $tpl->assign_by_ref('PREV_YEAR_URL',$pc_prev_year);
475 $tpl->assign_by_ref('NEXT_YEAR_URL',$pc_next_year);
476 $tpl->assign_by_ref('MONTH_START_DATE',$month_view_start);
477 $tpl->assign_by_ref('MONTH_END_DATE',$month_view_end);
478 $tpl->assign_by_ref('TODAY_DATE',$today_date);
479 $tpl->assign_by_ref('DATE',$Date);
480 $tpl->assign_by_ref('SCHEDULE_BASE_URL', pnModURL(__POSTCALENDAR__,'user','submit'));
481 $tpl->assign_by_ref('interval',$intervals);
483 //=================================================================
484 // Parse the template
485 //=================================================================
486 $template = "$template_name/views/$viewtype/$template_view_load.html";
487 if(!$print) {
488 $output .= "\n\n<!-- START POSTCALENDAR OUTPUT [-: HTTP://POSTCALENDAR.TV :-] -->\n\n";
489 $output .= $tpl->fetch($template,$cacheid); // cache id
490 $output .= "\n\n<!-- END POSTCALENDAR OUTPUT [-: HTTP://POSTCALENDAR.TV :-] -->\n\n";
491 } else {
492 $theme = pnUserGetTheme();
493 echo "<html><head>";
494 echo "<LINK REL=\"StyleSheet\" HREF=\"themes/$theme/style/styleNN.css\" TYPE=\"text/css\">\n\n\n";
495 echo "<style type=\"text/css\">\n";
496 echo "@import url(\"themes/$theme/style/style.css\"); ";
497 echo "</style>\n";
498 echo "</head><body>\n";
499 echo $output;
500 $tpl->display($template,$cacheid);
501 echo postcalendar_footer();
502 echo "\n</body></html>";
503 session_write_close();
504 exit;
506 //=================================================================
507 // Return the output
508 //=================================================================
509 return $output;
513 * postcalendar_userapi_eventPreview
514 * Creates the detailed event display and outputs html.
515 * Accepts an array of key/value pairs
516 * @param array $event array of event details from the form
517 * @return string html output
518 * @access public
520 function postcalendar_userapi_eventPreview($args)
522 // get the theme globals :: is there a better way to do this?
523 pnThemeLoad(pnUserGetTheme());
524 global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $bgcolor5;
525 global $textcolor1, $textcolor2;
527 extract($args); unset($args);
528 $uid = pnUserGetVar('uid');
529 //=================================================================
530 // Setup Smarty Template Engine
531 //=================================================================
532 $tpl =& new pcSmarty();
533 $tpl->caching = false;
534 // add preceding zeros
535 $event_starttimeh = sprintf('%02d',$event_starttimeh);
536 $event_starttimem = sprintf('%02d',$event_starttimem);
537 $event_startday = sprintf('%02d',$event_startday);
538 $event_startmonth = sprintf('%02d',$event_startmonth);
539 $event_endday = sprintf('%02d',$event_endday);
540 $event_endmonth = sprintf('%02d',$event_endmonth);
542 if(!(bool)_SETTING_TIME_24HOUR) {
543 if($event_startampm == _PM_VAL) {
544 if($event_starttimeh != 12) {
545 $event_starttimeh+=12;
547 } elseif($event_startampm == _AM_VAL) {
548 if($event_starttimeh == 12) {
549 $event_starttimeh = 00;
554 $event_startampm." - ";
555 $startTime = $event_starttimeh.':'.$event_starttimem.' ';
557 $event = array();
558 $event['eid'] = '';
559 $event['uname'] = $uname;
560 $event['catid'] = $event_category;
561 if($pc_html_or_text == 'html') {
562 $prepFunction = 'pcVarPrepHTMLDisplay';
563 } else {
564 $prepFunction = 'pcVarPrepForDisplay';
566 $event['title'] = $prepFunction($event_subject);
567 $event['hometext'] = $prepFunction($event_desc);
568 $event['desc'] = $event['hometext'];
569 $event['date'] = $event_startyear.$event_startmonth.$event_startday;
570 $event['duration'] = $event_duration;
571 $event['duration_hours'] = $event_dur_hours;
572 $event['duration_minutes'] = $event_dur_minutes;
573 $event['endDate'] = $event_endyear.'-'.$event_endmonth.'-'.$event_endday;
574 $event['startTime'] = $startTime;
575 $event['recurrtype'] = '';
576 $event['recurrfreq'] = '';
577 $event['recurrspec'] = $event_recurrspec;
578 $event['topic'] = $event_topic;
579 $event['alldayevent'] = $event_allday;
580 $event['conttel'] = $prepFunction($event_conttel);
581 $event['contname'] = $prepFunction($event_contname);
582 $event['contemail'] = $prepFunction($event_contemail);
583 $event['website'] = $prepFunction(postcalendar_makeValidURL($event_website));
584 $event['fee'] = $prepFunction($event_fee);
585 $event['location'] = $prepFunction($event_location);
586 $event['street1'] = $prepFunction($event_street1);
587 $event['street2'] = $prepFunction($event_street2);
588 $event['city'] = $prepFunction($event_city);
589 $event['state'] = $prepFunction($event_state);
590 $event['postal'] = $prepFunction($event_postal);
592 //=================================================================
593 // get event's topic information
594 //=================================================================
595 if(_SETTING_DISPLAY_TOPICS) {
596 list($dbconn) = pnDBGetConn();
597 $pntable = pnDBGetTables();
598 $topics_table = $pntable['topics'];
599 $topics_column = $pntable['topics_column'];
600 $topicsql = "SELECT $topics_column[topictext],$topics_column[topicimage]
601 FROM $topics_table
602 WHERE $topics_column[topicid] = $event[topic]
603 LIMIT 1";
604 $topic_result = $dbconn->Execute($topicsql);
605 list($event['topictext'],$event['topicimg']) = $topic_result->fields;
606 $topic_result->Close();
607 } else {
608 $event['topictext'] = $event['topicimg'] = '';
610 //=================================================================
611 // Find out what Template we're using
612 //=================================================================
613 $template_name = _SETTING_TEMPLATE;
614 if(!isset($template_name)) {
615 $template_name = 'default';
617 //=================================================================
618 // populate the template
619 //=================================================================
620 if(!empty($event['location']) || !empty($event['street1']) ||
621 !empty($event['street2']) || !empty($event['city']) ||
622 !empty($event['state']) || !empty($event['postal'])) {
623 $tpl->assign('LOCATION_INFO',true);
624 } else {
625 $tpl->assign('LOCATION_INFO',false);
627 if(!empty($event['contname']) || !empty($event['contemail']) ||
628 !empty($event['conttel']) || !empty($event['website'])) {
629 $tpl->assign('CONTACT_INFO',true);
630 } else {
631 $tpl->assign('CONTACT_INFO',false);
633 $tpl->assign_by_ref('A_EVENT',$event);
634 $tpl->assign('STYLE',$GLOBALS['style']);
635 //=================================================================
636 // Parse the template
637 //=================================================================
638 $output = "\n\n<!-- POSTCALENDAR HTTP://WWW.BAHRAINI.TV -->\n\n";
639 $output .= "\n\n<!-- POSTCALENDAR TEMPLATE START -->\n\n";
640 $output .= $tpl->fetch($template_name.'/user/preview.html');
641 $output .= "\n\n<!-- POSTCALENDAR TEMPLATE END -->\n\n";
643 return $output;
647 * checkEventCollision
648 * Returns an array containing any events that collide with the specified event
649 * @params array(key=>value)
650 * @return array $events[][]
652 function checkEventCollision($edata) {
654 extract($edata);
655 $sdate = ($event_startmonth.'/'.$event_startday.'/'.$event_startyear);
656 $edate = $sdate;
657 //hour from forms is 12 not 24 format, convert here
658 if($event_startampm == 2 && $event_starttimeh != 12)
659 $event_starttimeh += 12;
660 elseif ($event_startampm == 1 && $event_starttimeh == 12)
661 $event_starttimeh -= 12;
663 $stime = date("H:i:00",strtotime($event_starttimeh.':'.$event_starttimem.':00'));
664 $etime = date("H:i:00",$event_duration + strtotime($stime));
665 //echo "stime is: $stime, etime is: $etime sdate is: $sdate edate is: $edate<br />";
666 $a = array('collideFlag' => true,'start'=>$edate,'end'=>$sdate, 'provider_id' => $event_userid, 'stime' => $stime, 'etime' => $etime);
667 $eventsByDate =& postcalendar_userapi_pcGetEvents($a);
668 //print_r($eventsByDate);
669 $collisions = array();
670 foreach ($eventsByDate as $day) {
671 foreach ($day as $event) {
672 if ($event['duration'] == 0 && $event['alldayevent'] == 0) {
673 continue;
675 elseif($event['alldayevent'] == 1) {
676 $collisions[] = $event;
678 $festart = strtotime($event['startTime']);
679 $feend = strtotime($event['startTime'] + $event['duration']);
680 $estart = strtotime($stime);
681 $eend = strtotime($etime);
683 //echo "festart = $festart feend = $feend estart = $estart eend = $eend<br />";
684 if ($festart < $eend && $feend > $estart) {
685 $collisions[] = $event;
687 elseif ($festart <= $estart && $feend <= $eend && $feend >= $estart) {
688 $collisions[] = $event;
690 elseif ($festart >= $estart && $festart < $eend) {
691 $collisions[] = $event;
695 //foreach ($collisions as $collide) {
696 // echo "collided: " . $collide['title'] . "<br />";
698 return $collisions;
702 * postcalendar_userapi_pcQueryEventsFA
703 * Returns an array containing the event's information for first available queiries
704 * @params array(key=>value)
705 * @params string key eventstatus
706 * @params int value -1 == hidden ; 0 == queued ; 1 == approved
707 * @return array $events[][]
709 function &postcalendar_userapi_pcQueryEventsFA($args) {
711 $end = '0000-00-00';
712 extract($args);
713 $eventstatus = 1;
714 if (is_numeric($event_status))
715 $eventstatus = $event_status;
718 if(!isset($start)) { $start = Date_Calc::dateNow('%Y-%m-%d'); }
719 list($sy,$sm,$sd) = explode('-',$start);
721 list($dbconn) = pnDBGetConn();
722 $pntable = pnDBGetTables();
723 // link to the events tables
724 $table = $pntable['postcalendar_events'];
725 $cattable = $pntable['postcalendar_categories'];
727 $sql = "SELECT DISTINCT a.pc_eid, a.pc_informant, a.pc_catid, a.pc_title, a.pc_time, a.pc_hometext,
728 a.pc_eventDate, a.pc_duration, a.pc_endDate, a.pc_startTime, a.pc_recurrtype,
729 a.pc_recurrfreq, a.pc_recurrspec, a.pc_topic, a.pc_alldayevent, a.pc_location,
730 a.pc_conttel, a.pc_contname, a.pc_contemail, a.pc_website, a.pc_fee,
731 a.pc_sharing, b.pc_catcolor, b.pc_catname, b.pc_catdesc, a.pc_pid, a.pc_aid,
732 concat(u.fname,' ',u.lname) as provider_name, concat(pd.fname,' ',pd.lname) as patient_name,
733 concat(u2.fname, ' ', u2.lname) as owner_name, DOB as patient_dob
734 FROM $table AS a, $cattable AS b
735 LEFT JOIN users as u ON a.pc_aid = u.id
736 LEFT JOIN users as u2 ON a.pc_aid = u2.id
737 LEFT JOIN patient_data as pd ON a.pc_pid=pd.pid
738 WHERE b.pc_catid = a.pc_catid
739 AND a.pc_eventstatus = $eventstatus
740 AND (a.pc_endDate >= '$start' OR a.pc_endDate = '0000-00-00')
741 AND a.pc_eventDate <= '$end'
742 AND (a.pc_aid = '" . $provider_id . "' OR a.pc_aid = '')";
744 //======================================================================
745 // START SEARCH FUNCTIONALITY
746 //======================================================================
747 if(!empty($s_keywords)) $sql .= "AND ($s_keywords) ";
748 if(!empty($s_category)) $sql .= "AND ($s_category) ";
749 if(!empty($s_topic)) $sql .= "AND ($s_topic) ";
750 if(!empty($collide_etime) && !empty($collide_stime)) {
751 $sql .= "AND NOT ((pc_endTime <= '$collide_stime') OR (pc_startTime >= '$collide_etime')) AND pc_endTime IS NOT NULL ";
753 if(!empty($category)) $sql .= "AND (a.pc_catid = '".pnVarPrepForStore($category)."') ";
754 if(!empty($topic)) $sql .= "AND (a.pc_topic = '".pnVarPrepForStore($topic)."') ";
755 //======================================================================
756 // Serch sort and limitation
757 //======================================================================
758 if(empty($sort)) $sql .= "GROUP BY a.pc_eid ORDER BY a.pc_startTime ASC";
759 else $sql .= "GROUP BY a.pc_eid ORDER BY a.$sort";
760 //======================================================================
761 // END SEARCH FUNCTIONALITY
762 //======================================================================
763 //echo "<Br />sql: $sql<br />";
764 $result = $dbconn->Execute($sql);
765 if($dbconn->ErrorNo() != 0) die ($dbconn->ErrorMsg());
767 // put the information into an array for easy access
768 $events = array();
769 // return an empty array if we don't have any results
770 if(!isset($result)) { return $events; }
773 for($i=0; !$result->EOF; $result->MoveNext()) {
775 // get the results from the query
776 if(isset($tmp)) { unset($tmp); } $tmp = array();
777 list($tmp['eid'], $tmp['uname'], $tmp['catid'],
778 $tmp['title'], $tmp['time'], $tmp['hometext'],
779 $tmp['eventDate'], $tmp['duration'], $tmp['endDate'],
780 $tmp['startTime'], $tmp['recurrtype'], $tmp['recurrfreq'],
781 $tmp['recurrspec'], $tmp['topic'], $tmp['alldayevent'],
782 $tmp['location'], $tmp['conttel'], $tmp['contname'],
783 $tmp['contemail'], $tmp['website'], $tmp['fee'], $tmp['sharing'],
784 $tmp['catcolor'], $tmp['catname'], $tmp['catdesc'],
785 $tmp['pid'], $tmp['aid'], $tmp['provider_name'], $tmp['patient_name'],
786 $tmp['owner_name'],$tmp['patient_dob']) = $result->fields;
788 // grab the name of the topic
789 $topicname = pcGetTopicName($tmp['topic']);
790 // get the user id of event's author
791 $cuserid = @$nuke_users[strtolower($tmp['uname'])];
792 // check the current event's permissions
793 // the user does not have permission to view this event
794 // if any of the following evaluate as false
795 if(!pnSecAuthAction(0, 'PostCalendar::Event', "$tmp[title]::$tmp[eid]", ACCESS_OVERVIEW)) {
796 continue;
797 } elseif(!pnSecAuthAction(0, 'PostCalendar::Category', "$tmp[catname]::$tmp[catid]", ACCESS_OVERVIEW)) {
798 continue;
799 } elseif(!pnSecAuthAction(0, 'PostCalendar::User', "$tmp[uname]::$cuserid", ACCESS_OVERVIEW)) {
800 continue;
801 } elseif(!pnSecAuthAction(0, 'PostCalendar::Topic', "$topicname::$tmp[topic]", ACCESS_OVERVIEW)) {
802 continue;
803 } elseif($tmp['sharing'] == SHARING_PRIVATE && $cuserid != $userid) {
804 continue;
807 // add event to the array if we passed the permissions check
808 // this is the common information
809 $events[$i]['eid'] = $tmp['eid'];
810 $events[$i]['uname'] = $tmp['uname'];
811 $events[$i]['uid'] = $cuserid;
812 $events[$i]['catid'] = $tmp['catid'];
813 $events[$i]['time'] = $tmp['time'];
814 $events[$i]['eventDate'] = $tmp['eventDate'];
815 $events[$i]['duration'] = $tmp['duration'];
816 // there has to be a more intelligent way to do this
817 @list($events[$i]['duration_hours'],$dmin) = @explode('.',($tmp['duration']/60/60));
818 $events[$i]['duration_minutes'] = substr(sprintf('%.2f','.' . 60*($dmin/100)),2,2);
819 //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
820 $events[$i]['endDate'] = $tmp['endDate'];
821 $events[$i]['startTime'] = $tmp['startTime'];
822 $events[$i]['recurrtype'] = $tmp['recurrtype'];
823 $events[$i]['recurrfreq'] = $tmp['recurrfreq'];
824 $events[$i]['recurrspec'] = $tmp['recurrspec'];
827 $rspecs = unserialize($tmp['recurrspec']);
828 $events[$i]['event_repeat_freq'] = $rspecs['event_repeat_freq'];
829 $events[$i]['event_repeat_freq_type'] = $rspecs['event_repeat_freq_type'];
830 $events[$i]['event_repeat_on_num'] = $rspecs['event_repeat_on_num'];
831 $events[$i]['event_repeat_on_day'] = $rspecs['event_repeat_on_day'];
832 $events[$i]['event_repeat_on_freq'] = $rspecs['event_repeat_on_freq'];
834 $events[$i]['topic'] = $tmp['topic'];
835 $events[$i]['alldayevent'] = $tmp['alldayevent'];
836 $events[$i]['catcolor'] = $tmp['catcolor'];
837 $events[$i]['catname'] = $tmp['catname'];
838 $events[$i]['catdesc'] = $tmp['catdesc'];
839 $events[$i]['pid'] = $tmp['pid'];
840 $events[$i]['patient_name']= $tmp['patient_name'];
841 $events[$i]['provider_name'] = $tmp['provider_name'];
842 $events[$i]['owner_name'] = $tmp['owner_name'];
843 $events[$i]['patient_dob'] = $tmp['patient_dob'];
844 $events[$i]['patient_age'] = date("Y") - substr(($tmp['patient_dob']),0,4);
845 $events[$i]['sharing'] = $tmp['sharing'];
846 $events[$i]['aid'] = $tmp['aid'];
847 $events[$i]['intervals'] = ceil(($tmp['duration']/60) / $GLOBALS['calendar_interval']);
848 if($events[$i]['intervals'] == 0)
849 $events[$i]['intervals'] = 1;
850 // is this a public event to be shown as busy?
851 if($tmp['sharing'] == SHARING_BUSY && $cuserid != $userid) {
852 // make it not display any information
853 $events[$i]['title'] = _USER_BUSY_TITLE;
854 $events[$i]['hometext'] = _USER_BUSY_MESSAGE;
855 $events[$i]['desc'] = _USER_BUSY_MESSAGE;
856 $events[$i]['conttel'] = '';
857 $events[$i]['contname'] = '';
858 $events[$i]['contemail'] = '';
859 $events[$i]['website'] = '';
860 $events[$i]['fee'] = '';
861 $events[$i]['location'] = '';
862 $events[$i]['street1'] = '';
863 $events[$i]['street2'] = '';
864 $events[$i]['city'] = '';
865 $events[$i]['state'] = '';
866 $events[$i]['postal'] = '';
867 } else {
868 $display_type = substr($tmp['hometext'],0,6);
869 if($display_type == ':text:') {
870 $prepFunction = 'pcVarPrepForDisplay';
871 $tmp['hometext'] = substr($tmp['hometext'],6);
872 } elseif($display_type == ':html:') {
873 $prepFunction = 'pcVarPrepHTMLDisplay';
874 $tmp['hometext'] = substr($tmp['hometext'],6);
875 } else {
876 $prepFunction = 'pcVarPrepHTMLDisplay';
878 unset($display_type);
879 $events[$i]['title'] = $prepFunction($tmp['title']);
880 $events[$i]['hometext'] = $prepFunction($tmp['hometext']);
881 $events[$i]['desc'] = $events[$i]['hometext'];
882 $events[$i]['conttel'] = $prepFunction($tmp['conttel']);
883 $events[$i]['contname'] = $prepFunction($tmp['contname']);
884 $events[$i]['contemail'] = $prepFunction($tmp['contemail']);
885 $events[$i]['website'] = $prepFunction(postcalendar_makeValidURL($tmp['website']));
886 $events[$i]['fee'] = $prepFunction($tmp['fee']);
887 $loc = unserialize($tmp['location']);
888 $events[$i]['location'] = $prepFunction($loc['event_location']);
889 $events[$i]['street1'] = $prepFunction($loc['event_street1']);
890 $events[$i]['street2'] = $prepFunction($loc['event_street2']);
891 $events[$i]['city'] = $prepFunction($loc['event_city']);
892 $events[$i]['state'] = $prepFunction($loc['event_state']);
893 $events[$i]['postal'] = $prepFunction($loc['event_postal']);
895 $i++;
897 unset($tmp);
898 $result->Close();
899 return $events;
906 * postcalendar_userapi_pcQueryEvents
907 * Returns an array containing the event's information
908 * @params array(key=>value)
909 * @params string key eventstatus
910 * @params int value -1 == hidden ; 0 == queued ; 1 == approved
911 * @return array $events[][]
913 function &postcalendar_userapi_pcQueryEvents($args)
916 $end = '0000-00-00';
917 extract($args);
920 $pc_username = pnVarCleanFromInput('pc_username');
921 if (empty($pc_username )) {
922 $pc_username = "__PC_ALL__";
925 //pennfirm echo "PC Username: $pc_username";
926 $topic = pnVarCleanFromInput('pc_topic');
927 $category = pnVarCleanFromInput('pc_category');
930 if(!empty($pc_username) && (strtolower($pc_username) != 'anonymous')) {
931 if($pc_username=='__PC_ALL__' || $pc_username == -1) {
932 $ruserid = -1;
933 } else {
934 $ruserid = getIDfromUser($pc_username);
938 if(!isset($eventstatus)) { $eventstatus = 1; }
939 // sanity check on eventstatus
940 if((int)$eventstatus < -1 || (int)$eventstatus > 1) { $eventstatus = 1; }
941 if(!isset($start)) { $start = Date_Calc::dateNow('%Y-%m-%d'); }
942 list($sy,$sm,$sd) = explode('-',$start);
944 list($dbconn) = pnDBGetConn();
945 $pntable = pnDBGetTables();
946 // link to the events tables
947 $table = $pntable['postcalendar_events'];
948 $cattable = $pntable['postcalendar_categories'];
949 $topictable = $pntable['postcalendar_topics'];
951 $sql = "SELECT DISTINCT a.pc_eid, a.pc_informant, a.pc_catid, a.pc_title, a.pc_time, a.pc_hometext,
952 a.pc_eventDate, a.pc_duration, a.pc_endDate, a.pc_startTime, a.pc_recurrtype,
953 a.pc_recurrfreq, a.pc_recurrspec, a.pc_topic, a.pc_alldayevent, a.pc_location,
954 a.pc_conttel, a.pc_contname, a.pc_contemail, a.pc_website, a.pc_fee,
955 a.pc_sharing, b.pc_catcolor, b.pc_catname, b.pc_catdesc, a.pc_pid, a.pc_aid,
956 concat(u.fname,' ',u.lname) as provider_name, concat(pd.lname,', ',pd.fname) as patient_name,
957 concat(u2.fname, ' ', u2.lname) as owner_name, DOB as patient_dob, pd.pubpid
958 FROM $table AS a, $cattable AS b
959 LEFT JOIN users as u ON a.pc_aid = u.id
960 LEFT JOIN users as u2 ON a.pc_aid = u2.id
961 LEFT JOIN patient_data as pd ON a.pc_pid=pd.pid
962 WHERE b.pc_catid = a.pc_catid
963 AND a.pc_eventstatus = $eventstatus
964 AND ((a.pc_endDate >= '$start' AND a.pc_eventDate <= '$end') OR
965 (a.pc_endDate = '0000-00-00' AND a.pc_eventDate >= '$start' AND a.pc_eventDate <= '$end')) ";
967 // The above two lines replace these:
968 // AND (a.pc_endDate >= '$start' OR a.pc_endDate = '0000-00-00')
969 // AND a.pc_eventDate <= '$end' ";
971 if(!empty($providerID))
973 $ruserid = $providerID;
976 if(isset($ruserid)) {
977 // get all events for the specified username
978 if($ruserid == -1) {
979 $sql .= "AND (a.pc_sharing = '" . SHARING_BUSY . "' ";
980 $sql .= "OR a.pc_sharing = '" . SHARING_PUBLIC . "') ";
981 } else {
982 $sql .= "AND a.pc_aid = " . $ruserid . " ";
984 } elseif(!pnUserLoggedIn()) {
985 // get all events for anonymous users
986 $sql .= "AND a.pc_sharing = '" . SHARING_GLOBAL . "' ";
987 } else {
988 // get all events for logged in user plus global events
989 $sql .= "AND (a.pc_aid = " . $_SESSION['authUserID'] . " OR a.pc_sharing = '" . SHARING_GLOBAL . "') ";
991 //======================================================================
992 // START SEARCH FUNCTIONALITY
993 //======================================================================
994 if(!empty($s_keywords)) $sql .= "AND ($s_keywords) ";
995 if(!empty($s_category)) $sql .= "AND ($s_category) ";
996 if(!empty($s_topic)) $sql .= "AND ($s_topic) ";
997 if(!empty($category)) $sql .= "AND (a.pc_catid = '".pnVarPrepForStore($category)."') ";
998 if(!empty($topic)) $sql .= "AND (a.pc_topic = '".pnVarPrepForStore($topic)."') ";
999 //======================================================================
1000 // Serch sort and limitation
1001 //======================================================================
1002 if(empty($sort)) $sql .= "GROUP BY a.pc_eid ORDER BY a.pc_time DESC";
1003 else $sql .= "GROUP BY a.pc_eid ORDER BY a.$sort";
1004 //======================================================================
1005 // END SEARCH FUNCTIONALITY
1006 //======================================================================
1007 //echo "sq: $sql<br />";
1009 // echo "<!-- " . $sql . " -->\n"; // debugging
1011 $result = $dbconn->Execute($sql);
1012 if($dbconn->ErrorNo() != 0) die ($dbconn->ErrorMsg());
1014 // put the information into an array for easy access
1015 $events = array();
1016 // return an empty array if we don't have any results
1017 if(!isset($result)) { return $events; }
1020 for($i=0; !$result->EOF; $result->MoveNext()) {
1022 // get the results from the query
1023 if(isset($tmp)) { unset($tmp); } $tmp = array();
1024 list($tmp['eid'], $tmp['uname'], $tmp['catid'],
1025 $tmp['title'], $tmp['time'], $tmp['hometext'],
1026 $tmp['eventDate'], $tmp['duration'], $tmp['endDate'],
1027 $tmp['startTime'], $tmp['recurrtype'], $tmp['recurrfreq'],
1028 $tmp['recurrspec'], $tmp['topic'], $tmp['alldayevent'],
1029 $tmp['location'], $tmp['conttel'], $tmp['contname'],
1030 $tmp['contemail'], $tmp['website'], $tmp['fee'], $tmp['sharing'],
1031 $tmp['catcolor'], $tmp['catname'], $tmp['catdesc'],
1032 $tmp['pid'], $tmp['aid'], $tmp['provider_name'], $tmp['patient_name'],
1033 $tmp['owner_name'],$tmp['patient_dob'],$tmp['pubpid']) = $result->fields;
1035 // grab the name of the topic
1036 $topicname = pcGetTopicName($tmp['topic']);
1037 // get the user id of event's author
1038 $cuserid = @$nuke_users[strtolower($tmp['uname'])];
1039 // check the current event's permissions
1040 // the user does not have permission to view this event
1041 // if any of the following evaluate as false
1042 if(!pnSecAuthAction(0, 'PostCalendar::Event', "$tmp[title]::$tmp[eid]", ACCESS_OVERVIEW)) {
1043 continue;
1044 } elseif(!pnSecAuthAction(0, 'PostCalendar::Category', "$tmp[catname]::$tmp[catid]", ACCESS_OVERVIEW)) {
1045 continue;
1046 } elseif(!pnSecAuthAction(0, 'PostCalendar::User', "$tmp[uname]::$cuserid", ACCESS_OVERVIEW)) {
1047 continue;
1048 } elseif(!pnSecAuthAction(0, 'PostCalendar::Topic', "$topicname::$tmp[topic]", ACCESS_OVERVIEW)) {
1049 continue;
1050 } elseif($tmp['sharing'] == SHARING_PRIVATE && $cuserid != $userid) {
1051 continue;
1054 // add event to the array if we passed the permissions check
1055 // this is the common information
1059 $events[$i]['intervals'] =($tmp['duration']/60)/ $GLOBALS['day_calandar_interval'];//sets the number of rows this event should span
1060 print_r($events[$i]['intervals']);
1062 $events[$i]['eid'] = $tmp['eid'];
1063 $events[$i]['uname'] = $tmp['uname'];
1064 $events[$i]['uid'] = $cuserid;
1065 $events[$i]['catid'] = $tmp['catid'];
1066 $events[$i]['time'] = $tmp['time'];
1067 $events[$i]['eventDate'] = $tmp['eventDate'];
1068 $events[$i]['duration'] = $tmp['duration'];
1069 // there has to be a more intelligent way to do this
1070 @list($events[$i]['duration_hours'],$dmin) = @explode('.',($tmp['duration']/60/60));
1071 $events[$i]['duration_minutes'] = substr(sprintf('%.2f','.' . 60*($dmin/100)),2,2);
1072 //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
1073 $events[$i]['endDate'] = $tmp['endDate'];
1074 $events[$i]['startTime'] = $tmp['startTime'];
1075 $events[$i]['recurrtype'] = $tmp['recurrtype'];
1076 $events[$i]['recurrfreq'] = $tmp['recurrfreq'];
1077 $events[$i]['recurrspec'] = $tmp['recurrspec'];
1078 $events[$i]['topic'] = $tmp['topic'];
1079 $events[$i]['alldayevent'] = $tmp['alldayevent'];
1080 $events[$i]['catcolor'] = $tmp['catcolor'];
1081 $events[$i]['catname'] = $tmp['catname'];
1082 $events[$i]['catdesc'] = $tmp['catdesc'];
1083 $events[$i]['pid'] = $tmp['pid'];
1084 $events[$i]['pubpid'] = $tmp['pubpid'];
1085 $events[$i]['patient_name']= $tmp['patient_name'];
1086 $events[$i]['provider_name'] = $tmp['provider_name'];
1087 $events[$i]['owner_name'] = $tmp['owner_name'];
1088 $events[$i]['patient_dob'] = $tmp['patient_dob'];
1089 $events[$i]['patient_age'] = date("Y") - substr(($tmp['patient_dob']),0,4);
1090 $events[$i]['sharing'] = $tmp['sharing'];
1091 $events[$i]['aid'] = $tmp['aid'];
1092 $events[$i]['topictext'] = $topicname;
1093 $events[$i]['intervals'] = ceil(($tmp['duration']/60) / $GLOBALS['calendar_interval']);
1094 if($events[$i]['intervals'] == 0)
1095 $events[$i]['intervals'] = 1;
1096 // is this a public event to be shown as busy?
1097 if($tmp['sharing'] == SHARING_BUSY && $cuserid != $userid) {
1098 // make it not display any information
1099 $events[$i]['title'] = _USER_BUSY_TITLE;
1100 $events[$i]['hometext'] = _USER_BUSY_MESSAGE;
1101 $events[$i]['desc'] = _USER_BUSY_MESSAGE;
1102 $events[$i]['conttel'] = '';
1103 $events[$i]['contname'] = '';
1104 $events[$i]['contemail'] = '';
1105 $events[$i]['website'] = '';
1106 $events[$i]['fee'] = '';
1107 $events[$i]['location'] = '';
1108 $events[$i]['street1'] = '';
1109 $events[$i]['street2'] = '';
1110 $events[$i]['city'] = '';
1111 $events[$i]['state'] = '';
1112 $events[$i]['postal'] = '';
1113 } else {
1114 $display_type = substr($tmp['hometext'],0,6);
1115 if($display_type == ':text:') {
1116 $prepFunction = 'pcVarPrepForDisplay';
1117 $tmp['hometext'] = substr($tmp['hometext'],6);
1118 } elseif($display_type == ':html:') {
1119 $prepFunction = 'pcVarPrepHTMLDisplay';
1120 $tmp['hometext'] = substr($tmp['hometext'],6);
1121 } else {
1122 $prepFunction = 'pcVarPrepHTMLDisplay';
1124 unset($display_type);
1125 $events[$i]['title'] = $prepFunction($tmp['title']);
1126 $events[$i]['hometext'] = $prepFunction($tmp['hometext']);
1127 $events[$i]['desc'] = $events[$i]['hometext'];
1128 $events[$i]['conttel'] = $prepFunction($tmp['conttel']);
1129 $events[$i]['contname'] = $prepFunction($tmp['contname']);
1130 $events[$i]['contemail'] = $prepFunction($tmp['contemail']);
1131 $events[$i]['website'] = $prepFunction(postcalendar_makeValidURL($tmp['website']));
1132 $events[$i]['fee'] = $prepFunction($tmp['fee']);
1133 $loc = unserialize($tmp['location']);
1134 $events[$i]['location'] = $prepFunction($loc['event_location']);
1135 $events[$i]['street1'] = $prepFunction($loc['event_street1']);
1136 $events[$i]['street2'] = $prepFunction($loc['event_street2']);
1137 $events[$i]['city'] = $prepFunction($loc['event_city']);
1138 $events[$i]['state'] = $prepFunction($loc['event_state']);
1139 $events[$i]['postal'] = $prepFunction($loc['event_postal']);
1141 $i++;
1143 unset($tmp);
1144 $result->Close();
1145 return $events;
1148 function getBlockTime($time) {
1150 if ($time == 0 || strlen($time) == 0) {
1152 return "all_day";
1154 $ts = strtotime($time);
1155 $half = 0;
1156 $minutes = date("i",$ts);
1157 $hour = date("H",$ts);
1158 if ($minutes >= 30)
1159 $half = 1;
1160 $blocknum = (($hour * 2) +$half);
1161 return strval($blocknum);
1164 function &postcalendar_userapi_pcGetEvents($args)
1166 $s_keywords = $s_category = $s_topic = '';
1167 extract($args);
1169 $date =& postcalendar_getDate();
1170 $cy = substr($date,0,4);
1171 $cm = substr($date,4,2);
1172 $cd = substr($date,6,2);
1173 if(isset($start) && isset($end)) {
1174 // parse start date
1175 list($sm,$sd,$sy) = explode('/',$start);
1176 // parse end date
1177 list($em,$ed,$ey) = explode('/',$end);
1179 $s = (int) "$sy$sm$sd";
1180 if($s > $date) {
1181 $cy = $sy;
1182 $cm = $sm;
1183 $cd = $sd;
1185 $start_date = Date_Calc::dateFormat($sd,$sm,$sy,'%Y-%m-%d');
1186 $end_date = Date_Calc::dateFormat($ed,$em,$ey,'%Y-%m-%d');
1187 } else {
1188 $sm = $em = $cm;
1189 $sd = $ed = $cd;
1190 $sy = $cy;
1191 $ey = $cy+2;
1192 $start_date = $sy.'-'.$sm.'-'.$sd;
1193 $end_date = $ey.'-'.$em.'-'.$ed;
1195 if ($faFlag && !isset($events)) {
1196 $a = array('faFlag' => true,'start'=>$start_date,'end'=>$end_date,'s_keywords'=>$s_keywords,'s_category'=>$s_category,'s_topic'=>$s_topic,'viewtype'=>$viewtype, 'provider_id' => $provider_id, 'event_status' => $event_status);
1197 //print_r($a);
1198 $events =& pnModAPIFunc(__POSTCALENDAR__,'user','pcQueryEventsFA',$a);
1200 elseif ($collideFlag && !isset($events)) {
1202 $a = array('collideFlag' => true,'start'=>$start_date,'end'=>$end_date, 'provider_id' => $provider_id, 'collide_stime' => $stime, 'collide_etime' => $etime);
1203 $events =& pnModAPIFunc(__POSTCALENDAR__,'user','pcQueryEventsFA',$a);
1205 elseif ($listappsFlag && !isset($events)) {
1207 $a = array('listappsFlag' => true,'start'=>$start_date,'end'=>$end_date, 'patient_id' => $patient_id, 's_keywords' => $s_keywords);
1208 $events =& pnModAPIFunc(__POSTCALENDAR__,'user','pcQueryEvents',$a);
1210 else if(!isset($events)) {
1211 if(!isset($s_keywords)) $s_keywords = '';
1212 $a = array('start'=>$start_date,'end'=>$end_date,'s_keywords'=>$s_keywords,'s_category'=>$s_category,'s_topic'=>$s_topic,'viewtype'=>$viewtype, "sort" => "pc_startTime ASC, a.pc_duration ASC ",'providerID' => $providerID);
1213 $events =& pnModAPIFunc(__POSTCALENDAR__,'user','pcQueryEvents',$a);
1216 //==============================================================
1217 // Here we build an array consisting of the date ranges
1218 // specific to the current view. This array is then
1219 // used to build the calendar display.
1220 //==============================================================
1221 $days = array();
1222 $sday = Date_Calc::dateToDays($sd,$sm,$sy);
1223 $eday = Date_Calc::dateToDays($ed,$em,$ey);
1224 for($cday = $sday; $cday <= $eday; $cday++) {
1225 $d = Date_Calc::daysToDate($cday,'%d');
1226 $m = Date_Calc::daysToDate($cday,'%m');
1227 $y = Date_Calc::daysToDate($cday,'%Y');
1228 $store_date = Date_Calc::dateFormat($d,$m,$y,'%Y-%m-%d');
1229 $days[$store_date] = array();
1232 $days = calculateEvents($days,$events,$viewtype);
1233 return $days;
1236 function calculateEvents($days,$events,$viewtype) {
1237 $date =& postcalendar_getDate();
1238 $cy = substr($date,0,4);
1239 $cm = substr($date,4,2);
1240 $cd = substr($date,6,2);
1242 foreach($events as $event) {
1243 // get the name of the topic
1244 $topicname = pcGetTopicName($event['topic']);
1246 // parse the event start date
1247 list($esY,$esM,$esD) = explode('-',$event['eventDate']);
1248 // grab the recurring specs for the event
1249 $event_recurrspec = @unserialize($event['recurrspec']);
1250 // determine the stop date for this event
1251 if($event['endDate'] == '0000-00-00') {
1252 $stop = $end_date;
1253 } else {
1254 $stop = $event['endDate'];
1257 $eventD = $event['eventDate'];
1258 $eventS = $event['startTime'];
1260 switch($event['recurrtype']) {
1261 //==============================================================
1262 // Events that do not repeat only have a startday
1263 //==============================================================
1264 case NO_REPEAT :
1266 if(isset($days[$event['eventDate']])) {
1267 array_push($days[$event['eventDate']],$event);
1268 if ($viewtype == "week") {
1269 //echo "non repeating date eventdate: $eventD startime:$eventS block #: " . getBlockTime($eventS) ."<br />";
1271 fillBlocks($eventD,&$days);
1272 //echo "for $eventD loading " . getBlockTime($eventS) . "<br /><br />";
1273 $gbt = getBlockTime($eventS);
1274 $days[$eventD]['blocks'][$gbt][$eventD][] = $event;
1275 //echo "event is: " . print_r($days[$eventD]['blocks'][$gbt],true) . " <br />";
1276 //echo "begin printing blocks for $eventD<br />";
1277 //print_r($days[$eventD]['blocks']);
1278 //echo "end printing blocks<br />";
1282 break;
1283 //==============================================================
1284 // Find events that repeat at a certain frequency
1285 // Every,Every Other,Every Third,Every Fourth
1286 // Day,Week,Month,Year,MWF,TR,M-F,SS
1287 //==============================================================
1288 case REPEAT :
1289 $rfreq = $event_recurrspec['event_repeat_freq'];
1290 $rtype = $event_recurrspec['event_repeat_freq_type'];
1291 // we should bring the event up to date to make this a tad bit faster
1292 // any ideas on how to do that, exactly??? dateToDays probably.
1293 $nm = $esM; $ny = $esY; $nd = $esD;
1294 $occurance = Date_Calc::dateFormat($nd,$nm,$ny,'%Y-%m-%d');
1295 while($occurance < $start_date) {
1296 $occurance =& __increment($nd,$nm,$ny,$rfreq,$rtype);
1297 list($ny,$nm,$nd) = explode('-',$occurance);
1299 while($occurance <= $stop) {
1300 if(isset($days[$occurance])) {
1301 array_push($days[$occurance],$event);
1302 if ($viewtype == "week") {
1303 fillBlocks($occurance,&$days);
1304 //echo "for $occurance loading " . getBlockTime($eventS) . "<br /><br />";
1305 $gbt = getBlockTime($eventS);
1306 $days[$occurance]['blocks'][$gbt][$occurance][] = $event;
1307 //echo "begin printing blocks for $eventD<br />";
1308 //print_r($days[$occurance]['blocks']);
1309 //echo "end printing blocks<br />";
1312 $occurance =& __increment($nd,$nm,$ny,$rfreq,$rtype);
1313 list($ny,$nm,$nd) = explode('-',$occurance);
1315 break;
1317 //==============================================================
1318 // Find events that repeat on certain parameters
1319 // On 1st,2nd,3rd,4th,Last
1320 // Sun,Mon,Tue,Wed,Thu,Fri,Sat
1321 // Every N Months
1322 //==============================================================
1323 case REPEAT_ON :
1324 $rfreq = $event_recurrspec['event_repeat_on_freq'];
1325 $rnum = $event_recurrspec['event_repeat_on_num'];
1326 $rday = $event_recurrspec['event_repeat_on_day'];
1328 //==============================================================
1329 // Populate - Enter data into the event array
1330 //==============================================================
1331 $nm = $esM; $ny = $esY; $nd = $esD;
1332 // make us current
1334 while($ny < $cy) {
1336 $occurance = date('Y-m-d',mktime(0,0,0,$nm+$rfreq,$nd,$ny));
1337 list($ny,$nm,$nd) = explode('-',$occurance);
1340 // populate the event array
1341 while($ny <= $cy) {
1343 $dnum = $rnum; // get day event repeats on
1344 do {
1345 $occurance = Date_Calc::NWeekdayOfMonth($dnum--,$rday,$nm,$ny,$format="%Y-%m-%d");
1346 } while($occurance === -1);
1347 if(isset($days[$occurance]) && $occurance <= $stop) {
1348 array_push($days[$occurance],$event);
1349 if ($viewtype == "week") {
1350 fillBlocks($occurance,&$days);
1351 //echo "for $occurance loading " . getBlockTime($eventS) . "<br /><br />";
1352 $gbt = getBlockTime($eventS);
1353 $days[$occurance]['blocks'][$gbt][$occurance][] = $event;
1356 $occurance = date('Y-m-d',mktime(0,0,0,$nm+$rfreq,$nd,$ny));
1357 list($ny,$nm,$nd) = explode('-',$occurance);
1359 break;
1360 } // <- end of switch($event['recurrtype'])
1361 } // <- end of foreach($events as $event)
1362 return $days;
1365 function fillBlocks($td,&$ar) {
1366 if (strlen ($td) > 0 && !isset($ar[$td]['blocks'])) {
1367 $ar[$td]['blocks'] = array();
1368 for ($j=0;$j<48;$j++)
1369 $ar[strval($td)]['blocks'][strval($j)] = array();
1370 $ar[strval($td)]['blocks']["all_day"] = array();
1377 * __increment()
1378 * returns the next valid date for an event based on the
1379 * current day,month,year,freq and type
1380 * @private
1381 * @returns string YYYY-MM-DD
1383 function &__increment($d,$m,$y,$f,$t)
1385 if($t == REPEAT_EVERY_DAY) {
1386 return date('Y-m-d',mktime(0,0,0,$m,($d+$f),$y));
1387 } elseif($t == REPEAT_EVERY_WORK_DAY) {
1388 //echo "special occurance<br />";
1389 $beginday = date("D",mktime(0,0,0,$m,$d,$y));
1390 $dayincrement = 1;
1391 if ($beginday == "Fri") {
1392 $dayincrement = 3;
1394 elseif ($beginday == "Sat") {
1395 $dayincrement = 2;
1397 return date('Y-m-d',mktime(0,0,0,$m,($d+$dayincrement),$y));
1399 } elseif($t == REPEAT_EVERY_WEEK) {
1400 return date('Y-m-d',mktime(0,0,0,$m,($d+(7*$f)),$y));
1401 } elseif($t == REPEAT_EVERY_MONTH) {
1402 return date('Y-m-d',mktime(0,0,0,($m+$f),$d,$y));
1403 } elseif($t == REPEAT_EVERY_YEAR) {
1404 return date('Y-m-d',mktime(0,0,0,$m,$d,($y+$f)));