Skip to content

Instantly share code, notes, and snippets.

@muskie9
Created March 9, 2016 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muskie9/07a8f118b03a5b07d080 to your computer and use it in GitHub Desktop.
Save muskie9/07a8f118b03a5b07d080 to your computer and use it in GitHub Desktop.
<?php
/**
* @property string Title
*
* @method DataList|Member[] Youth
*/
class School extends DataObject
{
/**
* @var string
*/
private static $singular_name = 'School';
/**
* @var string
*/
private static $plural_name = 'Schools';
/**
* @var string
*/
private static $description = 'A school related to youth members';
/**
* @var array
*/
private static $db = array(
'Title' => 'Varchar(255)',
'Active' => 'Boolean',
);
/**
* @var array
*/
private static $has_many = array(
'Youth' => 'Member',
'Teams' => 'Team',
);
/**
* @var array
*/
private static $summary_fields = array(
'Title' => 'Title',
'Youth.count' => 'Youth',
'Active.Nice' => 'Active School'
);
/**
* @var array
*/
private static $searchable_fields = array(
'Title',
'Active',
);
/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
$this->extend('updateCMSFields', $fields);
return $fields;
}
/**
* @return ValidationResult
*/
public function validate()
{
$result = parent::validate();
return $result;
}
}
/* ====================================
Controller Method
==================================== */
/**
* @param SS_HTTPRequest $request
* @return HTMLText
*/
public function schools(SS_HTTPRequest $request)
{
$pageSize = 20;
$start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
$schools = School::get();
debug::show($schools->count());//736
//filter based on https://docs.silverstripe.org/en/3.1/developer_guides/model/searchfilters/
if($request->getVar('Title')){
$schools = $schools->filterAny(array('Title:PartialMatchFilter' => $request->getVar('Title')));
}
//the above filter results in the following error
//[User Error] Uncaught InvalidArgumentException: ExactMatchFilter does not accept PartialMatchFilter as modifiers
//show Active by default and when filtered to
if (!array_key_exists('Active', $this->request->getVars()) || $this->request->getVar('Active') == 1) {
$schools = $records->filter('Active', true);
} else {
$schools = $records->filter('Active', false);
}
$records = PaginatedList::create($schools, $this->request);
$records->setPageStart($start);
$records->setPageLength($pageSize);
return $this
->customise(array('Results' => $schools))
->renderWith(array('SchoolList', 'Page'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment