From ff2edcccd18d883ef4b6d23b83be5a5a62714e17 Mon Sep 17 00:00:00 2001 From: Peter Bowyer Date: Mon, 20 Aug 2018 20:49:30 +0100 Subject: [PATCH 1/2] Initial refactoring. Still got to work out how to deal with $total and $maxCnt --- .../snippets/taggergettags.snippet.php | 102 +---------------- .../tagger/model/tagger/tagger.class.php | 107 ++++++++++++++++++ 2 files changed, 108 insertions(+), 101 deletions(-) diff --git a/core/components/tagger/elements/snippets/taggergettags.snippet.php b/core/components/tagger/elements/snippets/taggergettags.snippet.php index 57cb5be..b64a54b 100644 --- a/core/components/tagger/elements/snippets/taggergettags.snippet.php +++ b/core/components/tagger/elements/snippets/taggergettags.snippet.php @@ -41,126 +41,26 @@ $tagger = $modx->getService('tagger','Tagger',$modx->getOption('tagger.core_path',null,$modx->getOption('core_path').'components/tagger/').'model/tagger/',$scriptProperties); if (!($tagger instanceof Tagger)) return ''; -$resources = $modx->getOption('resources', $scriptProperties, ''); -$parents = $modx->getOption('parents', $scriptProperties, ''); -$groups = $modx->getOption('groups', $scriptProperties, ''); $target = (int) $modx->getOption('target', $scriptProperties, $modx->resource->id, true); -$showUnused = (int) $modx->getOption('showUnused', $scriptProperties, '0'); -$showUnpublished = (int) $modx->getOption('showUnpublished', $scriptProperties, '0'); -$showDeleted = (int) $modx->getOption('showDeleted', $scriptProperties, '0'); $linkCurrentTags = (int) $modx->getOption('linkCurrentTags', $scriptProperties, '0'); $linkOneTagPerGroup = (int) $modx->getOption('linkOneTagPerGroup', $scriptProperties, '0'); -$contexts = $modx->getOption('contexts', $scriptProperties, ''); $translate = (int) $modx->getOption('translate', $scriptProperties, '0'); $defaultRowTpl = $modx->getOption('rowTpl', $scriptProperties, ''); $outTpl = $modx->getOption('outTpl', $scriptProperties, ''); $wrapIfEmpty = $modx->getOption('wrapIfEmpty', $scriptProperties, 1); $separator = $modx->getOption('separator', $scriptProperties, ''); -$limit = intval($modx->getOption('limit', $scriptProperties, 0)); -$offset = intval($modx->getOption('offset', $scriptProperties, 0)); -$totalPh = $modx->getOption('totalPh', $scriptProperties, 'tags_total'); - $weight = (int) $modx->getOption('weight', $scriptProperties, '0'); $friendlyURL = (int) $modx->getOption('friendlyURL', $scriptProperties, $modx->getOption('friendly_urls', null, 0)); $linkTagScheme = $modx->getOption('linkTagScheme', $scriptProperties, $modx->getOption('link_tag_scheme', null, -1)); - -$sort = $modx->getOption('sort', $scriptProperties, '{}'); -$sort = $modx->fromJSON($sort); -if ($sort === null || $sort == '' || count($sort) == 0) { - $sort = array( - 'tag' => 'ASC' - ); -} - -$resources = $tagger->explodeAndClean($resources); -$parents = $tagger->explodeAndClean($parents); -$groups = $tagger->explodeAndClean($groups); -$contexts = $tagger->explodeAndClean($contexts); $toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, ''); -$c = $modx->newQuery('TaggerTag'); - -$c->leftJoin('TaggerTagResource', 'Resources'); -$c->leftJoin('TaggerGroup', 'Group'); -$c->leftJoin('modResource', 'Resource', array('Resources.resource = Resource.id')); - -if (!empty($parents)) { - $c->where(array( - 'Resource.parent:IN' => $parents, - )); -} - -if (!empty($contexts)) { - $c->where(array( - 'Resource.context_key:IN' => $contexts, - )); -} - -if ($showUnpublished == 0) { - $c->where(array( - 'Resource.published' => 1, - 'OR:Resource.published:IN' => null, - )); -} - -if ($showDeleted == 0) { - $c->where(array( - 'Resource.deleted' => 0, - 'OR:Resource.deleted:IS' => null, - )); -} - -if ($showUnused == 0) { - $c->having(array( - 'cnt > 0', - )); -} - -if (!empty($resources)) { - $c->where(array( - 'Resources.resource:IN' => $resources - )); -} - -if ($groups) { - $c->where(array( - 'Group.id:IN' => $groups, - 'OR:Group.name:IN' => $groups, - 'OR:Group.alias:IN' => $groups, - )); -} -$c->select($modx->getSelectColumns('TaggerTag', 'TaggerTag')); -$c->select($modx->getSelectColumns('TaggerGroup', 'Group', 'group_')); -$c->select(array('cnt' => 'COUNT(Resources.tag)')); -$c->groupby($modx->getSelectColumns('TaggerTag', 'TaggerTag') . ',' . $modx->getSelectColumns('TaggerGroup', 'Group')); - -$c->prepare(); -$countQuery = new xPDOCriteria($modx, "SELECT COUNT(*) as total, MAX(cnt) as max_cnt FROM ({$c->toSQL(false)}) cq", $c->bindings, $c->cacheFlag); -$stmt = $countQuery->prepare(); - -if ($stmt && $stmt->execute()) { - $fetchedData = $stmt->fetch(PDO::FETCH_ASSOC); - $total = intval($fetchedData['total']); - $maxCnt = intval($fetchedData['max_cnt']); -} else { - $total = 0; - $maxCnt = 0; -} +$tags = $tagger->getTags($scriptProperties); $modx->setPlaceholder($totalPh, $total); -foreach ($sort as $field => $dir) { - $dir = (strtolower($dir) == 'asc') ? 'asc' : 'desc'; - $c->sortby($field, $dir); -} - -$c->limit($limit, $offset); - -$tags = $modx->getIterator('TaggerTag', $c); - $out = array(); // prep for &tpl_N diff --git a/core/components/tagger/model/tagger/tagger.class.php b/core/components/tagger/model/tagger/tagger.class.php index 7a1cc03..2f10007 100644 --- a/core/components/tagger/model/tagger/tagger.class.php +++ b/core/components/tagger/model/tagger/tagger.class.php @@ -137,4 +137,111 @@ public function getCurrentTags() return $currentTags; } + + public function getTags($scriptProperties) + { + $resources = $this->modx->getOption('resources', $scriptProperties, ''); + $parents = $this->modx->getOption('parents', $scriptProperties, ''); + $groups = $this->modx->getOption('groups', $scriptProperties, ''); + $showUnused = (int) $this->modx->getOption('showUnused', $scriptProperties, '0'); + $showUnpublished = (int) $this->modx->getOption('showUnpublished', $scriptProperties, '0'); + $showDeleted = (int) $this->modx->getOption('showDeleted', $scriptProperties, '0'); + $contexts = $this->modx->getOption('contexts', $scriptProperties, ''); + $limit = intval($this->modx->getOption('limit', $scriptProperties, 0)); + $offset = intval($this->modx->getOption('offset', $scriptProperties, 0)); + $totalPh = $this->modx->getOption('totalPh', $scriptProperties, 'tags_total'); + + $sort = $this->modx->getOption('sort', $scriptProperties, '{}'); + $sort = $this->modx->fromJSON($sort); + if ($sort === null || $sort == '' || count($sort) == 0) { + $sort = array( + 'tag' => 'ASC' + ); + } + + $resources = $this->explodeAndClean($resources); + $parents = $this->explodeAndClean($parents); + $groups = $this->explodeAndClean($groups); + $contexts = $this->explodeAndClean($contexts); + + $c = $this->modx->newQuery('TaggerTag'); + + $c->leftJoin('TaggerTagResource', 'Resources'); + $c->leftJoin('TaggerGroup', 'Group'); + $c->leftJoin('modResource', 'Resource', array('Resources.resource = Resource.id')); + + if (!empty($parents)) { + $c->where(array( + 'Resource.parent:IN' => $parents, + )); + } + + if (!empty($contexts)) { + $c->where(array( + 'Resource.context_key:IN' => $contexts, + )); + } + + if ($showUnpublished == 0) { + $c->where(array( + 'Resource.published' => 1, + 'OR:Resource.published:IN' => null, + )); + } + + if ($showDeleted == 0) { + $c->where(array( + 'Resource.deleted' => 0, + 'OR:Resource.deleted:IS' => null, + )); + } + + if ($showUnused == 0) { + $c->having(array( + 'cnt > 0', + )); + } + + if (!empty($resources)) { + $c->where(array( + 'Resources.resource:IN' => $resources + )); + } + + if ($groups) { + $c->where(array( + 'Group.id:IN' => $groups, + 'OR:Group.name:IN' => $groups, + 'OR:Group.alias:IN' => $groups, + )); + } + $c->select($this->modx->getSelectColumns('TaggerTag', 'TaggerTag')); + $c->select($this->modx->getSelectColumns('TaggerGroup', 'Group', 'group_')); + $c->select(array('cnt' => 'COUNT(Resources.tag)')); + $c->groupby($this->modx->getSelectColumns('TaggerTag', 'TaggerTag') . ',' . $this->modx->getSelectColumns('TaggerGroup', 'Group')); + + $c->prepare(); + + $countQuery = new xPDOCriteria($this->modx, "SELECT COUNT(*) as total, MAX(cnt) as max_cnt FROM ({$c->toSQL(false)}) cq", $c->bindings, $c->cacheFlag); + $stmt = $countQuery->prepare(); + + if ($stmt && $stmt->execute()) { + $fetchedData = $stmt->fetch(PDO::FETCH_ASSOC); + $total = intval($fetchedData['total']); + $maxCnt = intval($fetchedData['max_cnt']); + } else { + $total = 0; + $maxCnt = 0; + } + + + foreach ($sort as $field => $dir) { + $dir = (strtolower($dir) == 'asc') ? 'asc' : 'desc'; + $c->sortby($field, $dir); + } + + $c->limit($limit, $offset); + + return $this->modx->getIterator('TaggerTag', $c); + } } \ No newline at end of file From ead3b40991c59b1eff0f416e4c90ce73a99e5388 Mon Sep 17 00:00:00 2001 From: Peter Bowyer Date: Mon, 20 Aug 2018 21:03:05 +0100 Subject: [PATCH 2/2] Add a new result class to hold tags, total and max count. Not the return signature that I want from getTags(), but not clear how to beter return everything from the two queries. Two separate methods would be better, but there is overlap and the two are tied together. Propose and see how others want to refactor. --- .../elements/snippets/taggergettags.snippet.php | 6 +++++- .../components/tagger/model/tagger/tagger.class.php | 13 ++++++------- .../tagger/model/tagger/taggerresult.class.php | 13 +++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 core/components/tagger/model/tagger/taggerresult.class.php diff --git a/core/components/tagger/elements/snippets/taggergettags.snippet.php b/core/components/tagger/elements/snippets/taggergettags.snippet.php index b64a54b..ce20821 100644 --- a/core/components/tagger/elements/snippets/taggergettags.snippet.php +++ b/core/components/tagger/elements/snippets/taggergettags.snippet.php @@ -55,9 +55,13 @@ $friendlyURL = (int) $modx->getOption('friendlyURL', $scriptProperties, $modx->getOption('friendly_urls', null, 0)); $linkTagScheme = $modx->getOption('linkTagScheme', $scriptProperties, $modx->getOption('link_tag_scheme', null, -1)); $toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, ''); +$totalPh = $modx->getOption('totalPh', $scriptProperties, 'tags_total'); -$tags = $tagger->getTags($scriptProperties); +$taggerResult = $tagger->getTags($scriptProperties); +$tags = $taggerResult->tags; +$total = $taggerResult->total; +$maxCnt = $taggerResult->maxCnt; $modx->setPlaceholder($totalPh, $total); diff --git a/core/components/tagger/model/tagger/tagger.class.php b/core/components/tagger/model/tagger/tagger.class.php index 2f10007..92d6a84 100644 --- a/core/components/tagger/model/tagger/tagger.class.php +++ b/core/components/tagger/model/tagger/tagger.class.php @@ -149,7 +149,6 @@ public function getTags($scriptProperties) $contexts = $this->modx->getOption('contexts', $scriptProperties, ''); $limit = intval($this->modx->getOption('limit', $scriptProperties, 0)); $offset = intval($this->modx->getOption('offset', $scriptProperties, 0)); - $totalPh = $this->modx->getOption('totalPh', $scriptProperties, 'tags_total'); $sort = $this->modx->getOption('sort', $scriptProperties, '{}'); $sort = $this->modx->fromJSON($sort); @@ -225,13 +224,12 @@ public function getTags($scriptProperties) $countQuery = new xPDOCriteria($this->modx, "SELECT COUNT(*) as total, MAX(cnt) as max_cnt FROM ({$c->toSQL(false)}) cq", $c->bindings, $c->cacheFlag); $stmt = $countQuery->prepare(); + $result = new TaggerResult(); + if ($stmt && $stmt->execute()) { $fetchedData = $stmt->fetch(PDO::FETCH_ASSOC); - $total = intval($fetchedData['total']); - $maxCnt = intval($fetchedData['max_cnt']); - } else { - $total = 0; - $maxCnt = 0; + $result->total = intval($fetchedData['total']); + $result->maxCnt = intval($fetchedData['max_cnt']); } @@ -242,6 +240,7 @@ public function getTags($scriptProperties) $c->limit($limit, $offset); - return $this->modx->getIterator('TaggerTag', $c); + $result->tags = $this->modx->getIterator('TaggerTag', $c); + return $result; } } \ No newline at end of file diff --git a/core/components/tagger/model/tagger/taggerresult.class.php b/core/components/tagger/model/tagger/taggerresult.class.php new file mode 100644 index 0000000..20d43a6 --- /dev/null +++ b/core/components/tagger/model/tagger/taggerresult.class.php @@ -0,0 +1,13 @@ +