public function getTalkType($request, $db) { $talk_type_id = $this->getItemId($request); // verbosity - here for consistency as we don't have verbose talk type details to return at the moment $verbose = $this->getVerbosity($request); $mapper = new TalkTypeMapper($db, $request); $list = $mapper->getTalkTypeById($talk_type_id, $verbose); if (count($list['talk_types']) == 0) { throw new Exception('Talk type not found', 404); } return $list; }
/** * Create a talk * * This method creates a new talk after being called via the URL * "/events/[eventId]/talks" * * @param Request $request * @param PDO $db * * @throws Exception * @return array|bool */ public function createTalkAction(Request $request, PDO $db) { if (!isset($request->user_id)) { throw new Exception("You must be logged in to create data", 400); } $talk['event_id'] = $this->getItemId($request); if (empty($talk['event_id'])) { throw new Exception("POST expects a talk representation sent to a specific event URL", 400); } $event_mapper = new EventMapper($db, $request); $talk_mapper = new TalkMapper($db, $request); $talk_type_mapper = new TalkTypeMapper($db, $request); $is_admin = $event_mapper->thisUserHasAdminOn($talk['event_id']); if (!$is_admin) { throw new Exception("You do not have permission to add talks to this event", 400); } // get the event so we can get the timezone info $list = $event_mapper->getEventById($talk['event_id'], true); if (count($list['events']) == 0) { throw new Exception('Event not found', 404); } $event = $list['events'][0]; $talk['title'] = filter_var($request->getParameter('talk_title'), FILTER_SANITIZE_STRING); if (empty($talk['title'])) { throw new Exception("The talk title field is required", 400); } $talk['description'] = filter_var($request->getParameter('talk_description'), FILTER_SANITIZE_STRING); if (empty($talk['description'])) { throw new Exception("The talk description field is required", 400); } $talk['type'] = filter_var($request->getParameter('type', 'Talk'), FILTER_SANITIZE_STRING); $talk_types = $talk_type_mapper->getTalkTypesLookupList(); if (!array_key_exists($talk['type'], $talk_types)) { throw new Exception("The type '{$talk['type']}' is unknown", 400); } $talk['type_id'] = $talk_types[$talk['type']]; $start_date = filter_var($request->getParameter('start_date'), FILTER_SANITIZE_STRING); if (empty($start_date)) { throw new Exception("Please give the date and time of the talk", 400); } $tz = new DateTimeZone($event['tz_continent'] . '/' . $event['tz_place']); $talk['date'] = (new DateTime($start_date, $tz))->format('U'); $talk['language'] = filter_var($request->getParameter('language'), FILTER_SANITIZE_STRING); if (empty($talk['language'])) { // default to UK English $talk['language'] = 'English - UK'; } // When the language doesn't exist, the talk will not be found $language_mapper = new LanguageMapper($db, $request); if (!$language_mapper->isLanguageValid($talk['language'])) { throw new Exception("The language '{$talk['type']}' is unknown", 400); } $talk['duration'] = filter_var($request->getParameter('duration'), FILTER_SANITIZE_NUMBER_INT); if (empty($talk['duration'])) { $talk['duration'] = 60; } $talk['slides_link'] = filter_var($request->getParameter('slides_link'), FILTER_SANITIZE_URL); $talk['speakers'] = array_map(function ($speaker) { $speaker = filter_var($speaker, FILTER_SANITIZE_STRING); $speaker = trim($speaker); return $speaker; }, (array) $request->getParameter('speakers')); $new_id = $talk_mapper->createTalk($talk); // Update the cache count for the number of talks at this event $event_mapper->cacheTalkCount($talk['event_id']); $uri = $request->base . '/' . $request->version . '/talks/' . $new_id; header("Location: " . $uri, true, 201); $new_talk = $talk_mapper->getTalkById($new_id); return $new_talk; }
/** * Read the talk fields from the request body and validate and return an * array ready for saving to the database. * * This is common for createTalk() and editTalk(). * * @param PDO $db * @param Request $request * @param int $event_id * * @return array */ protected function getTalkDataFromRequest(PDO $db, Request $request, $event_id) { // get the event so we can get the timezone info & it $event_mapper = new EventMapper($db, $request); $list = $event_mapper->getEventById($event_id, true); if (count($list['events']) == 0) { throw new Exception('Event not found', 404); } $event = $list['events'][0]; $talk['title'] = filter_var($request->getParameter('talk_title'), FILTER_SANITIZE_STRING); if (empty($talk['title'])) { throw new Exception("The talk title field is required", 400); } $talk['description'] = filter_var($request->getParameter('talk_description'), FILTER_SANITIZE_STRING); if (empty($talk['description'])) { throw new Exception("The talk description field is required", 400); } $talk['type'] = filter_var($request->getParameter('type', 'Talk'), FILTER_SANITIZE_STRING); $talk_type_mapper = new TalkTypeMapper($db, $request); $talk_types = $talk_type_mapper->getTalkTypesLookupList(); if (!array_key_exists($talk['type'], $talk_types)) { throw new Exception("The type '{$talk['type']}' is unknown", 400); } $talk['type_id'] = $talk_types[$talk['type']]; $start_date = filter_var($request->getParameter('start_date'), FILTER_SANITIZE_STRING); if (empty($start_date)) { throw new Exception("Please give the date and time of the talk", 400); } $tz = new DateTimeZone($event['tz_continent'] . '/' . $event['tz_place']); $talk['date'] = (new DateTime($start_date, $tz))->format('U'); $event_start_date = (new DateTime($event['start_date']))->format('U'); $event_end_date = (new DateTime($event['end_date']))->add(new DateInterval('P1D'))->format('U'); if ($talk['date'] < $event_start_date || $talk['date'] >= $event_end_date) { throw new Exception("The talk must be held between the start and end date of the event", 400); } $talk['language'] = filter_var($request->getParameter('language'), FILTER_SANITIZE_STRING); if (empty($talk['language'])) { // default to UK English $talk['language'] = 'English - UK'; } // When the language doesn't exist, the talk will not be found $language_mapper = new LanguageMapper($db, $request); if (!$language_mapper->isLanguageValid($talk['language'])) { throw new Exception("The language '{$talk['type']}' is unknown", 400); } $talk['duration'] = filter_var($request->getParameter('duration'), FILTER_SANITIZE_NUMBER_INT); if (empty($talk['duration'])) { $talk['duration'] = 60; } $talk['slides_link'] = filter_var($request->getParameter('slides_link'), FILTER_SANITIZE_URL); $talk['speakers'] = array_map(function ($speaker) { $speaker = filter_var($speaker, FILTER_SANITIZE_STRING); $speaker = trim($speaker); return $speaker; }, (array) $request->getParameter('speakers')); return $talk; }