From 66b8cbe8b0ea46f19bf2b6f801f3899a41c9a1cc Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 23 Oct 2024 13:32:06 +0200
Subject: [PATCH 1/2] fix: quiqqer/watcher change requirement to suggestion

Related: quiqqer/products#400
---
 composer.json | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/composer.json b/composer.json
index bccc4959..defee153 100644
--- a/composer.json
+++ b/composer.json
@@ -29,7 +29,6 @@
         "quiqqer/slider": "^2",
         "quiqqer/tags": "^2",
         "quiqqer/utils": "^2",
-        "quiqqer/watcher": "^2",
         "quiqqer/translator": "^2",
         "quiqqer-asset/mustache": "*",
         "quiqqer-asset/urijs": "*",
@@ -40,7 +39,8 @@
         "phpunit/phpunit-mock-objects": "~3.1"
     },
     "suggest": {
-        "quiqqer/tax": "VAT management"
+        "quiqqer/tax": "VAT management",
+        "quiqqer/watcher": "QUIQQER Watcher module (kind of logger)"
     },
     "autoload": {
         "psr-4": {
-- 
GitLab


From 7ed292bf0c6886e817cc3bec57c85ee0fc06a765 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 23 Oct 2024 13:54:19 +0200
Subject: [PATCH 2/2] fix: missing watcher class check

Related: quiqqer/products#400
---
 .phive/phars.xml                            |  2 +-
 src/QUI/ERP/Products/Category/Category.php  | 31 ++++++----
 src/QUI/ERP/Products/Crons.php              |  5 +-
 src/QUI/ERP/Products/Field/Field.php        | 32 +++++-----
 src/QUI/ERP/Products/Handler/Categories.php | 13 ++--
 src/QUI/ERP/Products/Handler/Fields.php     | 16 ++---
 src/QUI/ERP/Products/Handler/Products.php   | 24 ++++----
 src/QUI/ERP/Products/Product/Model.php      | 67 ++++++++++++---------
 8 files changed, 109 insertions(+), 81 deletions(-)

diff --git a/.phive/phars.xml b/.phive/phars.xml
index 5bfa092b..3dbb1a3e 100644
--- a/.phive/phars.xml
+++ b/.phive/phars.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <phive xmlns="https://phar.io/phive">
-  <phar name="phpstan" version="1.11.8" installed="1.11.8" location="./tools/phpstan" copy="false"/>
+  <phar name="phpstan" version="^1.10.67" installed="1.10.67" location="./tools/phpstan" copy="false"/>
   <phar name="phpunit" version="^10.5.20" installed="10.5.20" location="./tools/phpunit" copy="false"/>
   <phar name="phpcs" version="^3.10.1" installed="3.10.1" location="./tools/phpcs" copy="false"/>
   <phar name="phpcbf" version="^3.10.1" installed="3.10.1" location="./tools/phpcbf" copy="false"/>
diff --git a/src/QUI/ERP/Products/Category/Category.php b/src/QUI/ERP/Products/Category/Category.php
index 0ad737aa..d81440f9 100644
--- a/src/QUI/ERP/Products/Category/Category.php
+++ b/src/QUI/ERP/Products/Category/Category.php
@@ -21,6 +21,7 @@
 use function array_merge;
 use function array_reverse;
 use function array_shift;
+use function class_exists;
 use function defined;
 use function ini_get;
 use function is_array;
@@ -943,13 +944,15 @@ public function save(QUI\Interfaces\Users\User $User = null): void
             $fields[] = $attributes;
         }
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.category.save', [
-                'id' => $this->getId()
-            ]),
-            'Category->save',
-            $fields
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.category.save', [
+                    'id' => $this->getId()
+                ]),
+                'Category->save',
+                $fields
+            );
+        }
 
         QUI::getDataBase()->update(
             QUI\ERP\Products\Utils\Tables::getCategoryTableName(),
@@ -982,12 +985,14 @@ public function delete($User = false): void
 
         QUI\Permissions\Permission::checkPermission('category.delete', $User);
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.category.delete', [
-                'id' => $this->getId(),
-                'title' => $this->getTitle()
-            ])
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.category.delete', [
+                    'id' => $this->getId(),
+                    'title' => $this->getTitle()
+                ])
+            );
+        }
 
         // get children ids
         $ids = [];
