Skip to content

Commit

Permalink
Merge pull request #1180 from didoda/feat/new-bedita-client
Browse files Browse the repository at this point in the history
Update web-tools
  • Loading branch information
didoda authored Sep 19, 2024
2 parents 45c142a + 832dd0e commit b43cbdf
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 108 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require": {
"php": ">=7.4",
"bedita/i18n": "^4.4.3",
"bedita/web-tools": "^3.10.1",
"bedita/web-tools": "^4.0.2",
"cakephp/authentication": "^2.9",
"cakephp/cakephp": "~4.5.0",
"cakephp/plugin-installer": "^1.3",
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/Model/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ public function initialize(): void
public function index(): ?Response
{
$this->getRequest()->allowMethod(['get']);
$options = $this->getRequest()->getQueryParams();
$params = $this->getRequest()->getQueryParams();
$options = $params;
$options['page_size'] = empty($options['page_size']) ? 20 : $options['page_size'];
$options['sort'] = empty($options['sort']) ? 'name' : $options['sort'];
$response = ApiClientProvider::getApiClient()->get('/model/tags', $options);
$resources = Hash::combine((array)Hash::get($response, 'data'), '{n}.id', '{n}');
CacheTools::setModuleCount((array)$response, 'tags');
if (empty($params['q']) && empty($params['filter'])) {
CacheTools::setModuleCount((array)$response, 'tags');
}
$roots = $this->Categories->getAvailableRoots($resources);
$tagsTree = $this->Categories->tree($resources);
$this->set(compact('resources', 'roots', 'tagsTree'));
Expand Down
44 changes: 22 additions & 22 deletions src/Controller/ModulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ public function index(): ?Response
}

try {
$response = $this->apiClient->getObjects($this->objectType, $this->Query->index());
CacheTools::setModuleCount((array)$response, $this->Modules->getConfig('currentModuleName'));
$params = $this->Query->index();
$response = $this->apiClient->getObjects($this->objectType, $params);
if (empty($params['q']) && empty($params['filter'])) {
CacheTools::setModuleCount((array)$response, $this->Modules->getConfig('currentModuleName'));
}
} catch (BEditaClientException $e) {
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);
Expand Down Expand Up @@ -379,28 +382,25 @@ public function clone($id): ?Response
public function delete(): ?Response
{
$this->getRequest()->allowMethod(['post']);
$ids = [];
if (!empty($this->getRequest()->getData('ids'))) {
if (is_string($this->getRequest()->getData('ids'))) {
$ids = explode(',', (string)$this->getRequest()->getData('ids'));
}
} elseif (!empty($this->getRequest()->getData('id'))) {
$ids = [$this->getRequest()->getData('id')];
}
foreach ($ids as $id) {
try {
$this->apiClient->deleteObject($id, $this->objectType);
$id = $this->getRequest()->getData('id');
$ids = $this->getRequest()->getData('ids');
$ids = is_string($ids) ? explode(',', $ids) : $ids;
$ids = empty($ids) ? [$id] : $ids;
try {
$this->apiClient->deleteObjects($ids, $this->objectType);
$eventManager = $this->getEventManager();
foreach ($ids as $id) {
$event = new Event('Controller.afterDelete', $this, ['id' => $id, 'type' => $this->objectType]);
$this->getEventManager()->dispatch($event);
} catch (BEditaClientException $e) {
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);
if (!empty($this->getRequest()->getData('id'))) {
return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $this->getRequest()->getData('id')]);
}

return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $id]);
$eventManager->dispatch($event);
}
} catch (BEditaClientException $e) {
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);
if (!empty($this->getRequest()->getData('id'))) {
return $this->redirect(['_name' => 'modules:view', 'object_type' => $this->objectType, 'id' => $this->getRequest()->getData('id')]);
}

return $this->redirect($this->referer());
}
$this->Flash->success(__('Object(s) deleted'));

Expand Down
136 changes: 69 additions & 67 deletions src/Controller/TrashController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ public function index(): ?Response
$this->getRequest()->allowMethod(['get']);

