/** * @param Request $request * @param $cdbid * @return Response */ public function addLink(Request $request, $cdbid) { $callback = function () use($request, $cdbid) { $repository = $this->eventRepository; if ($request->getContentType() !== 'form') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not x-www-form-urlencoded.'); return $rsp; } $required = ['lang' => 'Language code', 'subbrand' => 'Sub-brand', 'description' => 'Description', 'linktype' => 'Link type', 'plaintext' => 'Plain text']; foreach ($required as $requiredProperty => $humanReadable) { if (!$request->request->get($requiredProperty)) { throw new InvalidArgumentException(sprintf('%s is required.', $humanReadable)); } } $type = strtolower($request->request->get('linktype')); // At this point only collaboration "links" are supported. if ($type !== 'collaboration') { throw new InvalidArgumentException('Link type should be "collaboration". Any other type is not supported.'); } $eventId = new String($cdbid); $language = new Language(strtolower($request->request->get('lang'))); $collaborationData = new CollaborationData(new String($request->request->get('subbrand')), new String($request->request->get('plaintext'))); if ($request->request->has('title')) { $collaborationData = $collaborationData->withTitle(new String($request->request->get('title'))); } if ($request->request->has('copyright')) { $collaborationData = $collaborationData->withCopyright(new String($request->request->get('copyright'))); } if ($request->request->has('link')) { $collaborationData = $collaborationData->withLink(Url::fromNative($request->request->get('link'))); } $description = json_decode($request->request->get('description')); if (is_null($description)) { throw new InvalidArgumentException('Description is not a valid json string.'); } if (!empty($description->text)) { $collaborationData = $collaborationData->withText(new String($description->text)); } if (!empty($description->keyword)) { $collaborationData = $collaborationData->withKeyword(new String($description->keyword)); } if (!empty($description->article)) { $collaborationData = $collaborationData->withArticle(new String($description->article)); } if (!empty($description->image)) { $collaborationData = $collaborationData->withImage(new String($description->image)); } $command = new AddCollaborationLink($eventId, $language, $collaborationData); $commandHandler = new EntryAPIEventCommandHandler($repository); $commandHandler->handle($command); $link = $this->entryapiLinkBaseUrl . $cdbid; $rsp = new Rsp('0.1', 'INFO', 'LinkCreated', $link, null); return $rsp; }; return $this->processEventRequest($callback); }
/** * @param Application $app * @return Response */ public function connect(Application $app) { $app['entry_api.command_handler'] = $app->share(function (Application $app) { return new SecurityDecoratedCommandHandler(new EntryAPIEventCommandHandler($app['event_repository']), $app['event.security']); }); /** @var ControllerCollection $controllers */ $controllers = $app['controllers_factory']; $app['entryapi_event_controller'] = $app->share(function (Application $app) { $controller = new EventController($app['event_repository'], $app['entryapi.link_base_url']); return $controller; }); $controllers->get('event/checkpermission', function (Request $request, Application $app) { /** @var String[] $eventIds */ $eventIds = []; if (!empty($request->query->get('ids'))) { $eventIds = explode(",", $request->query->get('ids')); $eventIds = array_filter($eventIds, function ($item) { return trim($item) !== ''; }); $eventIds = array_map(function ($cdbid) { return new String($cdbid); }, $eventIds); } $uitId = $request->query->get('user'); /** @var PermissionQueryInterface $repository */ $repository = $app['event_permission.repository']; $editableEvents = $repository->getEditableEvents(new String($uitId)); if (empty($eventIds)) { $eventIds = $editableEvents; } /** @var EventPermission[] $eventPermissions */ $eventPermissions = array_map(function (string $cdbid) use($editableEvents) { $isEditable = in_array($cdbid, $editableEvents); return new EventPermission($cdbid->toNative(), $isEditable); }, $eventIds); return $this->createPermissionResponse(new EventPermissionCollection($eventPermissions)); }); $controllers->post('/event', function (Request $request, Application $app) { $callback = function () use($request, $app) { if ($request->getContentType() !== 'xml') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not XML.'); return $this->createResponse($rsp); } $xml = new SizeLimitedEventXmlString($request->getContent()); /** @var \DOMElement $eventElement */ $eventElement = $xml->eventElement(); $id = $eventElement->getAttribute('cdbid'); if ($id) { // First try to retrieve the event from the JSON-LD read model. // This will result in a EventNotFoundException if the event // does not exist. /** @var \CultuurNet\UDB3\EventServiceInterface $service */ $service = $app['event_service']; $service->getEvent($id); $command = new UpdateEventFromCdbXml(new String($id), $xml); } else { $uuidGenerator = new \Broadway\UuidGenerator\Rfc4122\Version4Generator(); $id = $uuidGenerator->generate(); $command = new AddEventFromCdbXml(new String($id), $xml); } $commandHandler = $app['entry_api.command_handler']; $commandHandler->handle($command); $link = $app['entryapi.link_base_url'] . $id; $status = $command instanceof UpdateEventFromCdbXml ? 'ItemModified' : 'ItemCreated'; $rsp = new Rsp('0.1', 'INFO', $status, $link, null); return $rsp; }; return $this->processEventRequest($callback); }); $controllers->post('/event/{cdbid}/keywords', function (Request $request, Application $app, $cdbid) { $callback = function () use($request, $app, $cdbid) { $repository = $app['event_repository']; if ($request->getContentType() !== 'form') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not x-www-form-urlencoded.'); return $rsp; } $keywordsString = new String($request->request->get('keywords', '')); $visiblesString = new String($request->request->get('visibles', '')); $keywordsVisiblesPair = new KeywordsVisiblesPair($keywordsString, $visiblesString); $eventId = new String($cdbid); $command = new MergeLabels($eventId, $keywordsVisiblesPair->getLabels()); $commandHandler = new EntryAPIEventCommandHandler($repository); $commandHandler->handle($command); $link = $app['entryapi.link_base_url'] . $cdbid; $rsp = new Rsp('0.1', 'INFO', 'KeywordsCreated', $link, null); return $rsp; }; return $this->processEventRequest($callback); }); $controllers->delete('/event/{cdbid}/keywords', 'entryapi_event_controller:deleteKeyword'); $controllers->post('/event/{cdbid}/translations', 'entryapi_event_controller:translate'); $controllers->delete('/event/{cdbid}/translations', 'entryapi_event_controller:deleteTranslation'); $controllers->post('/event/{cdbid}/links', 'entryapi_event_controller:addLink'); $putCallback = function (Request $request, Application $app, $cdbid) { $callback = function () use($request, $app, $cdbid) { // First try to retrieve the event from the JSON-LD read model. // This will result in a EventNotFoundException if the event // does not exist. /** @var \CultuurNet\UDB3\EventServiceInterface $service */ $service = $app['event_service']; $service->getEvent($cdbid); if ($request->getContentType() !== 'xml') { $rsp = rsp::error('UnexpectedFailure', 'Content-Type is not XML.'); return $this->createResponse($rsp); } $xml = new SizeLimitedEventXmlString($request->getContent()); $eventId = new String($cdbid); $command = new UpdateEventFromCdbXml($eventId, $xml); $commandHandler = $app['entry_api.command_handler']; $commandHandler->handle($command); $link = $app['entryapi.link_base_url'] . $eventId; $rsp = new Rsp('0.1', 'INFO', 'ItemModified', $link, null); return $rsp; }; return $this->processEventRequest($callback); }; $controllers->put('/event/{cdbid}', $putCallback); // Culturefeed Entry UI Drupal module seems to use POST instead of PUT. $controllers->post('/event/{cdbid}', $putCallback); return $controllers; }