/** * Add a track to a talk by POSTing to /talks/123/tracks with the `track_uri` * in the body * * @param PDO $db * @param Request $request */ public function addTrackToTalk(Request $request, PDO $db) { if (!isset($request->user_id)) { throw new Exception("You must be logged in to create data", 400); } $talk_id = $this->getItemId($request); $talk_mapper = new TalkMapper($db, $request); $talk = $talk_mapper->getTalkById($talk_id); if (!$talk) { throw new Exception("Talk not found", 404); } $is_admin = $talk_mapper->thisUserHasAdminOn($talk_id); $is_speaker = $talk_mapper->isUserASpeakerOnTalk($talk_id, $request->user_id); if (!($is_admin || $is_speaker)) { throw new Exception("You do not have permission to add this talk to a track", 400); } $track_uri = $request->getParameter("track_uri"); $pattern = '@/' . $request->version . '/tracks/([\\d]+)$@'; if (!preg_match($pattern, $track_uri, $matches)) { throw new Exception('Invalid track_uri', 400); } $track_id = $matches[1]; // is this track on this event? $event_mapper = new EventMapper($db, $request); $track_events = $event_mapper->getEventByTrackId($track_id, true, false, false); if (!$track_events || !$track_events[0]['ID']) { throw new Exception("Associated event not found", 400); } $track_event_id = $track_events[0]['ID']; if ($talk->event_id != $track_event_id) { throw new Exception("This talk cannot be added to this track", 400); } // add talk to track $talk_mapper->addTalkToTrack($talk_id, $track_id); $uri = $request->base . '/' . $request->version . '/talks/' . $talk_id; header('Location: ' . $uri, null, 201); exit; }