diff --git a/src/QUI/ERP/Products/Crons.php b/src/QUI/ERP/Products/Crons.php
index db5213d6..4ecfd1ff 100644
--- a/src/QUI/ERP/Products/Crons.php
+++ b/src/QUI/ERP/Products/Crons.php
@@ -11,6 +11,7 @@
 use QUI\Exception;
 use QUI\System\Log;
 
+use function class_exists;
 use function count;
 use function ini_get;
 use function set_time_limit;
@@ -34,7 +35,9 @@ class Crons
     public static function updateProductCache(): void
     {
         // global watcher disable
-        QUI\Watcher::$globalWatcherDisable = true;
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::$globalWatcherDisable = true;
+        }
 
         // clear search cache
         QUI\ERP\Products\Search\Cache::clear();
diff --git a/src/QUI/ERP/Products/Field/Field.php b/src/QUI/ERP/Products/Field/Field.php
index 586fc602..ed5e5bba 100644
--- a/src/QUI/ERP/Products/Field/Field.php
+++ b/src/QUI/ERP/Products/Field/Field.php
@@ -15,6 +15,7 @@
 use QUI\Locale;
 
 use function array_filter;
+use function class_exists;
 use function floor;
 use function get_class;
 use function is_array;
@@ -358,14 +359,15 @@ public function save(): void
             $data['options'] = json_encode($options);
         }
 
-
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.field.save', [
-                'id' => $this->getId()
-            ]),
-            '',
-            $data
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.field.save', [
+                    'id' => $this->getId()
+                ]),
+                '',
+                $data
+            );
+        }
 
         QUI::getDataBase()->update(
             QUI\ERP\Products\Utils\Tables::getFieldTableName(),
@@ -407,12 +409,14 @@ public function delete(): void
 
         QUI::getEvents()->fireEvent('onQuiqqerProductsFieldDeleteBefore', [$this]);
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.field.delete', [
-                'id' => $this->getId(),
-                'title' => $this->getTitle()
-            ])
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.field.delete', [
+                    'id' => $this->getId(),
+                    'title' => $this->getTitle()
+                ])
+            );
+        }
 
         $fieldId = $this->getId();
 
diff --git a/src/QUI/ERP/Products/Handler/Categories.php b/src/QUI/ERP/Products/Handler/Categories.php
index c6efc976..31eacf49 100644
--- a/src/QUI/ERP/Products/Handler/Categories.php
+++ b/src/QUI/ERP/Products/Handler/Categories.php
@@ -8,6 +8,7 @@
 
 use QUI;
 
+use function class_exists;
 use function get_class;
 use function is_null;
 use function is_object;
@@ -255,11 +256,13 @@ public static function createCategory($parentId = null, string $title = '')
             'limit' => 1
         ]);
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.category.create', [
-                'title' => $title
-            ])
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.category.create', [
+                    'title' => $title
+                ])
+            );
+        }
 
         if (empty($result)) {
             QUI::getDataBase()->insert(
diff --git a/src/QUI/ERP/Products/Handler/Fields.php b/src/QUI/ERP/Products/Handler/Fields.php
index 896761a8..50def7ce 100644
--- a/src/QUI/ERP/Products/Handler/Fields.php
+++ b/src/QUI/ERP/Products/Handler/Fields.php
@@ -360,13 +360,15 @@ public static function createField(array $attributes = []): QUI\ERP\Products\Fie
 
         $newId = $data['id'] ?? QUI::getDataBase()->getPDO()->lastInsertId();
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.fields.create', [
-                'id' => $newId
-            ]),
-            '',
-            $data
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.fields.create', [
+                    'id' => $newId
+                ]),
+                '',
+                $data
+            );
+        }
 
         // add language var, if not exists
         self::setFieldTranslations($newId, $attributes);
diff --git a/src/QUI/ERP/Products/Handler/Products.php b/src/QUI/ERP/Products/Handler/Products.php
index 72d4769a..d0bf8c10 100644
--- a/src/QUI/ERP/Products/Handler/Products.php
+++ b/src/QUI/ERP/Products/Handler/Products.php
@@ -572,16 +572,18 @@ public static function createProduct(
 
         $newId = QUI::getDataBase()->getPDO()->lastInsertId();
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.create', [
-                'id' => $newId
-            ]),
-            '',
-            [
-                'fieldData' => $fieldData,
-                'categories' => ',' . implode(',', $categoryIds) . ','
-            ]
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.create', [
+                    'id' => $newId
+                ]),
+                '',
+                [
+                    'fieldData' => $fieldData,
+                    'categories' => ',' . implode(',', $categoryIds) . ','
+                ]
+            );
+        }
 
         $Product = self::getNewProductInstance($newId);
 
@@ -829,7 +831,7 @@ public static function countProducts(array $queryParams = []): int
      */
     public static function cleanup(): void
     {
-        if (class_exists('\\QUI\\Watcher')) {
+        if (class_exists('\QUI\Watcher')) {
             QUI\Watcher::$globalWatcherDisable = true;
         }
 
diff --git a/src/QUI/ERP/Products/Product/Model.php b/src/QUI/ERP/Products/Product/Model.php
index 53d52578..47704467 100644
--- a/src/QUI/ERP/Products/Product/Model.php
+++ b/src/QUI/ERP/Products/Product/Model.php
@@ -33,6 +33,7 @@
 use function array_unique;
 use function array_values;
 use function ceil;
+use function class_exists;
 use function constant;
 use function count;
 use function current;
@@ -1437,19 +1438,21 @@ protected function productSave(array $fieldData, User $EditUser = null): void
 
         // update
         if (Products::$writeProductDataToDb) {
-            QUI\Watcher::addString(
-                QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.save', [
-                    'id' => $this->getId()
-                ]),
-                '',
-                [
-                    'categories' => ',' . implode(',', $categoryIds) . ',',
-                    'category' => $mainCategory,
-                    'fieldData' => json_encode($fieldData),
-                    'permissions' => json_encode($this->permissions),
-                    'priority' => $this->getPriority()
-                ]
-            );
+            if (class_exists('\QUI\Watcher')) {
+                QUI\Watcher::addString(
+                    QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.save', [
+                        'id' => $this->getId()
+                    ]),
+                    '',
+                    [
+                        'categories' => ',' . implode(',', $categoryIds) . ',',
+                        'category' => $mainCategory,
+                        'fieldData' => json_encode($fieldData),
+                        'permissions' => json_encode($this->permissions),
+                        'priority' => $this->getPriority()
+                    ]
+                );
+            }
 
             QUI::getDataBase()->update(
                 QUI\ERP\Products\Utils\Tables::getProductTableName(),
@@ -1969,12 +1972,14 @@ public function delete(): void
     {
         QUI\Permissions\Permission::checkPermission('product.delete');
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.delete', [
-                'id' => $this->getId(),
-                'title' => $this->getTitle(),
-            ])
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.delete', [
+                    'id' => $this->getId(),
+                    'title' => $this->getTitle(),
+                ])
+            );
+        }
 
         QUI::getEvents()->fireEvent('onQuiqqerProductsProductDeleteBegin', [$this]);
 
@@ -2400,11 +2405,13 @@ public function deactivate(?QUI\Interfaces\Users\User $EditUser = null): void
 
         $this->active = false;
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.deactivate', [
-                'id' => $this->getId()
-            ])
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.deactivate', [
+                    'id' => $this->getId()
+                ])
+            );
+        }
 
         QUI::getDataBase()->update(
             QUI\ERP\Products\Utils\Tables::getProductTableName(),
@@ -2452,11 +2459,13 @@ public function activate(?QUI\Interfaces\Users\User $EditUser = null): void
             ]);
         }
 
-        QUI\Watcher::addString(
-            QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.activate', [
-                'id' => $this->getId()
-            ])
-        );
+        if (class_exists('\QUI\Watcher')) {
+            QUI\Watcher::addString(
+                QUI::getLocale()->get('quiqqer/products', 'watcher.message.product.activate', [
+                    'id' => $this->getId()
+                ])
+            );
+        }
 
         // duplicate article no. check
         $articleNo = $this->getFieldValue(Fields::FIELD_PRODUCT_NO);
-- 
GitLab