Issue with confirmation plugin and onBeforeProcess

Status
Not open for further replies.

keianrad

Member
I used this code in a php plugin of the assessment form to create two records in two separate tables (Client and address book) and catch id of the client and save it in the assessment table.
Code is working but because the process script of php plugin set as "onBeforeProcess" and form has a confirmation plugin to validate data, it creates two duplicate records in each table, because this code runs two times.

I tried to change the process script of php plugin to other options but it didn't work.

I think I need to condition to run the code only once time after validation data but I don't know how!

Code:
// create a record in the Client table
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->insert('app_clients')
->set('date_time = ' . $myDb->quote('{app_assessment___date_time}'))
->set('type = ' . $myDb->quote('lead'))
->set('created_by = ' . $myDb->quote('{app_assessment___created_by}'))
->set('office_id = ' . $myDb->quote('{app_assessment___preferred_office}'))
->set('status = ' . $myDb->quote('open'))
->set('name = ' . $myDb->quote('{app_assessment___name}'));
$myDb->setQuery($myQuery);
$myDb->execute();

// Catch client id from client table
$formModel->updateFormData('app_assessment___client_id', $myId);

// create a record in the address table
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->insert('app_address_book')
->set('date_time = ' . $myDb->quote('{app_assessment___date_time}'))
->set('client_id = ' . $myDb->quote($myId))
->set('email = ' . $myDb->quote('{app_assessment___email}'))
->set('office_id = ' . $myDb->quote('{app_assessment___preferred_office}'))
->set('mobile = ' . $myDb->quote('{app_assessment___mobile}'))
->set('country = ' . $myDb->quote('{app_assessment___country_of_residence}'))
->set('name = ' . $myDb->quote('{app_assessment___name}'));
$myDb->setQuery($myQuery);
$myDb->execute();
 
There's an input variable, fabrik_confirmation, which is set to 1 on the first submit (prior to the confirmation page showing), and 2 on the "real" submit after confirmation.

So you'd probably want to do ...

Code:
if ($this->app->input->get('fabrik_confirmation', '') === '1') {
   return true;
}

Although really, if you are adding rows to other tables, you should do your processing onAfterProcess. The onBeforeProcess hook fires BEFORE you form data is written to the database, which could fail. Plus of course it won't get fired in the pre-confirmation submit.

-- hugh
 
Because I need to catch id of the record that create in other table (client id) and save it in the assessment record then I have to do the procces onBeforeProcess.

I used the code but it doesn't work.


Code:
if ($this->app->input->get('fabrik_confirmation', '') === '2') {
   return true;

// Update Client List
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->insert('app_clients')
->set('date_time = ' . $myDb->quote('{app_assessment___date_time}'))
->set('type = ' . $myDb->quote('lead'))
->set('created_by = ' . $myDb->quote('{app_assessment___created_by}'))
->set('office_id = ' . $myDb->quote('{app_assessment___preferred_office}'))
->set('status = ' . $myDb->quote('open'))
->set('name = ' . $myDb->quote('{app_assessment___name}'));
$myDb->setQuery($myQuery);
$myDb->execute();
$myId = $myDb->insertid();


// Update form data with insert id
$formModel->updateFormData('app_assessment___client_id', $myId);

// insert client in app_address list
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->insert('app_address_book')
->set('date_time = ' . $myDb->quote('{app_assessment___date_time}'))
->set('client_id = ' . $myDb->quote($myId))
->set('email = ' . $myDb->quote('{app_assessment___email}'))
->set('office_id = ' . $myDb->quote('{app_assessment___preferred_office}'))
->set('mobile = ' . $myDb->quote('{app_assessment___mobile}'))
->set('country = ' . $myDb->quote('{app_assessment___country_of_residence}'))
->set('name = ' . $myDb->quote('{app_assessment___name}'));
$myDb->setQuery($myQuery);
$myDb->execute();
$myId = $myDb->insertid();

}
 
Reread my post. :)

Put the code I gave you at the top. Don't insert your code in that block.

You should still do this after process, and manually update your main row with the insert id.

Sent from my HTC6545LVW using Tapatalk
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top