[7442] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class EventsResource extends CalendarAdapter { |
---|
| 4 | public function post($request){ |
---|
| 5 | // to Receive POST Params (use $this->params) |
---|
| 6 | parent::post($request); |
---|
| 7 | |
---|
| 8 | if($this-> isLoggedIn()) |
---|
| 9 | { |
---|
| 10 | $user_id = $this->getUserId(); |
---|
| 11 | $tz_offset = $this->getTimezoneOffset(); |
---|
| 12 | $date_start = $this->getParam('dateStart'); |
---|
| 13 | $date_end = $this->getParam('dateEnd'); |
---|
| 14 | $split_event = $this->getParam('splitEvent'); // Boolean 1 or 0 - if you want to split the events that last for more than 1 day |
---|
| 15 | |
---|
| 16 | // check the dates parameters formats (ex: 31/12/2012 23:59:59, but the time is optional) |
---|
| 17 | $regex_date = '/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/([12][0-9]{3})( ([01][0-9]|2[0-3])(:[0-5][0-9]){2})?$/'; |
---|
| 18 | |
---|
| 19 | if(!preg_match($regex_date, $date_start)) |
---|
| 20 | Errors::runException("CALENDAR_INVALID_START_DATE"); |
---|
| 21 | |
---|
| 22 | if(!preg_match($regex_date, $date_end)) |
---|
| 23 | Errors::runException("CALENDAR_INVALID_END_DATE"); |
---|
| 24 | |
---|
| 25 | // get the start timestamp UNIX from the parameter |
---|
| 26 | $start_arr = explode(' ', $date_start); |
---|
| 27 | $start_date_arr = explode('/', $start_arr[0]); |
---|
| 28 | $start_time_arr = !empty($start_arr[1]) ? explode(':', $start_arr[1]) : array('00', '00', '00'); |
---|
| 29 | $datetime = mktime($start_time_arr[0],$start_time_arr[1],$start_time_arr[2],$start_date_arr[1],$start_date_arr[0],$start_date_arr[2]) - ($tz_offset); |
---|
| 30 | |
---|
| 31 | // get the end timestamp UNIX from the parameter |
---|
| 32 | $end_arr = explode(' ', $date_end); |
---|
| 33 | $end_date_arr = explode('/', $end_arr[0]); |
---|
| 34 | $end_time_arr = !empty($end_arr[1]) ? explode(':', $end_arr[1]) : array('23', '59', '59'); |
---|
| 35 | $edatetime = mktime($end_time_arr[0],$end_time_arr[1],$end_time_arr[2],$end_date_arr[1],$end_date_arr[0],$end_date_arr[2]) - ($tz_offset); |
---|
| 36 | |
---|
| 37 | $sql = 'SELECT DISTINCT ' |
---|
| 38 | .'phpgw_cal.cal_id, ' |
---|
| 39 | .'phpgw_cal.datetime, ' |
---|
| 40 | .'phpgw_cal.edatetime, ' |
---|
| 41 | .'phpgw_cal.title, ' |
---|
| 42 | .'phpgw_cal.description, ' |
---|
| 43 | .'phpgw_cal.location ' |
---|
| 44 | .'FROM phpgw_cal_user, phpgw_cal ' |
---|
| 45 | .'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) '; |
---|
| 46 | |
---|
| 47 | // user clauses |
---|
| 48 | $sql .= ' AND (phpgw_cal_user.cal_login = ' . $user_id . ' OR (phpgw_cal.owner = ' . $user_id . ')) '; |
---|
| 49 | |
---|
| 50 | // date range clauses |
---|
| 51 | $sql .= ' AND ( ' |
---|
| 52 | .' ( (phpgw_cal.datetime >= '.$datetime.') AND (phpgw_cal.edatetime <= '.$edatetime.') ) ' |
---|
| 53 | .'OR ( (phpgw_cal.datetime <= '.$datetime.') AND (phpgw_cal.edatetime >= '.$edatetime.') ) ' |
---|
| 54 | .'OR ( (phpgw_cal.datetime >= '.$datetime.') AND (phpgw_cal.datetime <= '.$edatetime.') AND (phpgw_cal.edatetime >= '.$edatetime.') ) ' |
---|
| 55 | .'OR ( (phpgw_cal.datetime <= '.$datetime.') AND (phpgw_cal.edatetime >= '.$datetime.') AND (phpgw_cal.edatetime <= '.$edatetime.') ) ' |
---|
| 56 | .') '; |
---|
| 57 | |
---|
| 58 | $sql .= ' ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC '; |
---|
| 59 | |
---|
| 60 | if (!$this->getDb()->query($sql)) |
---|
| 61 | return false; |
---|
| 62 | |
---|
| 63 | $events = array(); |
---|
| 64 | while($this->getDb()->next_record()) { |
---|
| 65 | $row = $this->getDb()->row(); |
---|
| 66 | $id = $row['cal_id']; |
---|
| 67 | $sdate = $row['datetime'] + ($tz_offset); |
---|
| 68 | $edate = $row['edatetime'] + ($tz_offset); |
---|
| 69 | $datetime += ($tz_offset); |
---|
| 70 | $edatetime += ($tz_offset); |
---|
| 71 | |
---|
| 72 | $association_arr = array( |
---|
| 73 | 'eventID' => 'cal_id', |
---|
| 74 | 'eventName' => 'title', |
---|
| 75 | 'eventDescription' => 'description', |
---|
| 76 | 'eventLocation' => 'location' |
---|
| 77 | ); |
---|
| 78 | |
---|
| 79 | // if the event starts and ends in the same day OR the user does not want to split the event, |
---|
| 80 | // return the event in only one register |
---|
| 81 | if(date('d/m/Y', $sdate) == date('d/m/Y', $edate) || !$split_event){ |
---|
| 82 | foreach($association_arr AS $k => $v) |
---|
| 83 | $events[$id][$k] = mb_convert_encoding($row[$v],"UTF8", "ISO_8859-1"); |
---|
| 84 | |
---|
| 85 | $events[$id]['eventStartDate'] = date('d/m/Y H:i', $sdate); |
---|
| 86 | $events[$id]['eventEndDate'] = date('d/m/Y H:i', $edate); |
---|
| 87 | $events[$id]['eventAllDay'] = '0'; |
---|
| 88 | } |
---|
| 89 | // if the event lasts for more than one day AND the user wants it splitted day by day, |
---|
| 90 | // return the event splitted into as many parts of the array as the days remaining |
---|
| 91 | else{ |
---|
| 92 | // walk through the event date range, adding one register for each day |
---|
| 93 | $step_date = $sdate; |
---|
| 94 | $step_date_ymd = date('Ymd', $step_date); |
---|
| 95 | while($step_date_ymd <= date('Ymd', $edate) && $step_date_ymd <= date('Ymd', $edatetime)){ |
---|
| 96 | if($step_date_ymd >= date('Ymd', $datetime) && $step_date_ymd <= date('Ymd', $edatetime)){ |
---|
| 97 | foreach($association_arr AS $k => $v) |
---|
| 98 | $events_tmp[$k] = $row[$v]; |
---|
| 99 | |
---|
| 100 | // the first day of the event |
---|
| 101 | if($step_date == $sdate){ |
---|
| 102 | $events_tmp['eventStartDate'] = date('d/m/Y H:i', $sdate); |
---|
| 103 | $events_tmp['eventEndDate'] = date('d/m/Y 23:59', $sdate); |
---|
| 104 | $events_tmp['eventAllDay'] = '0'; |
---|
| 105 | } |
---|
| 106 | // the last day of the event |
---|
| 107 | elseif($step_date_ymd == date('Ymd', $edate)){ |
---|
| 108 | $events_tmp['eventStartDate'] = date('d/m/Y 00:00', $edate); |
---|
| 109 | $events_tmp['eventEndDate'] = date('d/m/Y H:i', $edate); |
---|
| 110 | $events_tmp['eventAllDay'] = '0'; |
---|
| 111 | } |
---|
| 112 | // the other days |
---|
| 113 | else{ |
---|
| 114 | $events_tmp['eventStartDate'] = date('d/m/Y 00:00', $step_date); |
---|
| 115 | $events_tmp['eventEndDate'] = date('d/m/Y 23:59', $step_date); |
---|
| 116 | $events_tmp['eventAllDay'] = '1'; |
---|
| 117 | } |
---|
| 118 | $events[$id.$step_date_ymd] = $events_tmp; |
---|
| 119 | } |
---|
| 120 | $step_date = strtotime("+1 day", $step_date); |
---|
| 121 | $step_date_ymd = date('Ymd', $step_date); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | $result = array ('events' => array_values($events)); |
---|
| 127 | |
---|
| 128 | $this->setResult($result); |
---|
| 129 | } |
---|
| 130 | //to Send Response (JSON RPC format) |
---|
| 131 | return $this->getResponse(); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | } |
---|