Last active
February 5, 2017 20:54
-
-
Save bueckl/1b1f7beab82c6150f6dcfa919d048139 to your computer and use it in GitHub Desktop.
ModelAdmin Custom Search #SearchContext #Search on has_many Relation #SS3 #Silverstripe
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 | |
class YachtAdmin extends ModelAdmin { | |
private static $managed_models = array( | |
'Yacht' | |
); | |
public function getSearchContext(){ | |
$customClass = 'Yacht'; | |
if($this->modelClass == $customClass){ | |
$context = new YachtSearchContext($customClass); | |
foreach($context->getFields() as $field){ | |
if(isset($_REQUEST['q']) && isset($_REQUEST['q'][$field->getName()])){ | |
$field->setValue($_REQUEST['q'][$field->getName()]); | |
} | |
$field->setName(sprintf('q[%s]', $field->getName())); | |
} | |
foreach($context->getFilters() as $filter){ | |
$filter->setFullName(sprintf('q[%s]', $filter->getFullName())); | |
} | |
return $context; | |
} | |
return parent::getSearchContext(); | |
} | |
... | |
class YachtSearchContext extends SearchContext { | |
public function __construct($modelClass, $fields = null, $filters = null) { | |
$filters = array( | |
'EventYear' => new PartialMatchFilter('EventYear'), | |
'Name' => new PartialMatchFilter('Name'), | |
'Company' => new PartialMatchFilter('Company') | |
); | |
parent::__construct($modelClass, null, $filters); | |
} | |
function getSearchFields(){ | |
if($this->fields && $this->fields->count()){ | |
return $this->fields; | |
} | |
$fields = new FieldList( | |
TextField::create('Name'), | |
TextField::create('Company__CompanyName', 'Company'), | |
CheckboxSetField::create('EventYear')->setSource(array( | |
'2016' => '2016', | |
'2017' => '2017', | |
'2018' => '2018' | |
)) | |
); | |
$this->extend('updateCustomerSearchFields', $fields); | |
$this->fields = $fields; | |
return $this->fields; | |
} | |
function getFields(){ | |
return $this->getSearchFields(); | |
} | |
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null) { | |
$dataList = parent::getQuery($searchParams, $sort, $limit, $existingQuery); | |
$params = is_object($searchParams) ? $searchParams->getVars() : $searchParams; | |
$query = $dataList->dataQuery(); | |
if(!is_object($searchParams)){ | |
if(isset($params['Name']) && !empty($params['Name'])){ | |
$query->where('"Name" LIKE \'%' . Convert::raw2sql($params['Name']) . '%\''); | |
} | |
if(isset($params['Company__CompanyName'])){ | |
// Get ID | |
$CompanyID = Company::get()->filter('CompanyName:PartialMatch', $params['Company__CompanyName'])->First(); | |
if ($CompanyID->ID) { | |
$query->where('"CompanyID" = '.$CompanyID->ID); | |
} | |
} | |
$this->extend('updateGetQuery', $query, $params); | |
} | |
return $dataList->setDataQuery($query); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment