PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/gocart/models/customer_model.php

https://bitbucket.org/adrianricardo/newstatus
PHP | 469 lines | 367 code | 70 blank | 32 comment | 38 complexity | d321532f8132f19b6cd4c8613a9b1aaf MD5 | raw file
  1. <?php
  2. Class Customer_model extends CI_Model
  3. {
  4. //this is the expiration for a non-remember session
  5. var $session_expire = 7200;
  6. function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. /********************************************************************
  11. ********************************************************************/
  12. function get_customers($limit=0, $offset=0, $order_by='id', $direction='DESC')
  13. {
  14. $this->db->order_by($order_by, $direction);
  15. if($limit>0)
  16. {
  17. $this->db->limit($limit, $offset);
  18. }
  19. $result = $this->db->get('customers');
  20. return $result->result();
  21. }
  22. function count_customers()
  23. {
  24. return $this->db->count_all_results('customers');
  25. }
  26. function get_customer($id)
  27. {
  28. $result = $this->db->get_where('customers', array('id'=>$id));
  29. return $result->row();
  30. }
  31. function get_metrics($id){
  32. $sql = "SELECT order_count, IFNULL(total_revenue,0) as total_revenue, s.id, COUNT(p.`product_id`) as
  33. products_viewed from (SELECT c.customer_id as id, COUNT(c.customer_id) as
  34. order_count, SUM(c.total) as total_revenue FROM cs_orders c WHERE c.customer_id = ".$id.")
  35. s join cs_product_views p on s.id = p.customer_id";
  36. $query = $this->db->query($sql);
  37. return $query->row();
  38. }
  39. function get_subscribers()
  40. {
  41. $this->db->where('email_subscribe','1');
  42. $res = $this->db->get('customers');
  43. return $res->result_array();
  44. }
  45. function get_address_list($id)
  46. {
  47. $addresses = $this->db->where('customer_id', $id)->get('customers_address_bank')->result_array();
  48. // unserialize the field data
  49. if($addresses)
  50. {
  51. foreach($addresses as &$add)
  52. {
  53. $add['field_data'] = unserialize($add['field_data']);
  54. }
  55. }
  56. return $addresses;
  57. }
  58. function get_address($address_id)
  59. {
  60. $address= $this->db->where('id', $address_id)->get('customers_address_bank')->row_array();
  61. if($address)
  62. {
  63. $address_info = unserialize($address['field_data']);
  64. $address['field_data'] = $address_info;
  65. $address = array_merge($address, $address_info);
  66. }
  67. return $address;
  68. }
  69. function save_address($data)
  70. {
  71. // prepare fields for db insertion
  72. $data['field_data'] = serialize($data['field_data']);
  73. // update or insert
  74. if(!empty($data['id']))
  75. {
  76. $this->db->where('id', $data['id']);
  77. $this->db->update('customers_address_bank', $data);
  78. return $data['id'];
  79. } else {
  80. $this->db->insert('customers_address_bank', $data);
  81. return $this->db->insert_id();
  82. }
  83. }
  84. function delete_address($id, $customer_id)
  85. {
  86. $this->db->where(array('id'=>$id, 'customer_id'=>$customer_id))->delete('customers_address_bank');
  87. return $id;
  88. }
  89. function save($customer)
  90. {
  91. if ($customer['id'])
  92. {
  93. $this->db->where('id', $customer['id']);
  94. $this->db->update('customers', $customer);
  95. return $customer['id'];
  96. }
  97. else
  98. {
  99. $this->db->insert('customers', $customer);
  100. return $this->db->insert_id();
  101. }
  102. }
  103. function deactivate($id)
  104. {
  105. $customer = array('id'=>$id, 'active'=>0);
  106. $this->save_customer($customer);
  107. }
  108. function delete($id)
  109. {
  110. /*
  111. deleting a customer will remove all their orders from the system
  112. this will alter any report numbers that reflect total sales
  113. deleting a customer is not recommended, deactivation is preferred
  114. */
  115. //this deletes the customers record
  116. $this->db->where('id', $id);
  117. $this->db->delete('customers');
  118. // Delete Address records
  119. $this->db->where('customer_id', $id);
  120. $this->db->delete('customers_address_bank');
  121. //get all the orders the customer has made and delete the items from them
  122. $this->db->select('id');
  123. $result = $this->db->get_where('orders', array('customer_id'=>$id));
  124. $result = $result->result();
  125. foreach ($result as $order)
  126. {
  127. $this->db->where('order_id', $order->id);
  128. $this->db->delete('order_items');
  129. }
  130. //delete the orders after the items have already been deleted
  131. $this->db->where('customer_id', $id);
  132. $this->db->delete('orders');
  133. }
  134. function check_email($str, $id=false)
  135. {
  136. $this->db->select('email');
  137. $this->db->from('customers');
  138. $this->db->where('email', $str);
  139. if ($id)
  140. {
  141. $this->db->where('id !=', $id);
  142. }
  143. $count = $this->db->count_all_results();
  144. if ($count > 0)
  145. {
  146. return true;
  147. }
  148. else
  149. {
  150. return false;
  151. }
  152. }
  153. /*
  154. these functions handle logging in and out
  155. */
  156. function logout()
  157. {
  158. $this->session->unset_userdata('customer');
  159. $this->go_cart->destroy(false);
  160. //$this->session->sess_destroy();
  161. }
  162. function login($email, $password, $remember=false)
  163. {
  164. $this->db->select('*');
  165. $this->db->where('email', $email);
  166. $this->db->where('active', 1);
  167. $this->db->where('password', sha1($password));
  168. $this->db->limit(1);
  169. $result = $this->db->get('customers');
  170. $customer = $result->row_array();
  171. if ($customer)
  172. {
  173. // Retrieve customer addresses
  174. $this->db->where(array('customer_id'=>$customer['id'], 'id'=>$customer['default_billing_address']));
  175. $address = $this->db->get('customers_address_bank')->row_array();
  176. if($address)
  177. {
  178. $fields = unserialize($address['field_data']);
  179. $customer['bill_address'] = $fields;
  180. $customer['bill_address']['id'] = $address['id']; // save the addres id for future reference
  181. }
  182. $this->db->where(array('customer_id'=>$customer['id'], 'id'=>$customer['default_shipping_address']));
  183. $address = $this->db->get('customers_address_bank')->row_array();
  184. if($address)
  185. {
  186. $fields = unserialize($address['field_data']);
  187. $customer['ship_address'] = $fields;
  188. $customer['ship_address']['id'] = $address['id'];
  189. } else {
  190. $customer['ship_to_bill_address'] = 'true';
  191. }
  192. // Set up any group discount
  193. if($customer['group_id']!=0)
  194. {
  195. $group = $this->get_group($customer['group_id']);
  196. if($group) // group might not exist
  197. {
  198. if($group->discount_type == "fixed")
  199. {
  200. $customer['group_discount_formula'] = "- ". $group->discount;
  201. }
  202. else
  203. {
  204. $percent = (100-(float)$group->discount)/100;
  205. $customer['group_discount_formula'] = '* ('.$percent.')';
  206. }
  207. }
  208. }
  209. if(!$remember)
  210. {
  211. $customer['expire'] = time()+$this->session_expire;
  212. }
  213. else
  214. {
  215. $customer['expire'] = false;
  216. }
  217. // put our customer in the cart
  218. $this->go_cart->save_customer($customer);
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. function fblogin($id, $remember=false)
  227. {
  228. $this->db->select('*');
  229. $this->db->where('social_id', $id);
  230. $this->db->where('active', 1);
  231. $this->db->limit(1);
  232. $result = $this->db->get('customers');
  233. $customer = $result->row_array();
  234. if ($customer)
  235. {
  236. // Retrieve customer addresses
  237. $this->db->where(array('customer_id'=>$customer['id'], 'id'=>$customer['default_billing_address']));
  238. $address = $this->db->get('customers_address_bank')->row_array();
  239. if($address)
  240. {
  241. $fields = unserialize($address['field_data']);
  242. $customer['bill_address'] = $fields;
  243. $customer['bill_address']['id'] = $address['id']; // save the addres id for future reference
  244. }
  245. $this->db->where(array('customer_id'=>$customer['id'], 'id'=>$customer['default_shipping_address']));
  246. $address = $this->db->get('customers_address_bank')->row_array();
  247. if($address)
  248. {
  249. $fields = unserialize($address['field_data']);
  250. $customer['ship_address'] = $fields;
  251. $customer['ship_address']['id'] = $address['id'];
  252. } else {
  253. $customer['ship_to_bill_address'] = 'true';
  254. }
  255. // Set up any group discount
  256. if($customer['group_id']!=0)
  257. {
  258. $group = $this->get_group($customer['group_id']);
  259. if($group) // group might not exist
  260. {
  261. if($group->discount_type == "fixed")
  262. {
  263. $customer['group_discount_formula'] = "- ". $group->discount;
  264. }
  265. else
  266. {
  267. $percent = (100-(float)$group->discount)/100;
  268. $customer['group_discount_formula'] = '* ('.$percent.')';
  269. }
  270. }
  271. }
  272. if(!$remember)
  273. {
  274. $customer['expire'] = time()+$this->session_expire;
  275. }
  276. else
  277. {
  278. $customer['expire'] = false;
  279. }
  280. // put our customer in the cart
  281. $this->go_cart->save_customer($customer);
  282. return true;
  283. }
  284. else
  285. {
  286. return false;
  287. }
  288. }
  289. function is_logged_in($redirect = false, $default_redirect = 'secure/login/')
  290. {
  291. //$redirect allows us to choose where a customer will get redirected to after they login
  292. //$default_redirect points is to the login page, if you do not want this, you can set it to false and then redirect wherever you wish.
  293. $customer = $this->go_cart->customer();
  294. if (!isset($customer['id']))
  295. {
  296. //this tells gocart where to go once logged in
  297. if ($redirect)
  298. {
  299. $this->session->set_flashdata('redirect', $redirect);
  300. }
  301. if ($default_redirect)
  302. {
  303. redirect($default_redirect);
  304. }
  305. return false;
  306. }
  307. else
  308. {
  309. //check if the session is expired if not reset the timer
  310. if($customer['expire'] && $customer['expire'] < time())
  311. {
  312. $this->logout();
  313. if($redirect)
  314. {
  315. $this->session->set_flashdata('redirect', $redirect);
  316. }
  317. if($default_redirect)
  318. {
  319. redirect('secure/login');
  320. }
  321. return false;
  322. }
  323. else
  324. {
  325. //update the session expiration to last more time if they are not remembered
  326. if($customer['expire'])
  327. {
  328. $customer['expire'] = time()+$this->session_expire;
  329. $this->go_cart->save_customer($customer);
  330. }
  331. }
  332. return true;
  333. }
  334. }
  335. function reset_password($email)
  336. {
  337. $this->load->library('encrypt');
  338. $customer = $this->get_customer_by_email($email);
  339. if ($customer)
  340. {
  341. $this->load->helper('string');
  342. $this->load->library('email');
  343. $new_password = random_string('alnum', 8);
  344. $customer['password'] = sha1($new_password);
  345. $this->save($customer);
  346. $this->email->from($this->config->item('email'), $this->config->item('site_name'));
  347. $this->email->to($email);
  348. $this->email->subject($this->config->item('site_name').': Password Reset');
  349. $this->email->message('Your password has been reset to <strong>'. $new_password .'</strong>.');
  350. $this->email->send();
  351. return true;
  352. }
  353. else
  354. {
  355. return false;
  356. }
  357. }
  358. function get_customer_by_email($email)
  359. {
  360. $result = $this->db->get_where('customers', array('email'=>$email));
  361. return $result->row_array();
  362. }
  363. function isUser($social_id)
  364. {
  365. $result = $this->db->get_where('customers', array('social_id'=>$social_id));
  366. if ($result->num_rows() > 0){
  367. return true;
  368. }
  369. else{
  370. return false;
  371. }
  372. }
  373. /// Customer groups functions
  374. function get_groups()
  375. {
  376. return $this->db->get('customer_groups')->result();
  377. }
  378. function get_group($id)
  379. {
  380. return $this->db->where('id', $id)->get('customer_groups')->row();
  381. }
  382. function delete_group($id)
  383. {
  384. $this->db->where('id', $id);
  385. $this->db->delete('customer_groups');
  386. }
  387. function save_group($data)
  388. {
  389. if(!empty($data['id']))
  390. {
  391. $this->db->where('id', $data['id'])->update('customer_groups', $data);
  392. return $data['id'];
  393. } else {
  394. $this->db->insert('customer_groups', $data);
  395. return $this->db->insert_id();
  396. }
  397. }
  398. }