Skip to content

Commit

Permalink
Support of recurring events #38
Browse files Browse the repository at this point in the history
Now, you will get all the recurring events in a given date of range.

Note: You have to pass bearer token, start_date and end_date for getting recurring events.
  • Loading branch information
gaurav-handysolver committed Aug 5, 2022
1 parent 4b7ebb0 commit cd97be8
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public static function onRestApiAddRules()
['pattern' => 'mail/<messageId:\d+>/entries', 'route' => 'smartVillage/mail/entry/index', 'verb' => 'GET'],

//calendar
//Recurring Events endpoints
['pattern' => 'calendar/recurring', 'route' => 'smartVillage/calendar/recurring-events/index', 'verb' => 'GET'],
['pattern'=> 'calendar/container/<containerId:\d+>/recurring', 'route'=> 'smartVillage/calendar/recurring-events/recurring-container','verb'=>'GET'],
['pattern' => 'calendar/entry/<Id:\d+>/recurring','route' => 'smartVillage/calendar/recurring-events/recurring-entry','verb'=>'GET'],


['pattern' => 'calendar', 'route' => 'smartVillage/calendar/calendar/find', 'verb' => 'GET'],
['pattern' => 'calendar/entry/<Id:\d+>', 'route' => 'smartVillage/calendar/calendar/view', 'verb' => 'GET'],
['pattern' => 'calendar/container/<containerId:\d+>', 'route' => 'smartVillage/calendar/calendar/find-by-container', 'verb' => ['GET', 'HEAD']],
Expand Down
176 changes: 176 additions & 0 deletions controllers/calendar/RecurringEventsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace humhub\modules\smartVillage\controllers\calendar;

use DateTime;
use humhub\modules\calendar\interfaces\CalendarService;
use humhub\modules\calendar\models\CalendarEntry;
use humhub\modules\calendar\models\fullcalendar\FullCalendar;
use humhub\modules\content\components\ActiveQueryContent;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\rest\components\BaseContentController;
use humhub\modules\rest\definitions\ContentDefinitions;

