PageRenderTime 143ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/assets/addons/cart/addon.php

http://three-cms.googlecode.com/
PHP | 226 lines | 120 code | 17 blank | 89 comment | 9 complexity | 49a6e98bbb230cbbeede7003f927a5f5 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Cart v0.1
  4. ---------------------------------------------------------------------------
  5. Author: Giel Berkers
  6. Website: www.gielberkers.com
  7. ---------------------------------------------------------------------------
  8. This addon makes use of Code Igniters Cart-library to create a simple
  9. shopping cart addon.
  10. Usage:
  11. To add a product to the cart, use a form with certain hidden fields:
  12. <form method="post" action="">
  13. Quantity: <input name="cart[qty]" type="text" value="1" size="3" />
  14. <input type="hidden" name="cart[id]" value="{$idContent}" />
  15. <!-- $name and $price could be fields of your dataobject: -->
  16. <input type="hidden" name="cart[name]" value="{$name}" />
  17. <input type="hidden" name="cart[price]" value="{$price}" />
  18. <input type="submit" name="cart[add]" value="Add to cart" />
  19. <!-- Add specific product options like this: -->
  20. <input type="hidden" name="cart[options][code]" value="{$code}" />
  21. <select name="cart[options][size]">
  22. <option value="S">Small</option>
  23. <option value="M">Medium</option>
  24. <option value="L">Large</option>
  25. </select>
  26. </form>
  27. To destroy (empty) your cart you can use a form:
  28. <form method="post" action="">
  29. <input type="submit" name="cart[destroy]" value="Empty cart" />
  30. </form>
  31. Or a function:
  32. {$cart->destroy()}
  33. Other functions:
  34. {$cart->totalItems()} Returns the total amount of items in your cart
  35. {$cart->total()} Returns the total amount of your cart
  36. {$cart->getCart()} Gets the content of your cart
  37. */
  38. class Cart extends AddonBaseModel
  39. {
  40. var $cart;
  41. var $email;
  42. /**
  43. * Initialize
  44. */
  45. function init()
  46. {
  47. @session_start();
  48. $this->frontEnd = true;
  49. }
  50. /**
  51. * This function tells Three CMS on which hook a function needs to be called
  52. */
  53. function getHooks()
  54. {
  55. // Since this addon only does something on the frontend, there are no hooks:
  56. $hooks = array(
  57. array(
  58. 'hook'=>'LoadPage',
  59. 'callback'=>'loadUtilities'
  60. ),
  61. array(
  62. 'hook'=>'PreRenderPage',
  63. 'callback'=>'preRender'
  64. )
  65. );
  66. return $hooks;
  67. }
  68. // Load the Code Igniter shopping cart and stuff:
  69. function loadUtilities($context)
  70. {
  71. // Load the CI-cart library:
  72. $context['page']->load->library('cart');
  73. // Make a reference to the cart-object, since it will not be automaticly
  74. // coupled this this Addon-Object:
  75. $this->cart = $context['page']->cart;
  76. // Do the same for the e-mail class:
  77. $context['page']->load->library('email');
  78. $this->email = $context['page']->email;
  79. }
  80. // Check for POST-data
  81. function preRender($context)
  82. {
  83. if(isset($_POST['cart'])) {
  84. $data = $this->input->post('cart');
  85. if(isset($data['add'])) {
  86. // Add product to the cart:
  87. // Check if the item already exists, if so, perform an update:
  88. $update = false;
  89. foreach($this->cart->contents() as $row) {
  90. if($row['id']==$data['id']) {
  91. $update = true;
  92. $this->cart->update(array('rowid'=>$row['rowid'], 'qty'=>$row['qty'] + $data['qty']));
  93. }
  94. }
  95. if(!$update) {
  96. // No update performed, insert as a new item:
  97. $this->cart->insert($data);
  98. }
  99. } elseif(isset($data['update'])) {
  100. // Update cart
  101. if(isset($_POST['cart']['update_qty'])) {
  102. foreach($_POST['cart']['update_qty'] as $rowid=>$value) {
  103. $this->cart->update(array('rowid'=>$rowid, 'qty'=>$value));
  104. }
  105. }
  106. } elseif(isset($data['remove'])) {
  107. // Remove from cart
  108. if(isset($_POST['cart']['remove_id'])) {
  109. foreach($_POST['cart']['remove_id'] as $rowid=>$value) {
  110. $this->cart->update(array('rowid'=>$rowid, 'qty'=>0));
  111. }
  112. }
  113. } elseif(isset($data['destroy'])) {
  114. // Destroy (empty) the cart:
  115. $this->destroy();
  116. } elseif(isset($data['order_complete'])) {
  117. $this->complete($data);
  118. }
  119. }
  120. }
  121. //// Frontend functions:
  122. /**
  123. * Get the shopping cart
  124. * @return array A 2-dimensional array with the content of the shopping cart
  125. */
  126. function getCart()
  127. {
  128. return $this->cart->contents();
  129. }
  130. /**
  131. * Get the total amount of products in the cart:
  132. * @return int The total amount products
  133. */
  134. function totalItems()
  135. {
  136. $quantity = 0;
  137. foreach($this->getCart() as $item) {
  138. $quantity += $item['qty'];
  139. }
  140. return $quantity;
  141. }
  142. /**
  143. * Get the total amount of the cart:
  144. * @return number The total amount of the cart
  145. */
  146. function total()
  147. {
  148. return $this->cart->total();
  149. }
  150. /**
  151. * Destroy (empty) your cart:
  152. */
  153. function destroy()
  154. {
  155. $this->cart->destroy();
  156. }
  157. /**
  158. * Complete the order:
  159. */
  160. function complete($data)
  161. {
  162. if(isset($data['order_type'])) {
  163. switch($data['order_type']) {
  164. case 'email' :
  165. {
  166. // Send an e-mail to a user:
  167. if(isset($data['order_recipient'])) {
  168. $this->db->select('email');
  169. $this->db->where('username', $data['order_recipient']);
  170. $query = $this->db->get('users');
  171. if($query->num_rows==1) {
  172. $result = $query->result();
  173. $email_address = $result[0]->email;
  174. $cart = $this->getCart();
  175. $clientDetails = $data['client'];
  176. // Create message:
  177. ob_start();
  178. include('email.tpl');
  179. $content = ob_get_clean();
  180. // Load Configuration:
  181. // $cartConfig = array();
  182. include('cart_config.php');
  183. $this->email->initialize($cartConfig);
  184. // Send HTML mail:
  185. $this->email->from($cartConfig['from']);
  186. $this->email->to($email_address);
  187. $this->email->subject($cartConfig['subject']);
  188. $this->email->message($content);
  189. $ok = $this->email->send();
  190. }
  191. die();
  192. }
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. ?>