From 29595c6e22d13b8f9de30356849e2cb412060bd9 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 26 Aug 2021 16:12:55 +0200 Subject: [PATCH] chg: [tag] Continuation of integrating tagging plugin - WiP - Can tag/untag any model --- src/Controller/Component/CRUDComponent.php | 20 +++++++++++--- src/Controller/IndividualsController.php | 1 + src/View/Helper/TagHelper.php | 27 +++++++++++-------- .../genericElements/Form/Fields/tagsField.php | 8 +----- .../SingleViews/Fields/tagsField.php | 10 ++----- templates/genericTemplates/tag.php | 8 +----- webroot/js/bootstrap-helper.js | 3 +++ webroot/js/main.js | 15 ++++++----- 8 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 2f32bcd..0342a67 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -7,6 +7,7 @@ use Cake\Error\Debugger; use Cake\Utility\Hash; use Cake\Utility\Inflector; use Cake\View\ViewBuilder; +use Cake\ORM\TableRegistry; use Cake\Http\Exception\MethodNotAllowedException; use Cake\Http\Exception\NotFoundException; @@ -355,6 +356,7 @@ class CRUDComponent extends Component if ($this->taggingSupported()) { $params['contain'][] = 'Tags'; + $this->setAllTags(); } $data = $this->Table->get($id, $params); @@ -417,6 +419,7 @@ class CRUDComponent extends Component throw new Exception("Table {$this->TableAlias} does not support tagging"); } if ($this->request->is('get')) { + $this->setAllTags(); if(!empty($id)) { $params = [ 'contain' => 'Tags', @@ -455,9 +458,9 @@ class CRUDComponent extends Component $message = $this->getMessageBasedOnResult( $bulkSuccesses == count($ids), $isBulk, - __('{0} tagged.', $this->ObjectAlias), + __('{0} tagged with `{1}`.', $this->ObjectAlias, $input['tag_list']), __('All {0} have been tagged.', Inflector::pluralize($this->ObjectAlias)), - __('Could not tag {0}.', $this->ObjectAlias), + __('Could not tag {0} with `{1}`.', $this->ObjectAlias, $input['tag_list']), __('{0} / {1} {2} have been tagged.', $bulkSuccesses, count($ids), @@ -476,6 +479,7 @@ class CRUDComponent extends Component throw new Exception("Table {$this->TableAlias} does not support tagging"); } if ($this->request->is('get')) { + $this->setAllTags(); if(!empty($id)) { $params = [ 'contain' => 'Tags', @@ -516,9 +520,9 @@ class CRUDComponent extends Component $message = $this->getMessageBasedOnResult( $bulkSuccesses == count($ids), $isBulk, - __('{0} untagged.', $this->ObjectAlias), + __('{0} untagged with `{1}`.', $this->ObjectAlias, implode(', ', $tagsToRemove)), __('All {0} have been untagged.', Inflector::pluralize($this->ObjectAlias)), - __('Could not untag {0}.', $this->ObjectAlias), + __('Could not untag {0} with `{1}`.', $this->ObjectAlias, $input['tag_list']), __('{0} / {1} {2} have been untagged.', $bulkSuccesses, count($ids), @@ -549,6 +553,7 @@ class CRUDComponent extends Component $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); } $this->Controller->set('entity', $data); + $this->setAllTags(); $this->Controller->viewBuilder()->setLayout('ajax'); $this->Controller->render('/genericTemplates/tag'); } @@ -837,6 +842,13 @@ class CRUDComponent extends Component return $this->Table->behaviors()->has('Tag'); } + public function setAllTags() + { + $this->Tags = TableRegistry::getTableLocator()->get('Tags.Tags'); + $allTags = $this->Tags->find()->all()->toList(); + $this->Controller->set('allTags', $allTags); + } + public function toggle(int $id, string $fieldName = 'enabled', array $params = []): void { if (empty($id)) { diff --git a/src/Controller/IndividualsController.php b/src/Controller/IndividualsController.php index 13e37c8..046824f 100644 --- a/src/Controller/IndividualsController.php +++ b/src/Controller/IndividualsController.php @@ -9,6 +9,7 @@ use Cake\Database\Expression\QueryExpression; use Cake\Http\Exception\NotFoundException; use Cake\Http\Exception\MethodNotAllowedException; use Cake\Http\Exception\ForbiddenException; +use Cake\ORM\TableRegistry; class IndividualsController extends AppController { diff --git a/src/View/Helper/TagHelper.php b/src/View/Helper/TagHelper.php index 9e38f1c..9b0449b 100644 --- a/src/View/Helper/TagHelper.php +++ b/src/View/Helper/TagHelper.php @@ -27,15 +27,20 @@ class TagHelper extends Helper $field = 'tag_list'; $values = !empty($options['allTags']) ? array_map(function($tag) { return [ - 'text' => h($tag['text']), - 'value' => h($tag['text']), + 'text' => h($tag['label']), + 'value' => h($tag['label']), 'data-colour' => h($tag['colour']), + 'data-text-colour' => h($tag['text_colour']), ]; }, $options['allTags']) : []; $selectConfig = [ 'multiple' => true, - // 'value' => $options['tags'], - 'class' => ['tag-input', 'd-none'] + 'class' => ['tag-input', 'd-none'], + 'data-url' => $this->Url->build([ + 'controller' => $this->getView()->getName(), + 'action' => 'tag', + $this->getView()->get('entity')['id'] + ]), ]; return $this->Form->select($field, $values, $selectConfig); } @@ -63,11 +68,11 @@ class TagHelper extends Helper $html .= '
'; $html .= '
'; foreach ($tags as $tag) { - if (is_array($tag)) { + if (is_object($tag)) { $html .= $this->tag($tag); } else { $html .= $this->tag([ - 'name' => $tag + 'label' => $tag ]); } } @@ -81,13 +86,13 @@ class TagHelper extends Helper return $html; } - public function tag(array $tag, array $options = []) + public function tag($tag, array $options = []) { if (empty($this->_config)) { $this->_config = array_merge($this->defaultConfig, $options); } $tag['colour'] = !empty($tag['colour']) ? $tag['colour'] : $this->getConfig('default_colour'); - $textColour = $this->TextColour->getTextColour(h($tag['colour'])); + $textColour = !empty($tag['text_colour']) ? $tag['text_colour'] : $this->TextColour->getTextColour(h($tag['colour']));; if (!empty($this->getConfig('editable'))) { $deleteButton = $this->Bootstrap->button([ @@ -103,7 +108,7 @@ class TagHelper extends Helper 'action' => 'untag', $this->getView()->get('entity')['id'] ]), - h($tag['name']) + h($tag['label']) ), ], ]); @@ -118,9 +123,9 @@ class TagHelper extends Helper 'mx-1', 'align-middle', ], - 'title' => h($tag['name']), + 'title' => h($tag['label']), 'style' => sprintf('color:%s; background-color:%s', $textColour, h($tag['colour'])), - ], h($tag['name']) . $deleteButton); + ], h($tag['label']) . $deleteButton); return $html; } } diff --git a/templates/element/genericElements/Form/Fields/tagsField.php b/templates/element/genericElements/Form/Fields/tagsField.php index fdc99f1..ba35700 100644 --- a/templates/element/genericElements/Form/Fields/tagsField.php +++ b/templates/element/genericElements/Form/Fields/tagsField.php @@ -1,13 +1,7 @@ 'tlp:red', 'text' => 'tlp:red', 'colour' => 'red'], - ['id' => 'tlp:green', 'text' => 'tlp:green', 'colour' => 'green'], - ['id' => 'tlp:amber', 'text' => 'tlp:amber', 'colour' => '#983965'], - ['id' => 'tlp:white', 'text' => 'tlp:white', 'colour' => 'white'], - ]; $tagsHtml = $this->Tag->tags([ 'allTags' => $allTags, - 'tags' => $entity['tag_list'], + 'tags' => $entity['tags'], 'picker' => true, ]); ?> diff --git a/templates/element/genericElements/SingleViews/Fields/tagsField.php b/templates/element/genericElements/SingleViews/Fields/tagsField.php index 564ee3e..16f7a71 100644 --- a/templates/element/genericElements/SingleViews/Fields/tagsField.php +++ b/templates/element/genericElements/SingleViews/Fields/tagsField.php @@ -1,15 +1,9 @@ 'tlp:red', 'text' => 'tlp:red', 'colour' => 'red'], - ['id' => 'tlp:green', 'text' => 'tlp:green', 'colour' => 'green'], - ['id' => 'tlp:amber', 'text' => 'tlp:amber', 'colour' => '#983965'], - ['id' => 'tlp:white', 'text' => 'tlp:white', 'colour' => 'white'], -]; +$tags = Cake\Utility\Hash::get($data, 'tags'); echo $this->Tag->tags([ 'allTags' => $allTags, - 'tags' => $tagList, + 'tags' => $tags, 'picker' => true, 'editable' => true, ]); diff --git a/templates/genericTemplates/tag.php b/templates/genericTemplates/tag.php index 4f97df8..c1bc69b 100644 --- a/templates/genericTemplates/tag.php +++ b/templates/genericTemplates/tag.php @@ -1,13 +1,7 @@ 'tlp:red', 'text' => 'tlp:red', 'colour' => 'red'], - ['id' => 'tlp:green', 'text' => 'tlp:green', 'colour' => 'green'], - ['id' => 'tlp:amber', 'text' => 'tlp:amber', 'colour' => '#983965'], - ['id' => 'tlp:white', 'text' => 'tlp:white', 'colour' => 'white'], - ]; echo $this->Tag->tags([ 'allTags' => $allTags, - 'tags' => $entity->tag_list, + 'tags' => $entity->tags, 'picker' => true, 'editable' => true, ]); diff --git a/webroot/js/bootstrap-helper.js b/webroot/js/bootstrap-helper.js index 48be80c..cc740d3 100644 --- a/webroot/js/bootstrap-helper.js +++ b/webroot/js/bootstrap-helper.js @@ -1121,6 +1121,9 @@ class HtmlHelper { } static tag(options={}) { + if (!options.colour) { + options.colour = '#924da6' + } const $tag = $('') .addClass(['tag', 'badge', 'border']) .css({color: getTextColour(options.colour), 'background-color': options.colour}) diff --git a/webroot/js/main.js b/webroot/js/main.js index 7c5cd73..57e7560 100644 --- a/webroot/js/main.js +++ b/webroot/js/main.js @@ -119,7 +119,7 @@ function createTagPicker(clicked) { function templateTag(state) { if (!state.id) { - return state.text; + return state.label; } if (state.colour === undefined) { state.colour = $(state.element).data('colour') @@ -141,7 +141,7 @@ function createTagPicker(clicked) { .append($('').text('Save').prepend($('').addClass('fa fa-save mr-1'))) .click(function() { const tags = $select.select2('data').map(tag => tag.text) - addTags(tags, $(this)) + addTags($select.data('url'), tags, $(this)) }) const $cancelButton = $('').addClass(['btn btn-secondary btn-sm', 'align-self-start']).attr('type', 'button') .append($('').text('Cancel').prepend($('').addClass('fa fa-times mr-1'))) @@ -176,11 +176,15 @@ function deleteTag(url, tag, clicked) { }) const theToast = UI.toast({ variant: 'success', - title: 'Tag deleted', + title: result.message, bodyHtml: $('
').append( $('').text('Cancel untag operation.'), $('