Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ cd ./scheduler-howto-slim
$ mysql -uuser -ppass scheduler < mysql_dump.sql
```

* update the db connection settings in server.js
* update the db connection settings in app/settings.php
* install dependencies using composer
```
$ composer install
Expand Down
214 changes: 99 additions & 115 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
use Slim\Routing\RouteCollectorProxy;

return function (App $app) {
$app->get('/', function (Request $request, Response $response) {
$payload = file_get_contents('../app/templates/index.html');
$filePath = __DIR__ . '/templates/index.html';
$payload = file_get_contents($filePath);
$response->getBody()->write($payload);
return $response;
});
$app->get('/basic', function (Request $request, Response $response) {
$payload = file_get_contents('../app/templates/basic.html');
$filePath = __DIR__ . '/templates/basic.html';
$payload = file_get_contents($filePath);
$response->getBody()->write($payload);
return $response;
});
$app->get('/recurring', function (Request $request, Response $response) {
$payload = file_get_contents('../app/templates/recurring.html');
$filePath = __DIR__ . '/templates/recurring.html';
$payload = file_get_contents($filePath);
$response->getBody()->write($payload);
return $response;
});
$app->group('/events', function ($group) {
$app->group('/events', function (RouteCollectorProxy $group) {
$group->get('', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$db = $this->get(PDO::class);
$queryText = 'SELECT * FROM `events`';
$params = $request->getQueryParams();
$queryParams = [];
Expand All @@ -44,7 +47,7 @@
});

$group->post('', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$db = $this->get(PDO::class);
$body = $request->getParsedBody();

$queryText = 'INSERT INTO `events` SET
Expand All @@ -61,7 +64,7 @@
$query->execute($queryParams);
$result = [
'tid' => $db->lastInsertId(),
'action' => $resultAction
'action' => 'inserted'
];

$payload = json_encode($result);
Expand All @@ -72,15 +75,16 @@


$group->put('/{id}', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$id = $request->getAttribute('route')->getArgument('id');
$db = $this->get(PDO::class);
$id = $args['id'];
$body = $request->getParsedBody();
parse_str(file_get_contents("php://input"), $body);

$queryText = 'UPDATE `events` SET
`start_date`=?,
`end_date`=?,
`text`=?
WHERE `id`=?';
`start_date`=?,
`end_date`=?,
`text`=?
WHERE `id`=?';

$queryParams = [
$body['start_date'],
Expand All @@ -92,17 +96,16 @@
$query = $db->prepare($queryText);
$query->execute($queryParams);

$result = [
'action' => 'updated'
];
$result = ['action' => 'updated'];
$payload = json_encode($result);

$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'application/json');
});

$group->delete('/{id}', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$id = $request->getAttribute('route')->getArgument('id');
$db = $this->get(PDO::class);
$id = $args['id'];

$queryText = 'DELETE FROM `events` WHERE `id`=? ;';

Expand All @@ -122,7 +125,7 @@

$app->group('/recurring_events', function ($group) {
$group->get('', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$db = $this->get(PDO::class);
$queryText = 'SELECT * FROM `recurring_events`';
$params = $request->getQueryParams();
$queryParams = [];
Expand All @@ -140,35 +143,34 @@
});

$group->post('', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$db = $this->get(PDO::class);
$body = $request->getParsedBody();

$queryText = 'INSERT INTO `recurring_events` SET
`start_date`=?,
`end_date`=?,
`text`=?,
`event_pid`=?,
`event_length`=?,
`rec_type`=?';
$queryText = "INSERT INTO `recurring_events` SET
`start_date`=?,
`end_date`=?,
`text`=?,
`duration`=?,
`rrule`=?,
`recurring_event_id`=?,
`original_start`=?,
`deleted`=?";
$queryParams = [
$body['start_date'],
$body['end_date'],
$body['text'],
// recurring events columns
$body['event_pid'] ? $body['event_pid'] : 0,
$body['event_length'] ? $body['event_length'] : 0,
$body['rec_type']
$body["start_date"],
$body["end_date"],
$body["text"],
$body["duration"] ? $body["duration"] : null,
$body["rrule"] ? $body["rrule"] : null,
$body["recurring_event_id"] ? $body["recurring_event_id"] : null,
$body["original_start"] ? $body["original_start"] : null,
(isset($body["deleted"]) && $body["deleted"] === "true") ? 1 :
((isset($body["deleted"]) && $body["deleted"] === "false") ? 0 : null)
];

// delete a single occurrence from recurring series
$resultAction = 'inserted';
if ($body['rec_type'] === "none") {
$resultAction = 'deleted';//!
}
/*
end of recurring events data processing
*/

// delete a single occurrence from recurring series
$resultAction = 'inserted';
if (isset($body["deleted"]) && $body["deleted"] === "true") {
$resultAction = 'deleted';
}
$query = $db->prepare($queryText);
$query->execute($queryParams);
$result = [
Expand All @@ -182,98 +184,80 @@
return $response->withHeader('Content-Type', 'application/json');
});


$group->put('/{id}', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$id = $request->getAttribute('route')->getArgument('id');
$db = $this->get(PDO::class);
$id = $args['id'];
$body = $request->getParsedBody();
parse_str(file_get_contents("php://input"), $body);

$queryText = 'UPDATE `recurring_events` SET
`start_date`=?,
`end_date`=?,
`text`=?,
`event_pid`=?,
`event_length`=?,
`rec_type`=?
WHERE `id`=?';

$queryText = "UPDATE `recurring_events` SET
`start_date`=?,
`end_date`=?,
`text`=?,
`duration`=?,
`rrule`=?,
`recurring_event_id`=?,
`original_start`=?,
`deleted`=?
WHERE `id`=?";
$queryParams = [
$body['start_date'],
$body['end_date'],
$body['text'],

$body['event_pid'] ? $body['event_pid'] : 0,
$body['event_length'] ? $body['event_length'] : 0,
$body['rec_type'],//!
$body["start_date"],
$body["end_date"],
$body["text"],
$body["duration"] ? $body["duration"] : null,
$body["rrule"] ? $body["rrule"] : null,
$body["recurring_event_id"] ? $body["recurring_event_id"] : null,
$body["original_start"] ? $body["original_start"] : null,
$body["deleted"] ? $body["deleted"] : null,
$id
];
if ($body['rec_type'] && $body['rec_type'] != 'none') {
//all modified occurrences must be deleted when you update recurring series
//https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
$subQueryText = 'DELETE FROM `recurring_events` WHERE `event_pid`=? ;';
$subQuery = $db->prepare($subQueryText);
$subQuery->execute([$id]);

if ($body["rrule"] && $body["recurring_event_id"] == null) {
//all modified occurrences must be deleted when you update recurring series
//https://docs.dhtmlx.com/scheduler/server_integration.html#recurringevents
$subQueryText = "DELETE FROM `recurring_events` WHERE `recurring_event_id`=? ;";
$subQuery = $db->prepare($subQueryText);
$subQuery->execute([$id]);
}

$query = $db->prepare($queryText);
$query->execute($queryParams);

$result = [
'action' => 'updated'
];
$result = ['action' => 'updated'];
$payload = json_encode($result);

$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'application/json');
});

$group->delete('/{id}', function (Request $request, Response $response, array $args) {
$db = $this->get('PDO');
$id = $request->getAttribute('route')->getArgument('id');
// some logic specific to recurring events support
// https://docs.dhtmlx.com/scheduler/server_integration.html#savingrecurringevents
$subQueryText = 'SELECT * FROM `events` WHERE id=? LIMIT 1;';
$db = $this->get(PDO::class);
$id = $args['id'];

// Fetch the event
$subQueryText = "SELECT * FROM `recurring_events` WHERE `id` = ? LIMIT 1;";
$subQuery = $db->prepare($subQueryText);
$subQuery->execute([$id]);
$event = $subQuery->fetch(PDO::FETCH_ASSOC);

if ($event['event_pid']) {
// deleting a modified occurrence from a recurring series
// If an event with the event_pid value was deleted - it needs updating
// with rec_type==none instead of deleting.
$subQueryText='UPDATE `recurring_events` SET `rec_type`=\'none\' WHERE `id`=?;';
$subQuery = $db->prepare($subQueryText);
$query->execute($queryParams);

$result = [
'action' => 'deleted'
];

$payload = json_encode($result);

$response->getBody()->write($payload);
return $response->withHeader('Content-Type', 'application/json');
}

if ($event['rec_type'] && $event['rec_type'] != 'none') {//!
// if a recurring series deleted, delete all modified occurrences of the series
$subQueryText = 'DELETE FROM `recurring_events` WHERE `event_pid`=? ;';
$subQuery = $db->prepare($subQueryText);
$subQuery->execute([$id]);
if ($event && $event["recurring_event_id"]) {
// Modified occurrence of a recurring event - mark as deleted
$updateQueryText = "UPDATE `recurring_events` SET `deleted` = 1 WHERE `id` = ?;";
$updateQuery = $db->prepare($updateQueryText);
$updateQuery->execute([$id]);
} else {
if ($event && $event["rrule"]) {
// Deleting recurring series - delete all modified occurrences as well
$deleteModifiedOccurrencesQueryText = "DELETE FROM `recurring_events` WHERE `recurring_event_id` = ?;";
$deleteModifiedOccurrencesQuery = $db->prepare($deleteModifiedOccurrencesQueryText);
$deleteModifiedOccurrencesQuery->execute([$id]);
}
// Delete the event itself
$deleteQueryText = "DELETE FROM `recurring_events` WHERE `id` = ?;";
$deleteQuery = $db->prepare($deleteQueryText);
$deleteQuery->execute([$id]);
}

/*
end of recurring events data processing
*/

$queryText = 'DELETE FROM `recurring_events` WHERE `id`=? ;';

$query = $db->prepare($queryText);
$query->execute([$id]);

$result = [
'action' => 'deleted'
];

$result = ['action' => 'deleted'];
$payload = json_encode($result);

$response->getBody()->write($payload);
Expand Down
2 changes: 1 addition & 1 deletion app/templates/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</div>
<script>
scheduler.config.date_format="%Y-%m-%d %H:%i";
scheduler.init("scheduler_here", new Date(2019,0,20), "week");
scheduler.init("scheduler_here", new Date(2026,0,6), "week");
scheduler.setLoadMode("day");
scheduler.load("/events", "json");

Expand Down
2 changes: 1 addition & 1 deletion app/templates/recurring.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
recurring: true
});
scheduler.config.date_format="%Y-%m-%d %H:%i";
scheduler.init("scheduler_here", new Date(2019,0,20), "week");
scheduler.init("scheduler_here", new Date(2026,0,6), "week");
scheduler.setLoadMode("day");
scheduler.load("/recurring_events", "json");

Expand Down
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
}
],
"require": {
"php": ">=7.1",
"php": "^7.4 || ^8.0",
"ext-json": "*",
"illuminate/database": "^6.5",
"monolog/monolog": "^1.24",
"php-di/php-di": "^6.0",
"slim/psr7": "^0.5",
"slim/slim": "^4.1"
"monolog/monolog": "^2.8",
"php-di/php-di": "^6.4",
"slim/psr7": "^1.5",
"slim/slim": "^4.10"
},
"require-dev": {
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^9.5.26"
},
"config": {
"process-timeout": 0,
Expand Down
Loading