PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/library/customer.php

https://bitbucket.org/monobasic/shop.volero.ch
PHP | 168 lines | 136 code | 32 blank | 0 comment | 16 complexity | fdcc6c8309e4d3c4c3839d89c21eb12a MD5 | raw file
  1. <?php
  2. final class Customer {
  3. private $customer_id;
  4. private $firstname;
  5. private $lastname;
  6. private $email;
  7. private $telephone;
  8. private $fax;
  9. private $newsletter;
  10. private $customer_group_id;
  11. private $address_id;
  12. public function __construct($registry) {
  13. $this->config = $registry->get('config');
  14. $this->db = $registry->get('db');
  15. $this->request = $registry->get('request');
  16. $this->session = $registry->get('session');
  17. if (isset($this->session->data['customer_id'])) {
  18. $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "' AND status = '1'");
  19. if ($customer_query->num_rows) {
  20. $this->customer_id = $customer_query->row['customer_id'];
  21. $this->firstname = $customer_query->row['firstname'];
  22. $this->lastname = $customer_query->row['lastname'];
  23. $this->email = $customer_query->row['email'];
  24. $this->telephone = $customer_query->row['telephone'];
  25. $this->fax = $customer_query->row['fax'];
  26. $this->newsletter = $customer_query->row['newsletter'];
  27. $this->customer_group_id = $customer_query->row['customer_group_id'];
  28. $this->address_id = $customer_query->row['address_id'];
  29. $this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '" . $this->db->escape(isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '') . "', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "'");
  30. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "' AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'");
  31. if (!$query->num_rows) {
  32. $this->db->query("INSERT INTO " . DB_PREFIX . "customer_ip SET customer_id = '" . (int)$this->session->data['customer_id'] . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', date_added = NOW()");
  33. }
  34. } else {
  35. $this->logout();
  36. }
  37. }
  38. }
  39. public function login($email, $password, $override = false) {
  40. if ($override) {
  41. $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer where LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND status = '1'");
  42. } elseif (!$this->config->get('config_customer_approval')) {
  43. $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND password = '" . $this->db->escape(md5($password)) . "' AND status = '1'");
  44. } else {
  45. $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(strtolower($email)) . "' AND password = '" . $this->db->escape(md5($password)) . "' AND status = '1' AND approved = '1'");
  46. }
  47. if ($customer_query->num_rows) {
  48. $this->session->data['customer_id'] = $customer_query->row['customer_id'];
  49. if ($customer_query->row['cart'] && is_string($customer_query->row['cart'])) {
  50. $cart = unserialize($customer_query->row['cart']);
  51. foreach ($cart as $key => $value) {
  52. if (!array_key_exists($key, $this->session->data['cart'])) {
  53. $this->session->data['cart'][$key] = $value;
  54. } else {
  55. $this->session->data['cart'][$key] += $value;
  56. }
  57. }
  58. }
  59. if ($customer_query->row['wishlist'] && is_string($customer_query->row['wishlist'])) {
  60. if (!isset($this->session->data['wishlist'])) {
  61. $this->session->data['wishlist'] = array();
  62. }
  63. $wishlist = unserialize($customer_query->row['wishlist']);
  64. foreach ($wishlist as $product_id) {
  65. if (!in_array($product_id, $this->session->data['wishlist'])) {
  66. $this->session->data['wishlist'][] = $product_id;
  67. }
  68. }
  69. }
  70. $this->customer_id = $customer_query->row['customer_id'];
  71. $this->firstname = $customer_query->row['firstname'];
  72. $this->lastname = $customer_query->row['lastname'];
  73. $this->email = $customer_query->row['email'];
  74. $this->telephone = $customer_query->row['telephone'];
  75. $this->fax = $customer_query->row['fax'];
  76. $this->newsletter = $customer_query->row['newsletter'];
  77. $this->customer_group_id = $customer_query->row['customer_group_id'];
  78. $this->address_id = $customer_query->row['address_id'];
  79. $this->db->query("UPDATE " . DB_PREFIX . "customer SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$customer_query->row['customer_id'] . "'");
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. public function logout() {
  86. unset($this->session->data['customer_id']);
  87. $this->customer_id = '';
  88. $this->firstname = '';
  89. $this->lastname = '';
  90. $this->email = '';
  91. $this->telephone = '';
  92. $this->fax = '';
  93. $this->newsletter = '';
  94. $this->customer_group_id = '';
  95. $this->address_id = '';
  96. }
  97. public function isLogged() {
  98. return $this->customer_id;
  99. }
  100. public function getId() {
  101. return $this->customer_id;
  102. }
  103. public function getFirstName() {
  104. return $this->firstname;
  105. }
  106. public function getLastName() {
  107. return $this->lastname;
  108. }
  109. public function getEmail() {
  110. return $this->email;
  111. }
  112. public function getTelephone() {
  113. return $this->telephone;
  114. }
  115. public function getFax() {
  116. return $this->fax;
  117. }
  118. public function getNewsletter() {
  119. return $this->newsletter;
  120. }
  121. public function getCustomerGroupId() {
  122. return $this->customer_group_id;
  123. }
  124. public function getAddressId() {
  125. return $this->address_id;
  126. }
  127. public function getBalance() {
  128. $query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'");
  129. return $query->row['total'];
  130. }
  131. public function getRewardPoints() {
  132. $query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'");
  133. return $query->row['total'];
  134. }
  135. }
  136. ?>