Skip to content

Instantly share code, notes, and snippets.

@demeritcowboy
Created May 15, 2019 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demeritcowboy/7f80cc44ddcfa39520e4baefdc0c41ab to your computer and use it in GitHub Desktop.
Save demeritcowboy/7f80cc44ddcfa39520e4baefdc0c41ab to your computer and use it in GitHub Desktop.
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