[5341] | 1 | <?php |
---|
| 2 | //Definindo Constantes |
---|
| 3 | require_once ROOTPATH.'/modules/calendar/constants.php'; |
---|
| 4 | |
---|
| 5 | require_once ROOTPATH.'/modules/calendar/interceptors/Helpers.php'; |
---|
| 6 | |
---|
| 7 | class DBMapping extends Helpers { |
---|
| 8 | |
---|
[5715] | 9 | public function encodeCreateSchedulable( &$uri , &$params , &$criteria , $original ){ |
---|
| 10 | $params['type_id'] = EVENT_ID; |
---|
| 11 | |
---|
| 12 | if(!is_numeric($params['startTime']) ) |
---|
| 13 | $params['startTime'] = self::parseTimeDate( $params['startTime'], $params['timezone'] ); |
---|
[5592] | 14 | |
---|
[5715] | 15 | if (!is_numeric($params['endTime'])){ |
---|
| 16 | $params['endTime'] = self::parseTimeDate( $params['endTime'], $params['timezone'] ); |
---|
| 17 | |
---|
| 18 | if($params['allDay']) |
---|
| 19 | $params['endTime'] = $params['endTime'] + 86400000; |
---|
| 20 | } |
---|
[5341] | 21 | // if( !isset( $new['repeat'] )) |
---|
| 22 | // $new['range_end'] = '7287926400'; // 12/12/2200 |
---|
| 23 | // else |
---|
[5592] | 24 | $params['rangeEnd'] = $params['endTime']; |
---|
[5715] | 25 | $params['rangeStart'] = $params['startTime']; |
---|
[5592] | 26 | |
---|
[5341] | 27 | /////////////////////////////////////////////////////////////////// |
---|
| 28 | |
---|
[5437] | 29 | $params['dtstamp'] = (isset($params['dtstamp'])) ? $params['dtstamp'] :time().'000'; |
---|
| 30 | $params['lastUpdate'] = (isset($params['lastUpdate'])) ? $params['lastUpdate'] :time().'000'; |
---|
| 31 | $params['type'] = EVENT_ID; |
---|
| 32 | $params['uid'] = isset($params['uid']) ? $params['uid'] : $this->_makeUid(); |
---|
[5715] | 33 | } |
---|
| 34 | |
---|
| 35 | static function parseTimeDate($time, $timezone){ |
---|
| 36 | return strtotime( $time . ' ' .$timezone).'000'; |
---|
| 37 | } |
---|
[5592] | 38 | |
---|
[5341] | 39 | public function encodeCreateAlarm( &$uri , &$params , &$criteria , $original ){ |
---|
| 40 | |
---|
| 41 | if(!isset($params['schedulable']) || !isset($params['rangeStart']) || !isset($params['rangeEnd']) ) |
---|
| 42 | { |
---|
| 43 | $participant = Controller::read( array( 'concept' => 'participant' , 'id' => $params['participant'] ) , array('schedulable') ); |
---|
| 44 | $schedulable = Controller::read( array( 'concept' => 'schedulable' , 'id' => $participant['schedulable'] ) , array('startTime' , 'endTime' ) ); |
---|
| 45 | |
---|
| 46 | $params['schedulable'] = $participant['schedulable']; |
---|
[5652] | 47 | $params['rangeStart'] = strtotime('- '.$params['time'].' '.self::codeAlarmUnit($params['unit']) , (int)($schedulable['startTime'] / 1000)).'000'; |
---|
| 48 | $params['rangeEnd'] = $schedulable['endTime']; |
---|
[5341] | 49 | } |
---|
| 50 | |
---|
[5437] | 51 | $params['type'] = self::codeAlarmType($params['type']); |
---|
[5341] | 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
[5514] | 55 | public function encodeCreateSuggestion( &$uri , &$params , &$criteria , $original ){ |
---|
| 56 | $params['dtstamp'] = (isset($params['dtstamp'])) ? $params['dtstamp'] :time().'000'; |
---|
| 57 | } |
---|
| 58 | |
---|
[5341] | 59 | public function encodeUpdateAlarm( &$uri , &$params , &$criteria , $original ){ |
---|
| 60 | if(isset($params['type'])) |
---|
| 61 | $params['type'] = self::codeAlarmType($params['type']); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public function encodeCreateAttachment( &$uri , &$params , &$criteria , $original ){ |
---|
[5514] | 65 | |
---|
| 66 | if(!isset($params['source'])) return false; |
---|
| 67 | |
---|
| 68 | if(isset($_FILES[$params['source']])) |
---|
| 69 | if(isset($params['id'])) |
---|
| 70 | $params = array_merge($_FILES[$params['source']], array('id' => $params['id'])); |
---|
| 71 | else |
---|
| 72 | $params = $_FILES[$params['source']]; |
---|
| 73 | |
---|
[5592] | 74 | if(isset($params['owner'])) |
---|
| 75 | $params['owner'] = Config::me('uidNumber'); |
---|
[5514] | 76 | |
---|
[5341] | 77 | } |
---|
[5514] | 78 | |
---|
[5341] | 79 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 80 | |
---|
| 81 | public function encodeSignatureAlarmType( &$uri , &$params , &$criteria , $original ){ |
---|
[5437] | 82 | $params['type'] = self::codeAlarmType($params['type']); |
---|
[5341] | 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | public function insertOwnerLink (&$uri , &$params , &$criteria , $original ){ |
---|
| 87 | $params['owner'] = Config::me('uidNumber'); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | public function encodeServiceUser (&$uri , &$params , &$criteria , $original ){ |
---|
| 91 | if(isset($params['isExternal']) && $params['isExternal'] == '1') |
---|
| 92 | $uri['service'] = 'PostgreSQL'; |
---|
[5715] | 93 | } |
---|
| 94 | |
---|
| 95 | public function prepareRepeat(&$uri , &$params , &$criteria , $original ){ |
---|
| 96 | |
---|
[5798] | 97 | if( isset($params['startTime']) || isset($params['endTime']) ) |
---|
| 98 | { |
---|
[5715] | 99 | $timezone = Controller::read( array( 'concept' => 'schedulable', 'id' => $params['schedulable'] ), array('timezone') ); |
---|
[5798] | 100 | |
---|
| 101 | if( isset($params['startTime']) ) |
---|
| 102 | $params['startTime'] = self::parseTimeDate( $params['startTime'], $timezone['timezone'] ); |
---|
| 103 | if( isset($params['endTime']) ) |
---|
| 104 | $params['endTime'] = self::parseTimeDate( $params['endTime'], $timezone['timezone'] ); |
---|
| 105 | } |
---|
[5715] | 106 | } |
---|
[5341] | 107 | |
---|
[5715] | 108 | public function findSchedulable( &$uri , &$params , &$criteria , $original ) |
---|
[5437] | 109 | { |
---|
| 110 | if(isset($criteria['filter'][3][1]) && $criteria['filter'][3][1] == 'calendar') |
---|
| 111 | { |
---|
[5715] | 112 | $start = $criteria['filter'][1][2]; |
---|
| 113 | $end = $criteria['filter'][2][2]; |
---|
| 114 | |
---|
[5737] | 115 | $ids = array(); |
---|
| 116 | $occ = array(); |
---|
| 117 | |
---|
[5752] | 118 | if( $occurrences = self::checkOccurrences( $start, $end ) ) |
---|
[5737] | 119 | foreach( $occurrences as $id => $occurrence ) |
---|
| 120 | { |
---|
| 121 | $ids[] = $id; |
---|
| 122 | $occ[] = $occurrence; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | $sql = ' SELECT calendar_object.id as id ,calendar_object.cal_uid as "uid", calendar_object.type_id as "type", calendar_object.dtstart as "startTime", calendar_object.summary as "summary", calendar_object.description as "description", calendar_object.dtend as "endTime" , calendar_object.location as "location", calendar_object.allday as "allDay", calendar_object.transp as "transparent", calendar_object.class_id as "class", calendar_object.repeat as "repeat", calendar_object.range_start as "rangeStart",calendar_object.range_end as "rangeEnd", calendar_object.last_update as "lastUpdate", calendar_object.dtstamp as "dtstamp", calendar_object.sequence as "sequence", calendar_object.tzid as "timezone" ,calendar_to_calendar_object.calendar_id as calendar FROM calendar_to_calendar_object , calendar_object WHERE (range_start >= \''.$start.'\' AND range_end <= \''.$end.'\' AND calendar_to_calendar_object.calendar_id IN (\''. implode('\',\'', $criteria['filter'][3][2]).'\')) AND calendar_to_calendar_object.calendar_object_id = calendar_object.id'.( !empty($ids) ? ' AND calendar_object.id NOT IN (\'' .implode( '\',\'', $ids ). '\')' : ''); |
---|
| 126 | |
---|
[5715] | 127 | $params = Controller::service('PostgreSQL')->execResultSql($sql); |
---|
[5741] | 128 | |
---|
| 129 | $params = array_merge( $params, $occ ); |
---|
[5773] | 130 | $params = self::deepnessFindEvent( $uri , $params , $criteria , $original); |
---|
[5715] | 131 | |
---|
[5437] | 132 | return false; |
---|
| 133 | } |
---|
| 134 | } |
---|
[5715] | 135 | |
---|
| 136 | public function deepnessFindRepeatOccurrence( &$uri , &$result , &$criteria , $original ) |
---|
| 137 | { |
---|
| 138 | |
---|
| 139 | if( !isset($criteria['deepness']) || $criteria['deepness'] == 0 ) |
---|
| 140 | return; |
---|
| 141 | |
---|
| 142 | foreach( $result as $i => &$res ) |
---|
| 143 | if( isset($res['repeat']) ) |
---|
| 144 | $res['repeat'] = Controller::read( array( 'concept' => 'repeat', 'id' => $res['repeat'] ), false, array( 'deepness' => intval( $criteria['deepness'] ) - 1 ) ); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | public function deepnessRepeat( &$uri , &$result , &$criteria , $original ) |
---|
| 148 | { |
---|
| 149 | |
---|
| 150 | if( !isset($criteria['deepness']) || $criteria['deepness'] == 0 ) |
---|
| 151 | return; |
---|
| 152 | |
---|
| 153 | $result['schedulable'] = Controller::find( array( 'concept' => 'schedulable' ), false, array( 'filter' => array( '=', 'id', $result['schedulable'] ), 'deepness' => intval( $criteria['deepness'] ) - 1 ) ); |
---|
| 154 | $result['schedulable'] = $result['schedulable'][0]; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | public function saveOccurrences( &$uri , &$result , &$criteria , $original ){ |
---|
[5746] | 158 | $ranges = Controller::find( array( 'concept' => 'repeatRange' ), array( 'rangeStart', 'rangeEnd' ), array( 'filter' => array( '=', 'user', Config::me("uidNumber") ) ) ); |
---|
[5715] | 159 | |
---|
| 160 | if( !is_array( $ranges ) || !isset( $ranges[0]['rangeStart'] ) || !isset( $ranges[0]['rangeEnd'] ) ) |
---|
| 161 | return; |
---|
| 162 | |
---|
| 163 | if( isset( $result['id'] ) ) |
---|
| 164 | $id = $result['id']; |
---|
| 165 | else |
---|
| 166 | $id = $uri['id']; |
---|
| 167 | |
---|
| 168 | $repeat = Controller::read( array( 'concept' => 'repeat', 'id' => $id ) ); |
---|
| 169 | |
---|
| 170 | unset( $repeat['schedulable'] ); |
---|
| 171 | unset( $repeat['id'] ); |
---|
| 172 | |
---|
[5741] | 173 | $exceptions = array(); |
---|
[5715] | 174 | |
---|
[5746] | 175 | if( isset( $original['properties']['exceptions'] ) ) |
---|
[5741] | 176 | { |
---|
[5746] | 177 | $exceptions = explode( ',', $original['properties']['exceptions'] ); |
---|
[5741] | 178 | unset( $repeat['exceptions'] ); |
---|
| 179 | } |
---|
[6011] | 180 | |
---|
[5746] | 181 | $lastExceptions = Controller::find( array( 'concept' => 'repeatOccurrence' ), array("occurrence") , array( 'filter' => array('AND', array( '=', 'repeat', $id ) , array( '=', 'exception', 1 ) ) )); |
---|
[6011] | 182 | |
---|
[5746] | 183 | //Recurepa as execeções anteriores caso exista |
---|
[6011] | 184 | if(isset($lastExceptions) && count($lastExceptions) && $lastExceptions) |
---|
[5746] | 185 | foreach($lastExceptions as $key => $value) |
---|
| 186 | array_push($exceptions, $lastExceptions[$key]['occurrence']); |
---|
| 187 | |
---|
[5741] | 188 | $params = array_diff( self::decodeRepeat( $repeat, $ranges[0]['rangeStart'], $ranges[0]['rangeEnd'] ), $exceptions ); |
---|
[5746] | 189 | |
---|
[5715] | 190 | Controller::delete( array( 'concept' => 'repeatOccurrence' ), false, array( 'filter' => array( '=', 'repeat', $id ) ) ); |
---|
| 191 | |
---|
| 192 | if( !empty($params) ) |
---|
[5746] | 193 | Controller::service('PostgreSQL')->execResultSql( "INSERT INTO calendar_repeat_occurrence(repeat_id,exception,occurrence)VALUES('".$id."','0','".implode( "'),('".$id."','0','", $params )."')".( empty($exceptions) ? "" : ",('".$id."','1','".implode( "'),('".$id."','1','", $exceptions )."')" ) ); |
---|
[5715] | 194 | } |
---|
| 195 | |
---|
[5752] | 196 | public function checkOccurrences( $start, $end ){ |
---|
[5715] | 197 | |
---|
| 198 | $ranges = Controller::find( array( 'concept' => 'repeatRange' ), array( 'rangeStart', 'rangeEnd' ), array( 'filter' => array( '=', 'user', Config::me("uidNumber") ) ) ); |
---|
| 199 | $ranges = $ranges[0]; |
---|
| 200 | |
---|
| 201 | $origStart = $start; |
---|
| 202 | $origEnd = $end; |
---|
| 203 | |
---|
| 204 | if( $initialized = (isset($ranges['rangeStart']) && isset($ranges['rangeEnd'])) ) |
---|
| 205 | { |
---|
| 206 | if( $ranges['rangeStart'] <= $start ) |
---|
| 207 | $start = false; |
---|
| 208 | if( $ranges['rangeEnd'] >= $end ) |
---|
| 209 | $end = false; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | $repeats = self::findRepeats(); |
---|
| 213 | |
---|
| 214 | if( !is_array( $repeats ) || empty( $repeats ) ) |
---|
| 215 | return( false ); |
---|
| 216 | |
---|
| 217 | $result = array(); |
---|
| 218 | $ids = array(); |
---|
| 219 | |
---|
| 220 | foreach( $repeats as $repeat ) |
---|
| 221 | { |
---|
| 222 | $ids[] = $id = $repeat['id']; unset( $repeat['id'] ); |
---|
| 223 | |
---|
| 224 | if( !isset( $result[ $id ] ) ) |
---|
[5752] | 225 | $result[ $id ] = !$initialized ? array( $repeat['startTime'] ) : array(); |
---|
[5715] | 226 | |
---|
| 227 | if( !$initialized ) |
---|
| 228 | $result[ $id ] = array_merge( $result[ $id ], self::decodeRepeat( $repeat, $start, $end ) ); |
---|
| 229 | else |
---|
| 230 | { |
---|
| 231 | if( $start ) |
---|
| 232 | $result[ $id ] = array_merge( $result[ $id ], self::decodeRepeat( $repeat, $start, $ranges['rangeStart'] ) ); |
---|
| 233 | |
---|
| 234 | if( $end ) |
---|
| 235 | $result[ $id ] = array_merge( $result[ $id ], self::decodeRepeat( $repeat, $ranges['rangeEnd'], $end ) ); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | if( empty( $result[ $id ] ) ) |
---|
| 239 | unset( $result[ $id ] ); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | if( $start || $end ) |
---|
| 243 | { |
---|
| 244 | Controller::begin( array( 'service' => 'PostgreSQL') ); |
---|
| 245 | |
---|
| 246 | foreach( $result as $id => $res ) |
---|
| 247 | Controller::service('PostgreSQL')->execResultSql( "INSERT INTO calendar_repeat_occurrence(repeat_id,occurrence)VALUES('".$id."','".implode( "'),('".$id."', '", $res )."')" ); |
---|
| 248 | |
---|
| 249 | $data = array(); |
---|
| 250 | |
---|
| 251 | if( $start ) |
---|
| 252 | $data['rangeStart'] = $start; |
---|
| 253 | |
---|
| 254 | if( $end ) |
---|
| 255 | $data['rangeEnd'] = $end; |
---|
| 256 | |
---|
| 257 | if( !$initialized ) |
---|
| 258 | $data['user'] = Config::me( 'uidNumber' ); |
---|
| 259 | |
---|
| 260 | Controller::call( ( $initialized ? 'replace' : 'create' ), array( 'concept' => 'repeatRange' ), $data, array( 'filter' => array( '=', 'user', Config::me('uidNumber') ) ) ); |
---|
| 261 | |
---|
| 262 | Controller::commit( array( 'service' => 'PostgreSQL' ) ); |
---|
| 263 | } |
---|
| 264 | |
---|
[5741] | 265 | // $return = Controller::find( array( 'concept' => 'repeatOccurrence' ), false, array( 'filter' => array( 'AND', array( '>=', 'occurrence', $origStart ), array( '<=', 'occurrence', $origEnd ), array( 'IN', 'repeat', $ids ) ), 'deepness' => $deep ) ); |
---|
[5715] | 266 | |
---|
[5741] | 267 | $return = Controller::service('PostgreSQL')->execResultSql( 'SELECT calendar_repeat_occurrence.occurrence as "occurrence", calendar_repeat.object_id as "schedulable" FROM calendar_repeat, calendar_repeat_occurrence WHERE calendar_repeat_occurrence.occurrence >= \''.$origStart.'\' AND calendar_repeat_occurrence.occurrence <= \''.$origEnd.'\' AND calendar_repeat_occurrence.repeat_id IN (\''.implode( '\',\'', $ids ).'\') AND calendar_repeat.id = calendar_repeat_occurrence.repeat_id AND calendar_repeat_occurrence.exception != 1' ); |
---|
| 268 | |
---|
[5715] | 269 | if( !is_array( $return ) ) |
---|
| 270 | return( false ); |
---|
| 271 | |
---|
| 272 | $result = array(); |
---|
| 273 | $params = array(); |
---|
| 274 | |
---|
| 275 | foreach( $return as $ret ) |
---|
| 276 | { |
---|
[5741] | 277 | $currentId = $ret['schedulable']; |
---|
[5715] | 278 | |
---|
| 279 | if( !isset( $result[ $currentId ] ) ) |
---|
| 280 | { |
---|
[5741] | 281 | $result[ $currentId ] = Controller::read( array( 'concept' => 'schedulable', 'id' => $currentId ) ); |
---|
[5715] | 282 | $result[ $currentId ][ 'occurrences' ] = array(); |
---|
[5737] | 283 | |
---|
[5715] | 284 | $calendarToCalendarObj = self::schedulable2calendarToObject( $currentId ); |
---|
[5741] | 285 | $result[ $currentId ]['calendar'] = $calendarToCalendarObj[0]['calendar_id']; |
---|
[5715] | 286 | } |
---|
| 287 | |
---|
| 288 | $result[ $currentId ][ 'occurrences' ][] = $ret['occurrence']; |
---|
| 289 | } |
---|
| 290 | |
---|
[5737] | 291 | return( $result ); |
---|
[5715] | 292 | } |
---|
| 293 | |
---|
| 294 | public static function findRepeats() |
---|
| 295 | { |
---|
| 296 | return Controller::service('PostgreSQL')->execResultSql( 'SELECT calendar_repeat.wkst as "wkst", calendar_repeat.byweekno as "byweekno", calendar_repeat.byminute as "byminute", calendar_repeat.bysecond as "bysecond", calendar_repeat.byyearday as "byyearday", calendar_repeat.bymonthday as "bymonthday", calendar_repeat.bysetpos as "bysetpos", calendar_repeat.byday as "byday", calendar_repeat.byhour as "byhour", calendar_repeat.interval as "interval", calendar_repeat.frequency as "frequency", calendar_repeat.until as "endTime", calendar_repeat.id as "id", calendar_repeat.count as "count", calendar_repeat.dtstart as "startTime" FROM calendar_repeat, calendar_participant WHERE calendar_repeat.object_id = calendar_participant.object_id AND calendar_participant.user_info_id = \''.Config::me('uidNumber').'\'' ); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | //HELPERS |
---|
| 300 | public static function decodeRepeat( $repeat , $start , $end ) { |
---|
| 301 | |
---|
| 302 | date_default_timezone_set( 'UTC' ); |
---|
| 303 | |
---|
| 304 | require_once ROOTPATH.'/plugins/when/When.php'; |
---|
| 305 | |
---|
| 306 | $r = new When(); |
---|
| 307 | |
---|
| 308 | if( $repeat['frequency'] === 'none' ) |
---|
| 309 | return( array() ); |
---|
[5953] | 310 | |
---|
| 311 | //Nao deve ser usando o horário da repeticao pois nela contem apenas o dias, |
---|
| 312 | //deve se recuperar o horário do evento para um correto calculo. |
---|
| 313 | if(max( $start, $repeat['startTime'] ) != $repeat['startTime']){ |
---|
| 314 | $time = new DateTime( '@'.(int)( $repeat['startTime'] / 1000 ), new DateTimeZone('UTC') ); |
---|
| 315 | |
---|
| 316 | $hoursOcurrence = new DateTime( '@'.(int)( $start / 1000 ), new DateTimeZone('UTC') ); |
---|
| 317 | $hoursOcurrence = $hoursOcurrence->format('H'); |
---|
| 318 | |
---|
| 319 | $diffTime = ((($time->format('H') - $hoursOcurrence) * (3600000)) + ($time->format('i') * (60000))); |
---|
| 320 | $start = new DateTime( '@'.(int)( ( $start + $diffTime ) / 1000 ), new DateTimeZone('UTC') ); |
---|
| 321 | |
---|
| 322 | |
---|
| 323 | }else |
---|
| 324 | $start = new DateTime( '@'.(int)( max( $start, $repeat['startTime'] ) / 1000 ), new DateTimeZone('UTC') ); |
---|
| 325 | |
---|
[5715] | 326 | foreach($repeat as $rule => $value) |
---|
| 327 | { |
---|
| 328 | if( !isset( $value ) || !$value || $value === "0" ) |
---|
| 329 | continue; |
---|
| 330 | |
---|
| 331 | switch(strtolower($rule)) |
---|
| 332 | { |
---|
| 333 | case "starttime": break; |
---|
| 334 | case "frequency": |
---|
| 335 | $r->recur( $start, $value ); |
---|
| 336 | break; |
---|
| 337 | case "endtime": |
---|
| 338 | $r->until( new DateTime( '@'.(int)( $value / 1000 ) ) ); |
---|
| 339 | break; |
---|
| 340 | case "count": case "interval": case "wkst": |
---|
| 341 | $r->$rule( $value ); |
---|
| 342 | break; |
---|
| 343 | default : |
---|
| 344 | $r->$rule( !is_array( $value ) ? explode( ',' , $value ) : $value ); |
---|
| 345 | break; |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | $return = array(); |
---|
[5752] | 350 | |
---|
[5715] | 351 | while($result = $r->next()) |
---|
| 352 | { |
---|
| 353 | $u = $result->format('U') * 1000; |
---|
| 354 | |
---|
[5729] | 355 | if( $u > $end ) //data da repetição atual maior que a data final da busca do usuario ? |
---|
[5715] | 356 | break; |
---|
| 357 | |
---|
| 358 | $return[] = $u; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | return( $return ); |
---|
| 362 | } |
---|
[5437] | 363 | |
---|
[5341] | 364 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 365 | |
---|
| 366 | public function updateCalendar( &$uri , &$params , &$criteria , $original ){ |
---|
| 367 | if( isset($params['calendar'])) |
---|
| 368 | { |
---|
| 369 | $calendarObjects = self::schedulable2calendarToObject( $uri['id'] ); |
---|
| 370 | $params2['calendar'] = $params['calendar']; |
---|
| 371 | |
---|
| 372 | foreach ($calendarObjects as $calendarObject) |
---|
| 373 | Controller::update( array('concept' => 'calendarToSchedulable' , 'id' => $calendarObject['calendar_to_calendar_object']) , $params2 ); |
---|
| 374 | |
---|
| 375 | unset( $params['calendar'] ); |
---|
| 376 | |
---|
| 377 | if(count($params) < 1) return false; |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | //Encode Update |
---|
| 382 | |
---|
| 383 | public function encodeUpdateSchedulable( &$uri , &$params , &$criteria , $original ){ |
---|
[5636] | 384 | |
---|
[5653] | 385 | $event = Controller::find( array( 'concept' => 'schedulable' , array('timezone', 'allDay') ,'id' => $params['id'] ) ); |
---|
[5636] | 386 | |
---|
| 387 | if(isset($params['startTime'])){ |
---|
| 388 | |
---|
| 389 | if(!is_numeric($params['startTime']) ) |
---|
| 390 | $params['startTime'] = strtotime( $params['startTime'] . ' ' .$event['timezone']).'000'; |
---|
[5641] | 391 | |
---|
| 392 | $params['rangeStart'] = $params['startTime'] ; |
---|
[5636] | 393 | |
---|
| 394 | }if(isset($params['endTime'])){ |
---|
| 395 | |
---|
[5682] | 396 | if (!is_numeric($params['endTime'])){ |
---|
[5636] | 397 | $params['endTime'] = strtotime( $params['endTime'] . ' ' .$event['timezone']).'000'; |
---|
[5653] | 398 | |
---|
[5682] | 399 | if($event['allDay']) |
---|
| 400 | $params['endTime'] = $params['endTime'] + 86400000; |
---|
| 401 | } |
---|
[5641] | 402 | $params['rangeEnd'] = $params['endTime'] ; |
---|
[5636] | 403 | |
---|
| 404 | } |
---|
[5341] | 405 | } |
---|
| 406 | |
---|
| 407 | static function putEvent(&$uri , &$result , &$criteria , $original) |
---|
| 408 | { |
---|
[5907] | 409 | if(Config::module('useCaldav' , 'expressoCalendar')) //Ignorar Put dos eventos ja vindos do caldav |
---|
[5341] | 410 | { |
---|
| 411 | require_once ROOTPATH.'/modules/calendar/interceptors/DAViCalAdapter.php'; |
---|
| 412 | |
---|
| 413 | $eventID = (isset($result['id'])) ? $result['id'] : $uri['id']; |
---|
| 414 | $event = Controller::read( array( 'concept' => 'schedulable' , 'id' => $eventID ) ); |
---|
| 415 | |
---|
| 416 | $participants = Controller::find( array( 'concept' => 'participant' ) , false ,array( 'filter' => array('=' , 'schedulable' , $eventID) )); |
---|
| 417 | |
---|
| 418 | if(is_array($participants) && count($participants) > 0) |
---|
| 419 | foreach ($participants as $ii => $vv) |
---|
| 420 | { |
---|
| 421 | if($vv['isExternal'] == 1 ) |
---|
| 422 | $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] , 'service' => 'PostgreSQL' ) ); |
---|
| 423 | else |
---|
| 424 | $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] )); |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | $event['URI']['concept'] = 'schedulable'; |
---|
| 428 | $event['participants'] = $participants; |
---|
| 429 | |
---|
| 430 | $ical = Controller::format( array( 'service' => 'iCal' ) , array($event) ); |
---|
| 431 | $calendars = self::schedulable2calendarToObject($original['properties']['id']); //Busca os calendarios do usuario logado que contenham o evento |
---|
| 432 | if( is_array($calendars) ) |
---|
| 433 | foreach ($calendars as $calendar) |
---|
[5907] | 434 | DAViCalAdapter::putIcal($ical , array('uid' => $event['uid'] , 'location' => $calendar['calendar_location'] )); |
---|
[5341] | 435 | } |
---|
| 436 | } |
---|
| 437 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 438 | |
---|
| 439 | public function verifyCalendarLocation( &$uri , &$params , &$criteria , $original ){ |
---|
| 440 | if(!isset($params['location'])) |
---|
[5907] | 441 | $params['location'] = Config::me('uid').'/'.$params['name']; |
---|
[5341] | 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | //TODO: Remover apos suporte a ManytoMany na api |
---|
| 446 | public function createCalendarToSchedulable( &$uri , &$result , &$criteria , $original ){ |
---|
| 447 | |
---|
[5514] | 448 | Controller::create(array('concept' => 'calendarToSchedulable'), array('calendar' => $original['properties']['calendar'], 'schedulable' => $result['id'] )); |
---|
| 449 | |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | public function createCreateSchedulableToAttachment( &$uri , &$params , &$criteria , $original ){ |
---|
| 453 | if(array_key_exists('attachments', $original['properties'])) |
---|
| 454 | foreach($original['properties']['attachments'] as $key => $value){ |
---|
| 455 | if(isset($params['id'])) |
---|
| 456 | Controller::create(array('concept' => 'schedulableToAttachment'), array('attachment' => $value['attachment'], 'schedulable' => $params['id'] )); |
---|
| 457 | } |
---|
| 458 | } |
---|
[5341] | 459 | |
---|
[5592] | 460 | public function deepnessFindCalendarShared( &$uri , &$result , &$criteria , $original ){ |
---|
[5715] | 461 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != '0' && count($result) > 0){ |
---|
[5592] | 462 | $calendarIds = array(); |
---|
| 463 | foreach($result as $key => $value) |
---|
| 464 | array_push($calendarIds, $value['calendar']); |
---|
| 465 | |
---|
| 466 | $calendar = Controller::find( array( 'concept' => 'calendar' ) , false ,array( 'filter' => array('AND', array('IN','id', $calendarIds), $original['criteria']['filter']))); |
---|
| 467 | |
---|
| 468 | if(!is_array($calendar)) |
---|
| 469 | $result = ''; |
---|
| 470 | |
---|
| 471 | $newResult = array(); |
---|
| 472 | foreach($calendar as $key => $value){ |
---|
| 473 | foreach($result as $k => $r){ |
---|
| 474 | |
---|
| 475 | if($r['calendar'] == $value['id']){ |
---|
| 476 | $r['calendar'] = $value; |
---|
| 477 | array_push($newResult, $r); |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | foreach($newResult as $key => &$value){ |
---|
[5804] | 483 | if($value['user'] != 0){ |
---|
| 484 | $user = $value['user']; |
---|
| 485 | $value['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $user )); |
---|
| 486 | |
---|
| 487 | if(!$value['user']) |
---|
| 488 | $value['user'] = Controller::read( array( 'concept' => 'group' , 'id' => $user )); |
---|
| 489 | } |
---|
[5592] | 490 | } |
---|
| 491 | |
---|
| 492 | |
---|
| 493 | $result = $newResult; |
---|
| 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
[5341] | 497 | //TODO: Remover apos suporte a deepness na api |
---|
[5746] | 498 | public function deepnessFindEvent( &$uri , &$result , &$criteria , $original ){ |
---|
[5715] | 499 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != 0) |
---|
[5341] | 500 | { |
---|
[5592] | 501 | |
---|
[5746] | 502 | $Time = new DateTime( 'now', new DateTimeZone('UTC') ); |
---|
| 503 | $DayLigth = array(); |
---|
[5341] | 504 | foreach ($result as $i => $v) |
---|
| 505 | { |
---|
[5592] | 506 | |
---|
| 507 | if(!isset($currentTimezone) || $currentTimezone != $original['criteria']['timezones'][$v['calendar']]){ |
---|
[5682] | 508 | $currentTimezone = isset($original['criteria']['timezones'][$v['calendar']]) ? $original['criteria']['timezones'][$v['calendar']] : $v['timezone']; |
---|
[5592] | 509 | $Time->setTimezone(new DateTimeZone($currentTimezone)); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | $Time->setTimestamp( (int)($v['startTime'] / 1000)); |
---|
| 513 | $DayLigth['calendar']['startTime'] = $Time->format('I') ? 1 : 0; |
---|
| 514 | |
---|
| 515 | $Time->setTimestamp( (int)($v['endTime'] / 1000)); |
---|
| 516 | $DayLigth['calendar']['endTime'] = $Time->format('I') ? 1 : 0; |
---|
| 517 | |
---|
| 518 | if($currentTimezone != $v['timezone']){ |
---|
| 519 | $currentTimezone = $v['timezone']; |
---|
| 520 | $Time->setTimezone(new DateTimeZone($v['timezone'])); |
---|
| 521 | |
---|
| 522 | $Time->setTimestamp( (int)($v['startTime'] / 1000)); |
---|
| 523 | $DayLigth['event']['startTime'] = $Time->format('I') ? 1 : 0; |
---|
| 524 | |
---|
| 525 | $Time->setTimestamp( (int)($v['endTime'] / 1000)); |
---|
| 526 | $DayLigth['event']['endTime'] = $Time->format('I') ? 1 : 0; |
---|
| 527 | }else |
---|
| 528 | $DayLigth['event'] = $DayLigth['calendar']; |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | $result[$i]['DayLigth'] = $DayLigth; |
---|
| 532 | |
---|
[5723] | 533 | if(isset($v['id'])) |
---|
| 534 | { |
---|
[5341] | 535 | $participants = Controller::find( array( 'concept' => 'participant' ) , false ,array( 'filter' => array('=', 'schedulable' , $v['id']) )); |
---|
| 536 | |
---|
| 537 | if(is_array($participants) && count($participants) > 0) |
---|
| 538 | foreach ($participants as $ii => $vv) |
---|
| 539 | { |
---|
| 540 | if($vv['isExternal'] == 1 ) |
---|
| 541 | $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] , 'service' => 'PostgreSQL' ) ); |
---|
| 542 | else |
---|
| 543 | $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] )); |
---|
| 544 | |
---|
| 545 | if($participants[$ii]['user']['id'] == Config::me('uidNumber')) |
---|
[5376] | 546 | $participants[$ii]['alarms'] = Controller::find( array( 'concept' => 'alarm' ) , null , array('filter' => array('AND' , array('=', 'participant' ,$vv['id'] ) ,array('=' , 'schedulable', $v['id'])) ) ); |
---|
[5341] | 547 | } |
---|
| 548 | $result[$i]['participants'] = $participants; |
---|
[5592] | 549 | |
---|
| 550 | |
---|
[5715] | 551 | |
---|
| 552 | $attachmentRelation = Controller::find( array( 'concept' => 'schedulableToAttachment' ) , false ,array( 'filter' => array('=', 'schedulable' , $v['id']) )); |
---|
| 553 | if(is_array($attachmentRelation)){ |
---|
| 554 | $attachments = array(); |
---|
| 555 | foreach($attachmentRelation as $key => $value) |
---|
| 556 | if(isset($value['attachment']) || !!$value['attachment']) |
---|
| 557 | $attachments[$key] = $value['attachment']; |
---|
| 558 | //Pega os anexos sem source |
---|
| 559 | $result[$i]['attachments'] = Controller::find( array( 'concept' => 'attachment' ) , array('id', 'name', 'type', 'size') ,array( 'filter' => array('IN', 'id' , $attachments) )); |
---|
| 560 | } |
---|
| 561 | |
---|
[5723] | 562 | $repeat = Controller::find( array( 'concept' => 'repeat' ), false, array( 'filter' => array( '=', 'schedulable', $v['id'] ) ) ); |
---|
[5715] | 563 | |
---|
[5741] | 564 | unset( $result[$i]['repeat'] ); |
---|
| 565 | |
---|
[5723] | 566 | if( is_array($repeat) ) |
---|
| 567 | $result[$i]['repeat'] = $repeat[0]; |
---|
| 568 | } |
---|
[5514] | 569 | |
---|
[5341] | 570 | } |
---|
| 571 | } |
---|
| 572 | |
---|
[5437] | 573 | return $result; |
---|
| 574 | |
---|
[5341] | 575 | } |
---|
[5592] | 576 | |
---|
[5341] | 577 | //TODO: Remover apos suporte a deepness na api |
---|
| 578 | public function deepnessReadEvent( &$uri , &$result , &$criteria , $original ){ |
---|
| 579 | |
---|
[5715] | 580 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != 0) |
---|
[5341] | 581 | { |
---|
| 582 | if(isset($result['id'])) |
---|
| 583 | { |
---|
| 584 | $participants = Controller::find( array( 'concept' => 'participant' ) , false ,array( 'filter' => array('=' , 'schedulable' , $result['id']) )); |
---|
| 585 | if(is_array($participants)) |
---|
| 586 | foreach ($participants as $ii => $vv) |
---|
| 587 | { |
---|
| 588 | if($vv['isExternal'] == 1 ) |
---|
| 589 | $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] , 'service' => 'PostgreSQL' ) ); |
---|
| 590 | else |
---|
| 591 | $participants[$ii]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $vv['user'] )); |
---|
| 592 | |
---|
[5376] | 593 | if($participants[$ii]['user']['id'] == Config::me('uidNumber')) |
---|
| 594 | $participants[$ii]['alarms'] = Controller::find( array( 'concept' => 'alarm' ) , null , array('filter' => array('AND' , array('=', 'participant' ,$vv['id'] ) ,array('=' , 'schedulable', $result['id'])) ) ); |
---|
| 595 | } |
---|
[5798] | 596 | $repeat = Controller::find( array( 'concept' => 'repeat' ), false, array( 'filter' => array( '=', 'schedulable', $result['id'] ) ) ); |
---|
| 597 | if(is_array($repeat)) |
---|
| 598 | $result['repeat'] = $repeat[0] ; |
---|
| 599 | |
---|
| 600 | $result['participants'] = $participants; |
---|
[5376] | 601 | } |
---|
[5341] | 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | //TODO: Remover apos suporte a deepness na api |
---|
| 606 | public function deepnessReadParticipant( &$uri , &$result , &$criteria , $original ){ |
---|
| 607 | |
---|
[5715] | 608 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != 0) |
---|
[5341] | 609 | { |
---|
| 610 | if(isset($result['id'])) |
---|
| 611 | { |
---|
| 612 | if($result['isExternal'] == 1 ) |
---|
| 613 | $result['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $result['user'] , 'service' => 'PostgreSQL' ) ); |
---|
| 614 | else |
---|
| 615 | $result['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $result['user'] )); |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | //TODO: Remover apos suporte a deepness na api |
---|
| 622 | public function deepnessFindParticipant( &$uri , &$result , &$criteria , $original ){ |
---|
| 623 | |
---|
[5715] | 624 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != 0) |
---|
[5341] | 625 | { |
---|
| 626 | foreach ($result as $i => $v) |
---|
| 627 | { |
---|
| 628 | if(isset($v['id'])) |
---|
| 629 | { |
---|
| 630 | if($result[$i]['isExternal'] == 1 ) |
---|
| 631 | $result[$i]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $result[$i]['user'] , 'service' => 'PostgreSQL' ) ); |
---|
| 632 | else |
---|
| 633 | $result[$i]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $result[$i]['user'] )); |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | //TODO: Remover apos suporte a deepness na api |
---|
| 641 | public function deepnessReadCalendarSignature( &$uri , &$result , &$criteria , $original ){ |
---|
| 642 | |
---|
[5715] | 643 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != 0) |
---|
[5341] | 644 | if(isset($result['calendar'])){ |
---|
| 645 | $result['calendar'] = Controller::read( array( 'concept' => 'calendar' , 'id' => $result['calendar'])); |
---|
| 646 | $result['defaultAlarms'] = Controller::find( array( 'concept' => 'calendarSignatureAlarm' ) , false ,array( 'filter' => array('=', 'calendarSignature' , $result['id']) )); |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | //TODO: Remover apos suporte a deepness na api |
---|
| 652 | public function deepnessFindCalendarSignature( &$uri , &$result , &$criteria , $original ){ |
---|
[5641] | 653 | |
---|
[5715] | 654 | if(isset($original['criteria']['deepness']) && $original['criteria']['deepness'] != 0) |
---|
[5341] | 655 | { |
---|
| 656 | foreach ($result as $i => $v) |
---|
| 657 | { |
---|
| 658 | if(isset($v['calendar'])){ |
---|
[5641] | 659 | $result[$i]['calendar'] = Controller::read( array( 'concept' => 'calendar' , 'id' => $v['calendar']), false, false); |
---|
[5804] | 660 | $result[$i]['defaultAlarms'] = Controller::find( array( 'concept' => 'calendarSignatureAlarm' ) , false ,array( 'filter' => array('=', 'calendarSignature' , $v['id']) )); |
---|
| 661 | //Caso não seja o dono da agenda retorna o objeto permission com as acls |
---|
| 662 | if($result[$i]['isOwner'] == 0){ |
---|
| 663 | $permission = Controller::find( array( 'concept' => 'calendarToPermission'), false ,array( 'filter' => array('AND', array('=', 'calendar' , $v['calendar']), array('=', 'user', Config::me('uidNumber') ) ) ) ); |
---|
[5592] | 664 | |
---|
[5804] | 665 | if(!is_array($permission) || !$permission ){ |
---|
[5641] | 666 | |
---|
[5804] | 667 | $permission = Controller::find( array( 'concept' => 'calendarToPermission'), false ,array( 'filter' => array('AND', array('=', 'calendar' , $v['calendar']), array('=', 'type', '1' ) ) ) ); |
---|
[5641] | 668 | |
---|
[5804] | 669 | } |
---|
| 670 | $result[$i]['permission'] = $permission[0]; |
---|
| 671 | } |
---|
| 672 | } |
---|
| 673 | //TODO - Padronizar retorno do deepness |
---|
[5947] | 674 | if(isset($v['user'])) |
---|
| 675 | $result[$i]['user'] = Controller::read( array( 'concept' => 'user' , 'id' => $v['user']), false, false); |
---|
[5804] | 676 | } |
---|
[5641] | 677 | } |
---|
[5341] | 678 | } |
---|
| 679 | //Decode Find |
---|
| 680 | public function decodeFindConcept( &$uri , &$result , &$criteria , $original ){ |
---|
| 681 | if($result && is_array($result) ) |
---|
| 682 | { |
---|
| 683 | $m = array_flip(self::${$uri['concept'].'Map'}); |
---|
| 684 | $new = array(); |
---|
| 685 | foreach ($result as $i => $v) |
---|
| 686 | $new[$i] = self::parseConcept( $result[$i] , $m ); |
---|
| 687 | |
---|
| 688 | |
---|
| 689 | $result = $new; |
---|
| 690 | } |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | public function decodeFindSchedulable( &$uri , &$result , &$criteria , $original ){ |
---|
| 694 | if($result && is_array($result) ) |
---|
| 695 | { |
---|
| 696 | $m = array_flip(self::${$uri['concept'].'Map'}); |
---|
| 697 | $m['calendar_id'] = 'calendar'; |
---|
| 698 | $new = array(); |
---|
| 699 | foreach ($result as $i => $v) |
---|
| 700 | $new[$i] = self::parseConcept( $result[$i] , $m ); |
---|
| 701 | |
---|
| 702 | |
---|
| 703 | $result = $new; |
---|
| 704 | } |
---|
| 705 | } |
---|
[5514] | 706 | |
---|
| 707 | public function decodeFindAttachment( &$uri , &$result , &$criteria , $original ){ |
---|
| 708 | if(isset($result)) |
---|
| 709 | foreach($result as $key => &$value) |
---|
| 710 | $value['source'] = base64_decode($value['source']); |
---|
| 711 | } |
---|
| 712 | |
---|
[5341] | 713 | |
---|
| 714 | public function decodeSignatureAlarmType( &$uri , &$result , &$criteria , $original ){ |
---|
| 715 | if( is_array($result) ) |
---|
| 716 | foreach($result as &$param) |
---|
| 717 | if(isset($param['type'])) |
---|
| 718 | $param['type'] = self::decodeAlarmType($param['type']); |
---|
| 719 | |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | ///////////////////////////////////////////////////////////////////////// |
---|
| 723 | |
---|
| 724 | static function dayAlarm( &$uri , &$params , &$criteria , $original ) { |
---|
| 725 | if(isset($criteria['filter'][1]) && $criteria['filter'][1] == 'date') |
---|
| 726 | { |
---|
| 727 | $filter = array( 'AND', |
---|
| 728 | array( '=' , 'sent' , '0') , |
---|
| 729 | array( '=' , 'type' , ALARM_ALERT) , |
---|
[5437] | 730 | array( '>=' , 'rangeStart' , $criteria['filter'][2]) , |
---|
[5724] | 731 | array( '<=' , 'rangeEnd' , $criteria['filter'][2] + 86400000) ); |
---|
[5341] | 732 | |
---|
| 733 | //Busca os Alarmes no Range |
---|
| 734 | $al = Controller::find( array('concept' => 'alarm'), false ,array( 'filter' => $filter)); |
---|
| 735 | |
---|
| 736 | if(is_array($al)) |
---|
| 737 | foreach ($al as $i => $v) |
---|
| 738 | { |
---|
| 739 | $ev = Controller::read( array( 'concept' => 'schedulable' , 'id' => $v['schedulable']) ) ; |
---|
| 740 | |
---|
| 741 | $unit = 0; |
---|
| 742 | switch (strtolower($v['unit'])) { |
---|
| 743 | case 'm': $unit = 60; break; |
---|
| 744 | case 'H': $unit = 3600; break; |
---|
| 745 | case 'd': $unit = 216000; break; } |
---|
| 746 | |
---|
| 747 | $al[$i]['sendTime'] = (((int)( $ev['startTime'] / 1000 )) - ( $v['time'] * $unit )); |
---|
| 748 | $al[$i]['schedulable'] = $ev; |
---|
| 749 | |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | $params = $al; |
---|
| 753 | return false; |
---|
| 754 | } |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | public function deleteSchedulable( &$uri , &$params , &$criteria , $original ){ |
---|
| 758 | |
---|
[5907] | 759 | if(Config::module('useCaldav' , 'expressoCalendar')) |
---|
[5341] | 760 | require_once ROOTPATH.'/modules/calendar/interceptors/DAViCalAdapter.php'; |
---|
| 761 | |
---|
| 762 | $calendarsToSchedulable = self::schedulable2calendarToObject($uri['id']); |
---|
[5998] | 763 | |
---|
| 764 | if( !self::ownerSchedulable($uri['id']) && !self::isAllowDeleteInCalendar($calendarsToSchedulable[0]['calendar_id'])) |
---|
| 765 | { |
---|
| 766 | |
---|
[5341] | 767 | foreach ($calendarsToSchedulable as $i => $v) |
---|
| 768 | { |
---|
| 769 | Controller::delete(array('concept' => 'calendarToSchedulable' , 'id' => $v['calendar_to_calendar_object'])); |
---|
| 770 | |
---|
[5907] | 771 | if(Config::module('useCaldav' , 'expressoCalendar')) |
---|
| 772 | DAViCalAdapter::deleteEvent($uri['id'], array('location' => $v['calendar_location'])); |
---|
[5341] | 773 | } |
---|
[5441] | 774 | |
---|
[5341] | 775 | /* Seta o participante como rejeitado */ |
---|
| 776 | Controller::update(array('concept' => 'participant'), array('status' => STATUS_CANCELLED) , |
---|
| 777 | array('filter' => |
---|
| 778 | array('AND', |
---|
[5441] | 779 | array('=' , 'user' ,Config::me('uidNumber') ), |
---|
[5341] | 780 | array('=' , 'schedulable' , $uri['id'] ) |
---|
| 781 | ))); |
---|
| 782 | |
---|
[5729] | 783 | return false; /* Mata o restante da execução */ |
---|
[5341] | 784 | } |
---|
[5907] | 785 | |
---|
| 786 | if(Config::module('useCaldav' , 'expressoCalendar')) |
---|
[5341] | 787 | foreach ($calendarsToSchedulable as $i => $v) |
---|
[5907] | 788 | DAViCalAdapter::deleteEvent($uri['id'], array('location' => $v['calendar_location'])); |
---|
[5341] | 789 | |
---|
| 790 | } |
---|
[5812] | 791 | |
---|
| 792 | |
---|
| 793 | public function deleteCalendarToPermissionDependences( &$uri , &$params , &$criteria , $original ){ |
---|
| 794 | $permission = Controller::read($uri, array('user','calendar')); |
---|
[5341] | 795 | |
---|
[5863] | 796 | $calendarSignature = Controller::find( array( 'concept' => 'calendarSignature' ) , array('id') ,array( 'filter' => array('AND', array('=','calendar', $permission['calendar']), array('=','user',$permission['user']), array('=','isOwner','0')))); |
---|
[5812] | 797 | |
---|
| 798 | if($calendarSignature) |
---|
| 799 | Controller::delete(array('concept' => 'calendarSignature', 'id' => $calendarSignature[0]['id'])); |
---|
| 800 | } |
---|
| 801 | |
---|
[5341] | 802 | public function deleteCalendarSignatureDependences( &$uri , &$params , &$criteria , $original ){ |
---|
| 803 | $signature = Controller::read($uri,array('isOwner','calendar')); |
---|
| 804 | |
---|
| 805 | if($signature['isOwner'] == '1') |
---|
| 806 | { |
---|
| 807 | $calendarToSchedulables = Controller::find(array('concept' => 'calendarToSchedulable') , null , array('filter' => array('=' , 'calendar' , $signature['calendar'] ))); |
---|
| 808 | |
---|
| 809 | $schedulables = array(); |
---|
| 810 | if(is_array($calendarToSchedulables)) |
---|
| 811 | foreach ($calendarToSchedulables as $key => $calendarToSchedulable) |
---|
| 812 | $schedulables[] = $calendarToSchedulable['schedulable'] ; |
---|
| 813 | |
---|
| 814 | if( !empty( $schedulables ) ) |
---|
| 815 | Controller::deleteALL(array('concept' => 'schedulable'),null , array('filter' => array('IN' , 'id' , $schedulables))); |
---|
| 816 | |
---|
| 817 | Controller::delete(array('concept' => 'calendar', 'id' => $signature['calendar'])); |
---|
| 818 | } |
---|
| 819 | |
---|
[5514] | 820 | } |
---|
| 821 | |
---|
| 822 | public function deleteAttachmentDependences( &$uri , &$params , &$criteria , $original ){ |
---|
[5341] | 823 | |
---|
[5514] | 824 | if(isset($original['URI']['id'])) |
---|
| 825 | Controller::delete(array('concept' => 'schedulableToAttachment', null , array('filter' => array('=' , 'attachment' , $original['URI']['id'])))); |
---|
| 826 | } |
---|
| 827 | |
---|
[5341] | 828 | public function createDefaultSignature( &$uri , &$result , &$criteria , $original ){ |
---|
[5863] | 829 | |
---|
| 830 | //Caso uma busca não retorne nenhum resultado e foi buscado pelas assinaturas do usuario logado apenas |
---|
| 831 | $isValidSignature = false; |
---|
[5947] | 832 | |
---|
[5863] | 833 | //Veirifica pois o usuário pode ter varias assinaturas mas não ser dona de nenhuma |
---|
| 834 | if(count($result) > 0){ |
---|
| 835 | foreach($result as $key){ |
---|
| 836 | if($key['isOwner'] != 0) |
---|
| 837 | $isValidSignature = true; |
---|
| 838 | } |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | if(!$isValidSignature && |
---|
| 842 | ( $original['criteria']['filter'][1][0] == '=' && |
---|
| 843 | $original['criteria']['filter'][1][1] == 'user' && |
---|
| 844 | $original['criteria']['filter'][1][2] == $_SESSION['phpgw_session']['account_id'] |
---|
[5341] | 845 | )) |
---|
| 846 | { |
---|
| 847 | |
---|
[5907] | 848 | if( Config::module('useCaldav' , 'expressoCalendar') ) |
---|
[5341] | 849 | { |
---|
| 850 | require_once ROOTPATH.'/modules/calendar/interceptors/DAViCalAdapter.php'; |
---|
| 851 | |
---|
| 852 | $calendario = DAViCalAdapter::findCalendars(); |
---|
| 853 | } |
---|
| 854 | |
---|
[5907] | 855 | if( Config::module('useCaldav' , 'expressoCalendar') && is_array($calendario) && count($calendario) > 0) |
---|
[5341] | 856 | { |
---|
| 857 | foreach ($calendario as $i => $v) |
---|
| 858 | { |
---|
| 859 | |
---|
| 860 | $urlA = explode('/', $v->url); |
---|
| 861 | $name = isset($v->displayname)? $v->displayname : $urlA[ (count($urlA)-2) ]; |
---|
| 862 | $cal = array('name' => $name, |
---|
| 863 | 'description' => isset($v->description)? $v->description : $name, |
---|
| 864 | 'timezone' => isset($v->timezone) ? $v->timezone : (date_default_timezone_get()) ? date_default_timezone_get() : 'America/Sao_Paulo', |
---|
| 865 | 'dtstamp' => time().'000', |
---|
[5947] | 866 | 'location' => $urlA[ (count($urlA)-3) ].'/'.$urlA[ (count($urlA)-2) ] |
---|
[5341] | 867 | ); |
---|
| 868 | |
---|
| 869 | $calCreated = Controller::create( array('concept' => 'calendar') , $cal ); |
---|
| 870 | |
---|
| 871 | |
---|
| 872 | |
---|
| 873 | $sig = array( 'user' => $_SESSION['wallet']['user']['uidNumber'], |
---|
| 874 | 'calendar' => $calCreated['id'], |
---|
| 875 | 'isOwner' => '1', |
---|
| 876 | 'dtstamp' => time().'000', |
---|
| 877 | 'fontColor' => 'FFFFFF', |
---|
| 878 | 'backgroundColor' => '3366CC', |
---|
| 879 | 'borderColor' => '3366CC', |
---|
| 880 | ); |
---|
| 881 | |
---|
| 882 | $sigCreated = Controller::create( array('concept' => 'calendarSignature') , $sig ); |
---|
| 883 | |
---|
| 884 | |
---|
| 885 | DAViCalAdapter::importCollection($v->url , $calCreated['id']); |
---|
| 886 | |
---|
| 887 | } |
---|
| 888 | } |
---|
| 889 | else |
---|
| 890 | { |
---|
[5729] | 891 | //Criaremos uma agenda padrão |
---|
[5341] | 892 | $cal = array('name' => 'Calendario', |
---|
[5729] | 893 | 'description' => 'Calendario Padrão', |
---|
[5341] | 894 | 'timezone' => (date_default_timezone_get()) ? date_default_timezone_get() : 'America/Sao_Paulo', |
---|
| 895 | 'dtstamp' => time().'000' |
---|
| 896 | ); |
---|
| 897 | |
---|
| 898 | $calCreated = Controller::create( array('concept' => 'calendar') , $cal ); |
---|
| 899 | |
---|
| 900 | $sig = array( 'user' => $_SESSION['wallet']['user']['uidNumber'], |
---|
| 901 | 'calendar' => $calCreated['id'], |
---|
| 902 | 'isOwner' => '1', |
---|
| 903 | 'dtstamp' => time().'000', |
---|
| 904 | 'fontColor' => 'FFFFFF', |
---|
| 905 | 'backgroundColor' => '3366CC', |
---|
| 906 | 'borderColor' => '3366CC', |
---|
| 907 | ); |
---|
| 908 | |
---|
| 909 | $sigCreated = Controller::create( array('concept' => 'calendarSignature') , $sig ); |
---|
| 910 | } |
---|
| 911 | |
---|
| 912 | $result = Controller::find($original['URI'] , $original['properties'] ? $original['properties'] : null , $original['criteria'] ); |
---|
| 913 | return false; |
---|
| 914 | } |
---|
| 915 | |
---|
| 916 | } |
---|
[5850] | 917 | //TODO - Criar conceito separado para participantes externos e remover o criterio notExternal |
---|
| 918 | public function findExternalPaticipants( &$uri , &$result , &$criteria , $original ) { |
---|
| 919 | if(Config::me('uidNumber') && !isset($criteria['notExternal'])) |
---|
[5462] | 920 | { |
---|
| 921 | $newuri['concept'] = 'user'; |
---|
| 922 | $newuri['service'] = 'PostgreSQL'; |
---|
| 923 | |
---|
| 924 | $newCriteria = $original['criteria']; |
---|
| 925 | $valid = true; |
---|
| 926 | |
---|
[5437] | 927 | $newCriteria['filter'] = array('AND',$newCriteria['filter'] , array('=' , 'owner', Config::me('uidNumber')) ); |
---|
[5366] | 928 | $externalUsers = Controller::find( $newuri , $original['properties'] ? $original['properties'] : null , $newCriteria ); |
---|
[5462] | 929 | |
---|
[5366] | 930 | if(is_array($externalUsers)) |
---|
| 931 | { |
---|
[5462] | 932 | foreach ($externalUsers as $i => $v) |
---|
[5366] | 933 | $externalUsers[$i]['isExternal'] = '1'; |
---|
| 934 | } |
---|
| 935 | else |
---|
| 936 | $externalUsers = array(); |
---|
| 937 | |
---|
| 938 | if(!is_array($result)) |
---|
[5462] | 939 | $result = array(); |
---|
[5366] | 940 | |
---|
| 941 | return array_merge($result, $externalUsers); |
---|
[5462] | 942 | } |
---|
[5366] | 943 | |
---|
[5341] | 944 | } |
---|
| 945 | |
---|
| 946 | public function davcalCreateCollection( &$uri , &$params , &$criteria , $original ){ |
---|
[5907] | 947 | if( Config::module('useCaldav' , 'expressoCalendar') ) |
---|
[5341] | 948 | { |
---|
| 949 | require_once ROOTPATH.'/modules/calendar/interceptors/DAViCalAdapter.php'; |
---|
[5907] | 950 | DAViCalAdapter::mkcalendar($params['location'] , $params['name'], isset($params['description']) ? $params['description'] : '' ); |
---|
[5341] | 951 | } |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | public function davcalDeleteCollection( &$uri , &$params , &$criteria , $original ){ |
---|
[5907] | 955 | if( Config::module('useCaldav' , 'expressoCalendar') && Config::module('onRemoveCascadeCalDav')) |
---|
[5341] | 956 | { |
---|
| 957 | require_once ROOTPATH.'/modules/calendar/interceptors/DAViCalAdapter.php'; |
---|
| 958 | $calendar = Controller::read($uri); |
---|
| 959 | DAViCalAdapter::rmCalendar($calendar['location']); |
---|
| 960 | } |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | public function davcalUpdateCollection( &$uri , &$params , &$criteria , $original ){ |
---|
[5907] | 964 | if( Config::module('useCaldav' , 'expressoCalendar') ) |
---|
[5341] | 965 | { |
---|
| 966 | require_once ROOTPATH.'/modules/calendar/interceptors/DAViCalAdapter.php'; |
---|
| 967 | if(isset($params['location'])) |
---|
| 968 | { |
---|
| 969 | $calendar = Controller::read($uri); |
---|
| 970 | if($calendar['location'] !== $params['location']) |
---|
| 971 | DAViCalAdapter::mvcalendar($calendar['location'], $params['location']); |
---|
| 972 | } |
---|
| 973 | } |
---|
| 974 | } |
---|
| 975 | |
---|
| 976 | private function _makeUid() { |
---|
| 977 | |
---|
| 978 | $date = date('Ymd\THisT'); |
---|
| 979 | $unique = substr(microtime(), 2, 4); |
---|
| 980 | $base = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPrRsStTuUvVxXuUvVwWzZ1234567890'; |
---|
| 981 | $start = 0; |
---|
| 982 | $end = strlen( $base ) - 1; |
---|
| 983 | $length = 6; |
---|
| 984 | $str = null; |
---|
| 985 | for( $p = 0; $p < $length; $p++ ) |
---|
| 986 | $unique .= $base{mt_rand( $start, $end )}; |
---|
| 987 | |
---|
| 988 | return $date.$unique.'@expresso-calendar'; |
---|
| 989 | } |
---|
| 990 | private function getStatus( $id ){ |
---|
| 991 | $a = array( |
---|
| 992 | STATUS_CONFIRMED => 'CONFIRMED', |
---|
| 993 | STATUS_CANCELLED => 'CANCELLED', |
---|
| 994 | STATUS_TENATIVE => 'TENATIVE', |
---|
| 995 | STATUS_UNANSWERED => 'NEEDS-ACTION', |
---|
| 996 | STATUS_DELEGATED => 'DELEGATED' |
---|
| 997 | ); |
---|
| 998 | return $a[ $id ]; |
---|
| 999 | } |
---|
| 1000 | private static function decodeAlarmType( $id ){ |
---|
| 1001 | $a = array( ALARM_ALERT => 'alert', |
---|
| 1002 | ALARM_MAIL => 'mail', |
---|
| 1003 | ALARM_SMS => 'sms' ); |
---|
| 1004 | |
---|
| 1005 | return $a[ $id ]; |
---|
| 1006 | } |
---|
| 1007 | private static function codeAlarmType( $type ){ |
---|
| 1008 | $a = array( 'alert' => ALARM_ALERT , |
---|
| 1009 | 'mail' => ALARM_MAIL , |
---|
| 1010 | 'sms' => ALARM_SMS ); |
---|
| 1011 | |
---|
| 1012 | return $a[ $type ]; |
---|
| 1013 | } |
---|
| 1014 | private static function codeAlarmUnit( $u ){ |
---|
| 1015 | if($u === 'd') return 'days'; |
---|
| 1016 | if($u === 'm') return 'minutes'; |
---|
| 1017 | if($u === 'H') return 'hours'; |
---|
| 1018 | } |
---|
| 1019 | |
---|
| 1020 | private static function ownerSchedulable( $id ){ |
---|
| 1021 | |
---|
| 1022 | $isOwner = Controller::find(array('concept' => 'participant'), array('id') , |
---|
| 1023 | array('filter' => |
---|
| 1024 | array('AND', |
---|
| 1025 | array('=' , 'isOrganizer' , '1'), |
---|
| 1026 | array('=' , 'user' ,$_SESSION['wallet']['user']['uidNumber'] ), |
---|
| 1027 | array('=' , 'schedulable' ,$id ) |
---|
| 1028 | ))); |
---|
| 1029 | |
---|
| 1030 | return ( isset( $isOwner[0]['id'] ) ) ? true : false; |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | private static function schedulable2calendarToObject($Schedulable) |
---|
| 1034 | { |
---|
[5715] | 1035 | return Controller::service('PostgreSQL')->execResultSql('SELECT calendar_to_calendar_object.id as calendar_to_calendar_Object , calendar.name as calendar_name ,calendar.location as calendar_location, calendar.id as calendar_id FROM calendar_to_calendar_object , calendar , calendar_signature' |
---|
[5341] | 1036 | .' WHERE calendar_signature.user_uidnumber = '.$_SESSION['wallet']['user']['uidNumber'] |
---|
[5907] | 1037 | // .' AND calendar_signature.is_owner = 1' |
---|
[5341] | 1038 | .' AND calendar_signature.calendar_id = calendar.id' |
---|
| 1039 | .' AND calendar_to_calendar_object.calendar_id = calendar.id' |
---|
| 1040 | .' AND calendar_to_calendar_object.calendar_object_id = '.addslashes($Schedulable)); |
---|
| 1041 | } |
---|
[5998] | 1042 | |
---|
| 1043 | protected static function isAllowDeleteInCalendar( $calendar ) { |
---|
| 1044 | $f = Controller::find(array('concept' => 'calendarToPermission') , false , array('filter' => array('AND' , array('=', 'user' , Config::me('uidNumber') ) , array('=', 'calendar' , $calendar)))); |
---|
| 1045 | return (strpos($f[0]['acl'], CALENDAR_ACL_REMOVE) === false) ? false : true; |
---|
| 1046 | } |
---|
| 1047 | |
---|
[5341] | 1048 | } |
---|
| 1049 | |
---|
| 1050 | ?> |
---|