Real estate website with Kirby

There’s a lot you can do using Kirby’s built-in filter() and filterBy() methods. I’m building a site right now that is a repository of a bunch of books, and browsing will be primarily through searches:

Then there are two ways I handle search queries - if a book contains a certain attribute (if it’s included in the Modern Library, for instance), and then, a basic filter. This allows for a lot of compound filtering.

The request is sent as a JSON object, via ajax, to a search script. This may be much more than what you’re looking for, but, I thought I’d post this here for anyone else to use / reference / critique:

<?php
define('DS', DIRECTORY_SEPARATOR);
require($_SERVER["DOCUMENT_ROOT"] . DS . 'kirby' . DS . 'bootstrap.php');
$kirby = kirby();
$site = $kirby->site();

$params = $_POST;

$books = $site->pages()->find('writers')->children()->visible()->children();

function contains($str, array $arr) {
  foreach($arr as $a) {
    if (stripos($str,$a) !== false || $a = "none" && strlen($str) == 0) return true;
  }
  return false;
}

foreach ($params as $param => $param_array) {
  switch($param) {
    case "regions":
      $books = $books->filter(function($book) use ($param_array) {
        if (contains((string)$book->region(), $param_array)) {
          return $book;
        }
      });
      break;
    case "languages":
      $books = $books->filter(function($book) use ($param_array) {
        if (contains((string)$book->language(), $param_array)) {
          return $book;
        }
      });
      break;
    case "collections":
      $books = $books->filter(function($book) use ($param_array) {        
        if (contains((string)$book->collections(), $param_array)) {
          return $book;
        }
      });
      break;
    case "filter":
      foreach($param_array as $filter => $filter_value) {
        switch ($filter) {
          case "min_pages":
            $books = $books->filterBy('page_count', '>', $filter_value);
            break;
          case "max_pages":
            $books = $books->filterBy('page_count', '<', $filter_value);
            break;  
          case "min_decade":
            $books = $books->filterBy('year_published', '>=', $filter_value);
            break;
          case "max_decade":
            $max = ($filter_value == 'max') ? 200000 : $filter_value;
            $books = $books->filterBy('year_published', '<=', $max);
            break;
        }
      }
      break;
  }
}
5 Likes