Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/javascript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

strategy:
matrix:
node-version: [20.x, 22.x]
node-version: [20.x, 22.x, 24.x]

steps:
- name: Checkout current revision
Expand Down
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": ">=8.3",
"bedita/i18n": "^5.1.0",
"bedita/web-tools": "^5.3.3",
"bedita/web-tools": "^5.4.0",
"cakephp/authentication": "^2.9",
"cakephp/cakephp": "~4.5.0",
"cakephp/plugin-installer": "^1.3",
Expand Down
2 changes: 1 addition & 1 deletion resources/js/app/components/index-cell/index-cell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default {
default: () => [],
},
schema: {
type: Object,
type: [Object, Array],
default: () => ({}),
},
settings: {
Expand Down
24 changes: 24 additions & 0 deletions resources/js/app/pages/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default {
type: String,
default: () => ([]),
},
objects: {
type: String,
default: () => '[]',
},
},

/**
Expand All @@ -42,6 +46,8 @@ export default {
data() {
return {
allIds: [],
objectsList: JSON.parse(this.objects),
selectedObjects: '[]',
selectedRows: [],
bulkField: null,
bulkValue: null,
Expand Down Expand Up @@ -87,6 +93,8 @@ export default {
} else {
this.$refs.checkAllCB.indeterminate = true;
}
const selectedObjects = this.objectsList.filter((o) => val.includes(o.id));
this.selectedObjects = JSON.stringify(selectedObjects);
},
},

Expand All @@ -107,13 +115,17 @@ export default {
}
},
checkAll() {
const oo = this.objectsList.map((o) => { return {id: o.id, type: o.type}; });
const selectedObjects = JSON.parse(JSON.stringify(oo));
this.selectedObjects = JSON.stringify(selectedObjects);
this.selectedRows = JSON.parse(JSON.stringify(this.allIds));
for (let id of this.allIds) {
let row = document.querySelector(`a[data-id="${id}"]`);
this.rowBackground(row, true);
}
},
unCheckAll() {
this.selectedObjects = '[]';
this.selectedRows = [];
for (let id of this.allIds) {
let row = document.querySelector(`a[data-id="${id}"]`);
Expand Down Expand Up @@ -191,6 +203,18 @@ export default {
} else {
this.selectedRows.push(cb.value);
}
// push into selectedObjects, if still not present
const obj = this.objectsList.find((o) => o.id == cb.value);
if (obj) {
const selectedObjects = JSON.parse(this.selectedObjects);
const objPosition = selectedObjects.findIndex((o) => o.id == cb.value);
if (objPosition == -1) {
selectedObjects.push({id: obj.id, type: obj.type});
} else {
selectedObjects.splice(objPosition, 1);
}
this.selectedObjects = JSON.stringify(selectedObjects);
}
} else {
let row = event.target.closest('a.table-row');
let checked = event.target.checked;
Expand Down
32 changes: 24 additions & 8 deletions src/Controller/BulkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class BulkController extends AppController
*/
protected $ids = [];

/**
* Selected objects (in the format [{id:<id>, type:<type>}, ...])
*
* @var array
*/
protected $objects = [];

/**
* Selected categories
*
Expand All @@ -59,6 +66,13 @@ class BulkController extends AppController
*/
protected $errors = [];

/**
* Saved ids
*
* @var array
*/
protected $saved = [];

/**
* @inheritDoc
*/
Expand All @@ -78,7 +92,8 @@ public function initialize(): void
public function attribute(): ?Response
{
$requestData = $this->getRequest()->getData();
$this->ids = explode(',', (string)Hash::get($requestData, 'ids'));
$this->objects = $requestData['objects'];
$this->objects = is_string($this->objects) ? json_decode($this->objects, true) : $this->objects;
$this->saveAttribute($requestData['attributes']);
$this->showResult();

Expand Down Expand Up @@ -184,13 +199,13 @@ public function position(): ?Response
*/
protected function saveAttribute(array $attributes): void
{
foreach ($this->ids as $id) {
try {
$this->apiClient->save($this->objectType, compact('id') + $attributes);
} catch (BEditaClientException $e) {
$this->errors[] = ['id' => $id, 'message' => $e->getAttributes()];
}
$itemsMap = [];
foreach ($this->objects as $item) {
$itemsMap[$item['type']][] = $item['id'];
}
$result = $this->apiClient->bulkEdit($itemsMap, $attributes);
$this->errors = (array)Hash::get($result, 'data.errors');
$this->saved = (array)Hash::get($result, 'data.saved');
}

/**
Expand Down Expand Up @@ -298,7 +313,8 @@ protected function moveToPosition(string $position): void
protected function showResult(): void
{
if (empty($this->errors)) {
$this->Flash->success(__('Bulk action performed on {0} objects', count($this->ids)));
$count = count($this->saved) > 0 ? count($this->saved) : count($this->ids);
$this->Flash->success(__('Bulk action performed on {0} objects', $count));

return;
}
Expand Down
19 changes: 19 additions & 0 deletions src/View/Helper/SchemaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,23 @@ public function filterListByType(array $filtersByType, ?array $schemasByType): a

return $list;
}

/**
* Get minimal objects list (id and type only) from full objects array
*
* @param array $objects Full objects array
* @return array
*/
public function minimalObjectsList(array $objects): array
{
$list = [];
foreach ($objects as $obj) {
$list[] = [
'id' => Hash::get($obj, 'id'),
'type' => Hash::get($obj, 'type'),
];
}

return $list;
}
}
6 changes: 3 additions & 3 deletions templates/Element/FilterBox/filter_box_common.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<span class="ml-05">{{ __('Reset') }}</span>
</button>

{% if hideFilterRelations == '' %}
{% if hideFilterRelations == '' and schema and schema.relations %}
<button class="toggle-filters button button-outlined is-width-auto" type="button" @click.prevent="toggleFilterRelations()">
<app-icon icon="carbon:filter-edit" v-show="!editFilterRelations"></app-icon><span class="ml-05" v-show="!editFilterRelations">{{ __('Relations') }}</span>
<app-icon icon="carbon:filter" v-show="editFilterRelations"></app-icon><span class="ml-05" v-show="editFilterRelations">{{ __('Relations') }}</span>
Expand All @@ -73,12 +73,12 @@
</div>
</div>

{% if hideFilterRelations == '' %}
{% if hideFilterRelations == '' and schema and schema.relations %}
<div class="more-filters" v-show="editFilterRelations">
<div class="filter-container">
<related-objects-filter
:initial-filter="{{ _view.request.getQuery('filter')|json_encode }}"
:relations-schema="{{ schema['relations']|json_encode }}"
:relations-schema="{{ schema['relations']|default({})|json_encode }}"
@edit-filter-relations="editFilterRelations = 1"
@update-filter-related="updateFilterRelations"
>
Expand Down
2 changes: 2 additions & 0 deletions templates/Element/Form/bulk_properties.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
})|raw }}
<div class="fieldset" :disabled="!selectedRows.length">
<input type="hidden" name="ids" v-bind:value="selectedRows">
<input type="hidden" name="objects" v-bind:value="selectedObjects">
{% do Form.unlockField('ids') %}
{% do Form.unlockField('objects') %}