class RecurringEventsController extends BaseContentController
{
public static $moduleId = 'Calendar';

/**
* {@inheritdoc}
*/
public function getContentActiveRecordClass()
{
return CalendarEntry::class;
}

/**
* {@inheritdoc}
*/
public function returnContentDefinition(ContentActiveRecord $contentRecord)
{
/** @var CalendarEntry $contentRecord */
return FullCalendar::getFullCalendarArray($contentRecord);
}

/**
* @return array
* @throws \Throwable
* Get all the recurring events lies between start_date and end_date
* We filter the events based on their title
*/
public function actionIndex(){
$output = [];
if(!isset($_GET['start_date']) || !isset($_GET['end_date'])){
return $this->returnError(400,"start_date and end_date parameters are required!");
}

//Get all the calendar entries lies between the start_date and end_date
$calendarService = new CalendarService();
$entries = $calendarService->getCalendarItems(new DateTime($_GET['start_date']), new DateTime($_GET['end_date']));

//If the calendar entry not found in given date of range
if($entries == null){
return $this->returnError(404, "No recurring events are present between ".$_GET['start_date'] ." and ". $_GET['end_date']);
}
//Get those calendar events which are recurring
$calendarEntryTitle = [];
$calendarEntries = CalendarEntry::find()->all();
foreach($calendarEntries as $calendarEntry){
if(isset($calendarEntry->rrule)){
array_push($calendarEntryTitle,$calendarEntry['title']);
}
}

foreach ($entries as $entry) {
/** @var ContentActiveRecord $entry */
//return only recurring events
if(in_array($entry['title'],$calendarEntryTitle)){
$output[] = $this->returnContentDefinition($entry);
}
}
//If recurring event not found between given date range
if($output == null){
return $this->returnError(404, "No recurring events are present between ".$_GET['start_date'] ." and ". $_GET['end_date']);
}
return $output;
}

/**
* @param $containerId
* @return array
* @throws \Throwable
* @throws \yii\base\Exception
* @throws \yii\db\IntegrityException
* $containerId = Id of the container
* Get all the recurring events based on the containerId
*/
public function actionRecurringContainer($containerId){
$contentContainer = ContentContainer::findOne(['id'=>$containerId]);
if($contentContainer == null){
return $this->returnError(404,"Content container not found!");
}

if(!isset($_GET['start_date']) || !isset($_GET['end_date'])){
return $this->returnError(400,"start_date and end_date parameters are required!");
}

/** @var ActiveQueryContent $calendarEntries */
$calendarEntries = CalendarEntry::find()->contentContainer($contentContainer->getPolymorphicRelation())->orderBy(['content.created_at' => SORT_DESC])->readable();

ContentDefinitions::handleTopicsParam($calendarEntries, $containerId);

//Get those calendar events which are recurring
$calendarEntryTitle = [];
foreach($calendarEntries->all() as $calendarEntry){
/** @var ContentActiveRecord $calendarEntry */
if(isset($calendarEntry->rrule)){
array_push($calendarEntryTitle,$calendarEntry['title']);
}
}

$output = [];

//Get all the calendar entries lies between the start_date and end_date
$calendarService = new CalendarService();
$entries = $calendarService->getCalendarItems(new DateTime($_GET['start_date']), new DateTime($_GET['end_date']));

foreach ($entries as $entry) {
/** @var ContentActiveRecord $entry */
//Filter out the entries by checking rrule and title
if(in_array($entry['title'],$calendarEntryTitle)){
$output[] = $this->returnContentDefinition($entry);
}

}
//If recurring event not found between given date range
if($output == null){
return $this->returnError(404, "No recurring events are present between ".$_GET['start_date'] ." and ". $_GET['end_date']);
}

return $output;
}

/**
* @param $Id
* @return array
* @throws \Throwable
* $id = id of the calendar entry
* Get all the recurring events based on the id of calendar entry
*
*/
public function actionRecurringEntry($Id){
$output = [];
$calendarEntry = CalendarEntry::findOne($Id);

if($calendarEntry == null){
return $this->returnError(404,"Calendar entry not found!");
}
//If Calendar entry is present but it is not recurring
if(!isset($calendarEntry->rrule)){
return $this->returnError(400,"Calendar event is not recurring");
}
if(!isset($_GET['start_date']) || !isset($_GET['end_date'])){
return $this->returnError(400,"start_date and end_date parameters are required!");
}

$calendarService = new CalendarService();
$entries = $calendarService->getCalendarItems(new DateTime($_GET['start_date']), new DateTime($_GET['end_date']));

//Get all the calendar entries lies between the start_date and end_date
foreach ($entries as $entry) {
/** @var ContentActiveRecord $entry */
//Filter out the entries by checking rrule and title
if($calendarEntry->title == $entry['title']){
$output[] = $this->returnContentDefinition($entry);
}
}
//If recurring event not found between given date range
if($output == null){
return $this->returnError(404, "No recurring events are present between ".$_GET['start_date'] ." and ". $_GET['end_date']);
}
return $output;

}

}
106 changes: 103 additions & 3 deletions docs/postman/Smart Village API.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,113 @@
{
"info": {
"_postman_id": "6083c583-3c4d-4eda-91b9-5fec0343fe5b",
"_postman_id": "1b674f22-95df-44a5-8d85-9979081e9287",
"name": "Smart Village API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "17408055"
},
"item": [
{
"name": "Calendar",
"item": [
{
"name": "Recurring Events",
"item": [
{
"name": "Get all calendar recurring events",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{API_URL}}/api/v2/calendar/recurring?start_date=2022-08-01&end_date=2022-09-01",
"host": [
"{{API_URL}}"
],
"path": [
"api",
"v2",
"calendar",
"recurring"
],
"query": [
{
"key": "start_date",
"value": "2022-08-01"
},
{
"key": "end_date",
"value": "2022-09-01"
}
]
}
},
"response": []
},
{
"name": "Get calendar recurring events by container",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{API_URL}}/api/v2/calendar/container/1/recurring?start_date=2022-08-01&end_date=2022-09-01",
"host": [
"{{API_URL}}"
],
"path": [
"api",
"v2",
"calendar",
"container",
"1",
"recurring"
],
"query": [
{
"key": "start_date",
"value": "2022-08-01"
},
{
"key": "end_date",
"value": "2022-09-01"
}
]
}
},
"response": []
},
{
"name": "Get calendar recurring events by id",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{API_URL}}/api/v2/calendar/entry/24/recurring?start_date=2022-07-30&end_date=2022-09-01",
"host": [
"{{API_URL}}"
],
"path": [
"api",
"v2",
"calendar",
"entry",
"24",
"recurring"
],
"query": [
{
"key": "start_date",
"value": "2022-07-30"
},
{
"key": "end_date",
"value": "2022-09-01"
}
]
}
},
"response": []
}
]
},
{
"name": "Get all calendars entries",
"request": {
Expand Down Expand Up @@ -911,4 +1011,4 @@
"response": []
}
]
}
}

0 comments on commit cd97be8

Please sign in to comment.