Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active December 31, 2019 19:25
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 xnau/13fe70b44dfdaef0096255c4cef7a885 to your computer and use it in GitHub Desktop.
Save xnau/13fe70b44dfdaef0096255c4cef7a885 to your computer and use it in GitHub Desktop.
Provides a way to use multiple fields to match a record in the database when importing or adding new records in Participants Database
<?php
/**
* Plugin Name: PDB Multiple Field Match Check
* Description: checks a new Participants Database submission against the database for a matching record using multiple fields
* Version: 2.1
*
* requires PDB version 1.9.4.2 or later
*/
add_action( 'pdb-incoming_record_match_object', 'xnau_check_for_match' );
// set up our custom feedback message
add_filter( 'pdb-validation_error_messages', 'xnau_check_for_match_feedback' );
/**
* checks the incoming submission for a match in the database
*
* @global wpdb $wpdb
* @param \PDb_submission\incoming_record_match $record_match the record match object
*/
function xnau_check_for_match( $record_match )
{
// edit this array to set up your match fields
$check_fields = array( 'first_name', 'last_name' );
$match_status = false; // assume no match
$post = $record_match->post;
$where = array();
$values = array();
$exception = '';
/*
* don't include the record id to prevent attempting to add a new record with
* the same id as an existing record
*
*/
if ( isset( $post['id'] ) ) {
unset( $post['id'] );
}
// put together the list of match fields and values
foreach ($check_fields as $field) {
if ( isset( $post[$field] ) ) {
$where[] = '`' . $field . '` LIKE "%s"';
$values[] = trim( $post[$field] );
}
}
if ( count( $where ) === 0 ) {
//nothing to check
return false;
}
// build the query to find our match
$sql = 'SELECT id FROM ' . Participants_Db::$participants_table . ' WHERE ' . $exception . '(' . implode( ' AND ', $where ) . ')';
global $wpdb;
$result = intval( $wpdb->get_var( $wpdb->prepare( $sql, $values ) ) );
// log the query and results if debugging
if (PDB_DEBUG) {
ob_start();
var_dump($result);
Participants_Db::debug_log(__FUNCTION__.' match mode: '.$record_match->match_mode().'
query: '. $wpdb->last_query . '
result: '. ob_get_clean() );
}
/*
* the $result will be the id of the matched record or 0 if no match
*/
if ( $result > 0 ) {
// provide the ID of the matched record to the object
$record_match->set_record_id( $result );
// we found a match set status to true
$match_status = true;
}
// set the match status in the match object
$record_match->set_match_status( $match_status );
}
/**
* adds a feedback message for signup submissions
*
* this message will be shown if the signup is rejected because it matches an
* existing record
*
* @param array $messages
* @return array
*/
function xnau_check_for_match_feedback( $messages )
{
// clear the error CSS so the wrong field does not get highlighted
add_filter( 'pdb-error_css', function ($css) {
return '';
} );
// set the feedback message
$messages['duplicate'] = 'You must choose a different First Name or Last Name, the name you submitted matches an existing record.';
return $messages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment