mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-15 22:13:13 +00:00
add CRUD route
This commit is contained in:
@@ -11,6 +11,14 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
|
|
||||||
class PSExposeController extends Controller
|
class PSExposeController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Get list of publication
|
||||||
|
* Use param "format=json" to retrieve a json
|
||||||
|
*
|
||||||
|
* @param PhraseaApplication $app
|
||||||
|
* @param Request $request
|
||||||
|
* @return string|\Symfony\Component\HttpFoundation\JsonResponse
|
||||||
|
*/
|
||||||
public function listPublicationAction(PhraseaApplication $app, Request $request)
|
public function listPublicationAction(PhraseaApplication $app, Request $request)
|
||||||
{
|
{
|
||||||
if ( $request->get('exposeName') == null) {
|
if ( $request->get('exposeName') == null) {
|
||||||
@@ -61,24 +69,32 @@ class PSExposeController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if ($request->get('format') == 'json') {
|
||||||
|
return $app->json([
|
||||||
|
'publications' => $publications
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
return $this->render("prod/WorkZone/ExposeList.html.twig", [
|
return $this->render("prod/WorkZone/ExposeList.html.twig", [
|
||||||
'publications' => $publications,
|
'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)
|
public function createPublicationAction(PhraseaApplication $app, Request $request)
|
||||||
{
|
{
|
||||||
try {
|
|
||||||
$records = RecordsRequest::fromRequest($app, $request);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return $app->json([
|
|
||||||
'success' => false,
|
|
||||||
'message' => 'An error occured when wanting to create publication!'
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$exposeName = $request->get('exposeName');
|
$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 = $app['conf']->get(['phraseanet-service', 'expose-service', 'exposes'], []);
|
||||||
$exposeConfiguration = $exposeConfiguration[$exposeName];
|
$exposeConfiguration = $exposeConfiguration[$exposeName];
|
||||||
|
|
||||||
@@ -97,7 +113,7 @@ class PSExposeController extends Controller
|
|||||||
$response = $this->postPublication($exposeClient, $exposeConfiguration['token'], json_decode($request->get('publicationData'), true));
|
$response = $this->postPublication($exposeClient, $exposeConfiguration['token'], json_decode($request->get('publicationData'), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($response->getStatusCode() !==201) {
|
if ($response->getStatusCode() !== 201) {
|
||||||
return $app->json([
|
return $app->json([
|
||||||
'success' => false,
|
'success' => false,
|
||||||
'message' => "An error occurred when creating publication: status-code " . $response->getStatusCode()
|
'message' => "An error occurred when creating publication: status-code " . $response->getStatusCode()
|
||||||
@@ -112,6 +128,154 @@ class PSExposeController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$path = empty($publicationsResponse['slug']) ? $publicationsResponse['id'] : $publicationsResponse['slug'] ;
|
||||||
|
$url = \p4string::addEndSlash($exposeConfiguration['expose_front_uri']) . $path;
|
||||||
|
|
||||||
|
$link = "<a style='color:blue;' target='_blank' href='" . $url . "'>" . $url . "</a>";
|
||||||
|
|
||||||
|
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 */
|
/** @var \record_adapter $record */
|
||||||
foreach ($records as $record) {
|
foreach ($records as $record) {
|
||||||
try {
|
try {
|
||||||
@@ -158,7 +322,7 @@ class PSExposeController extends Controller
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => 'publication_id',
|
'name' => 'publication_id',
|
||||||
'contents' => $publicationsResponse['id'],
|
'contents' => $publicationId,
|
||||||
|
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
@@ -233,15 +397,9 @@ class PSExposeController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$path = empty($publicationsResponse['slug']) ? $publicationsResponse['id'] : $publicationsResponse['slug'] ;
|
|
||||||
$url = \p4string::addEndSlash($exposeConfiguration['expose_front_uri']) . $path;
|
|
||||||
|
|
||||||
$link = "<a style='color:blue;' target='_blank' href='" . $url . "'>" . $url . "</a>";
|
|
||||||
|
|
||||||
return $app->json([
|
return $app->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'message' => "Publication successfully created!",
|
'message' => count($records) . "added to the publication!"
|
||||||
'link' => $link
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,6 +452,26 @@ class PSExposeController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function putPublication(Client $exposeClient, $publicationId, $token, $publicationData)
|
||||||
|
{
|
||||||
|
return $exposeClient->put('/publications/' . $publicationId, [
|
||||||
|
'headers' => [
|
||||||
|
'Authorization' => 'Bearer '. $token,
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
|
],
|
||||||
|
'json' => $publicationData
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function removePublication(Client $exposeClient, $publicationId, $token)
|
||||||
|
{
|
||||||
|
return $exposeClient->delete('/publications/' . $publicationId, [
|
||||||
|
'headers' => [
|
||||||
|
'Authorization' => 'Bearer '. $token
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
private function postSubDefinition(Client $exposeClient, $token, $path, $assetId, $subdefName, $isPreview = false, $isThumbnail = false)
|
private function postSubDefinition(Client $exposeClient, $token, $path, $assetId, $subdefName, $isPreview = false, $isThumbnail = false)
|
||||||
{
|
{
|
||||||
return $exposeClient->post('/sub-definitions', [
|
return $exposeClient->post('/sub-definitions', [
|
||||||
|
@@ -34,10 +34,22 @@ class PSExposeServiceProvider implements ControllerProviderInterface, ServicePro
|
|||||||
->method('POST')
|
->method('POST')
|
||||||
->bind('ps_expose_create_publication');
|
->bind('ps_expose_create_publication');
|
||||||
|
|
||||||
|
$controllers->match('/update-publication/{publicationId}', 'controller.ps.expose:updatePublicationAction')
|
||||||
|
->method('POST|PUT')
|
||||||
|
->bind('ps_expose_update_publication');
|
||||||
|
|
||||||
$controllers->match('/list-publication/', 'controller.ps.expose:listPublicationAction')
|
$controllers->match('/list-publication/', 'controller.ps.expose:listPublicationAction')
|
||||||
->method('GET')
|
->method('GET')
|
||||||
->bind('ps_expose_list_publication');
|
->bind('ps_expose_list_publication');
|
||||||
|
|
||||||
|
$controllers->match('/delete-publication/{publicationId}', 'controller.ps.expose:deletePublicationAction')
|
||||||
|
->method('POST|DELETE')
|
||||||
|
->bind('ps_expose_delete_publication');
|
||||||
|
|
||||||
|
$controllers->match('/publication/add-assets', 'controller.ps.expose:addPublicationAssetsAction')
|
||||||
|
->method('POST')
|
||||||
|
->bind('ps_expose_publication_add_assets');
|
||||||
|
|
||||||
return $controllers;
|
return $controllers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user