Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Parts_Controller extends Page_Controller
- {
- private static $allowed_actions = array(
- 'getCategoryPage',
- 'getPartsPage',
- 'getCarsPage'
- );
- public static $url_handlers = array(
- '' => 'index',
- '$CarURL/$ModelURL/$CategoryURL/$PartURL!' => 'getPartsPage',
- '$CarURL/$ModelURL/$CategoryURL!' => 'getCategoryPage',
- '$CarURL/$ModelURL!' => 'getCarsPage',
- '$CarURL!' => 'getCarsPage'
- );
- public function init()
- {
- parent::init();
- //return $request->param('PartURL');
- }
- public function index()
- {
- return $this->renderWith('Parts_Page', 'Page');
- }
- public function getCategoryPage($request)
- {
- $carRequested = $request->param('CarURL');
- $carModelRequested = $request->param('ModelURL');
- $categoryRequested = $request->param('CategoryURL');
- $Car = ModelFamily::get()->Filter(array("URLSegment" => $carRequested))->First();
- $CarModel = Model::get()->filter(array("URLSegment" => $carModelRequested))->First();
- $Category = Category::get()->filter(array("URLSegment" => $categoryRequested))->First();
- if ((!$Car) OR (!$CarModel) OR (!$Category)) {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- } else {
- $CarManufacture = Manufacture::get()->byID($Car->ManufactureID);
- $ProductsInCategory = new ArrayList();
- foreach ($CarModel->Products() as $Product) {
- if ($Product->CategoryID == $Category->ID) {
- $ProductsInCategory[] = $Product;
- }
- }
- foreach ($CarModel->ProductGroups() as $ProductGroup) {
- if ($ProductGroup->CategoryID == $Category->ID) {
- $ProductsInCategory[] = $ProductGroup;
- }
- }
- $CarModel->CategoryProducts = $ProductsInCategory;
- $CarModel->MetaTitle = "Parts - " . $CarManufacture->Name . " " . $Car->Name . " " . $CarModel->Name . " - " . $Category->Name;
- $CarModel->Categories = $this->getCategories($CarModel->ID);
- return $this->customise($CarModel)->renderWith(array('Category_Page', 'Page'));
- }
- }
- public function getCarsPage($request)
- {
- $CarRequested = $request->param('CarURL');
- $CarModelRequested = $request->param('ModelURL');
- if ($CarModelRequested) {
- if ($CarModel = Model::get()->filter(array('URLSegment' => $CarModelRequested))->First()) {
- if ($CarModelFamily = ModelFamily::get()->filter(array('URLSegment' => $CarRequested))->First()) {
- $Manufacture = Manufacture::get()->byID($CarModelFamily->ManufactureID);
- $CarModel->MetaTitle = "Parts - " . $Manufacture->Name . " " . $CarModelFamily->Name;
- $CarModel->ModelExists = true;
- $CarModel->Categories = $this->getCategories($CarModel->ID);
- return $this->customise($CarModel)->renderWith(array('Car_Model_Page', 'Page'));
- } else {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- }
- } else {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- }
- } else if ($CarRequested) {
- if ($CarModelFamilyValid = ModelFamily::get()->filter(array('URLSegment' => $CarRequested))->First()) {
- $Manufacture = Manufacture::get()->byID($CarModelFamilyValid->ManufactureID);
- $CarModelFamilyValid->MetaTitle = "Parts - " . $Manufacture->Name . " " . $CarModelFamilyValid->Name;
- $CarModelFamilyValid->Categories = NULL;
- return $this->customise($CarModelFamilyValid)->renderWith(array('Car_Page', 'Page'));
- } else {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- }
- } else {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- }
- }
- public function getPartsPage($request)
- {
- $CarModelRequested = $request->param('ModelURL');
- $CarRequested = $request->param('CarURL');
- $PartRequested = $request->param('PartURL');
- $CarModel = Model::get()->filter(array('URLSegment' => $CarModelRequested))->First();
- if (!$Car = ModelFamily::get()->filter(array('URLSegment' => $CarRequested))->First()) {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- } else {
- if ($Part = Product::get()->filter(array('URLSegment' => $PartRequested))->First()) {
- $Manufacture = Manufacture::get()->byID($Car->ManufactureID);
- $Part->CarFrom = $CarModel->ModelFamily()->URLSegment."/".$CarModel->URLSegment;
- $Part->MetaTitle = "Parts - " . $Manufacture->Name . " " . $Car->Name . " " . $Part->Name;
- $Part->Categories = $this->getCategories($CarModel->ID);
- return $this->customise($Part)->renderWith(array('Product_Page', 'Page'));
- } elseif ($Part = ProductGroup::get()->filter(array('URLSegment' => $PartRequested))->First()) {
- $Manufacture = Manufacture::get()->byID($Car->ManufactureID);
- $Part->CarFrom= $Manufacture->Name;
- $Part->MetaTitle = "Parts - " . $Manufacture->Name . " " . $Car->Name . " " . $Part->Name;
- $Part->Categories = $this->getCategories($CarModel->ID);
- return $this->customise($Part)->renderWith(array('Product_Page', 'Page'));
- } else {
- $this->httpError(404);
- return $this->redirect('page-not-found');
- }
- }
- }
- public function getCategories($carModel)
- {
- $CategoryList = new ArrayList();
- $CarModel = Model::get()->byID($carModel);
- foreach ($CarModel->Products() as $Product) {
- $Category = Category::get()->byID($Product->CategoryID);
- $CategoryList->add($Category);
- }
- $CategoryList->removeDuplicates();
- return $CategoryList;
- }
- public function FilterForm() {
- // Create fields
- $fields = new FieldList(
- new TextField('Name'),
- );
- // Create actions
- $actions = new FieldList(
- new FormAction('DoFilterForm', 'Submit')
- );
- return new Form($this, 'FilterForm', $fields, $actions);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement