From 1b1dcc85db65b7168afd6925076a21a1383da3a2 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 22 Apr 2021 12:49:43 -0400 Subject: [PATCH] Add tag helper methods to Template class (to work with existing 'tags' property similar to the one in Field class) --- wire/core/Template.php | 71 ++++++++++++++++++++++++++++++++++++++++- wire/core/Templates.php | 29 +++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/wire/core/Template.php b/wire/core/Template.php index 2f342d6d..490c7521 100644 --- a/wire/core/Template.php +++ b/wire/core/Template.php @@ -106,7 +106,7 @@ * Other * * @property int $compile Set to 1 to enable compilation, 2 to compile file and included files, 3 for auto, or 0 to disable. #pw-group-other - * @property string $tags Optional tags that can group this template with others in the admin templates list. #pw-group-other + * @property string $tags Optional tags that can group this template with others in the admin templates list. #pw-group-tags * @property string $pageLabelField CSV or space separated string of field names to be displayed by ProcessPageList (overrides those set with ProcessPageList config). #pw-group-other * @property int|bool $_importMode Internal use property set by template importer when importing #pw-internal * @property int|null $connectedFieldID ID of connected field or null or 0 if not applicable. #pw-internal @@ -1378,6 +1378,75 @@ class Template extends WireData implements Saveable, Exportable { return $this->wire('templates')->getPageClass($this, $withNamespace); } + /** + * Get tags array + * + * #pw-group-tags + * + * @return array + * @since 3.0.176 + * + */ + public function getTags() { + $tags = array(); + foreach(explode(' ', $this->tags) as $tag) { + if(!strlen($tag)) continue; + $tags[$tag] = $tag; + } + return $tags; + } + + /** + * Does this template have given tag? + * + * #pw-group-tags + * + * @param string $tag + * @return bool + * @since 3.0.176 + * + */ + public function hasTag($tag) { + $tags = $this->getTags(); + return isset($tags[$tag]); + } + + /** + * Add tag + * + * #pw-group-tags + * + * @param string $tag + * @return $this + * @since 3.0.176 + * + */ + public function addTag($tag) { + $tags = $this->getTags(); + if(isset($tags[$tag])) return $this; + $tags[$tag] = $tag; + $this->set('tags', implode(' ', $tags)); + return $this; + } + + /** + * Remove tag + * + * #pw-group-tags + * + * @param string $tag + * @return self + * @since 3.0.176 + * + */ + public function removeTag($tag) { + $tags = $this->getTags(); + if(!isset($tags[$tag])) return $this; + unset($tags[$tag]); + $this->set('tags', implode(' ', $tags)); + return $this; + } + /** * Check that all file asset paths are consistent with current pagefileSecure setting and access control * diff --git a/wire/core/Templates.php b/wire/core/Templates.php index db303a06..bf03cddf 100644 --- a/wire/core/Templates.php +++ b/wire/core/Templates.php @@ -785,6 +785,35 @@ class Templates extends WireSaveableItems { return $pageClass; } + /** + * Get all tags used by templates + * + * @param bool $getTemplateNames Get arrays of template names for each tag? (default=false) + * @return array + * @since 3.0.176 + * + */ + public function getTags($getTemplateNames = false) { + $tags = array(); + foreach($this as $template) { + /** @var Template $template */ + $templateTags = $template->tags; + if(empty($templateTags)) continue; + $templateTags = explode(' ', $templateTags); + foreach($templateTags as $tag) { + if(empty($tag)) continue; + if($getTemplateNames) { + if(!isset($tags[$tag])) $tags[$tag] = array(); + $tags[$tag][$template->name] = $template->name; + } else { + $tags[$tag] = $tag; + } + } + } + ksort($tags); + return $tags; + } + /** * Set a Permission for a Template for and specific Role