{% set options = Schema.controlOptions(key, null, schema.properties[key]) %}
{% set options = options|merge({
Expand Down
2 changes: 1 addition & 1 deletion templates/Pages/Modules/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ element('Modules/index_header', { 'meta': meta, 'filter': filter, 'Schema': Schema, 'hidePagination': indexViewType == 'tree'}) }}

{% set ids = Array.extract(objects, '{*}.id') %}
<modules-index inline-template ids='{{ ids|json_encode }}'>
<modules-index inline-template ids='{{ ids|json_encode }}' objects={{ Schema.minimalObjectsList(objects)|json_encode }}>
<div class="module-index">
{{ element('Modules/' ~ indexViewType) }}
{{ element(Element.sidebar()) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/Pages/Translations/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ element('Modules/index_header', { 'meta': meta, 'filter': filter, 'Schema': Schema }) }}

{% set ids = Array.extract(objects, '{*}.id') %}
<modules-index inline-template ids='{{ ids|json_encode }}'>
<modules-index inline-template ids='{{ ids|json_encode }}' objects={{ Schema.minimalObjectsList(objects)|json_encode }}>

<div class="module-index">

Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/Controller/BulkControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ public function testSaveAttribute(): void

// get object for test
$o = $this->getTestObject();
// set $this->controller->ids
$property = new \ReflectionProperty(BulkController::class, 'ids');
// set $this->controller->objects
$property = new \ReflectionProperty(BulkController::class, 'objects');
$property->setAccessible(true);
$property->setValue($this->controller, [$o['id']]);
$attributes = ['status' => 'on'];
$property->setValue($this->controller, [['id' => $o['id'], 'type' => $o['type']]]);
$attributes = ['status' => 'draft'];

// do controller call
$reflectionClass = new \ReflectionClass($this->controller);
Expand All @@ -311,10 +311,10 @@ public function testSaveAttribute(): void
static::assertEmpty($this->controller->getErrors());

// do controller call
// set $this->controller->ids
$property = new \ReflectionProperty(BulkController::class, 'ids');
// set $this->controller->objects
$property = new \ReflectionProperty(BulkController::class, 'objects');
$property->setAccessible(true);
$property->setValue($this->controller, ['123456789']);
$property->setValue($this->controller, [['id' => 1, 'type' => 'users']]);
$method->invokeArgs($this->controller, [$attributes]);

// check not empty errors
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/View/Helper/SchemaHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,26 @@ public function testfilterListByType(array $filtersByType, ?array $schemasByType
$actual = $this->Schema->filterListByType($filtersByType, $schemasByType);
static::assertSame($expected, $actual);
}

/**
* Test `minimalObjectsList` method.
*
* @return void
* @covers ::minimalObjectsList()
*/
public function testMinimalObjectsList(): void
{
$input = [
['id' => 1, 'title' => 'First', 'type' => 'documents', 'extra' => 'data'],
['id' => 2, 'title' => 'Second', 'type' => 'documents', 'extra' => 'data'],
['id' => 3, 'title' => 'Third', 'type' => 'images', 'extra' => 'data'],
];
$expected = [
['id' => 1, 'type' => 'documents'],
['id' => 2, 'type' => 'documents'],
['id' => 3, 'type' => 'images'],
];
$actual = $this->Schema->minimalObjectsList($input);
static::assertSame($expected, $actual);
}
}
Loading