/** * Return a list of calendars name of a specif caldav calendar * @param Request $request * @return \Illuminate\Http\Response */ public function getCalendars(Request $request) { //TODO test this $this->validate($request, ['url' => 'required|max:255', 'username' => 'required|max:255', 'password' => 'required']); try { $caldavClient = new SimpleCaldavAdapter(); $caldavClient->connect($request->input('url'), $request->input('username'), $request->input('password')); $calendars = $caldavClient->findCalendars(); return array_keys($calendars); } catch (\it\thecsea\caldav_client_adapter\CaldavException $e) { return Response::json(['error' => $e->getMessage()], 422); } }
/** * @return array|\it\thecsea\caldav_client_adapter\EventInterface[] * @throws \it\thecsea\caldav_client_adapter\CaldavException * @throws \Illuminate\Contracts\Encryption\DecryptException * @throws CaldavException */ private function getEvents() { //TODO catch php errors like array key $caldavClient = new SimpleCaldavAdapter(); $caldavClient->connect($this->calendar->url, $this->calendar->username, \Crypt::decrypt($this->calendar->password)); $calendars = $caldavClient->findCalendars(); if (!isset($calendars[$this->calendar->calendar_name])) { throw new CaldavException("calendar inserted doesn't exist"); } $caldavClient->setCalendar($calendars[$this->calendar->calendar_name]); /** * 26 hours before to avoid tiemezone problems and dst problems * 30 days after */ return $caldavClient->getEvents(date('Ymd\\THis\\Z', time() - 93600), date('Ymd\\THis\\Z', time() + 2592000)); }