form php insert new record on save with condition

Status
Not open for further replies.

bea

Active Member
Hi,
I think I'm missing some steps with F3. The Wiki for PHP is confusing me..F3.0, F3.1, F3.2...
I've managed to get a new record, on save, but without element $machine and $status from previous record like in my F2 spript.

I need some scripts to write a new record into the same table, also into another one (later).
I would appreciate any help :)
Cheers,
Bianka

PHP:
<?php
$db = JFactory::getDbo();
$machine = (int)"{fab_cp1_line_tour___machine}";
$status = JRequest::getVar('fab_cp1_line_tour___status_raw');
if($status[0] == 2) {
$query = $db->getQuery(true);
$query->insert('fab_cp1_line_tour')
->set('machine = ' . $db->quote('{fab_cp1_line_tour___machine}'))
->set('status = ' . $db->quote('{fab_cp1_line_tour___status}'));
$db->setQuery($query);
$db->execute();
$id = $db->insertid();
}
?>


PHP:
<?php
// Fabrik 2.0!!!

defined('_JEXEC') or die();

//Generiert neues Dokument

$status = JRequest::getVar('fab_min_shiftreport___done_raw');
$report = JRequest::getVar('fab_min_shiftreport___report');
$report = $report[0];
if($report[0] != 2 && $status[0] == 2) {
$db =& JFactory::getDBO();
$date =& JFactory::getDate();
$myuser =& JFactory::getUser();
$done = 1;
$report_new = 0;
$info = $db->Quote($formModel->_formData['comment_raw']);
$db->setQuery("INSERT INTO fab_min_shiftreport(done,report,info1)
VALUES ($done,$report_new,$info)");
$db->query();
}
?>
 
Last edited:
What is your setup?
php plugin "Process script" when?
Is the code in a file (you can't use placeholders there) or in the plugin's Code field?

Are fab_cp1_line_tour___machine and fab_cp1_line_tour___status_raw elements in your form or URL params?

JRequest::getVar is deprecated, use
$myApp= JFactory::getApplication();
$var = $myApp->input->getString('tablename___element');


so if you are handling form elements in a script file
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

$mydb= JFactory::getDbo();

$machine=(int)$formModel->getElementData('fab_cp1_line_tour___machine');

$status= $formModel->getElementData('fab_cp1_line_tour___status',true);
$status=is_array($status) ? $status[0] : $status;

if($status == 2){
$query=$mydb->getQuery(true);
$query->insert('fab_cp1_line_tour')
->set('machine = '.$mydb->quote($machine))
->set('status = '.$mydb->quote($status));
$mydb->setQuery($query);
$mydb->execute();
$id=$mydb->insertid();
}
?>

For debugging you can include some
var_dump($xyz);exit;
or install the jDump extension and use
dump($xyz,'some text');
 
Hi troester,
many thanks. I have a form PHP spript (on before store).

I've tested your script, but the new 'machine' value is wrong ('machine' is a databasejoin, dump = (int) 1, should be 7). Status is a dropdown and I set a new value 'newstatus' and it works.
I don't understand
$myApp= JFactory::getApplication();
$var = $myApp->input->getString('tablename___element');

Sometime it's really hard to jump from F2 to F3.3 :(
Cheers, Bianka


PHP:
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

$mydb= JFactory::getDbo();

$machine=(int)$formModel->getElementData('fab_cp1_line_tour___machine');

$status= $formModel->getElementData('fab_cp1_line_tour___status',true);
$status=is_array($status) ? $status[0] : $status;

if($status == 2){
$newstatus=  1;
$query=$mydb->getQuery(true);
$query->insert('fab_cp1_line_tour')
->set('machine = '.$mydb->quote($machine))
->set('status = '.$mydb->quote($newstatus));
$mydb->setQuery($query);
$mydb->execute();
$id=$mydb->insertid();
}
?>
 
If machine is a dbjoin I assume you'll get an array (and (int)will cast whatever...)
So do no (int) but
$machine=is_array($machine) ? $machine[0]:$machine;

JRequest:: is a Joomla thing, not Fabrik.
 
Thank you! I think I've tested this, but maybe I did something wrong. Now it's working! (onBeforeProcess or onBeforeStore)

I've added some script to update element 'record', when 'status = 2'. It's working, but the wiki is absolutely confusing me about form PHP.
Enjoy your sunday!


PHP:
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

$mydb= JFactory::getDbo();

$machine=$formModel->getElementData('fab_cp1_line_tour___machine');
$machine=is_array($machine) ? $machine[0]:$machine;
$status= $formModel->getElementData('fab_cp1_line_tour___status',true);
$status=is_array($status) ? $status[0] : $status;
$record=$formModel->getElementData('fab_cp1_line_tour___record',true);
$record=is_array($record) ? $record[0] : $record;

if($record[0] != 2 && $status[0] == 2) {
$newstatus=  '1';
$newrecord = '1';
$query=$mydb->getQuery(true);
$query->insert('fab_cp1_line_tour')
->set('machine = '.$mydb->quote($machine))
->set('status = '.$mydb->quote($newstatus))
->set('record = '.$mydb->quote($newrecord));
$mydb->setQuery($query);
$mydb->execute();
$id=$mydb->insertid();
}

if($status[0] == '2')
{
  $formModel->updateFormData('fab_cp1_line_tour___record', '2', true);
}
else if($status[0] != '2')
{
  $formModel->updateFormData('fab_cp1_line_tour___record' , '1', true);
}
?>
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top