Index: /trunk/prototype/rest/calendar/EventsResource.php
===================================================================
--- /trunk/prototype/rest/calendar/EventsResource.php	(revision 7342)
+++ /trunk/prototype/rest/calendar/EventsResource.php	(revision 7733)
@@ -6,11 +6,12 @@
  		parent::post($request);
 
-		if($this-> isLoggedIn())
-		{
-			$user_id     = $this->getUserId();
-			$tz_offset   = $this->getTimezoneOffset();
+ 		$user_id     = $this->getUserId();
+		$tz_offset   = $this->getTimezoneOffset();
+
+		if($this-> isLoggedIn()) {
+
 			$date_start  = $this->getParam('dateStart');
 			$date_end    = $this->getParam('dateEnd');
-			$split_event = $this->getParam('splitEvent');	// Boolean 1 or 0 - if you want to split the events that last for more than 1 day
+
 
 			// check the dates parameters formats (ex: 31/12/2012 23:59:59, but the time is optional)
@@ -23,9 +24,10 @@
 				Errors::runException("CALENDAR_INVALID_END_DATE");
 
+
 			// get the start timestamp UNIX from the parameter
 			$start_arr      = explode(' ', $date_start);
 			$start_date_arr = explode('/', $start_arr[0]);
 			$start_time_arr = !empty($start_arr[1]) ? explode(':', $start_arr[1]) : array('00', '00', '00');
-			$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);
+			$rangeStart       = 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);
 
 			// get the end timestamp UNIX from the parameter
@@ -33,98 +35,99 @@
 			$end_date_arr   = explode('/', $end_arr[0]);
 			$end_time_arr   = !empty($end_arr[1]) ? explode(':', $end_arr[1]) : array('23', '59', '59');
-			$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);
+			$rangeEnd      = 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);
 
-			$sql  =  'SELECT DISTINCT '
-					.'phpgw_cal.cal_id, '
-					.'phpgw_cal.datetime, '
-					.'phpgw_cal.edatetime, '
-					.'phpgw_cal.title, '
-					.'phpgw_cal.description, '
-					.'phpgw_cal.location '
-					.'FROM phpgw_cal_user, phpgw_cal '
-					.'WHERE (phpgw_cal_user.cal_id = phpgw_cal.cal_id) ';
+			$rangeStart = $rangeStart * 1000;
+			$rangeEnd = $rangeEnd * 1000;
 
-			// user clauses
-			$sql .= ' AND (phpgw_cal_user.cal_login = ' . $user_id . ' OR (phpgw_cal.owner = ' . $user_id . ')) ';
+			$concept = "schedulable";
+			$id = false;
 
-			// date range clauses
-			$sql .= ' AND ( '
-						.'   ( (phpgw_cal.datetime >= '.$datetime.') AND (phpgw_cal.edatetime <= '.$edatetime.') ) '
-						.'OR ( (phpgw_cal.datetime <= '.$datetime.') AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
-						.'OR ( (phpgw_cal.datetime >= '.$datetime.') AND (phpgw_cal.datetime <= '.$edatetime.') AND (phpgw_cal.edatetime >= '.$edatetime.') ) '
-						.'OR ( (phpgw_cal.datetime <= '.$datetime.') AND (phpgw_cal.edatetime >= '.$datetime.') AND (phpgw_cal.edatetime <= '.$edatetime.') ) '
-					.') ';
+			$criteria = array();
+			$criteria['order'] = "startTime";
+			$criteria['deepness'] = 2;
 
-			$sql .= ' ORDER BY phpgw_cal.datetime ASC, phpgw_cal.edatetime ASC ';
+			$criteria['timezones'] = array();
+			$criteria['timezones'][1] = 'America/Sao_Paulo';
+			$criteria['timezones'][3] = 'America/Sao_Paulo';
 
-			if (!$this->getDb()->query($sql))
-				return false;
 
