Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.92 KB | None | 0 0
  1. <?php
  2. class ModelAccountCustomer extends Model {
  3.     public function addCustomer($data) {
  4.         if (isset($data['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($data['customer_group_id'], $this->config->get('config_customer_group_display'))) {
  5.             $customer_group_id = $data['customer_group_id'];
  6.         } else {
  7.             $customer_group_id = $this->config->get('config_customer_group_id');
  8.         }
  9.        
  10.         $this->load->model('account/customer_group');
  11.        
  12.         $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
  13.        
  14.         $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$customer_group_id . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', status = '1', approved = '" . (int)!$customer_group_info['approval'] . "', date_added = NOW()");
  15.        
  16.         $customer_id = $this->db->getLastId();
  17.            
  18.         $this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '" . $this->db->escape($data['company']) . "', company_id = '" . $this->db->escape($data['company_id']) . "', tax_id = '" . $this->db->escape($data['tax_id']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "'");
  19.        
  20.         $address_id = $this->db->getLastId();
  21.  
  22.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
  23.        
  24.         $this->language->load('mail/customer');
  25.        
  26.         $subject = sprintf($this->language->get('text_subject'), $this->config->get('config_name'));
  27.        
  28.         $message = sprintf($this->language->get('text_welcome'), $this->config->get('config_name')) . "\n\n";
  29.        
  30.         if (!$customer_group_info['approval']) {
  31.             $message .= $this->language->get('text_login') . "\n";
  32.         } else {
  33.             $message .= $this->language->get('text_approval') . "\n";
  34.         }
  35.        
  36.         $message .= $this->url->link('account/login', '', 'SSL') . "\n\n";
  37.         $message .= $this->language->get('text_services') . "\n\n";
  38.         $message .= $this->language->get('text_thanks') . "\n";
  39.         $message .= $this->config->get('config_name');
  40.        
  41.         $mail = new Mail();
  42.         $mail->protocol = $this->config->get('config_mail_protocol');
  43.         $mail->parameter = $this->config->get('config_mail_parameter');
  44.         $mail->hostname = $this->config->get('config_smtp_host');
  45.         $mail->username = $this->config->get('config_smtp_username');
  46.         $mail->password = $this->config->get('config_smtp_password');
  47.         $mail->port = $this->config->get('config_smtp_port');
  48.         $mail->timeout = $this->config->get('config_smtp_timeout');            
  49.         $mail->setTo($data['email']);
  50.         $mail->setFrom($this->config->get('config_email'));
  51.         $mail->setSender($this->config->get('config_name'));
  52.         $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
  53.         $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
  54.         $mail->send();
  55.        
  56.         // Send to main admin email if new account email is enabled
  57.         if ($this->config->get('config_account_mail')) {
  58.             $mail->setTo($this->config->get('config_email'));
  59.             $mail->send();
  60.            
  61.             // Send to additional alert emails if new account email is enabled
  62.             $emails = explode(',', $this->config->get('config_alert_emails'));
  63.            
  64.             foreach ($emails as $email) {
  65.                 if (strlen($email) > 0 && preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $email)) {
  66.                     $mail->setTo($email);
  67.                     $mail->send();
  68.                 }
  69.             }
  70.         }
  71.     }
  72.    
  73.     public function editCustomer($data) {
  74.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
  75.     }
  76.  
  77.     public function editPassword($email, $password) {
  78.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET password = '" . $this->db->escape(md5($password)) . "' WHERE email = '" . $this->db->escape($email) . "'");
  79.     }
  80.  
  81.     public function editNewsletter($newsletter) {
  82.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET newsletter = '" . (int)$newsletter . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
  83.     }
  84.                    
  85.     public function getCustomer($customer_id) {
  86.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
  87.        
  88.         return $query->row;
  89.     }
  90.    
  91.     public function getCustomerByEmail($email) {
  92.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE email = '" . $this->db->escape($email) . "'");
  93.        
  94.         return $query->row;
  95.     }
  96.        
  97.     public function getCustomerByToken($token) {
  98.         $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE token = '" . $this->db->escape($token) . "' AND token != ''");
  99.        
  100.         $this->db->query("UPDATE " . DB_PREFIX . "customer SET token = ''");
  101.        
  102.         return $query->row;
  103.     }
  104.        
  105.     public function getCustomers($data = array()) {
  106.         $sql = "SELECT *, CONCAT(c.firstname, ' ', c.lastname) AS name, cg.name AS customer_group FROM " . DB_PREFIX . "customer c LEFT JOIN " . DB_PREFIX . "customer_group cg ON (c.customer_group_id = cg.customer_group_id) ";
  107.  
  108.         $implode = array();
  109.        
  110.         if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
  111.             $implode[] = "LCASE(CONCAT(c.firstname, ' ', c.lastname)) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
  112.         }
  113.        
  114.         if (isset($data['filter_email']) && !is_null($data['filter_email'])) {
  115.             $implode[] = "c.email = '" . $this->db->escape($data['filter_email']) . "'";
  116.         }
  117.        
  118.         if (isset($data['filter_customer_group_id']) && !is_null($data['filter_customer_group_id'])) {
  119.             $implode[] = "cg.customer_group_id = '" . $this->db->escape($data['filter_customer_group_id']) . "'";
  120.         }  
  121.        
  122.         if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
  123.             $implode[] = "c.status = '" . (int)$data['filter_status'] . "'";
  124.         }  
  125.        
  126.         if (isset($data['filter_approved']) && !is_null($data['filter_approved'])) {
  127.             $implode[] = "c.approved = '" . (int)$data['filter_approved'] . "'";
  128.         }  
  129.            
  130.         if (isset($data['filter_ip']) && !is_null($data['filter_ip'])) {
  131.             $implode[] = "c.customer_id IN (SELECT customer_id FROM " . DB_PREFIX . "customer_ip WHERE ip = '" . $this->db->escape($data['filter_ip']) . "')";
  132.         }  
  133.                
  134.         if (isset($data['filter_date_added']) && !is_null($data['filter_date_added'])) {
  135.             $implode[] = "DATE(c.date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
  136.         }
  137.        
  138.         if ($implode) {
  139.             $sql .= " WHERE " . implode(" AND ", $implode);
  140.         }
  141.        
  142.         $sort_data = array(
  143.             'name',
  144.             'c.email',
  145.             'customer_group',
  146.             'c.status',
  147.             'c.ip',
  148.             'c.date_added'
  149.         ); 
  150.            
  151.         if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  152.             $sql .= " ORDER BY " . $data['sort'];  
  153.         } else {
  154.             $sql .= " ORDER BY name";  
  155.         }
  156.            
  157.         if (isset($data['order']) && ($data['order'] == 'DESC')) {
  158.             $sql .= " DESC";
  159.         } else {
  160.             $sql .= " ASC";
  161.         }
  162.        
  163.         if (isset($data['start']) || isset($data['limit'])) {
  164.             if ($data['start'] < 0) {
  165.                 $data['start'] = 0;
  166.             }          
  167.  
  168.             if ($data['limit'] < 1) {
  169.                 $data['limit'] = 20;
  170.             }  
  171.            
  172.             $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  173.         }      
  174.        
  175.         $query = $this->db->query($sql);
  176.        
  177.         return $query->rows;   
  178.     }
  179.        
  180.     public function getTotalCustomersByEmail($email) {
  181.         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(strtolower($email)) . "'");
  182.        
  183.         return $query->row['total'];
  184.     }
  185.    
  186.     public function getIps($customer_id) {
  187.         $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ip` WHERE customer_id = '" . (int)$customer_id . "'");
  188.        
  189.         return $query->rows;
  190.     }  
  191.    
  192.     public function isBlacklisted($ip) {
  193.         $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer_ip_blacklist` WHERE ip = '" . $this->db->escape($ip) . "'");
  194.        
  195.         return $query->num_rows;
  196.     }  
  197. }
  198. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement