2023-01-12 08:59:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Function to display the front end checkboxes for the ajut filter
|
|
|
|
*
|
|
|
|
* references:
|
|
|
|
* basic start
|
|
|
|
* https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
|
|
|
|
*
|
2023-01-12 09:05:22 +01:00
|
|
|
* wordpress full form example
|
|
|
|
* https://wordpress.stackexchange.com/questions/383599/form-checkbox-value-going-to-dynamic-url
|
|
|
|
*
|
2023-01-12 08:59:33 +01:00
|
|
|
* checkbox adaptation query
|
|
|
|
* https://wordpress.stackexchange.com/a/102915
|
|
|
|
*
|
|
|
|
* form GET and POST
|
|
|
|
* https://www.formget.com/php-checkbox/
|
|
|
|
*
|
|
|
|
* @package Oficina_de_Suport_theme
|
|
|
|
*/
|
2023-01-12 09:05:22 +01:00
|
|
|
?>
|
2023-01-15 15:23:07 +01:00
|
|
|
<?php
|
|
|
|
if( ! function_exists( 'ofisuport_ajuts_filters_form') ) {
|
2023-01-12 08:59:33 +01:00
|
|
|
function ofisuport_ajuts_filters_form() {
|
2023-01-12 09:05:22 +01:00
|
|
|
?>
|
|
|
|
<div id="archive-filters" class="ofisuport-filters">
|
|
|
|
<?php
|
2023-01-12 08:59:33 +01:00
|
|
|
// output all possible values of a checkbox
|
|
|
|
$groupkey = "group_63ab636898703"; // write here the key for the group of fields from acf
|
|
|
|
if( $groupkey ) {
|
2023-01-12 09:05:22 +01:00
|
|
|
?>
|
|
|
|
<?php // start the form ?>
|
|
|
|
<form id="form-ajuts" name="search-ajuts">
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php
|
2023-01-12 08:59:33 +01:00
|
|
|
$groupkey_fields = acf_get_fields($groupkey);
|
|
|
|
foreach( $groupkey_fields as $field_key ) {
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 08:59:33 +01:00
|
|
|
if( $field_key['type'] == 'checkbox' ){
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 08:59:33 +01:00
|
|
|
// https://wordpress.stackexchange.com/a/102915
|
|
|
|
$fields = get_field_object($field_key['key']);
|
|
|
|
if( $fields ) {
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 08:59:33 +01:00
|
|
|
// check for values from meta fields db in url
|
|
|
|
foreach( $GLOBALS['my_query_filters'] as $key => $fieldname ) {
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
// check for values in url and get value if available
|
2023-01-12 08:59:33 +01:00
|
|
|
if( isset($_GET[ $fieldname ]) ) {
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 08:59:33 +01:00
|
|
|
$filteredvalues['value'] = explode(',', $_GET[ $fieldname ]);
|
2023-01-15 15:23:07 +01:00
|
|
|
};
|
|
|
|
//end check for values in url
|
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
// construct the checkboxes
|
|
|
|
if( $fieldname == $fields['name']) {
|
2023-01-15 15:23:07 +01:00
|
|
|
?>
|
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<section id="fund-filter-<?php echo $fields['name']; ?>" class="<?php echo $fields['name']; ?>">
|
|
|
|
<h5><?php echo $fields['label'];?></h5>
|
|
|
|
<?php foreach( $fields['choices'] as $choicevalue => $choicelabel ) { ?>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-13 18:30:23 +01:00
|
|
|
<div class="filter" data-filter="<?php echo $fields['name'];?>">
|
2023-01-26 16:37:42 +01:00
|
|
|
<input class="ofisuport-filter" type="checkbox" name="<?php echo $fields['name'];?>" <?php //if( in_array($choicevalue,$filteredvalues['value']) ) { echo 'checked'; }; ?> value="<?php echo $choicevalue ?>" />
|
2023-01-13 18:30:23 +01:00
|
|
|
<label for="<?php echo $choicevalue; ?>"><?php echo $choicelabel;?></label>
|
|
|
|
</div>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php };//end foreach fields['choices'] ?>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
</section>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php }; //end if fieldname == fields['name'] ?>
|
|
|
|
<?php // end of construct checkboxes ?>
|
|
|
|
|
|
|
|
<?php }; // end check for values from meta fields db in url ?>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php }; //end if field_key['key'] ?>
|
2023-01-12 09:03:16 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php }; //end if type checkbox ?>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php }; //end foreach groupkey field ?>
|
2023-01-13 20:23:42 +01:00
|
|
|
<?php //echo '<p><span id="filterlink"></span></p>';?>
|
|
|
|
<p><a id="submitfilteredlink" name="submit-ajut" class="button button-more" href="">enviar</a> </p>
|
2023-01-12 09:05:22 +01:00
|
|
|
</form>
|
2023-01-15 15:06:20 +01:00
|
|
|
<?php //close the form and subit ?>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<?php }; //end if groupkey ?>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
</div>
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
<script type="text/javascript">
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
(function($) {
|
|
|
|
// change
|
2023-01-13 20:23:42 +01:00
|
|
|
$('#archive-filters').on('click', 'input', function(){
|
2023-01-15 15:23:07 +01:00
|
|
|
|
|
|
|
// vars
|
2023-01-12 09:05:22 +01:00
|
|
|
var url = '<?php echo home_url('ajuts/'); ?>';
|
|
|
|
args = {};
|
2023-01-13 18:30:23 +01:00
|
|
|
var appendtoURL = '';
|
|
|
|
var eachfiltered = '';
|
|
|
|
var valueschecked = '';
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
// loop over filters
|
|
|
|
$('#archive-filters .filter').each(function(){
|
2023-01-13 18:30:23 +01:00
|
|
|
// check if is first appearance of each
|
|
|
|
if( eachfiltered != $(this).data('filter') ){
|
|
|
|
// save already calculated field=val01,val02,val03
|
2023-01-13 19:55:40 +01:00
|
|
|
if( eachfiltered != '' && valueschecked != '' ){
|
|
|
|
// remove last ','
|
|
|
|
valueschecked = valueschecked.slice(0, -1);
|
|
|
|
appendtoURL += eachfiltered +'=' +valueschecked +'&';
|
|
|
|
};
|
2023-01-13 18:30:23 +01:00
|
|
|
// start new fieldname filtered
|
|
|
|
eachfiltered = $(this).data('filter');
|
2023-01-15 15:23:07 +01:00
|
|
|
// reset values
|
2023-01-13 18:30:23 +01:00
|
|
|
valueschecked = '';
|
|
|
|
// find checked inputs
|
|
|
|
$(this).find('input:checked').each(function(){
|
|
|
|
// if not empty save value and ,
|
|
|
|
if( $(this).val() != '' ){
|
2023-01-13 19:55:40 +01:00
|
|
|
valueschecked += $(this).val() + ',';
|
|
|
|
};
|
2023-01-13 18:30:23 +01:00
|
|
|
});
|
2023-01-13 19:55:40 +01:00
|
|
|
//alert( appendtoURL +":" +eachfiltered +"=" +valuescheked);
|
2023-01-13 18:30:23 +01:00
|
|
|
} else {
|
|
|
|
// we have previous values checked
|
|
|
|
// find checked inputs
|
2023-01-13 19:55:40 +01:00
|
|
|
//if( $(this).val() != '' ){
|
2023-01-13 18:30:23 +01:00
|
|
|
// if not empty append value and ,
|
|
|
|
$(this).find('input:checked').each(function(){
|
|
|
|
valueschecked += $(this).val() + ',';
|
|
|
|
});
|
2023-01-13 19:55:40 +01:00
|
|
|
//alert( appendtoURL +":not:" +eachfiltered +"=" +valuescheked);
|
|
|
|
//};
|
2023-01-13 18:30:23 +01:00
|
|
|
};
|
2023-01-12 09:05:22 +01:00
|
|
|
});
|
|
|
|
|
2023-01-13 20:23:42 +01:00
|
|
|
// remove last ','
|
|
|
|
valueschecked = valueschecked.slice(0, -1);
|
2023-01-13 18:30:23 +01:00
|
|
|
// save last cycled filtered value already calculated field=val01,val02,val03
|
|
|
|
appendtoURL += eachfiltered +'=' +valueschecked +'&';
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-13 18:30:23 +01:00
|
|
|
// remove last &
|
2023-01-13 19:55:40 +01:00
|
|
|
appendtoURL = appendtoURL.slice(0, -1);
|
2023-01-13 18:30:23 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
// update url
|
2023-01-13 19:55:40 +01:00
|
|
|
url += '?' +appendtoURL;
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-13 20:23:42 +01:00
|
|
|
// show or modify the url
|
|
|
|
//alert( url );
|
|
|
|
//$('#filterlink').text(url);
|
2023-01-12 09:05:22 +01:00
|
|
|
|
2023-01-13 20:23:42 +01:00
|
|
|
// convert a href url to new value
|
|
|
|
$('#submitfilteredlink').attr('href',url);
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
});
|
2023-01-15 15:23:07 +01:00
|
|
|
|
2023-01-12 09:05:22 +01:00
|
|
|
})(jQuery);
|
|
|
|
</script>
|
|
|
|
|
2023-01-15 15:23:07 +01:00
|
|
|
<?php
|
|
|
|
}; //end ofisuport_ajuts_filters_form
|
|
|
|
}; // end if ! functions_exists
|
2023-01-12 08:59:33 +01:00
|
|
|
?>
|