barcodebuddy icon indicating copy to clipboard operation
barcodebuddy copied to clipboard

Userfield as variable

Open Will711990 opened this issue 2 years ago • 2 comments

Hi !

I forked your amazing repository, because I wish use a value of a Userfield in the "incl/processing.inc.php"

But it was unsuccessful...

How could I create a variable to use this userfield: Capture d’écran 2022-01-31 à 12 32 46

And use it in "incl/processing.inc.php". I tried to modified "incl/api.inc.php", and "db.inc.php", but I don't modify as expected, because it doesn't work...

Could you help me ?

Thank you !!!

Will711990 avatar Jan 31 '22 11:01 Will711990

Hi, you will probably also need to edit the object that is created in the incl/api.php file: https://github.com/Forceu/barcodebuddy/blob/master/incl/api.inc.php

Forceu avatar Jan 31 '22 12:01 Forceu

Hi, I tried to work on it, but unsuccessful...

My goal is to get this value:

Capture d’écran 2022-02-01 à 12 11 07

In file api.inc.php, I added:

const API_USERFIELDS       = 'userfields/products';
class GrocyProduct {

    public $quantiteAConsommer;

    public static function parseUserfieldsInfo(array $infoArray): GrocyProduct {
        checkIfNumeric($infoArray["id"]);

        $result                        = new GrocyProduct();
        $result->quantiteAConsommer    = $infoArray["QuantiteAConsommer"];
        return $result;
    }
class API {

    public static function getUserfields(int $productId): ?GrocyProduct {
        $url = API_USERFIELDS . "/" . $productId;

        $result = null;  // Assure assignment in event curl throws exception.
        $curl   = new CurlGenerator($url);

        try {
            $result = $curl->execute(true);
        } catch (Exception $e) {
            self::processError($e, "Could not lookup Grocy product info");
        }

        if ($result != null) {
            if (isset($result["userfields"])) {
                return GrocyProduct::parseUserfieldsInfo($result);
            } else {
                return null;
            }
        }
        return null;
    }
}

In file processing.inc.php, I added:

$quantiteAConsommer = API::getUserfields($productInfo->id);

And I try to display $quantiteAConsommer in the log with:

$log = new LogOutput("Consuming " . $quantiteAConsommer, EVENT_TYPE_ADD_KNOWN_BARCODE);

But it stay empty...

Will711990 avatar Feb 01 '22 11:02 Will711990