From cb8038bf257f40229f1e5763f61c88a6608e85c5 Mon Sep 17 00:00:00 2001 From: Tam Date: Wed, 26 Jun 2019 13:04:24 +0100 Subject: [PATCH] Add usage column / update icons --- CHANGELOG.md | 8 +++++ composer.json | 2 +- src/TagManager.php | 33 +++++++++++++++++++- src/elements/Tag.php | 48 ++++++++++++++++++++++++++--- src/elements/db/TagQuery.php | 38 +++++++++++++++++++++++ src/icon-mask.svg | 10 ++---- src/icon.svg | 19 +++--------- src/models/Settings.php | 27 ++++++++++++++++ src/templates/_settings.twig | 9 ++++++ src/translations/en/tag-manager.php | 14 +++++++++ 10 files changed, 180 insertions(+), 28 deletions(-) create mode 100644 src/elements/db/TagQuery.php create mode 100644 src/models/Settings.php create mode 100644 src/templates/_settings.twig create mode 100644 src/translations/en/tag-manager.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0890d98..7f088b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 1.0.5 - 2019-06-26 +### Added +- Add 'Usage' column to tags index table (#10) +- Add setting to enable usage column + +### Changed +- Updated plugin icon + ## 1.0.4 - 2019-06-03 ### Fixed - Fix JS issues when activating delete tag modal (#9 via @svale) diff --git a/composer.json b/composer.json index 3ab385d..a534541 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "ether/tags", "description": "A tag manager for Craft 3", "type": "craft-plugin", - "version": "1.0.4", + "version": "1.0.5", "keywords": [ "craft", "cms", diff --git a/src/TagManager.php b/src/TagManager.php index 3d6ad15..ea5d23c 100644 --- a/src/TagManager.php +++ b/src/TagManager.php @@ -8,6 +8,7 @@ namespace ether\tagManager; +use Craft; use craft\base\Element; use craft\base\Plugin; use craft\elements\actions\Edit; @@ -18,6 +19,10 @@ use craft\web\UrlManager; use ether\tagManager\elements\actions\Delete; use ether\tagManager\elements\Tag; +use ether\tagManager\models\Settings; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; use yii\base\Event; /** @@ -34,7 +39,7 @@ class TagManager extends Plugin public $schemaVersion = '1.0.0'; - public $hasCpSettings = false; + public $hasCpSettings = true; public $hasCpSection = true; @@ -81,6 +86,32 @@ public function getCpNavItem () return $item; } + protected function createSettingsModel () + { + return new Settings(); + } + + /** + * @return bool|Settings|null + */ + public function getSettings () + { + return parent::getSettings(); + } + + /** + * @return string|null + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError + */ + protected function settingsHtml () + { + return Craft::$app->getView()->renderTemplate('tag-manager/_settings', [ + 'settings' => $this->getSettings(), + ]); + } + // Events // ========================================================================= diff --git a/src/elements/Tag.php b/src/elements/Tag.php index 31f95f6..47a0156 100644 --- a/src/elements/Tag.php +++ b/src/elements/Tag.php @@ -9,7 +9,10 @@ namespace ether\tagManager\elements; use Craft; +use craft\elements\db\ElementQueryInterface; use craft\helpers\UrlHelper; +use ether\tagManager\elements\db\TagQuery; +use ether\tagManager\TagManager; use Throwable; use yii\base\InvalidConfigException; use yii\db\Query; @@ -29,9 +32,16 @@ class Tag extends \craft\elements\Tag /** @var Tag|null */ public $replaceWith; + public $usage; + // Methods // ========================================================================= + public static function find (): ElementQueryInterface + { + return new TagQuery(static::class); + } + /** * @return null|string * @throws InvalidConfigException @@ -143,18 +153,44 @@ protected static function defineSources (string $context = null): array protected static function defineTableAttributes (): array { - return [ - 'title' => ['label' => Craft::t('app', 'Title')], - 'group' => ['label' => Craft::t('app', 'Group')], + $attrs = [ + 'title' => ['label' => Craft::t('app', 'Title')], + 'group' => ['label' => Craft::t('app', 'Group')], + ]; + + if (TagManager::getInstance()->getSettings()->enableUsage) + { + $attrs += [ + 'usage' => ['label' => Craft::t('tag-manager', 'Usage')], + ]; + } + + $attrs += [ 'dateCreated' => ['label' => Craft::t('app', 'Date Created')], 'dateUpdated' => ['label' => Craft::t('app', 'Date Updated')], ]; + + return $attrs; } protected static function defineSortOptions (): array { - return [ + $opts = [ 'title' => Craft::t('app', 'Title'), + ]; + + if (TagManager::getInstance()->getSettings()->enableUsage) + { + $opts += [ + [ + 'label' => Craft::t('app', 'Usage'), + 'orderBy' => 'usage', + 'attribute' => 'usage', + ], + ]; + } + + $opts += [ [ 'label' => Craft::t('app', 'Date Created'), 'orderBy' => 'elements.dateCreated', @@ -164,8 +200,10 @@ protected static function defineSortOptions (): array 'label' => Craft::t('app', 'Date Updated'), 'orderBy' => 'elements.dateUpdated', 'attribute' => 'dateUpdated', - ] + ], ]; + + return $opts; } } \ No newline at end of file diff --git a/src/elements/db/TagQuery.php b/src/elements/db/TagQuery.php new file mode 100644 index 0000000..d6f1423 --- /dev/null +++ b/src/elements/db/TagQuery.php @@ -0,0 +1,38 @@ +getSettings()->enableUsage) + return parent::beforePrepare(); + + $getUsage = new Expression( + '(SELECT COUNT(*) FROM (SELECT [[r.sourceId]], [[r.sourceSiteId]] FROM {{%relations}} r WHERE [[r.targetId]] = [[elements.id]] GROUP BY [[r.sourceId]], [[r.sourceSiteId]]) as usage) as [[usage]]' + ); + + $this->addSelect($getUsage); + $this->subQuery->addSelect($getUsage); + + return parent::beforePrepare(); + } + +} \ No newline at end of file diff --git a/src/icon-mask.svg b/src/icon-mask.svg index 9923c81..d2a7a9c 100644 --- a/src/icon-mask.svg +++ b/src/icon-mask.svg @@ -1,7 +1,3 @@ - - - - - - - \ No newline at end of file + + + diff --git a/src/icon.svg b/src/icon.svg index b2f9c94..762e020 100644 --- a/src/icon.svg +++ b/src/icon.svg @@ -1,16 +1,7 @@ - - - Plugin Icon - Created with Sketch. - - - - - - - - - + + + + - \ No newline at end of file + diff --git a/src/models/Settings.php b/src/models/Settings.php new file mode 100644 index 0000000..9560506 --- /dev/null +++ b/src/models/Settings.php @@ -0,0 +1,27 @@ + 'Usage', + 'Enable Usage' => 'Enable Usage', + 'Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).' => + 'Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).', +]; \ No newline at end of file