mostly filebased Content Presentation System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 satır
3.5KB

  1. <?php
  2. namespace Controller;
  3. class Cart {
  4. private $basket;
  5. function __construct() {
  6. $f3 = \Base::instance();
  7. $f3->route('POST /api/cart/add', '\Controller\Cart->add');
  8. $f3->route('GET /api/cart/del', '\Controller\Cart->del');
  9. $f3->route('GET /api/cart/more', '\Controller\Cart->more');
  10. $f3->route('GET /api/cart/less', '\Controller\Cart->less');
  11. $this->basket = new \Basket( 'cart' );
  12. //$this->basket->drop();
  13. }
  14. function index() {
  15. $f3 = \Base::instance();
  16. $tmp = $this->basket->find();
  17. $cart = [];
  18. foreach ($tmp as $i) {
  19. $cart[] = [
  20. 'name' => $i->get('name'),
  21. 'price' => $i->get('price'),
  22. 'amount' => $i->get('amount')
  23. ];
  24. }
  25. $cart_total = 0.0;
  26. foreach ($cart as $item) {
  27. $cart_total += $item['amount'] * $item['price'];
  28. }
  29. $f3->set('cart_meta',array_merge(
  30. $f3->get('cart_meta') ? : [],
  31. [
  32. 'cart_total' => $cart_total
  33. ])
  34. );
  35. $f3->set('cart', $cart);
  36. $this->sanitize_cart();
  37. }
  38. function sanitize_cart() {
  39. $f3 = \Base::instance();
  40. }
  41. function add() {
  42. $f3 = \Base::instance();
  43. $caller = $f3->get('POST.caller');
  44. $name = $f3->get('POST.name');
  45. $price = $f3->get('POST.price');
  46. $amount = $f3->get('POST.amount') ? : 1;
  47. $previously_in_cart = $this->basket->find('name',$name);
  48. if (count($previously_in_cart) > 0) {
  49. // item already in cart
  50. // add to counter
  51. $this->basket->load('name',$name);
  52. $this->basket->set('amount',$this->basket->get('amount') + $amount);
  53. $this->basket->save();
  54. $this->basket->reset();
  55. } else {
  56. $this->basket->set('name',$name);
  57. $this->basket->set('price',$price);
  58. $this->basket->set('amount',$amount);
  59. $this->basket->save();
  60. $this->basket->reset();
  61. };
  62. //echo $caller;
  63. $f3->reroute($caller);
  64. }
  65. function del($name = false) {
  66. $f3 = \Base::instance();
  67. $caller = $f3->get('GET.caller');
  68. $name = $name ? : urldecode($f3->get('GET.name'));
  69. $this->basket->erase('name', $name);
  70. $f3->reroute($caller);
  71. }
  72. function more() {
  73. $f3 = \Base::instance();
  74. $caller = $f3->get('GET.caller');
  75. $this->change_quantity(1);
  76. $f3->reroute($caller);
  77. }
  78. function less() {
  79. $f3 = \Base::instance();
  80. $caller = $f3->get('GET.caller');
  81. $this->change_quantity(-1);
  82. $f3->reroute($caller);
  83. }
  84. function change_quantity($amount = 0) {
  85. $f3 = \Base::instance();
  86. $caller = $f3->get('GET.caller');
  87. $name = urldecode($f3->get('GET.name'));
  88. $this->basket->load('name', $name);
  89. $new_amount = $this->basket->get('amount') + $amount;
  90. if ($new_amount > 0) {
  91. $this->basket->set('amount',$this->basket->get('amount') + $amount);
  92. } else {
  93. $this->del($name);
  94. }
  95. $this->basket->save();
  96. $this->basket->reset();
  97. }
  98. function checkout() {
  99. $t = \Template::instance();
  100. return $t->render('checkout.htm');
  101. }
  102. }