get('exposeName') == null) { return $this->render("prod/WorkZone/ExposeList.html.twig", [ 'publications' => [], ]); } $exposeConfiguration = $app['conf']->get(['phraseanet-service', 'expose-service', 'exposes'], []); $exposeConfiguration = $exposeConfiguration[$request->get('exposeName')]; $exposeClient = new Client(['base_uri' => $exposeConfiguration['expose_base_uri'], 'http_errors' => false]); if (!isset($exposeConfiguration['token'])) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $request->get('exposeName')); } if ($exposeConfiguration == null ) { return $this->render("prod/WorkZone/ExposeList.html.twig", [ 'publications' => [], ]); } $response = $exposeClient->get('/publications', [ 'headers' => [ 'Authorization' => 'Bearer '. $exposeConfiguration['token'], 'Content-Type' => 'application/json' ] ]); $publicationsID = []; if ($response->getStatusCode() == 200) { $body = json_decode($response->getBody()->getContents(),true); $publicationsID = array_column($body['hydra:member'], 'id'); } $publications = []; foreach ($publicationsID as $publicationID) { $resPublication = $exposeClient->get('/publications/' . $publicationID , [ 'headers' => [ 'Authorization' => 'Bearer '. $exposeConfiguration['token'], 'Content-Type' => 'application/json' ] ]); if ($response->getStatusCode() == 200) { $publications[] = json_decode($resPublication->getBody()->getContents(),true); } } // if ($request->get('format') == 'json') { return $app->json([ 'publications' => $publications ]); } return $this->render("prod/WorkZone/ExposeList.html.twig", [ 'publications' => $publications, ]); } /** * Create a publication * Require params "exposeName" and "publicationData" * * @param PhraseaApplication $app * @param Request $request * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function createPublicationAction(PhraseaApplication $app, Request $request) { $exposeName = $request->get('exposeName'); // TODO: taken account admin config ,acces_token for user or client_credentiels $exposeConfiguration = $app['conf']->get(['phraseanet-service', 'expose-service', 'exposes'], []); $exposeConfiguration = $exposeConfiguration[$exposeName]; $exposeClient = new Client(['base_uri' => $exposeConfiguration['expose_base_uri'], 'http_errors' => false]); try { if (!isset($exposeConfiguration['token'])) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $exposeName); } $response = $this->postPublication($exposeClient, $exposeConfiguration['token'], json_decode($request->get('publicationData'), true)); if ($response->getStatusCode() == 401) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $exposeName); $response = $this->postPublication($exposeClient, $exposeConfiguration['token'], json_decode($request->get('publicationData'), true)); } if ($response->getStatusCode() !== 201) { return $app->json([ 'success' => false, 'message' => "An error occurred when creating publication: status-code " . $response->getStatusCode() ]); } $publicationsResponse = json_decode($response->getBody(),true); } catch (\Exception $e) { return $app->json([ 'success' => false, 'message' => "An error occurred when creating publication!" ]); } $path = empty($publicationsResponse['slug']) ? $publicationsResponse['id'] : $publicationsResponse['slug'] ; $url = \p4string::addEndSlash($exposeConfiguration['expose_front_uri']) . $path; $link = "" . $url . ""; return $app->json([ 'success' => true, 'message' => "Publication successfully created!", 'link' => $link ]); } /** * Update a publication * Require params "exposeName" and "publicationId" * * @param PhraseaApplication $app * @param Request $request * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function updatePublicationAction(PhraseaApplication $app, Request $request) { $exposeName = $request->get('exposeName'); // TODO: taken account admin config ,acces_token for user or client_credentiels $exposeConfiguration = $app['conf']->get(['phraseanet-service', 'expose-service', 'exposes'], []); $exposeConfiguration = $exposeConfiguration[$exposeName]; $exposeClient = new Client(['base_uri' => $exposeConfiguration['expose_base_uri'], 'http_errors' => false]); try { if (!isset($exposeConfiguration['token'])) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $exposeName); } $response = $this->putPublication($exposeClient, $request->get('publicationId'), $exposeConfiguration['token'], json_decode($request->get('publicationData'), true)); if ($response->getStatusCode() == 401) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $exposeName); $response = $this->putPublication($exposeClient, $request->get('publicationId'), $exposeConfiguration['token'], json_decode($request->get('publicationData'), true)); } if ($response->getStatusCode() !== 201) { return $app->json([ 'success' => false, 'message' => "An error occurred when updating publication: status-code " . $response->getStatusCode() ]); } } catch (\Exception $e) { return $app->json([ 'success' => false, 'message' => "An error occurred when updating publication!" ]); } return $app->json([ 'success' => true, 'message' => "Publication successfully updated!" ]); } /** * Delete a Publication * require params "exposeName" and "publicationId" * * @param PhraseaApplication $app * @param Request $request * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function deletePublication(PhraseaApplication $app, Request $request) { $exposeName = $request->get('exposeName'); // TODO: taken account admin config ,acces_token for user or client_credentiels $exposeConfiguration = $app['conf']->get(['phraseanet-service', 'expose-service', 'exposes'], []); $exposeConfiguration = $exposeConfiguration[$exposeName]; $exposeClient = new Client(['base_uri' => $exposeConfiguration['expose_base_uri'], 'http_errors' => false]); try { if (!isset($exposeConfiguration['token'])) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $exposeName); } $response = $this->removePublication($exposeClient, $request->get('publicationId'), $exposeConfiguration['token']); if ($response->getStatusCode() == 401) { $exposeConfiguration = $this->generateAndSaveToken($exposeConfiguration, $exposeName); $response = $this->removePublication($exposeClient, $request->get('publicationId'), $exposeConfiguration['token']); } if ($response->getStatusCode() !== 204) { return $app->json([ 'success' => false, 'message' => "An error occurred when deleting publication: status-code " . $response->getStatusCode() ]); } } catch (\Exception $e) { return $app->json([ 'success' => false, 'message' => "An error occurred when deleting publication!" ]); } return $app->json([ 'success' => true, 'message' => "Publication successfully deleted!" ]); } /** * Add assets in a publication * Require params "lst" , "exposeName" and "publicationId" * "lst" is a list of record as "baseId_recordId" * * @param PhraseaApplication $app * @param Request $request * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function addPublicationAssetsAction(PhraseaApplication $app, Request $request) { $exposeName = $request->get('exposeName'); $publicationId = $request->get('publicationId'); if ($publicationId == null) { return $app->json([ 'success' => false, 'message' => 'Need to give publicationId to add asset in publication!' ]); } try { $records = RecordsRequest::fromRequest($app, $request); } catch (\Exception $e) { return $app->json([ 'success' => false, 'message' => 'An error occured when wanting to create publication!' ]); } // TODO: taken account admin config ,acces_token for user or client_credentiels $exposeConfiguration = $app['conf']->get(['phraseanet-service', 'expose-service', 'exposes'], []); $exposeConfiguration = $exposeConfiguration[$exposeName]; $exposeClient = new Client(['base_uri' => $exposeConfiguration['expose_base_uri'], 'http_errors' => false]); /** @var \record_adapter $record */ foreach ($records as $record) { try { $helpers = new PhraseanetExtension($app); $canSeeBusiness = $helpers->isGrantedOnCollection($record->getBaseId(), [\ACL::CANMODIFRECORD]); $captionsByfield = $record->getCaption($helpers->getCaptionFieldOrder($record, $canSeeBusiness)); $description = "