Skip to content

Commit

Permalink
Merge pull request #6 from PlasticStudio/feature/sorting
Browse files Browse the repository at this point in the history
Add default sorting
  • Loading branch information
monkeyfeet authored Sep 9, 2020
2 parents 26ae479 + 73507a5 commit 2ceb998
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The built-in SilverStripe search form is a very simple search engine. This plugi
* `sorts`: associative list of sort options (if `sorts` are not defined, results will be sorted by default to `Title ASC`)
* `Label`: front-end field label
* `Sort`: SQL sort string
* `defaults`: Default attributes or settings, as opposed to those submitted through the search form (currently only configured to use `sort`).


# Example configuration
Expand Down Expand Up @@ -109,4 +110,6 @@ PlasticStudio\Search\SearchPageController:
published_desc:
Label: 'Publish date (oldest first)'
Sort: 'DatePublished ASC'
defaults:
sort: 'Title ASC'
```
101 changes: 72 additions & 29 deletions src/SearchPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SearchPageController extends PageController {
private static $types;
private static $filters;
private static $sort;
private static $defaults;
private static $results;

public function index($request){
Expand Down Expand Up @@ -99,6 +100,19 @@ public static function get_sorts_available(){

return $array;
}

public static function get_defaults_available(){
$defaults = Config::inst()->get('PlasticStudio\Search\SearchPageController', 'defaults');
$array = [];

if ($defaults){
foreach ($defaults as $key => $value){
$array[$key] = $value;
}
}

return $array;
}

public static function get_types(){
return self::$types;
Expand Down Expand Up @@ -163,6 +177,10 @@ public static function get_sort(){
return self::$sort;
}

public static function set_sort($sort){
self::$sort = $sort;
}

public static function get_mapped_sort(){
$sorts_available = self::get_sorts_available();
$sort = self::get_sort();
Expand All @@ -175,8 +193,24 @@ public static function get_mapped_sort(){
}
}

public static function set_sort($sort){
self::$sort = $sort;
public static function get_defaults(){
return self::$defaults;
}

public static function set_defaults($defaults){
self::$defaults = $defaults;
}

public static function get_mapped_defaults(){
$defaults_available = self::get_defaults_available();
$defaults = self::get_defaults();

// If no sort, assume the first item
if (!$defaults){
return reset($defaults_available);
} else {
return $defaults_available[$defaults];
}
}

public static function get_results(){
Expand Down Expand Up @@ -401,18 +435,20 @@ public function PerformSearch(){
// join the relationship table to our record(s)
$joins.= "LEFT JOIN \"".$filter['Table']."\" ON \"".$filter['Table']."\".\"ID\" = \"".$table_with_column."\".\"".$filter['Column']."\"";

if (is_array($filter['Value'])){
$ids = '';
foreach ($filter['Value'] as $id){
if ($ids != ''){
$ids.= ',';
if(!empty($filter['Value'])){
if (is_array($filter['Value'])){
$ids = '';
foreach ($filter['Value'] as $id){
if ($ids != ''){
$ids.= ',';
}
$ids.= "'".$id."'";
}
$ids.= "'".$id."'";
} else {
$ids = $filter['Value'];
}
} else {
$ids = $filter['Value'];
$where.= ' AND ('."\"".$table_with_column."\".\"".$filter['Column']."\" IN (". $ids .")".')';
}
$where.= ' AND ('."\"".$table_with_column."\".\"".$filter['Column']."\" IN (". $ids .")".')';

break;

Expand All @@ -427,23 +463,25 @@ public function PerformSearch(){
$filter_join = $filter['JoinTables'][$type['Key']];

$joins.= "LEFT JOIN \"".$filter_join['Table']."\" ON \"".$type['Table']."\".\"ID\" = \"".$filter_join['Table']."\".\"".$filter_join['Column']."\"";

if (is_array($filter['Value'])){
$ids = '';
foreach ($filter['Value'] as $id){
if ($ids != ''){
$ids.= ',';

if(!empty($filter['Value'])){
if (is_array($filter['Value'])){
$ids = '';
foreach ($filter['Value'] as $id){
if ($ids != ''){
$ids.= ',';
}
$ids.= "'".$id."'";
}
$ids.= "'".$id."'";
} else {
$ids = $filter['Value'];
}
} else {
$ids = $filter['Value'];
}

if ($relations_sql !== ''){
$relations_sql.= " AND ";
if ($relations_sql !== ''){
$relations_sql.= " AND ";
}
$relations_sql.= "\"".$filter_join['Table']."\".\"".$filter['Table']."ID\" IN (". $ids .")";
}
$relations_sql.= "\"".$filter_join['Table']."\".\"".$filter['Table']."ID\" IN (". $ids .")";
}

break;
Expand Down Expand Up @@ -483,17 +521,22 @@ public function PerformSearch(){
$allResults->merge($resultObjects);
}
}

// Apply sorting

$sort = false;
// Sorting applied throug form submission
if(isset(self::get_mapped_sort()['Sort'])){
$sort = self::get_mapped_sort()['Sort'];
$sort = str_replace("'", "\'", $sort);
$sort = str_replace('"', '\"', $sort);
$sort = str_replace('`', '\`', $sort);
}else{
$sort = 'Title ASC';
}
$allResults = $allResults->Sort($sort);
// Default sort defined in config
elseif(isset(self::get_mapped_defaults()['sort'])){
$sort = self::get_mapped_defaults()['sort'];
}
if($sort){
$allResults = $allResults->Sort($sort);
}

// Remove duplicates
$allResults->removeDuplicates('ID');
Expand Down

0 comments on commit 2ceb998

Please sign in to comment.