-			$events = array();
-			while($this->getDb()->next_record()) {
-				$row   = $this->getDb()->row();
-				$id    = $row['cal_id'];
-				$sdate = $row['datetime']  + ($tz_offset);
-				$edate = $row['edatetime'] + ($tz_offset);
-				$datetime  += ($tz_offset);
-				$edatetime += ($tz_offset);
+			$criteria['filter'] = array();
+			$criteria['filter'][0] = "AND";
+			$criteria['filter'][1] = array();
+			$criteria['filter'][1][0] = "OR";
 
-				$association_arr = array(
-							'eventID'          => 'cal_id',
-							'eventName'        => 'title',
-							'eventDescription' => 'description',
-							'eventLocation'    => 'location'
-						);
 
-				// if the event starts and ends in the same day OR the user does not want to split the event,
-				// return the event in only one register
-				if(date('d/m/Y', $sdate) == date('d/m/Y', $edate) || !$split_event){
-					foreach($association_arr AS $k => $v)
-						$events[$id][$k] = mb_convert_encoding($row[$v],"UTF8", "ISO_8859-1");
+			$criteria['filter'][1][1] = array();
+			$criteria['filter'][1][1][0] = "AND";
+			$criteria['filter'][1][1][1] = array();
+			$criteria['filter'][1][1][1][0] = ">=";
+			$criteria['filter'][1][1][1][1] = "rangeEnd";
+			$criteria['filter'][1][1][1][2] = $rangeStart; //START
+			$criteria['filter'][1][1][2] = array();
+			$criteria['filter'][1][1][2][0] = "=<";
+			$criteria['filter'][1][1][2][1] = "rangeEnd";
+			$criteria['filter'][1][1][2][2] = $rangeEnd; //END
 
-					$events[$id]['eventStartDate'] = date('d/m/Y H:i', $sdate);
-					$events[$id]['eventEndDate']   = date('d/m/Y H:i', $edate);
-					$events[$id]['eventAllDay']    = '0';
-				}
-				// if the event lasts for more than one day AND the user wants it splitted day by day,
-				// return the event splitted into as many parts of the array as the days remaining
-				else{
-					// walk through the event date range, adding one register for each day
-					$step_date     = $sdate;
-					$step_date_ymd = date('Ymd', $step_date);
-					while($step_date_ymd <= date('Ymd', $edate) && $step_date_ymd <= date('Ymd', $edatetime)){
-						if($step_date_ymd >= date('Ymd', $datetime) && $step_date_ymd <= date('Ymd', $edatetime)){
-							foreach($association_arr AS $k => $v)
-								$events_tmp[$k] = $row[$v];
 
-							// the first day of the event
-							if($step_date == $sdate){
-								$events_tmp['eventStartDate'] = date('d/m/Y H:i',   $sdate);
-								$events_tmp['eventEndDate']   = date('d/m/Y 23:59', $sdate);
-								$events_tmp['eventAllDay']    = '0';
-							}
-							// the last day of the event
-							elseif($step_date_ymd == date('Ymd', $edate)){
-								$events_tmp['eventStartDate'] = date('d/m/Y 00:00', $edate);
-								$events_tmp['eventEndDate']   = date('d/m/Y H:i',   $edate);
-								$events_tmp['eventAllDay']    = '0';
-							}
-							// the other days
-							else{
-								$events_tmp['eventStartDate'] = date('d/m/Y 00:00', $step_date);
-								$events_tmp['eventEndDate']   = date('d/m/Y 23:59', $step_date);
-								$events_tmp['eventAllDay']    = '1';
-							}
-							$events[$id.$step_date_ymd] = $events_tmp;
-						}
-						$step_date     = strtotime("+1 day", $step_date);
-						$step_date_ymd = date('Ymd', $step_date);
-					}
- 				}
+			$criteria['filter'][1][2] = array();
+			$criteria['filter'][1][2][0] = "AND";
+			$criteria['filter'][1][2][1] = array();
+			$criteria['filter'][1][2][1][0] = ">=";
+			$criteria['filter'][1][2][1][1] = "rangeStart";
+			$criteria['filter'][1][2][1][2] = $rangeStart; //START
+			$criteria['filter'][1][2][2] = array();
+			$criteria['filter'][1][2][2][0] = "=<";
+			$criteria['filter'][1][2][2][1] = "rangeStart";
+			$criteria['filter'][1][2][2][2] = $rangeEnd; //END
+
+
+			$criteria['filter'][1][3] = array();
+			$criteria['filter'][1][3][0] = "AND";
+			$criteria['filter'][1][3][1] = array();
+			$criteria['filter'][1][3][1][0] = "<=";
+			$criteria['filter'][1][3][1][1] = "rangeStart";
+			$criteria['filter'][1][3][1][2] = $rangeStart; //START
+			$criteria['filter'][1][3][2] = array();
+			$criteria['filter'][1][3][2][0] = ">=";
+			$criteria['filter'][1][3][2][1] = "rangeEnd";
+			$criteria['filter'][1][3][2][2] = $rangeEnd; //END
+
+			$criteria['filter'][2] = array("IN","calendar",array(1));
+
+			$properties = ( $criteria && isset( $criteria['properties'] ) )? $criteria['properties']: false;
+			$service = ( $criteria && isset( $criteria['service'] ) )? $criteria['service']: false;
+
+
+			$res = Controller::call( 'find', Controller::URI( $concept ),false,$criteria );
+
+			$arrEvents = array();
+			foreach ($res as $event) {
+
+
+				$timeZone = new DateTimeZone($event['timezone']);
+				$timeStart = new DateTime('@' . (int) ( $event['startTime'] / 1000 ), $timeZone);
+				$timeEnd = new DateTime('@' . (int) ( $event['endTime'] / 1000 ), $timeZone);
+
+				$timeStart->setTimezone($timeZone);
+				$timeEnd->setTimezone($timeZone);
+
+				$newEvent = array();
+				$newEvent['eventID'] =  "" . $event['id'];
+				$newEvent['eventName'] = "" .$event['summary'];
+				$newEvent['eventDescription'] = "" .$event['description'];
+				$newEvent['eventLocation'] = "" . $event['location'];
+				$newEvent['eventStartDate'] = "" . $timeStart->format('d/m/Y H:i:s');
+				$newEvent['eventEndDate'] = "" . $timeEnd->format('d/m/Y H:i:s');
+				//$newEvent['eventTimeZone'] = "" . $event['timezone'];
+				$newEvent['eventAllDay'] = "" . $event['allDay']; 
+
+				$arrEvents[] = $newEvent;
 			}
 
-			$result = array ('events' => array_values($events));
+			$result = array ('events' => $arrEvents); 
 
-			$this->setResult($result);
+			$this->setResult($result); 
+
+
 		}
 		//to Send Response (JSON RPC format)
