Created
March 26, 2014 10:28
-
-
Save codearachnid/9780436 to your computer and use it in GitHub Desktop.
Allow a certain amount of duplicates in a Gravity From by field before kicking back a failure check.
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
<?php | |
add_filter( 'gform_is_duplicate', 'custom_gform_is_duplicate', 10, 4 ); | |
function custom_gform_is_duplicate( $count, $form_id, $field, $value ){ | |
global $wpdb; | |
$lead_detail_table_name = GFFormsModel::get_lead_details_table_name(); | |
$lead_table_name = GFFormsModel::get_lead_table_name(); | |
$sql_comparison = "ld.value=%s"; | |
switch(RGFormsModel::get_input_type($field)){ | |
case "time" : | |
$value = sprintf("%02d:%02d %s", $value[0], $value[1], $value[2]); | |
break; | |
case "date" : | |
$value = GFFormsModel::prepare_date(rgar($field, "dateFormat"), $value); | |
break; | |
case "number" : | |
$value = GFCommon::clean_number($value, rgar($field, 'numberFormat')); | |
break; | |
case "phone" : | |
$value = str_replace(array(")", "(", "-", " "), array("", "", "", ""), $value); | |
$sql_comparison = 'replace(replace(replace(replace(ld.value, ")", ""), "(", ""), "-", ""), " ", "") = %s'; | |
break; | |
} | |
$inner_sql_template = " SELECT %s as input, ld.lead_id | |
FROM $lead_detail_table_name ld | |
INNER JOIN $lead_table_name l ON l.id = ld.lead_id | |
WHERE l.form_id=%d AND ld.form_id=%d | |
AND ld.field_number between %s AND %s | |
AND status='active' AND {$sql_comparison}"; | |
$sql = "SELECT count(distinct input) as match_count FROM ( "; | |
$input_count = 1; | |
if(is_array($field["inputs"])){ | |
$input_count = sizeof($field["inputs"]); | |
foreach($field["inputs"] as $input){ | |
$union = empty($inner_sql) ? "" : " UNION ALL "; | |
$inner_sql .= $union . $wpdb->prepare($inner_sql_template, $input["id"], $form_id, $form_id, $input["id"] - 0.001, $input["id"] + 0.001, $value[$input["id"]]); | |
} | |
} | |
else{ | |
$inner_sql = $wpdb->prepare($inner_sql_template, $field["id"], $form_id, $form_id, doubleval($field["id"]) - 0.001, doubleval($field["id"]) + 0.001, $value); | |
} | |
$sql .= $inner_sql . " | |
) as count | |
GROUP BY lead_id | |
ORDER BY match_count DESC"; | |
$count = apply_filters("custom_limit_gform_is_duplicate_{$form_id}", apply_filters('custom_limit_gform_is_duplicate', count( $wpdb->get_col($sql) ), $form_id, $field, $value), $form_id, $field, $value); | |
$input_count++; // CUSTOMIZE YOUR LIMIT (in this case 2 instead of 1) | |
return $count != null && $count >= $input_count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment