Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Userfield as variable #181

Open
Will711990 opened this issue Jan 31, 2022 · 2 comments
Open

Userfield as variable #181

Will711990 opened this issue Jan 31, 2022 · 2 comments
Labels
question Further information is requested

Comments

@Will711990
Copy link

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 !!!

@Forceu
Copy link
Owner

Forceu commented Jan 31, 2022

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 Forceu added the question Further information is requested label Jan 31, 2022
@Will711990
Copy link
Author

Will711990 commented Feb 1, 2022

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants