Advertisement
Guest User

Untitled

a guest
Apr 29th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. <?php
  2.  
  3. class Parts_Controller extends Page_Controller
  4. {
  5.  
  6. private static $allowed_actions = array(
  7. 'getCategoryPage',
  8. 'getPartsPage',
  9. 'getCarsPage'
  10. );
  11.  
  12. public static $url_handlers = array(
  13. '' => 'index',
  14. '$CarURL/$ModelURL/$CategoryURL/$PartURL!' => 'getPartsPage',
  15. '$CarURL/$ModelURL/$CategoryURL!' => 'getCategoryPage',
  16. '$CarURL/$ModelURL!' => 'getCarsPage',
  17. '$CarURL!' => 'getCarsPage'
  18. );
  19.  
  20. public function init()
  21. {
  22. parent::init();
  23. //return $request->param('PartURL');
  24.  
  25. }
  26.  
  27. public function index()
  28. {
  29. return $this->renderWith('Parts_Page', 'Page');
  30. }
  31.  
  32. public function getCategoryPage($request)
  33. {
  34.  
  35. $carRequested = $request->param('CarURL');
  36. $carModelRequested = $request->param('ModelURL');
  37. $categoryRequested = $request->param('CategoryURL');
  38.  
  39. $Car = ModelFamily::get()->Filter(array("URLSegment" => $carRequested))->First();
  40. $CarModel = Model::get()->filter(array("URLSegment" => $carModelRequested))->First();
  41. $Category = Category::get()->filter(array("URLSegment" => $categoryRequested))->First();
  42.  
  43. if ((!$Car) OR (!$CarModel) OR (!$Category)) {
  44. $this->httpError(404);
  45. return $this->redirect('page-not-found');
  46. } else {
  47.  
  48. $CarManufacture = Manufacture::get()->byID($Car->ManufactureID);
  49.  
  50. $ProductsInCategory = new ArrayList();
  51.  
  52.  
  53. foreach ($CarModel->Products() as $Product) {
  54. if ($Product->CategoryID == $Category->ID) {
  55. $ProductsInCategory[] = $Product;
  56. }
  57.  
  58. }
  59.  
  60. foreach ($CarModel->ProductGroups() as $ProductGroup) {
  61. if ($ProductGroup->CategoryID == $Category->ID) {
  62.  
  63. $ProductsInCategory[] = $ProductGroup;
  64.  
  65. }
  66. }
  67.  
  68. $CarModel->CategoryProducts = $ProductsInCategory;
  69. $CarModel->MetaTitle = "Parts - " . $CarManufacture->Name . " " . $Car->Name . " " . $CarModel->Name . " - " . $Category->Name;
  70.  
  71. $CarModel->Categories = $this->getCategories($CarModel->ID);
  72.  
  73.  
  74. return $this->customise($CarModel)->renderWith(array('Category_Page', 'Page'));
  75.  
  76. }
  77. }
  78.  
  79. public function getCarsPage($request)
  80. {
  81. $CarRequested = $request->param('CarURL');
  82. $CarModelRequested = $request->param('ModelURL');
  83.  
  84. if ($CarModelRequested) {
  85.  
  86. if ($CarModel = Model::get()->filter(array('URLSegment' => $CarModelRequested))->First()) {
  87.  
  88. if ($CarModelFamily = ModelFamily::get()->filter(array('URLSegment' => $CarRequested))->First()) {
  89.  
  90. $Manufacture = Manufacture::get()->byID($CarModelFamily->ManufactureID);
  91.  
  92. $CarModel->MetaTitle = "Parts - " . $Manufacture->Name . " " . $CarModelFamily->Name;
  93.  
  94. $CarModel->ModelExists = true;
  95. $CarModel->Categories = $this->getCategories($CarModel->ID);
  96. return $this->customise($CarModel)->renderWith(array('Car_Model_Page', 'Page'));
  97. } else {
  98. $this->httpError(404);
  99. return $this->redirect('page-not-found');
  100. }
  101.  
  102. } else {
  103. $this->httpError(404);
  104. return $this->redirect('page-not-found');
  105. }
  106.  
  107. } else if ($CarRequested) {
  108. if ($CarModelFamilyValid = ModelFamily::get()->filter(array('URLSegment' => $CarRequested))->First()) {
  109.  
  110. $Manufacture = Manufacture::get()->byID($CarModelFamilyValid->ManufactureID);
  111.  
  112. $CarModelFamilyValid->MetaTitle = "Parts - " . $Manufacture->Name . " " . $CarModelFamilyValid->Name;
  113. $CarModelFamilyValid->Categories = NULL;
  114.  
  115. return $this->customise($CarModelFamilyValid)->renderWith(array('Car_Page', 'Page'));
  116. } else {
  117. $this->httpError(404);
  118. return $this->redirect('page-not-found');
  119. }
  120. } else {
  121. $this->httpError(404);
  122. return $this->redirect('page-not-found');
  123. }
  124.  
  125. }
  126.  
  127. public function getPartsPage($request)
  128. {
  129.  
  130. $CarModelRequested = $request->param('ModelURL');
  131. $CarRequested = $request->param('CarURL');
  132. $PartRequested = $request->param('PartURL');
  133.  
  134. $CarModel = Model::get()->filter(array('URLSegment' => $CarModelRequested))->First();
  135.  
  136. if (!$Car = ModelFamily::get()->filter(array('URLSegment' => $CarRequested))->First()) {
  137. $this->httpError(404);
  138. return $this->redirect('page-not-found');
  139. } else {
  140.  
  141. if ($Part = Product::get()->filter(array('URLSegment' => $PartRequested))->First()) {
  142. $Manufacture = Manufacture::get()->byID($Car->ManufactureID);
  143. $Part->CarFrom = $CarModel->ModelFamily()->URLSegment."/".$CarModel->URLSegment;
  144. $Part->MetaTitle = "Parts - " . $Manufacture->Name . " " . $Car->Name . " " . $Part->Name;
  145. $Part->Categories = $this->getCategories($CarModel->ID);
  146.  
  147. return $this->customise($Part)->renderWith(array('Product_Page', 'Page'));
  148.  
  149. } elseif ($Part = ProductGroup::get()->filter(array('URLSegment' => $PartRequested))->First()) {
  150. $Manufacture = Manufacture::get()->byID($Car->ManufactureID);
  151. $Part->CarFrom= $Manufacture->Name;
  152. $Part->MetaTitle = "Parts - " . $Manufacture->Name . " " . $Car->Name . " " . $Part->Name;
  153. $Part->Categories = $this->getCategories($CarModel->ID);
  154.  
  155. return $this->customise($Part)->renderWith(array('Product_Page', 'Page'));
  156. } else {
  157. $this->httpError(404);
  158. return $this->redirect('page-not-found');
  159. }
  160.  
  161. }
  162.  
  163. }
  164.  
  165. public function getCategories($carModel)
  166. {
  167.  
  168. $CategoryList = new ArrayList();
  169. $CarModel = Model::get()->byID($carModel);
  170.  
  171. foreach ($CarModel->Products() as $Product) {
  172.  
  173. $Category = Category::get()->byID($Product->CategoryID);
  174. $CategoryList->add($Category);
  175.  
  176. }
  177.  
  178. $CategoryList->removeDuplicates();
  179.  
  180. return $CategoryList;
  181. }
  182.  
  183.  
  184. public function FilterForm() {
  185. // Create fields
  186. $fields = new FieldList(
  187. new TextField('Name'),
  188.  
  189. );
  190.  
  191. // Create actions
  192. $actions = new FieldList(
  193. new FormAction('DoFilterForm', 'Submit')
  194. );
  195.  
  196. return new Form($this, 'FilterForm', $fields, $actions);
  197. }
  198.  
  199.  
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement