diff --git a/README.md b/README.md index 0a02a1e..351d126 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/routes.php b/app/routes.php index b493349..0b79b78 100644 --- a/app/routes.php +++ b/app/routes.php @@ -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 = []; @@ -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 @@ -61,7 +64,7 @@ $query->execute($queryParams); $result = [ 'tid' => $db->lastInsertId(), - 'action' => $resultAction + 'action' => 'inserted' ]; $payload = json_encode($result); @@ -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'], @@ -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`=? ;'; @@ -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 = []; @@ -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 = [ @@ -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); diff --git a/app/templates/basic.html b/app/templates/basic.html index 5bead5e..20b14c2 100644 --- a/app/templates/basic.html +++ b/app/templates/basic.html @@ -33,7 +33,7 @@