try {
$response = $this->apiClient->getObjects('trash', $this->getRequest()->getQueryParams());
CacheTools::setModuleCount($response, 'trash');
$params = $this->getRequest()->getQueryParams();
$response = $this->apiClient->getObjects('trash', $params);
if (empty($params['q']) && empty($params['filter'])) {
CacheTools::setModuleCount($response, 'trash');
}
} catch (BEditaClientException $e) {
// Error! Back to dashboard.
$this->log($e->getMessage(), LogLevel::ERROR);
Expand Down Expand Up @@ -127,29 +130,16 @@ public function view($id): ?Response
public function restore(): ?Response
{
$this->getRequest()->allowMethod(['post']);
$ids = [];
if (!empty($this->getRequest()->getData('ids'))) {
$ids = $this->getRequest()->getData('ids');
if (is_string($ids)) {
$ids = explode(',', (string)$this->getRequest()->getData('ids'));
}
} else {
$ids = [$this->getRequest()->getData('id')];
}
foreach ($ids as $id) {
try {
$this->apiClient->restoreObject($id, 'objects');
} catch (BEditaClientException $e) {
// Error! Back to object view.
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);

if (!empty($this->getRequest()->getData('ids'))) {
return $this->redirect(['_name' => 'trash:list'] + $this->listQuery());
}

return $this->redirect(['_name' => 'trash:view', 'id' => $id]);
}
$id = $this->getRequest()->getData('id');
$ids = $this->getRequest()->getData('ids');
$ids = is_string($ids) ? explode(',', $ids) : $ids;
$ids = empty($ids) ? [$id] : $ids;
try {
$this->apiClient->restoreObjects($ids);
} catch (BEditaClientException $e) {
// Error! Back to object view.
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);
}

return $this->redirect(['_name' => 'trash:list'] + $this->listQuery());
Expand All @@ -163,33 +153,14 @@ public function restore(): ?Response
public function delete(): ?Response
{
$this->getRequest()->allowMethod(['post']);
$ids = [];
if (!empty($this->getRequest()->getData('ids'))) {
$ids = $this->getRequest()->getData('ids');
if (is_string($ids)) {
$ids = explode(',', (string)$this->getRequest()->getData('ids'));
}
} else {
$ids = [$this->getRequest()->getData('id')];
}
foreach ($ids as $id) {
try {
$this->deleteData($id);
} catch (BEditaClientException $e) {
// Error! Back to object view.
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);

if (!empty($this->getRequest()->getData('ids'))) {
return $this->redirect(['_name' => 'trash:list'] + $this->listQuery());
}

return $this->redirect(['_name' => 'trash:view', 'id' => $id]);
}
$id = $this->getRequest()->getData('id');
$ids = $this->getRequest()->getData('ids');
$ids = is_string($ids) ? explode(',', $ids) : $ids;
$ids = empty($ids) ? [$id] : $ids;
if ($this->deleteMulti($ids)) {
$this->Flash->success(__('Object(s) deleted from trash'));
}

$this->Flash->success(__('Object(s) deleted from trash'));

return $this->redirect(['_name' => 'trash:list'] + $this->listQuery());
}

Expand Down Expand Up @@ -224,18 +195,11 @@ public function emptyTrash(): ?Response
$response = $this->apiClient->getObjects('trash', $query);
$counter = 0;
while (Hash::get($response, 'meta.pagination.count', 0) > 0) {
foreach ($response['data'] as $data) {
try {
$this->deleteData($data['id']);
$counter++;
} catch (BEditaClientException $e) {
// Error! Back to trash index.
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);

return $this->redirect(['_name' => 'trash:list'] + $this->listQuery());
}
$ids = Hash::extract($response, 'data.{n}.id');
if (!$this->deleteMulti($ids)) {
return $this->redirect(['_name' => 'trash:list'] + $this->listQuery());
}
$counter += count($ids);
$response = $this->apiClient->getObjects('trash', $query);
}
$this->Flash->success(__(sprintf('%d objects deleted from trash', $counter)));
Expand All @@ -255,12 +219,50 @@ public function deleteData(string $id): void
$this->apiClient->remove($id);
// this for BE versions < 5.25.1, where streams are not deleted with media on delete
$streams = (array)Hash::get($response, 'data');
foreach ($streams as $stream) {
$search = $this->apiClient->get('/streams', ['filter' => ['uuid' => $stream['id']]]);
$count = (int)Hash::get($search, 'meta.pagination.count', 0);
if ($count === 1) {
$this->apiClient->delete(sprintf('/streams/%s', $stream['id']));
}
$this->removeStreams($streams);
}

/**
* Delete multiple data and related streams, if any.
*
* @param array $ids Object IDs
* @return bool
*/
public function deleteMulti(array $ids): bool
{
try {
$response = $this->apiClient->get('/streams', ['filter' => ['object_id' => $ids]]);
$this->apiClient->removeObjects($ids);
$streams = (array)Hash::get($response, 'data');
$this->removeStreams($streams);
} catch (BEditaClientException $e) {
// Error! Back to object view.
$this->log($e->getMessage(), LogLevel::ERROR);
$this->Flash->error($e->getMessage(), ['params' => $e]);

return false;
}

return true;
}

/**
* Remove streams
*
* @param array $streams The streams to remove
* @return void
*/
public function removeStreams(array $streams): void
{
// this for BE versions < 5.25.1, where streams are not deleted with media on delete
$uuids = (array)Hash::extract($streams, '{n}.id');
if (empty($uuids)) {
return;
}
$search = $this->apiClient->get('/streams', ['filter' => ['uuid' => $uuids]]);
$search = (array)Hash::get($search, 'data');
foreach ($search as $stream) {
$this->apiClient->delete(sprintf('/streams/%s', $stream['id']));
}
}
}
4 changes: 1 addition & 3 deletions src/Utility/PermissionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ public function addPermissions(string $objectId, array $roleIds): void
*/
public function removePermissions(array $objectPermissionIds): void
{
foreach ($objectPermissionIds as $id) {
ApiClientProvider::getApiClient()->deleteObject($id, 'object_permissions');
}
ApiClientProvider::getApiClient()->deleteObjects($objectPermissionIds, 'object_permissions');
}

/**
Expand Down
6 changes: 2 additions & 4 deletions tests/TestCase/Controller/AppControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,9 @@ public function testBeforeFilterCorrectTokens(): void
/** @var \Authentication\Identity|null $user */
$user = $this->AppController->Authentication->getIdentity();
$expectedtokens = $user->get('tokens');

$event = $this->AppController->dispatchEvent('Controller.initialize');

$this->AppController->dispatchEvent('Controller.initialize');
$apiClient = $this->accessProperty($this->AppController, 'apiClient');
$apiClientTokens = $this->accessProperty($apiClient, 'tokens');
$apiClientTokens = $apiClient->getTokens();

static::assertEquals($expectedtokens, $apiClientTokens);
}
Expand Down
Loading

0 comments on commit b43cbdf

Please sign in to comment.