simple example of DataObjects
managed with ModelAdmin
filterable in the frontend
Last active
August 29, 2015 14:02
-
-
Save Zauberfisch/5ae254201dc8528fda53 to your computer and use it in GitHub Desktop.
SilverStripe SkyScrapers Example
This file contains hidden or 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 Page extends SiteTree { | |
} | |
/** | |
* @property Page dataRecord | |
* @method Page data | |
*/ | |
class Page_Controller extends ContentController { | |
private static $allowed_actions = array( | |
'SearchForm', | |
); | |
public function init() { | |
parent::init(); | |
// ... | |
} | |
/** | |
* @return Form | |
*/ | |
public function SearchForm() { | |
// TODO you should use _t() for translations here | |
$fields = FieldList::create(array( | |
DropdownField::create('City', 'Pick a City', array( | |
'Vienna' => 'Vienna', | |
'London' => 'London', | |
'Wellington' => 'Wellington', | |
))->setEmptyString('Any City'), | |
$heightField = DropdownField::create('Height', 'Height', array( | |
'0-250' => '0-250 ft.', | |
'250-500' => '250-500 ft.', | |
'500-2000' => '500-2000 ft.', | |
))->setEmptyString('Any height'), | |
)); | |
$actions = FieldList::create(array( | |
FormAction::create('doSearch', 'Search'), | |
)); | |
$form = Form::create( | |
$this, // controller | |
__FUNCTION__, // Name of the form | |
$fields, | |
$actions | |
); | |
// use get and disable security tolen to have a canonical url. | |
$form->setFormMethod('GET'); | |
$form->disableSecurityToken(); | |
return $form; | |
} | |
/** | |
* NOTE: this method is called by Form created in SearchForm, this method is _NOT_ accessible by URL, only through the form | |
* it usually is best practice to do a redirect form this method to a action, | |
* but in this case it makes sense to display the content directly | |
* | |
* @param array $data | |
* @param Form $form | |
* @param SS_HTTPRequest $request | |
*/ | |
public function doSearch($data, Form $form, SS_HTTPRequest $request) { | |
// create a datalist (because the datalist is lazy loading, it will not run the sql query until we actually need the data) | |
$buildings = SkyScrapers::get(); | |
if (isset($data['City']) && $data['City']) { | |
// protect against sql injection | |
$sqlSaveCity = Convert::raw2sql($data['City']); | |
$buildings = $buildings->filter('City', $sqlSaveCity); | |
} | |
if (isset($data['Height']) && $data['Height']) { | |
$bounds = explode('-', $data['Height']); | |
if (count($bounds) == 2) { | |
// cast to int protects us against sql injections | |
$min = (int)$bounds[0]; | |
$max = (int)$bounds[1]; | |
$buildings = $buildings->filter('Height:GreaterThanOrEqual', $min); | |
$buildings = $buildings->filter('Height:LessThanOrEqual', $max); | |
} | |
} | |
$buildings = $buildings->sort('Year', 'DESC'); | |
return $this->customise(array( | |
// pass the results to the template, in template it can be used in a loop: <% loop $SearchResults % >$ID - $Title - $Year ($City, $Height)<br><% end_loop % > | |
'SearchResults' => $buildings, | |
// display the form that was just submitted instead of letting SilverStripe render a new one, | |
// this has the benefit that the selected values are selected again after the page reload | |
'SearchForm' => $form, | |
)); | |
} | |
} |
This file contains hidden or 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
<!doctype html> | |
<html lang="$ContentLocale"> | |
<head> | |
<title>$Title</title> | |
<% base_tag %> | |
$MetaTags('false') | |
</head> | |
<body> | |
<h1>$Title</h1> | |
$Content | |
<br><br> | |
$SearchForm | |
<% if $SearchResults %> | |
<%-- TODO you should use <%t ... %> for translations here --%> | |
<h3>Your search results:</h3> | |
<ul> | |
<% loop $SearchResults %> | |
<li> | |
<h4>$Title</h4> | |
<p>{$Height}feet high, built $Year in $City</p> | |
</li> | |
<% end_loop %> | |
</ul> | |
<% else %> | |
<h3>sorry, nothing found</h3> | |
<% end_if %> | |
</body> | |
</html> |
This file contains hidden or 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 | |
/** | |
* @property string Title | |
* @property int Year | |
* @property int Height | |
* @property string City | |
*/ | |
class SkyScrapers extends DataObject { | |
private static $db = array( | |
'Title' => 'Varchar(255)', | |
'Year' => 'Int', | |
'Height' => 'Int', | |
'City' => 'Varchar(255)', | |
); | |
private static $summary_fields = array( | |
'Title', | |
'Year', | |
'Height', | |
'City', | |
); | |
/** | |
* @return FieldList | |
*/ | |
public function getCMSFields() { | |
$fields = FieldList::create(array( | |
TextField::create('Title', $this->fieldLabel('Title')), | |
NumericField::create('Year', $this->fieldLabel('Year')), | |
NumericField::create('Height', $this->fieldLabel('Height')), | |
TextField::create('City', $this->fieldLabel('City')), | |
)); | |
return $fields; | |
} | |
/** | |
* @param bool $includerelations | |
* @return array | |
*/ | |
public function fieldLabels($includerelations = true) { | |
$labels = parent::fieldLabels($includerelations); | |
// TODO: you should use _t() for translations here | |
// $labels['Title'] = _t('SkyScrapers.Title', 'The Name of the Building'); | |
$labels['Title'] = 'The Name of the Building'; | |
$labels['Year'] = 'Year'; | |
$labels['Height'] = 'Height'; | |
$labels['City'] = 'City'; | |
return $labels; | |
} | |
} |
This file contains hidden or 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 | |
/** | |
* Admin area to manage the DataObjects | |
*/ | |
class SkyScrapersAdmin extends ModelAdmin { | |
private static $managed_models = 'SkyScrapers'; | |
private static $url_segment = 'skyscrapers'; | |
private static $menu_title = 'Sky Scrapers'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment