Skip to content
Code-Schnipsel Gruppen Projekte
  • Henning Leutz's avatar
    fix(phpstan): improved method validity checks and reduced error handling · 6100708f
    verfasst von Henning Leutz
    This commit fixes several issues in different PHP files.
    - Added `method_exists` checks for `isActive` method in `activate.php`, `deactivate.php` and
    `toggle.php` to prevent calls to an undefined method.
    - Similar check added for `getTitle` method in `get.php`.
    - Fixed sorting functionality in `search.php` by using `strcmp` instead of greater than comparison.
    - Removed a large number of ignored errors in `phpstan-baseline.neon`, indicating reduced error
    handling.
    - Corrected the `implode` parameters order in `Discount.php` and typecasted variable to string
    before using `explode` in `PriceFactor.php`.
    - Updated `getTitle` & `toPriceFactor` method parameters in `Discount.php` to use union types for
    compatibility with different data types.
    
    Related: #18
    6100708f
search.php 1,78 KiB
<?php

/**
 * This file contains package_quiqqer_discount_ajax_search
 */

/**
 * Search for discounts
 *
 * @param string $params - JSON query params
 *
 * @return array
 */
QUI::$Ajax->registerFunction(
    'package_quiqqer_discount_ajax_search',
    function ($fields, $params) {
        $Discounts = new QUI\ERP\Discount\Handler();
        $result = [];
        $Locale = QUI::getLocale();

        $allowedFields = $Discounts->getChildAttributes();

        $query = [];
        $params = json_decode($params, true);
        $fields = json_decode($fields, true);

        if (!is_array($fields)) {
            $fields = [];
        }

        if (isset($params['order'])) {
            $query['order'] = $params['order'];
        }

        if (isset($params['limit'])) {
            $query['limit'] = $params['limit'];
        }

        $allowedFields = array_flip($allowedFields);

        foreach ($fields as $field => $value) {
            if (!isset($allowedFields[$field]) && $field != 'id') {
                continue;
            }

            $query['where_or'][$field] = [
                'type' => '%LIKE%',
                'value' => $value
            ];
        }

        // search
        $data = $Discounts->getChildrenData($query);

        foreach ($data as $entry) {
            $entry['title'] = [
                'quiqqer/discount',
                'discount.' . $entry['id'] . '.title'
            ];

            $entry['text'] = $Locale->get(
                'quiqqer/discount',
                'discount.' . $entry['id'] . '.title'
            );

            $result[] = $entry;
        }

        usort($result, function ($a, $b) {
            return strcmp($a['text'], $b['text']);
        });

        return $result;
    },
    ['fields', 'params'],
    'Permission::checkAdminUser'
);