Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SubmitOnce server-side test/POC
/**
* Implements hook_civicrm_validateForm().
*
* @param string $formName
* @param array $fields
* @param array $files
* @param CRM_Core_Form $form
* @param array $errors
*/
function serverside_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if (!empty($fields['submit_once_unid'])) {
// I wouldn't do this here for real, just for POC.
CRM_Core_DAO::executeQuery("CREATE TABLE IF NOT EXISTS submitted_forms (unid varchar(64) PRIMARY KEY)");
$sqlParams = array( 1 => array( $fields['submit_once_unid'], 'String' ) );
$dao = CRM_Core_DAO::executeQuery("SELECT unid FROM submitted_forms WHERE unid = %1", $sqlParams);
if ($dao->fetch()) {
if (!empty($dao->unid)) {
$errors['submit_once_unid'] = ts('Duplicate submitted.');
}
}
}
}
/**
* Implements hook_civicrm_postProcess().
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function serverside_civicrm_postProcess($formName, &$form) {
if (!empty($form->_submitValues['submit_once_unid'])) {
$sqlParams = array( 1 => array( $form->_submitValues['submit_once_unid'], 'String' ) );
$dao = CRM_Core_DAO::executeQuery("INSERT INTO submitted_forms (unid) VALUES (%1)", $sqlParams);
}
}
/**
* Implements hook_civicrm_buildForm().
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function serverside_civicrm_buildForm($formName, &$form) {
// I wouldn't use uniqid for real. Something better, and ideally compatible with clustering.
$form->add('hidden', 'submit_once_unid', uniqid());
}
/**
* TODO: Some cron function to clean up old unids
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.