Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. <?php
  2.  
  3. class CompositeProduct extends Product {
  4.  
  5. public static $many_many = array(
  6. 'Products' => 'Product'
  7. );
  8.  
  9. public static $has_one = array(
  10. 'TileImage' => 'Image'
  11. );
  12.  
  13. /**
  14. * Summary fields for displaying Products in the CMS
  15. *
  16. * @var Array
  17. */
  18. public static $summary_fields = array(
  19. 'FirstImage' => 'Image',
  20. 'Title' => 'Name',
  21. 'Status' => 'Status',
  22. 'SummaryOfCategories' => 'Categories'
  23. );
  24.  
  25. public function getCMSFields() {
  26. $fields = parent::getCMSFields();
  27.  
  28. $fields->removeFieldFromTab('Root.Main', 'Price');
  29. $fields->removeFieldFromTab('Root.Main', 'Stock');
  30.  
  31. $fields->addFieldToTab(
  32. 'Root.TileImage',
  33. $uploadField = new UploadField('TileImage', 'Upload an image for teh category tile', '', '', '', 'Categories'));
  34. $uploadField->allowedExtensions = array('jpg', 'gif', 'png');
  35.  
  36. $fields->addFieldToTab('Root.Main', new HiddenField('Stock', 'Stock', -1));
  37.  
  38. //Related products
  39. $fields->addFieldToTab(
  40. 'Root.Products',
  41. new HeaderField('CompositeProductHeader', 'Select the products that are part of this composite product', 4)
  42. );
  43.  
  44. $gridFieldConfig = GridFieldConfig::create()->addComponents(
  45. new GridFieldToolbarHeader(),
  46. new GridFieldSortableHeader(),
  47. new GridFieldDataColumns(),
  48. new GridFieldManyRelationHandler(),
  49. new GridFieldPaginator(10),
  50. new GridFieldEditButton(),
  51. new GridFieldDeleteAction(),
  52. new GridFieldDetailForm()
  53. );
  54.  
  55. $tablefield = new GridField("Products", "Products:", $this->Products(), $gridFieldConfig);
  56. $fields->addFieldToTab('Root.Products', $tablefield);
  57.  
  58. return $fields;
  59. }
  60.  
  61. /**
  62. * Get the URL for this Product, products that are not part of the SiteTree are
  63. * displayed by the {@link Product_Controller}.
  64. *
  65. * @see SiteTree::Link()
  66. * @see Product_Controller::show()
  67. * @return String
  68. */
  69. function Link($action = null) {
  70.  
  71. if ($this->ParentID > -1) {
  72. //return Controller::join_links(Director::baseURL() . 'product/', $this->URLSegment .'/');
  73. return parent::Link($action);
  74. }
  75. return Controller::join_links(Director::baseURL() . 'compositeproduct/', $this->RelativeLink($action));
  76. }
  77.  
  78. static $allowed_children = array(
  79. 'CompositeProduct',
  80. 'Product'
  81. );
  82.  
  83.  
  84.  
  85. }
  86.  
  87. class CompositeProduct_Controller extends Product_Controller {
  88. /**
  89. * Add to cart form for adding Products, to show on the Product page.
  90. *
  91. * @param Int $quantity
  92. * @param String $redirectURL A URL to redirect to after the product is added, useful to redirect to cart page
  93. */
  94.  
  95. function ProductForm($quantity = null, $redirectURL = null) {
  96. $product = $this->data();
  97.  
  98. $products = $product->Products()->sort('Title', 'ASC');
  99. //SS_Log::log(new Exception(print_r($products->map(), true)), SS_Log::NOTICE);
  100.  
  101.  
  102.  
  103. $fields = new FieldList();
  104. $fields->push(new HiddenField('Redirect', 'Redirect', $redirectURL));
  105.  
  106. if ($products && $products->exists()) foreach ($products as $product) {
  107. $price = $product->dbObject('Price')->Nice();
  108. $fields->push(new LiteralField('ProductTitle' . $product->ID, '<a href="' . $product->URLSegment . '">' . $product->Title . '</a>' , 5));
  109. $fields->push(new HiddenField('Products[' . $product->ID . '][ProductClass]', 'ProductClass', $product->ClassName));
  110. $fields->push(new HiddenField('Products[' . $product->ID . '][ProductID]', 'ProductID', $product->ID));
  111. $fields->push(new LiteralField('Price', '<h4>' . $product->Price()->Nice() . '</h4>'));
  112. $fields->push(new TextField('Products[' . $product->ID . '][Quantity]', 'Quantity:', $quantity));
  113. }
  114.  
  115. $actions = new FieldList(
  116. new FormAction('add', 'Add To Cart')
  117. );
  118.  
  119. $validator = null;
  120.  
  121. $controller = Controller::curr();
  122. $form = new Form($controller, 'ProductForm', $fields, $actions, $validator);
  123. $form->disableSecurityToken();
  124.  
  125. return $form;
  126. }
  127.  
  128. public static $allowed_actions = array('ProductForm');
  129. /**
  130. * Add an item to the current cart ({@link Order}) for a given {@link Product}.
  131. *
  132. * @param Array $data
  133. * @param Form $form
  134. */
  135. function add(Array $data, Form $form) {
  136. $products = $data['Products'];
  137. foreach ($products as $productID => $productData) {
  138. //Get the product, the quantity and the product options
  139. $product = DataObject::get_by_id($productData['ProductClass'], $productData['ProductID']);
  140. $quantity = isset($productData['Quantity']) ? $productData['Quantity'] : 1;
  141. $productVariations = new Variation();
  142. $options = new ArrayList();
  143. if($quantity != 0){
  144. Cart::get_current_order()->addItem($product, $productVariations, $quantity, $options);
  145. }
  146. }
  147.  
  148. //Show feedback if redirecting back to the Product page
  149. if (!$this->getRequest()->requestVar('Redirect')) {
  150. $cartPage = DataObject::get_one('CartPage');
  151. $message = _t('ProductForm.PRODUCT_ADDED', 'The product was added to your cart.');
  152. if ($cartPage->exists()) {
  153. $message = _t(
  154. 'ProductForm.PRODUCT_ADDED_LINK',
  155. 'The product was added to {openanchor}your cart{closeanchor}',
  156. array(
  157. 'openanchor' => "<a href=\"{$cartPage->Link()}\">",
  158. 'closeanchor' => "</a>"
  159. )
  160. );
  161. }
  162. $form->sessionMessage(
  163. DBField::create_field("HTMLText", $message),
  164. 'good',
  165. false
  166. );
  167. }
  168. $this->goToNextPage();
  169. }
  170.  
  171. /**
  172. * Send user to next page based on current request vars,
  173. * if no redirect is specified redirect back.
  174. *
  175. * TODO make this work with AJAX
  176. */
  177. public function goToNextPage() {
  178.  
  179. $redirectURL = $this->getRequest()->requestVar('Redirect');
  180.  
  181. //Check if on site URL, if so redirect there, else redirect back
  182. if ($redirectURL && Director::is_site_url($redirectURL)) {
  183. $this->controller->redirect(Director::absoluteURL(Director::baseURL() . $redirectURL));
  184. }
  185. else {
  186. $this->redirectBack();
  187. }
  188. }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement