Created
May 15, 2019 16:59
-
-
Save demeritcowboy/7f80cc44ddcfa39520e4baefdc0c41ab to your computer and use it in GitHub Desktop.
SubmitOnce server-side test/POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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