Page 1 of 1

Procedure with parameters

Posted: Tue Feb 04, 2025 8:06 am
by yvesf
Hi,

I would like to create a procedure with parameters. It seems that inside procedure you can create functions with parameters.
What is the syntax ? How do you call a function in a procedure from the JavaScript ?
Many thx,

Yves

Re: Procedure with parameters

Posted: Tue Feb 04, 2025 9:27 am
by kev1n
Are you referring to a nuBuilder PHP procedure?

Re: Procedure with parameters

Posted: Tue Feb 04, 2025 11:30 am
by yvesf
yes nuBuilder PHP procedure

Re: Procedure with parameters

Posted: Tue Feb 04, 2025 2:22 pm
by kev1n
I'm not sure if I fully understand what you mean. Could you describe a specific use case?

To pass parameters to a nuBuilder Procedure, you can use nuRunPHPHiddenWithParams.

Re: Procedure with parameters

Posted: Wed Feb 05, 2025 1:50 am
by yvesf
Could you please show me an example of procedure with parameters ? I don't see how to put parameters when creating a nuBuilder PHP procedure

Re: Procedure with parameters

Posted: Wed Feb 05, 2025 2:10 am
by kev1n
A nuBuilder Procedure does not support direct parameters. However, you can still pass "parameters" using JavaScript, such as with nuRunPHPHiddenWithParams(). Search the forum for examples.

Re: Procedure with parameters

Posted: Wed Feb 05, 2025 9:40 pm
by yvesf
To clarify my request I have 2 procedures with only one difference : normally you would create a function with one parameter.
Here are the 2 procedures .
Procedure 1

Code: Select all

$activityid1='#primary_Key#';

$query="select act_order from activite where activite_id='".$activityid1."';"; // on recupère ordre l'activité qu'on veut descendre d'un cran.
$order_ori=nuRunquery($query);
$r  = db_fetch_object($order_ori);
$order=$r->act_order;
$order_new=$order+1; //nouvel ordre de l'activité récupérée
$order_new_str=strval($order_new);;
$update = "Update activite set act_order=".$order." where act_order =".$order_new.";";// l'activité ayant l'ordre en dessous remonte vers le haut
$result = nuRunQuery($update);
$update2 = "Update activite set act_order=".$order_new." where activite_id='".$activityid1."';"; //affectation du nouvel ordre à l'activité sélectionnée
$result2 = nuRunQuery($update2);
$js = " nuGetBreadcrumb();";
nuJavaScriptCallback($js);
And procedure 2

Code: Select all

$activityid1='#primary_Key#';

$query="select act_order from activite where activite_id='".$activityid1."';"; // on recupère ordre l'activité qu'on veut descendre d'un cran.
$order_ori=nuRunquery($query);
$r  = db_fetch_object($order_ori);
$order=$r->act_order;
$order_new=$order-1; //nouvel ordre de l'activité récupérée
$order_new_str=strval($order_new);;
$update = "Update activite set act_order=".$order." where act_order =".$order_new.";";// l'activité ayant l'ordre en dessous remonte vers le haut
$result = nuRunQuery($update);
$update2 = "Update activite set act_order=".$order_new." where activite_id='".$activityid1."';"; //affectation du nouvel ordre à l'activité sélectionnée
$result2 = nuRunQuery($update2);
$js = " nuGetBreadcrumb();";
nuJavaScriptCallback($js);
The only difference between those 2 procedures is the line $order_new which is equal to $order+1 in procedure 1 and $order-1 in procedure 2. It is the only difference.
How do you manage this situation ?

Thx,

Yves

Re: Procedure with parameters

Posted: Thu Feb 06, 2025 6:52 am
by kev1n
You can set a hash cookie using nuSetProperty() or nuRunPHPHiddenWithParams() with JavaScript and retrieve it with nuGetProperty() in PHP.
Then pass the Hash Cookie values to the php procedure updateActivityOrder()

In JavaScript, set the sort order Hash Cookie, e.g.

Code: Select all

nuSetProperty('sort_oder','up'); // or 'down'
nuRunPHPHidden(...) // call your procedure
Updated, consolidated PHP Procedure (untested):

Code: Select all

// Retrieve Hash Cookies

$activityId = nuGetProperty('primary_Key'); // Primary Key of the selected browse row
$sortOrder = nuGetProperty('sort_oder');	// Sort direction (up, down)

// Validate input

if (!$activityId || !in_array($sortOrder, ['up', 'down'])) {
    $js = " nuMessage('Invalid procedure parameters'); ";
} else {
    updateActivityOrder($activityId, $sortOrder);
    // Refresh the UI
    $js = "nuGetBreadcrumb();";
}

nuJavaScriptCallback($js);
	
function updateActivityOrder($activityId, $direction) {
    // Determine the order adjustment
    $orderChange = ($direction === 'up') ? -1 : 1;

    // Get the current order of the activity
    $query = "SELECT act_order FROM activite WHERE activite_id = ?";
    $order_ori = nuRunQuery($query, [$activityId]);
    $r = db_fetch_object($order_ori);

    if (db_num_rows($r) !== 1) {
        return; // If no record found, exit function
    }

    $order = $r->act_order;
    $order_new = $order + $orderChange;

    // Ensure the new order is valid
    if ($order_new < 1) {
        return; // Prevents setting a negative or zero order
    }

    // Swap the order values with safer queries
    $update = "UPDATE activite SET act_order = ? WHERE act_order = ?";
    nuRunQuery($update, [$order, $order_new]);

    $update2 = "UPDATE activite SET act_order = ? WHERE activite_id = ?";
    nuRunQuery($update2, [$order_new, $activityId]);
}