Gravity Perks: GP Limit Choices // Share Limits Across Multiple Fields
<?php
/**
* Gravity Perks: GP Limit Choices // Share Limits Across Multiple Fields
*
* Adds support for specifying groups of fields which share the same limit. For example, if you had two fields with a
* limit of 10, selections from both fields would contribute to that limit.
*
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/gravity-perks/
* @copyright 2013 Gravity Wiz
*/
class GWLimitChoicesSharedLimits {
function __construct( $args ) {
extract( wp_parse_args( $args, array(
'form_id' => false,
'field_groups' => array()
) ) );
$this->form_id = $form_id;
$this->field_groups = $field_groups;
add_filter( "gwlc_choice_counts_query_{$form_id}", array( $this, 'modify_choice_counts_query' ), 10, 2 );
}
function modify_choice_counts_query( $query, $field ) {
$form = RGFormsModel::get_form_meta( $field['formId'] );
$fields = array();
foreach( $this->field_groups as $field_group ) {
if( !in_array( $field['id'], $field_group ) )
continue;
$where = explode( 'AND ', $query['where'] );
foreach( $where as &$where_clause ) {
if( strpos( $where_clause, 'CAST(ld.field_number as unsigned)' ) !== false )
$where_clause = 'CAST(ld.field_number as unsigned) IN ( ' . implode( ', ', $field_group ) . ' )';
}
$query['where'] = implode( 'AND ', $where );
}
return $query;
}
}
new GWLimitChoicesSharedLimits( array(
'form_id' => 16,
'field_groups' => array(
array( 12, 20 )
)
) );