Skip to content

Instantly share code, notes, and snippets.

@stealerr
Created December 3, 2015 09:40
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 stealerr/476446efdc43e00c9ca8 to your computer and use it in GitHub Desktop.
Save stealerr/476446efdc43e00c9ca8 to your computer and use it in GitHub Desktop.
<?php
class CategoryPage extends Page{
private static $has_one = array(
'MainCategoryPage' => 'MainCategoryPage'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
//$fields->removeByName(array());
$mains = MainCategoryPage::get()->sort('Title DESC')->map('ID', 'Title');
$fields->insertBefore(DropdownField::create('MainCategoryPageID', 'Type of product', $mains), 'Title');
return $fields;
}
}
class CategoryPage_Controller extends Page_Controller {
}
<?php
class Page_Controller extends ContentController {
public function addTagFilters( $filter ){ //Example URL domain.td?healthy=1&cheap=1
$request = $this->getRequest();
$params = $request->getVars();
if(count($request->getVars()) > 1 ){
unset($params['url']); //Remove url from the request, so we only have the get variables
$filterable_params = array();
foreach($params as $key => $val){
array_push($filterable_params, $key);
}
$filter['Tags.Name'] = $filterable_params;
}
return $filter;
}
public function RelatedProducts(){
$zip = Session::get('ZipSort');
$filter = array('Quantity:GreaterThan' => 0);
if($zip){
$filter['Zip'] = $zip;
}
$prods = ProductPage::get();
if($this instanceof MainCategoryPage_Controller || $this->ClassName == 'CategoryPage'){
$filter = $this->addTagFilters($filter);
$prods = $prods->filter($filter)->sort('Created DESC');
}
return $prods;
}
}
<?php
class Tag extends DataObject{
private static $db = array(
'Name' => 'Varchar',
);
private static $has_one = array(
'MainCategoryPage' => 'MainCategoryPage'
);
private static $summary_fields = array(
'Name' => 'Name',
'MainCategoryPage.Title' => 'MainCategory'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
//$fields->removeByName(array());
$fields->addFieldsToTab('Root.Main', TextField::create('Name'));
$mains = MainCategoryPage::get()->sort('Title DESC')->map('ID', 'Title');
$fields->addFieldsToTab('Root.Main', DropdownField::create('MainCategoryPageID', 'Type of product', $mains));
return $fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment