Hi@all,
i seem to have run myself in some kind of dead-end.
I am trying to realize a local beverage-booking interface for some friends. Goal is to run it on a local server, and everyone with an account and access to the wifi can "buy" stuff. this part works just fine via
$form = $modules->get("InputfieldForm");
and
$user->wallet = $user->wallet - $cost;
$user->setAndSave('wallet',$user->wallet);
The Main interface will be a tablet, some kind of "Mainuser" (!=Admin), that has Access to "buy" all Stuff as any User.
I added 2 extensions to the user template: a field to store the account balance and a profields:table to log all the interactions.
I failed at 2 approches so far and will be thankfull for either a thrid approch or the correction of one of mine.
1st try:
generate the same form for each user
foreach ($users->find("roles=standart") as $guy){
code for form
}
then try to get the data to each individual user with
if($form->getErrors()) {
$out .= $form->render();
} else {
$booking = $guy->user_log->makeBlankItem(); //create new row
$page->of(false); // turn off cache creation
$booking->date = date('D, d M Y H:i:s'); //create date and time of transaction
$booking->guthaben_vor = $guthaben_vor; //log wallet before transaction
$booking->guthaben_nach = $guthaben_nach; //log wallet after transaction
$booking->preis = $preis/100; //log cost
$booking->getrank = $getrank; //log name
$guy->user_log->add($buchung); //apply log
$guy->save('user_log'); //save log
$guy->of(true); //reenable cache
$out .= "<p>gebucht.</p>"; //success message
$out .= "Guthaben: " . $guy->guthaben . "€"; //display actual wallet
$out .= $form->render(); //redisplay the form.
}
When i dont try to append the data to the users logfile, this does somehow work, but it changes the value of the wallet (account balance) for every user by the desired amount. I dont really get why.
2nd attempt (stupid):
just paste a bunch of hyperlinks, that call a specific template, that force-logins the specific user, waits for a booking/purcharse then loggs it off again and loggs in the "mainuser" (=Tablet).
Problem: I need some kind of timeout to make sure, that the User is logged off after a set time (yet to be defined), and the "Mainuser" aka Tablet user is force-logged in afterwards. I could only find clues on how to define gernal login timeouts for all accounts or specific accounts by name (not dynamic so useless for me). Thats why i define this as a stupid workaround since all users would be logged off after some set time.
Here is the pathetic Code i could write so far:
<?php
$out = '';
// create a new form
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'buchen');
//create new radio field (getränke)
$field = $modules->get("InputfieldRadios"); //fieldtype
$field->label = "Auswahl"; //label above
$field->attr("id+name","auswahl"); //for callback
foreach ($pages->get('/getranke/')->children as $getrank) {
$field->addOption($getrank->preis*100 . "|!|" . $getrank->title, $getrank->title); //value, lable. value to decypher later on. preis in cents
}
$form->append($field);
// oh a submit button!
$submit = $modules->get("InputfieldSubmit");
$submit->attr("value","Buchen");
$submit->attr("id+name","submit");
$form->append($submit);
// form was submitted so we process the form
if($input->post->submit) {
// user submitted the form, process it and check for errors
$form->processInput($input->post);
// here is a good point for extra/custom validation and manipulate fields
if($form->getErrors()) {
// the form is processed and populated
// but contains errors
$out .= $form->render();
} else {
$gebucht = explode("|!|",$form->get("auswahl")->value); //get string from choice
$preis = intval($gebucht[0]); //get cost from choice
$getrank = $gebucht[1]; // get name from choice
$guthaben_vor = $user->guthaben; //get wallet before transaction
$user->guthaben = $user->guthaben - $preis/100; //calculate new wallet
$user->setAndSave('guthaben',$user->guthaben); //save current wallet
$guthaben_nach = $user->guthaben; //set walalet after transaction
//set logfile
$buchung = $user->user_log->makeBlankItem(); //create new row
$page->of(false); // turn off cache creation
$buchung->date = date('D, d M Y H:i:s'); //create date and time of transaction
$buchung->guthaben_vor = $guthaben_vor; //log wallet before transaction
$buchung->guthaben_nach = $guthaben_nach; //log wallet after transaction
$buchung->preis = $preis/100; //log cost
$buchung->getrank = $getrank; //log name
$buchung->menge = 1;//PLACEHOLDER FOR NOW
$buchung->kommentar = 'Normale Buchung'; //log comment. PLACEHOLDER for now
$user->user_log->add($buchung); //apply log
$user->save('user_log'); //save log
$page->of(true);//reenable cache
$out .= "<p>gebucht.</p>"; //success message
$out .= "Guthaben: " . $user->guthaben . "€"; //display actual wallet
$out .= $form->render(); //redisplay the form.
}
} else {
// render out form without processing
$out .= $form->render();
}
echo $out;
?>
Got Problems with the same code copy-pasted and replacing $user with the foreach as $something.
Best regards,
Fox