[6423] | 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 | |
---|
[7463] | 8 | if( $this->isLoggedIn() ) |
---|
[6423] | 9 | { |
---|
| 10 | $date_start = $this->getParam('dateStart'); |
---|
| 11 | $date_end = $this->getParam('dateEnd'); |
---|
[7463] | 12 | |
---|
[6423] | 13 | // check the dates parameters formats (ex: 31/12/2012 23:59:59, but the time is optional) |
---|
| 14 | $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})?$/'; |
---|
| 15 | |
---|
| 16 | if(!preg_match($regex_date, $date_start)) |
---|
| 17 | Errors::runException("CALENDAR_INVALID_START_DATE"); |
---|
| 18 | |
---|
| 19 | if(!preg_match($regex_date, $date_end)) |
---|
| 20 | Errors::runException("CALENDAR_INVALID_END_DATE"); |
---|
| 21 | |
---|
| 22 | // get the start timestamp UNIX from the parameter |
---|
| 23 | $start_arr = explode(' ', $date_start); |
---|
| 24 | $start_date_arr = explode('/', $start_arr[0]); |
---|
| 25 | |
---|
| 26 | // get the end timestamp UNIX from the parameter |
---|
| 27 | $end_arr = explode(' ', $date_end); |
---|
| 28 | $end_date_arr = explode('/', $end_arr[0]); |
---|
[7463] | 29 | |
---|
| 30 | $return = array(); |
---|
[7740] | 31 | |
---|
| 32 | |
---|
| 33 | $start_year = (int)$start_date_arr[2]; |
---|
| 34 | $end_year = (int)$end_date_arr[2]; |
---|
| 35 | |
---|
| 36 | for( $j = $start_year; $j <= $end_year; $j++ ) |
---|
[7463] | 37 | { |
---|
[7740] | 38 | $start_month = (int)$start_date_arr[1]; |
---|
| 39 | $end_month = (int)$end_date_arr[1]; |
---|
| 40 | |
---|
| 41 | if ($start_year != $end_year) { |
---|
| 42 | if ($j == $start_year) { |
---|
| 43 | $end_month = 12; |
---|
| 44 | } else { |
---|
| 45 | $start_month = 1; |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | for( $i = $start_month; $i <= $end_month; $i++ ) |
---|
[7463] | 50 | { |
---|
| 51 | if( (int)$i < 10 ) |
---|
| 52 | $result = $this->getEvents("0".$i,$j); |
---|
| 53 | else |
---|
| 54 | $result = $this->getEvents($i,$j); |
---|
[7740] | 55 | |
---|
| 56 | |
---|
[7463] | 57 | if( count($result) > 0 ) |
---|
| 58 | { |
---|
| 59 | $return[] = $result; |
---|
| 60 | } |
---|
[6423] | 61 | } |
---|
[7463] | 62 | } |
---|
[6423] | 63 | |
---|
[7740] | 64 | |
---|
| 65 | |
---|
[7463] | 66 | if( count($return) > 0 ) |
---|
| 67 | { |
---|
| 68 | $i = 0; |
---|
| 69 | |
---|
| 70 | for( $j = 0 ; $j < count( $return); $j++) |
---|
| 71 | { |
---|
[7740] | 72 | foreach( $return[$j] as $key => $event ) |
---|
[7463] | 73 | { |
---|
[7740] | 74 | |
---|
| 75 | foreach ($event as $value) { |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | $events[$i]['eventID'] = "".$value['id']; |
---|
| 79 | $events[$i]['eventDate'] = "".$key; |
---|
| 80 | $events[$i]['eventName'] = mb_convert_encoding("".$value['title'],"UTF8","ISO_8859-1"); |
---|
| 81 | $events[$i]['eventDescription'] = mb_convert_encoding("".$value['description'], "UTF8","ISO_8859-1"); |
---|
| 82 | $events[$i]['eventLocation'] = mb_convert_encoding("".$value['location'], "UTF8","ISO_8859-1"); |
---|
| 83 | $events[$i]['eventParticipants']= $value['participants']; |
---|
| 84 | |
---|
| 85 | $starttime = $this->makeTime($value['start']); |
---|
| 86 | $endtime = $this->makeTime($value['end']); |
---|
| 87 | $actualdate = mktime(0,0,0,substr($key,4,2),substr($key, 6 ),substr($key,0,4)); |
---|
| 88 | $rawdate_offset = $actualdate - $this->getTimezoneOffset(); |
---|
| 89 | $nextday = mktime(0,0,0,substr($key,4,2),substr($key, 6 )+1,substr($key,0,4)) - $this->getTimezoneOffset(); |
---|
| 90 | |
---|
| 91 | if( $starttime <= $rawdate_offset && $endtime >= $nextday - 60 ) |
---|
[7463] | 92 | { |
---|
[7740] | 93 | $events[$i]['eventStartDate'] = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." 00:00"; |
---|
| 94 | $events[$i]['eventEndDate'] = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." 23:59"; |
---|
| 95 | $events[$i]['eventAllDay'] = "1"; |
---|
[6423] | 96 | } |
---|
[7463] | 97 | else |
---|
| 98 | { |
---|
[7740] | 99 | if( $value['start']['mday'] === $value['end']['mday'] ) |
---|
[7463] | 100 | { |
---|
[7740] | 101 | $hour_start = (((int)$value['start']['hour'] < 10 ) ? "0".$value['start']['hour'] : $value['start']['hour']).":".(((int)$value['start']['min'] < 10 ) ? "0".$value['start']['min'] : $value['start']['min'] ); |
---|
| 102 | $hour_end = (((int)$value['end']['hour'] < 10 ) ? "0".$value['end']['hour'] : $value['end']['hour']).":".(((int)$value['end']['min'] < 10 ) ? "0".$value['end']['min'] : $value['end']['min'] ); |
---|
[7463] | 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
[7740] | 106 | if( $events[$i-1] && $events[$i-1]['eventAllDay'] == "1" ) |
---|
| 107 | { |
---|
| 108 | $hour_start = "00:00"; |
---|
| 109 | $hour_end = (((int)$value['end']['hour'] < 10 ) ? "0".$value['end']['hour'] : $value['end']['hour']).":".(((int)$value['end']['min'] < 10 ) ? "0".$value['end']['min'] : $value['end']['min'] ); |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | $hour_start = (((int)$value['start']['hour'] < 10 ) ? "0".$value['start']['hour'] : $value['start']['hour']).":".(((int)$value['start']['min'] < 10 ) ? "0".$value['start']['min'] : $value['start']['min'] ); |
---|
| 114 | $hour_end = "23:59"; |
---|
| 115 | } |
---|
[7463] | 116 | } |
---|
[7740] | 117 | |
---|
| 118 | $events[$i]['eventStartDate'] = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." ".$hour_start; |
---|
| 119 | $events[$i]['eventEndDate'] = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." ".$hour_end; |
---|
| 120 | $events[$i]['eventAllDay'] = "0"; |
---|
[6423] | 121 | } |
---|
[7740] | 122 | |
---|
| 123 | $events[$i++]['eventExParticipants'] = $value['ex_participants']; |
---|
[6423] | 124 | } |
---|
[7740] | 125 | |
---|
[6423] | 126 | } |
---|
[7740] | 127 | |
---|
[7463] | 128 | } |
---|
[6423] | 129 | |
---|
[7463] | 130 | $this->setResult(array('events' => $events)); |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | $this->setResult(array('events' => array())); |
---|
| 135 | } |
---|
[6423] | 136 | } |
---|
[7463] | 137 | |
---|
[6423] | 138 | //to Send Response (JSON RPC format) |
---|
| 139 | return $this->getResponse(); |
---|
| 140 | } |
---|
[7489] | 141 | } |
---|