Last active
August 30, 2016 12:55
-
-
Save shoesforindustry/5058198 to your computer and use it in GitHub Desktop.
Kirby CMS Individual Multi Filter based on FilterBy with form selection. The idea was to be able to add fields to pages and be able to have a set of individual filters to select pages which match *some or all* of the filters. Select your filters and then hit the 'Filter' button: A list of selected filters will be displayed and if there are match…
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 | |
#Pages have these fields, the can have comma separated values for group, category and option | |
# Title: Event1 | |
# ---- | |
# Group: Group1 | |
# ---- | |
# Category: Cat2 | |
# ---- | |
# Option: Option1 | |
# ---- | |
# Text: Some text | |
# ---- | |
# Get the tagclouds, we are using three: groups, categories and options | |
# You also of course need the tagcloud plugin, but just as it is no hacks or duplicates. | |
$items = $pages->find('events'); | |
$groups = tagcloud($items, array('field' => 'group', | |
'sort' => 'name', | |
'sortdir' => 'asc')); | |
$categories = tagcloud($items, array('field' => 'category', | |
'sort' => 'name', | |
'sortdir' => 'asc')); | |
$options = tagcloud($items, array('field' => 'option', | |
'sort' => 'name', | |
'sortdir' => 'asc')); | |
# Next get the individual unique names from the tagclouds for the form select filter options | |
?> | |
<form action="" method="post"> | |
<h3>Select Group</h3> | |
<label for="groups">Select Group:</label> | |
<select name="groups"> | |
<option value="">All</option> | |
<?php foreach($groups as $group): ?> | |
<option value="<?php echo $group->name()?>" | |
<?php if(($_POST) and (strval($group->name()) == $_POST['groups'])) echo ' selected ="selected" ';?>> | |
<?php echo $group->name()?></option> | |
<?php endforeach ?> | |
</select> | |
<hr/> | |
<label for="categories">Select Category:</label> | |
<select name="categories"> | |
<option value="">All</option> | |
<?php foreach($categories as $category): ?> | |
<option value="<?php echo $category->name()?>" | |
<?php if(($_POST) and (strval($category->name()) == $_POST['categories'])) echo ' selected ="selected" ';?>> | |
<?php echo $category->name()?> | |
</option> | |
<?php endforeach ?> | |
</select> | |
<hr/> | |
<label for="options">Select Option:</label> | |
<select name="options"> | |
<option value="">All</option> | |
<?php foreach($options as $option): ?> | |
<option value="<?php echo $option->name()?>" | |
<?php if(($_POST) and (strval($option->name()) == $_POST['options'])) echo ' selected ="selected" ';?>> | |
<?php echo $option->name()?></option> | |
<?php endforeach ?> | |
</select> | |
<br/> | |
<button type="submit" id="submit" name="submit" value="Submit" class="btn">Filter</button> | |
</form> | |
<hr/> | |
<h3>Selected filters:</h3> | |
<?php | |
# Check to see if anything has been selected, if so then show the filters selected | |
if ($_POST){ | |
echo '<ul><li>Group: '.$_POST['groups'].'</li>'; | |
echo '<li> Categories: '.$_POST['categories'].'</li>'; | |
echo '<li> Options: '.$_POST['options'].'</li></ul><hr/><br/>'; | |
# If something is selected we need to set the appropriate filter logic | |
$glogic = ($_POST['groups'] == "") ? '!=' : '*=' ; | |
$clogic = ($_POST['categories'] == "") ? '!=' : '*=' ; | |
$ologic = ($_POST['options'] == "") ? '!=' : '*=' ; | |
$filtered=''; | |
$filtered = $pages->find('events') | |
->children() | |
->filterBy('group', $glogic, $_POST['groups']) | |
->filterBy('category', $clogic, $_POST['categories']) | |
->filterBy('option', $ologic, $_POST['options']); | |
# And then display pages that match the filters if any - you can use anything in the array | |
if ($filtered <>''){ | |
echo '<h3>Pages that match filters:</h3>'; | |
echo '<ul>'; | |
foreach($filtered as $page) echo '<li> <a href="'.$page->url().'">'.$page->title().'</a></li>'; | |
echo '</ul>'; | |
} | |
else #No Pages Match Filter | |
{ | |
echo '<h3>No pages match filters</h3>'; | |
} | |
} | |
else #No Filters Selected | |
{ | |
echo '<p>No filter selected</p>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment