source: sandbox/webservice/api/rest/calendar/EventsResource.php @ 7463

Revision 7463, 4.5 KB checked in by alexandrecorreia, 12 years ago (diff)

Ticket #2507 - Melhorias dos resources REST, servidor e calendar.

  • Property svn:executable set to *
Line 
1<?php
2
3class 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                        $date_start  = $this->getParam('dateStart');
11                        $date_end    = $this->getParam('dateEnd');
12                       
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]);
29                       
30                        $return = array();
31                       
32                        for( $j = (int)$start_date_arr[2]; $j <= (int)$end_date_arr[2]; $j++ )
33                        {
34                                for( $i = (int)$start_date_arr[1]; $i <= (int)$end_date_arr[1]; $i++ )
35                                {
36                                        if( (int)$i < 10 )
37                                                $result = $this->getEvents("0".$i,$j);
38                                        else
39                                                $result = $this->getEvents($i,$j);
40                                       
41                                        if( count($result) > 0 )
42                                        {
43                                                $return[] = $result;
44                                        }
45                                }
46                        }
47
48                        if( count($return) > 0 )
49                        {
50                                $i = 0;
51                               
52                                for( $j = 0 ; $j < count( $return); $j++)
53                                {
54                                        foreach( $return[$j] as $key => $value )
55                                        {
56                                                $events[$i]['eventID']                  = "".$value[0]['id'];
57                                                $events[$i]['eventDate']                = "".$key;
58                                                $events[$i]['eventName']                = mb_convert_encoding("".$value[0]['title'],"UTF8","ISO_8859-1");
59                                                $events[$i]['eventDescription'] = mb_convert_encoding("".$value[0]['description'], "UTF8","ISO_8859-1");
60                                                $events[$i]['eventLocation']    = mb_convert_encoding("".$value[0]['location'], "UTF8","ISO_8859-1");
61                                                $events[$i]['eventParticipants']= $value[0]['participants'];
62                                               
63                                                $starttime      = $this->makeTime($value[0]['start']);
64                                                $endtime        = $this->makeTime($value[0]['end']);
65                                                $actualdate = mktime(0,0,0,substr($key,4,2),substr($key, 6 ),substr($key,0,4));
66                                                $rawdate_offset = $actualdate - $this->getTimezoneOffset();
67                                                $nextday = mktime(0,0,0,substr($key,4,2),substr($key, 6 )+1,substr($key,0,4)) - $this->getTimezoneOffset();
68                                               
69                                                if( $starttime <= $rawdate_offset && $endtime >= $nextday - 60 )
70                                                {
71                                                        $events[$i]['eventStartDate']   = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." 00:00";
72                                                        $events[$i]['eventEndDate']             = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." 23:59";
73                                                        $events[$i]['eventAllDay']              = "1";
74                                                }
75                                                else
76                                                {
77                                                        if( $value[0]['start']['mday'] === $value[0]['end']['mday'] )
78                                                        {
79                                                                $hour_start     = (((int)$value[0]['start']['hour'] < 10 ) ? "0".$value[0]['start']['hour'] : $value[0]['start']['hour']).":".(((int)$value[0]['start']['min'] < 10 ) ? "0".$value[0]['start']['min'] : $value[0]['start']['min'] );
80                                                                $hour_end       = (((int)$value[0]['end']['hour'] < 10 ) ? "0".$value[0]['end']['hour'] : $value[0]['end']['hour']).":".(((int)$value[0]['end']['min'] < 10 ) ? "0".$value[0]['end']['min'] : $value[0]['end']['min'] );
81                                                        }
82                                                        else
83                                                        {
84                                                                if( $events[$i-1] && $events[$i-1]['eventAllDay'] == "1" )
85                                                                {
86                                                                        $hour_start     = "00:00";
87                                                                        $hour_end       = (((int)$value[0]['end']['hour'] < 10 ) ? "0".$value[0]['end']['hour'] : $value[0]['end']['hour']).":".(((int)$value[0]['end']['min'] < 10 ) ? "0".$value[0]['end']['min'] : $value[0]['end']['min'] );
88                                                                }
89                                                                else
90                                                                {
91                                                                        $hour_start     = (((int)$value[0]['start']['hour'] < 10 ) ? "0".$value[0]['start']['hour'] : $value[0]['start']['hour']).":".(((int)$value[0]['start']['min'] < 10 ) ? "0".$value[0]['start']['min'] : $value[0]['start']['min'] );
92                                                                        $hour_end       = "23:59";
93                                                                }
94                                                        }
95                                                               
96                                                        $events[$i]['eventStartDate']   = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." ".$hour_start;
97                                                        $events[$i]['eventEndDate']             = substr($key, 6 )."/".substr($key,4,2)."/".substr($key,0,4)." ".$hour_end;
98                                                        $events[$i]['eventAllDay']              = "0";
99                                                }
100                                               
101                                                $events[$i++]['eventExParticipants'] = $value[0]['ex_participants'];
102                                        }
103                                }
104
105                                $this->setResult(array('events' => $events));
106                        }
107                        else
108                        {
109                                $this->setResult(array('events' => array()));
110                        }
111                }
112
113                //to Send Response (JSON RPC format)
114                return $this->getResponse();
115        }
116}
Note: See TracBrowser for help on using the repository browser.