Workaround for the search for objects matching multiple tags
authorAlexander Ebert <ebert@woltlab.com>
Sun, 5 Jan 2020 19:08:00 +0000 (20:08 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 5 Jan 2020 19:08:00 +0000 (20:08 +0100)
wcfsetup/install/files/lib/system/tagging/TTaggedObjectList.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/tagging/TTaggedObjectList.class.php b/wcfsetup/install/files/lib/system/tagging/TTaggedObjectList.class.php
new file mode 100644 (file)
index 0000000..742367a
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace wcf\system\tagging;
+use wcf\util\StringUtil;
+
+/**
+ * Helper functions to query tagged objects.
+ * 
+ * @author      Alexander Ebert
+ * @copyright   2001-2020 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package     WoltLabSuite\Core\System\Tagging
+ */
+trait TTaggedObjectList {
+       /**
+        * Processes the `$orderBy` parameters to inject the parameter into the `GROUP BY` parameter.
+        * 
+        * @param string $groupBy
+        * @param string $orderBy
+        * @return string
+        */
+       protected function getGroupByFromOrderBy($groupBy, $orderBy) {
+               if (!empty($orderBy)) {
+                       $orderBy = explode(',', $orderBy);
+                       $orderBy = array_map(function ($order) {
+                               return StringUtil::trim(preg_replace('~\s+(?:ASC|DESC)\s*$~i', '', $order));
+                       }, $orderBy);
+                       
+                       $orderBy = ', ' . implode(', ', $orderBy);
+               }
+               
+               return "GROUP BY {$groupBy}{$orderBy}";
+       }
+}