From 7cfb815512be54227144a747e13f3ca26b150e42 Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Mon, 31 Aug 2020 14:44:46 +0200 Subject: [PATCH] fix: quiqqer/products#263 --- ajax/categories/search.php | 38 ++++++++++--- database.xml | 2 + events.xml | 1 + src/QUI/ERP/Products/EventHandling.php | 60 +++++++++++++++++++++ src/QUI/ERP/Products/Handler/Categories.php | 12 +++-- 5 files changed, 102 insertions(+), 11 deletions(-) diff --git a/ajax/categories/search.php b/ajax/categories/search.php index 423bee61..5390843e 100644 --- a/ajax/categories/search.php +++ b/ajax/categories/search.php @@ -25,18 +25,20 @@ function ($fields, $params) { $fields = []; } - if (isset($params['order'])) { - $query['order'] = $params['order']; - } +// if (isset($fields['order'])) { +// $query['order'] = $fields['order']; +// } - if (isset($params['limit'])) { - $query['limit'] = $params['limit']; + if (isset($fields['limit'])) { + $query['limit'] = $fields['limit']; } $allowedFields = $Categories->getChildAttributes(); $allowedFields = \array_flip($allowedFields); - foreach ($fields as $field => $value) { + $searchString = ''; + + foreach ($params as $field => $value) { if (!isset($allowedFields[$field]) && $field != 'id') { continue; } @@ -45,6 +47,20 @@ function ($fields, $params) { 'type' => '%LIKE%', 'value' => $value ]; + + if ($field === 'fields') { + $query['where_or']['title_cache'] = [ + 'type' => '%LIKE%', + 'value' => $value + ]; + + $query['where_or']['description_cache'] = [ + 'type' => '%LIKE%', + 'value' => $value + ]; + + $searchString = $value; + } } // search @@ -62,6 +78,16 @@ function ($fields, $params) { return $a['title'] > $b['title']; }); + // all products at the beginning + $AllProducts = new \QUI\ERP\Products\Category\AllProducts(); + + if (!empty($searchString) && \stripos($AllProducts->getTitle(), $searchString) !== false) { + $allProducts = $AllProducts->getAttributes(); + $allProducts['title'] = $AllProducts->getTitle(); + + \array_unshift($result, $allProducts); + } + return $result; }, ['fields', 'params'], diff --git a/database.xml b/database.xml index 30ea734a..29b1432c 100644 --- a/database.xml +++ b/database.xml @@ -35,6 +35,8 @@ <field type="BIGINT(20) DEFAULT 0">parentId</field> <field type="TEXT NULL">fields</field> <field type="TEXT NULL">sites</field> + <field type="TEXT NULL">title_cache</field> + <field type="TEXT NULL">description_cache</field> <primary>id</primary> <auto_increment>id</auto_increment> diff --git a/events.xml b/events.xml index a4cea0fe..0dd96833 100644 --- a/events.xml +++ b/events.xml @@ -3,6 +3,7 @@ <event on="onTemplateGetHeader" fire="\QUI\ERP\Products\EventHandling::onTemplateGetHeader"/> <event on="onPermissionsSet" fire="\QUI\ERP\Products\EventHandling::onPermissionsSet"/> <event on="onRequest" fire="\QUI\ERP\Products\EventHandling::onRequest"/> + <event on="onQuiqqerTranslatorPublish" fire="\QUI\ERP\Products\EventHandling::onQuiqqerTranslatorPublish"/> <!-- package events --> <event on="onPackageSetup" fire="\QUI\ERP\Products\EventHandling::onPackageSetup"/> diff --git a/src/QUI/ERP/Products/EventHandling.php b/src/QUI/ERP/Products/EventHandling.php index 6959bbac..ad5458e9 100644 --- a/src/QUI/ERP/Products/EventHandling.php +++ b/src/QUI/ERP/Products/EventHandling.php @@ -1179,4 +1179,64 @@ public static function onQuiqqerOrderSuccessful(QUI\ERP\Order\AbstractOrder $Ord } } } + + /** + * Update category title & description locale + * + * @throws QUI\Database\Exception + */ + public static function onQuiqqerTranslatorPublish() + { + $categoryTable = QUI\ERP\Products\Utils\Tables::getCategoryTableName(); + $translationTable = QUI\Translator::table(); + + $catIds = QUI::getDataBase()->fetch([ + 'select' => 'id', + 'from' => $categoryTable + ]); + + foreach ($catIds as $catId) { + try { + $title = ''; + $desc = ''; + + // title + $titleResult = QUI::getDataBase()->fetch([ + 'from' => $translationTable, + 'where' => [ + 'groups' => 'quiqqer/products', + 'var' => 'products.category.'.$catId['id'].'.title' + ], + 'limit' => 1 + ]); + + if (isset($titleResult[0])) { + $title = \json_encode($titleResult[0]); + } + + // desc + $descResult = QUI::getDataBase()->fetch([ + 'from' => $translationTable, + 'where' => [ + 'groups' => 'quiqqer/products', + 'var' => 'products.category.'.$catId['id'].'.description' + ], + 'limit' => 1 + ]); + + if (isset($descResult[0])) { + $desc = \json_encode($descResult[0]); + } + + QUI::getDataBase()->update($categoryTable, [ + 'title_cache' => $title, + 'description_cache' => $desc + ], [ + 'id' => $catId['id'] + ]); + } catch (\Exception $Exception) { + QUI\System\Log::addError($Exception->getMessage()); + } + } + } } diff --git a/src/QUI/ERP/Products/Handler/Categories.php b/src/QUI/ERP/Products/Handler/Categories.php index 0b071b44..62167035 100644 --- a/src/QUI/ERP/Products/Handler/Categories.php +++ b/src/QUI/ERP/Products/Handler/Categories.php @@ -45,7 +45,7 @@ public static function clearCache($categoryId = false) /** * Returns the cache name of a category * - * @param integer $categoryId + * @param integer|string $categoryId * @return string */ public static function getCacheName($categoryId) @@ -106,7 +106,7 @@ public static function getChildAttributes() } /** - * @param integer $id + * @param integer|string $id * @return QUI\ERP\Products\Interfaces\CategoryInterface * * @throws QUI\Exception @@ -183,7 +183,7 @@ public static function getMainCategory() /** * Checks if a category exists * - * @param integer $categoryId - category id + * @param integer|string $categoryId - category id * @return bool * @throws QUI\Exception */ @@ -228,7 +228,7 @@ public static function isCategory($Category) /** * Create a new category * - * @param integer $parentId - optional, ID of the parent + * @param integer|null|string $parentId - optional, ID of the parent * @param string $title - optional, translation text for current language * * @return QUI\ERP\Products\Interfaces\CategoryInterface @@ -244,6 +244,8 @@ public static function createCategory($parentId = null, $title = '') $parentId = 0; } + $parentId = (int)$parentId; + $result = QUI::getDataBase()->fetch([ 'from' => QUI\ERP\Products\Utils\Tables::getCategoryTableName(), 'limit' => 1 @@ -383,7 +385,7 @@ public static function getCategoryIds($queryParams = []) } /** - * @param integer $id + * @param integer|string $id * @throws QUI\Exception */ public static function deleteCategory($id) -- GitLab