Skip to content

Commit

Permalink
Add usage column / update icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Jun 26, 2019
1 parent aeaa5f7 commit cb8038b
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 28 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 32 additions & 1 deletion src/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace ether\tagManager;

use Craft;
use craft\base\Element;
use craft\base\Plugin;
use craft\elements\actions\Edit;
Expand All @@ -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;

/**
Expand All @@ -34,7 +39,7 @@ class TagManager extends Plugin

public $schemaVersion = '1.0.0';

public $hasCpSettings = false;
public $hasCpSettings = true;

public $hasCpSection = true;

Expand Down Expand Up @@ -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
// =========================================================================

Expand Down
48 changes: 43 additions & 5 deletions src/elements/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -164,8 +200,10 @@ protected static function defineSortOptions (): array
'label' => Craft::t('app', 'Date Updated'),
'orderBy' => 'elements.dateUpdated',
'attribute' => 'dateUpdated',
]
],
];

return $opts;
}

}
38 changes: 38 additions & 0 deletions src/elements/db/TagQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Tags for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/

namespace ether\tagManager\elements\db;

use ether\tagManager\TagManager;
use yii\db\Expression;

/**
* Class TagQuery
*
* @author Ether Creative
* @package ether\tagManager\elements\db
*/
class TagQuery extends \craft\elements\db\TagQuery
{

protected function beforePrepare (): bool
{
if (!TagManager::getInstance()->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();
}

}
10 changes: 3 additions & 7 deletions src/icon-mask.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 5 additions & 14 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Tags for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/

namespace ether\tagManager\models;

use craft\base\Model;

/**
* Class Settings
*
* @author Ether Creative
* @package ether\tagManager\models
*/
class Settings extends Model
{

/**
* @var bool Will enable the usage column (may be slow on larger sites)
*/
public $enableUsage = false;

}
9 changes: 9 additions & 0 deletions src/templates/_settings.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% import "_includes/forms" as forms %}

{{ forms.lightswitchField({
label: 'Enable Usage'|t('tag-manager'),
instructions: 'Will enable the usage column in the CP (has no effect on regular tag queries, may be slow on larger sites).'|t('tag-manager'),
name: 'enableUsage',
on: settings.enableUsage,
first: true,
}) }}
14 changes: 14 additions & 0 deletions src/translations/en/tag-manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Tags for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2019 Ether Creative
*/

return [
'Usage' => '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).',
];

0 comments on commit cb8038b

Please sign in to comment.