From f83b2928d2284a976c08b12d642ab9378060df2c Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sun, 22 May 2022 10:55:14 +0200 Subject: [PATCH] chg: [internal] Optimise fetching tags and taxonomies --- app/Controller/TagsController.php | 32 ++++++++++++++++--------------- app/Model/Taxonomy.php | 15 +++++---------- app/View/Tags/ajax/select_tag.ctp | 2 -- 3 files changed, 22 insertions(+), 27 deletions(-) delete mode 100644 app/View/Tags/ajax/select_tag.ctp diff --git a/app/Controller/TagsController.php b/app/Controller/TagsController.php index 6aec120ca..1b13dcd61 100644 --- a/app/Controller/TagsController.php +++ b/app/Controller/TagsController.php @@ -456,10 +456,11 @@ class TagsController extends AppController public function selectTaxonomy($id, $scope = 'event') { + $user = $this->_closeSession(); $localFlag = !empty($this->params['named']['local']) ? '/local:1' : ''; $items = array(); - $favourites = $this->Tag->FavouriteTag->find('count', array('conditions' => array('FavouriteTag.user_id' => $this->Auth->user('id')))); - if ($favourites) { + $hasFavourites = $this->Tag->FavouriteTag->hasAny(array('FavouriteTag.user_id' => $user['id'])); + if ($hasFavourites) { $items[] = array( 'name' => __('Favourite Tags'), 'value' => $this->baseurl . "/tags/selectTag/" . h($id) . "/favourites/" . h($scope) . $localFlag @@ -481,11 +482,11 @@ class TagsController extends AppController ); $this->loadModel('Taxonomy'); - $options = $this->Taxonomy->find('list', array('conditions' => array('enabled' => true), 'fields' => array('namespace'), 'order' => array('Taxonomy.namespace ASC'))); - foreach ($options as $k => $option) { + $taxonomies = $this->Taxonomy->find('list', array('conditions' => array('enabled' => true), 'fields' => array('namespace'), 'order' => array('Taxonomy.namespace ASC'))); + foreach ($taxonomies as $taxonomyId => $name) { $items[] = array( - 'name' => __('Taxonomy Library') . ":" . h($option), - 'value' => $this->baseurl . "/tags/selectTag/" . h($id) . "/" . h($k) . "/" . h($scope . $localFlag) + 'name' => __('Taxonomy Library') . ":" . h($name), + 'value' => $this->baseurl . "/tags/selectTag/" . h($id) . "/" . h($taxonomyId) . "/" . h($scope) . $localFlag ); } $this->set('items', $items); @@ -499,6 +500,7 @@ class TagsController extends AppController public function selectTag($id, $taxonomy_id, $scope = 'event', $filterData = '') { + $user = $this->_closeSession(); $this->loadModel('Taxonomy'); $expanded = array(); $this->set('taxonomy_id', $taxonomy_id); @@ -526,7 +528,7 @@ class TagsController extends AppController } } else { if ($taxonomy_id === '0') { - $temp = $this->Taxonomy->getAllTaxonomyTags(true, $this->Auth->user(), true, true, $local_tag); + $temp = $this->Taxonomy->getAllTaxonomyTags(true, $user, true, true, $local_tag); $tags = array(); foreach ($temp as $tag) { $tags[$tag['Tag']['id']] = $tag['Tag']; @@ -536,9 +538,9 @@ class TagsController extends AppController } elseif ($taxonomy_id === 'favourites') { $tags = array(); $conditions = array( - 'FavouriteTag.user_id' => $this->Auth->user('id'), - 'Tag.org_id' => array(0, $this->Auth->user('org_id')), - 'Tag.user_id' => array(0, $this->Auth->user('id')), + 'FavouriteTag.user_id' => $user['id'], + 'Tag.org_id' => array(0, $user['org_id']), + 'Tag.user_id' => array(0, $user['id']), 'Tag.hide_tag' => 0, ); if (!$local_tag) { @@ -560,8 +562,8 @@ class TagsController extends AppController 'Tag.hide_tag' => 0, ]; if (!$this->_isSiteAdmin()) { - $conditions['Tag.org_id'] = array(0, $this->Auth->user('org_id')); - $conditions['Tag.user_id'] = array(0, $this->Auth->user('id')); + $conditions['Tag.org_id'] = array(0, $user['org_id']); + $conditions['Tag.user_id'] = array(0, $user['id']); } if (!$local_tag) { $conditions['Tag.local_only'] = 0; @@ -590,10 +592,10 @@ class TagsController extends AppController } if (!$isSiteAdmin) { // Skip all tags that this user cannot use for tagging, determined by the org restriction on tags - if ($tag['org_id'] != '0' && $tag['org_id'] != $this->Auth->user('org_id')) { + if ($tag['org_id'] != '0' && $tag['org_id'] != $user['org_id']) { continue; } - if ($tag['user_id'] != '0' && $tag['user_id'] != $this->Auth->user('id')) { + if ($tag['user_id'] != '0' && $tag['user_id'] != $user['id']) { continue; } } @@ -657,7 +659,7 @@ class TagsController extends AppController ), )); $this->set('local', !empty($this->params['named']['local'])); - $this->render('ajax/select_tag'); + $this->render('/Elements/generic_picker'); } public function tagStatistics($percentage = false, $keysort = false) diff --git a/app/Model/Taxonomy.php b/app/Model/Taxonomy.php index a504d2792..d802f2d19 100644 --- a/app/Model/Taxonomy.php +++ b/app/Model/Taxonomy.php @@ -172,23 +172,19 @@ class Taxonomy extends AppModel */ private function __getTaxonomy($id, $options = array('full' => false, 'filter' => false)) { - $recursive = -1; - if ($options['full']) { - $recursive = 2; - } - $filter = false; if (isset($options['filter'])) { $filter = $options['filter']; } - $conditions = ['Taxonomy.id' => $id]; if (!is_numeric($id)) { $conditions = ['Taxonomy.namespace' => trim(mb_strtolower($id))]; + } else { + $conditions = ['Taxonomy.id' => $id]; } $taxonomy_params = array( - 'recursive' => -1, - 'contain' => array('TaxonomyPredicate' => array('TaxonomyEntry')), - 'conditions' => $conditions + 'recursive' => -1, + 'contain' => array('TaxonomyPredicate' => array('TaxonomyEntry')), + 'conditions' => $conditions ); $taxonomy = $this->find('first', $taxonomy_params); if (empty($taxonomy)) { @@ -348,7 +344,6 @@ class Taxonomy extends AppModel return false; } $this->Tag = ClassRegistry::init('Tag'); - $taxonomy = $this->__getTaxonomy($id, $options); if (isset($options['full']) && $options['full']) { $tagNames = array_column($taxonomy['entries'], 'tag'); $tags = $this->Tag->getTagsByName($tagNames, false); diff --git a/app/View/Tags/ajax/select_tag.ctp b/app/View/Tags/ajax/select_tag.ctp deleted file mode 100644 index e325b09d5..000000000 --- a/app/View/Tags/ajax/select_tag.ctp +++ /dev/null @@ -1,2 +0,0 @@ -
-element('generic_picker'); ?>