Skip to content

Commit 488e08e

Browse files
beni71HLeithnerQuyTonbembelimenlaoneo
authored
[6.0] Added possibility to batch remove a tag (#40613)
* Update tag.php * Added possibility to batch remove a tag * Changed text keys * Reverted string change * Deprecated batchTag function for b/c and created a new batchTags function * Fixed function docu spacing. * Fixed function docu spacing * Deprecated postStoreProcess function for b/c and created a new postStore function * Simplified else if structure. * Fixed texts in both language files. --------- Co-authored-by: Harald Leithner <[email protected]> Co-authored-by: Quy <[email protected]> Co-authored-by: Benjamin Trenkle <[email protected]> Co-authored-by: Allon Moritz <[email protected]> Co-authored-by: Gary Barclay <[email protected]>
1 parent 99d5507 commit 488e08e

File tree

8 files changed

+142
-12
lines changed

8 files changed

+142
-12
lines changed

administrator/language/en-GB/lib_joomla.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,11 @@ JLIB_HTML_BATCH_MOVE="Move"
379379
JLIB_HTML_BATCH_MOVE_QUESTION="Action to Perform"
380380
JLIB_HTML_BATCH_NO_CATEGORY="- Don't copy or move -"
381381
JLIB_HTML_BATCH_NOCHANGE="- Keep original Access Levels -"
382-
JLIB_HTML_BATCH_TAG_LABEL="Add Tag"
382+
JLIB_HTML_BATCH_TAG_ADD="Add"
383+
JLIB_HTML_BATCH_TAG_ADDREMOVE_QUESTION="Action to Perform"
384+
JLIB_HTML_BATCH_TAG_LABEL="Add or Remove Tag"
383385
JLIB_HTML_BATCH_TAG_NOCHANGE="- Keep original Tags -"
386+
JLIB_HTML_BATCH_TAG_REMOVE="Remove"
384387
JLIB_HTML_BATCH_USER_LABEL="Set User."
385388
JLIB_HTML_BATCH_USER_NOCHANGE="- Keep original User -"
386389
JLIB_HTML_BATCH_USER_NOUSER="No User."
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
3+
* @license GNU General Public License version 2 or later; see LICENSE.txt
4+
*/
5+
6+
(() => {
7+
const onSelect = () => {
8+
const batchTag = document.getElementById('batch-tag-id');
9+
const batchTagAddRemove = document.getElementById('batch-tag-addremove');
10+
let batchSelector;
11+
12+
const onChange = () => {
13+
if (!batchSelector.value
14+
|| (batchSelector.value && parseInt(batchSelector.value, 10) === 0)) {
15+
batchTagAddRemove.classList.add('hidden');
16+
} else {
17+
batchTagAddRemove.classList.remove('hidden');
18+
}
19+
};
20+
21+
if (batchTag) {
22+
batchSelector = batchTag;
23+
}
24+
25+
if (batchTagAddRemove) {
26+
batchTagAddRemove.classList.add('hidden');
27+
}
28+
29+
if (batchTagAddRemove) {
30+
batchSelector.addEventListener('change', onChange);
31+
}
32+
33+
// Cleanup
34+
document.removeEventListener('DOMContentLoaded', onSelect, true);
35+
};
36+
37+
// Document loaded
38+
document.addEventListener('DOMContentLoaded', onSelect, true);
39+
40+
// Joomla updated
41+
document.addEventListener('joomla:updated', onSelect, true);
42+
})();

build/media_source/system/joomla.asset.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@
304304
"defer": true
305305
}
306306
},
307+
{
308+
"name": "joomla.batch-tag-addremove",
309+
"type": "script",
310+
"uri": "layouts/joomla/html/batch/batch-tag-addremove.min.js",
311+
"attributes": {
312+
"defer": true
313+
}
314+
},
307315
{
308316
"name": "webcomponent.field-fancy-select-legacy",
309317
"type": "script",

language/en-GB/lib_joomla.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,11 @@ JLIB_HTML_BATCH_MOVE="Move"
375375
JLIB_HTML_BATCH_MOVE_QUESTION="Action to Perform"
376376
JLIB_HTML_BATCH_NO_CATEGORY="- Don't copy or move -"
377377
JLIB_HTML_BATCH_NOCHANGE="- Keep original Access Levels -"
378-
JLIB_HTML_BATCH_TAG_LABEL="Add Tag"
378+
JLIB_HTML_BATCH_TAG_ADD="Add"
379+
JLIB_HTML_BATCH_TAG_ADDREMOVE_QUESTION="Action to Perform"
380+
JLIB_HTML_BATCH_TAG_LABEL="Add or Remove Tag"
379381
JLIB_HTML_BATCH_TAG_NOCHANGE="- Keep original Tags -"
382+
JLIB_HTML_BATCH_TAG_REMOVE="Remove"
380383
JLIB_HTML_BATCH_USER_LABEL="Set User."
381384
JLIB_HTML_BATCH_USER_NOCHANGE="- Keep original User -"
382385
JLIB_HTML_BATCH_USER_NOUSER="No User."

layouts/joomla/html/batch/tag.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,35 @@
1010

1111
defined('_JEXEC') or die;
1212

13+
use Joomla\CMS\Factory;
1314
use Joomla\CMS\HTML\HTMLHelper;
1415
use Joomla\CMS\Language\Text;
1516

17+
// Create the add/remove tag options.
18+
$options = [
19+
HTMLHelper::_('select.option', 'a', Text::_('JLIB_HTML_BATCH_TAG_ADD')),
20+
HTMLHelper::_('select.option', 'r', Text::_('JLIB_HTML_BATCH_TAG_REMOVE'))
21+
];
22+
23+
/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
24+
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
25+
$wa->useScript('joomla.batch-tag-addremove');
26+
1627
?>
17-
<label id="batch-tag-lbl" for="batch-tag-id">
28+
<label id="batch-tag-choose-action-lbl" for="batch-tag-id">
1829
<?php echo Text::_('JLIB_HTML_BATCH_TAG_LABEL'); ?>
1930
</label>
20-
<select name="batch[tag]" class="form-select" id="batch-tag-id">
21-
<option value=""><?php echo Text::_('JLIB_HTML_BATCH_TAG_NOCHANGE'); ?></option>
22-
<?php echo HTMLHelper::_('select.options', HTMLHelper::_('tag.tags', ['filter.published' => [1]]), 'value', 'text'); ?>
23-
</select>
31+
<div id="batch-tag-choose-action" class="control-group">
32+
<select name="batch[tag]" class="form-select" id="batch-tag-id">
33+
<option value=""><?php echo Text::_('JLIB_HTML_BATCH_TAG_NOCHANGE'); ?></option>
34+
<?php echo HTMLHelper::_('select.options', HTMLHelper::_('tag.tags', ['filter.published' => [1]]), 'value', 'text'); ?>
35+
</select>
36+
</div>
37+
<div id="batch-tag-addremove" class="control-group radio">
38+
<fieldset id="batch-tag-addremove-id">
39+
<legend>
40+
<?php echo Text::_('JLIB_HTML_BATCH_TAG_ADDREMOVE_QUESTION'); ?>
41+
</legend>
42+
<?php echo HTMLHelper::_('select.radiolist', $options, 'batch[tag_addremove]', '', 'value', 'text', 'a'); ?>
43+
</fieldset>
44+
</div>

libraries/src/Helper/TagsHelper.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,29 @@ public static function getTypes($arrayType = 'objectList', $selectTypes = null,
821821
* @return boolean
822822
*
823823
* @since 3.1
824+
*
825+
* @deprecated 6.0 will be removed in 7.0
826+
* Please use postStore
824827
*/
825828
public function postStoreProcess(TableInterface $table, $newTags = [], $replace = true)
829+
{
830+
$this->postStore($table, $newTags, $replace);
831+
}
832+
833+
/**
834+
* Function that handles saving tags used in a table class after a store().
835+
*
836+
* @param TableInterface $table Table being processed.
837+
* @param array $newTags Array of new tags.
838+
* @param boolean $replace Flag indicating if all existing tags should be replaced.
839+
* This flag takes precedence before $remove.
840+
* @param boolean $remove Flag indicating if the tags in $newTags should be removed.
841+
*
842+
* @return boolean
843+
*
844+
* @since __DEPLOY_VERSION__
845+
*/
846+
public function postStore(TableInterface $table, $newTags = [], $replace = true, $remove = false)
826847
{
827848
if (!empty($table->newTags) && empty($newTags)) {
828849
$newTags = $table->newTags;
@@ -856,7 +877,11 @@ public function postStoreProcess(TableInterface $table, $newTags = [], $replace
856877
$ucmId = $ucmContentTable->core_content_id;
857878

858879
// Store the tag data if the article data was saved and run related methods.
859-
$result = $result && $this->tagItem($ucmId, $table, $newTags, $replace);
880+
if ($remove) {
881+
$result = $result && $this->unTagItem($ucmId, $table, $newTags);
882+
} else {
883+
$result = $result && $this->tagItem($ucmId, $table, $newTags, $replace);
884+
}
860885
}
861886
}
862887

libraries/src/MVC/Model/AdminModel.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,13 @@ public function batch($commands, $pks, $contexts)
325325

326326
foreach ($this->batch_commands as $identifier => $command) {
327327
if (!empty($commands[$identifier])) {
328-
if (!$this->$command($commands[$identifier], $pks, $contexts)) {
328+
if ($command === 'batchTag') {
329+
$removeTags = ArrayHelper::getValue($commands, 'tag_addremove', 'a') === 'r';
330+
331+
if (!$this->batchTags($commands[$identifier], $pks, $contexts, $removeTags)) {
332+
return false;
333+
}
334+
} elseif (!$this->$command($commands[$identifier], $pks, $contexts)) {
329335
return false;
330336
}
331337

@@ -693,8 +699,28 @@ protected function batchMove($value, $pks, $contexts)
693699
* @return boolean True if successful, false otherwise and internal error is set.
694700
*
695701
* @since 3.1
702+
*
703+
* @deprecated 6.0 will be removed in 7.0
704+
* Please use batchTags
696705
*/
697706
protected function batchTag($value, $pks, $contexts)
707+
{
708+
return $this->batchTags($value, $pks, $contexts);
709+
}
710+
711+
/**
712+
* Batch tag a list of item.
713+
*
714+
* @param integer $value The value of the new tag.
715+
* @param array $pks An array of row IDs.
716+
* @param array $contexts An array of item contexts.
717+
* @param boolean $removeTags Flag indicating whether the tags in $value have to be removed.
718+
*
719+
* @return boolean True if successful, false otherwise and internal error is set.
720+
*
721+
* @since __DEPLOY_VERSION__
722+
*/
723+
protected function batchTags($value, $pks, $contexts, $removeTags = false)
698724
{
699725
// Initialize re-usable member properties, and re-usable local variables
700726
$this->initBatch();
@@ -711,6 +737,7 @@ protected function batchTag($value, $pks, $contexts)
711737
'subject' => $this->table,
712738
'newTags' => $tags,
713739
'replaceTags' => false,
740+
'removeTags' => $removeTags,
714741
]
715742
);
716743

plugins/behaviour/taggable/src/Extension/Taggable.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,15 @@ public function onTableAfterStore(AfterStoreEvent $event)
171171
$newTags = $table->newTags ?? [];
172172

173173
if (empty($newTags)) {
174-
$result = $tagsHelper->postStoreProcess($table);
174+
$result = $tagsHelper->postStore($table);
175175
} else {
176176
if (\is_string($newTags) && (strpos($newTags, ',') !== false)) {
177177
$newTags = explode(',', $newTags);
178178
} elseif (!\is_array($newTags)) {
179179
$newTags = (array) $newTags;
180180
}
181181

182-
$result = $tagsHelper->postStoreProcess($table, $newTags);
182+
$result = $tagsHelper->postStore($table, $newTags);
183183
}
184184
}
185185

@@ -231,6 +231,7 @@ public function onTableSetNewTags(SetNewTagsEvent $event)
231231
$table = $event['subject'];
232232
$newTags = $event['newTags'];
233233
$replaceTags = $event['replaceTags'];
234+
$removeTags = $event['removeTags'];
234235

235236
// If the tags table doesn't implement the interface bail
236237
if (!($table instanceof TaggableTableInterface)) {
@@ -247,7 +248,7 @@ public function onTableSetNewTags(SetNewTagsEvent $event)
247248
$tagsHelper = $table->getTagsHelper();
248249
$tagsHelper->typeAlias = $table->getTypeAlias();
249250

250-
if (!$tagsHelper->postStoreProcess($table, $newTags, $replaceTags)) {
251+
if (!$tagsHelper->postStore($table, $newTags, $replaceTags, $removeTags)) {
251252
throw new \RuntimeException($table->getError());
252253
}
253254
}

0 commit comments

Comments
 (0)