catalog/model/api/customers.php
<?php
/**
* @package Arastta eCommerce
* @copyright 2015-2017 Arastta Association. All rights reserved.
* @copyright See CREDITS.txt for credits and other copyright notices.
* @license GNU GPL version 3; see LICENSE.txt
* @link https://arastta.org
*/
class ModelApiCustomers extends Model
{
public function getCustomer($customer_id)
{
$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
return $query->row;
}
public function addCustomer($data)
{
$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_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']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "', newsletter = '" . (int)$data['newsletter'] . "', salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', status = '" . (int)$data['status'] . "', approved = '" . (int)$data['approved'] . "', safe = '" . (int)$data['safe'] . "', date_added = NOW()");
$customer_id = $this->db->getLastId();
if (isset($data['address'])) {
foreach ($data['address'] as $address) {
$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($address['firstname']) . "', lastname = '" . $this->db->escape($address['lastname']) . "', company = '" . $this->db->escape($address['company']) . "', address_1 = '" . $this->db->escape($address['address_1']) . "', address_2 = '" . $this->db->escape($address['address_2']) . "', city = '" . $this->db->escape($address['city']) . "', postcode = '" . $this->db->escape($address['postcode']) . "', country_id = '" . (int)$address['country_id'] . "', zone_id = '" . (int)$address['zone_id'] . "', custom_field = '" . $this->db->escape(isset($address['custom_field']) ? serialize($address['custom_field']) : '') . "'");
if (isset($address['default'])) {
$address_id = $this->db->getLastId();
$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
}
}
}
return $this->getCustomer($customer_id);
}
public function editCustomer($customer_id, $data)
{
if (!isset($data['custom_field'])) {
$data['custom_field'] = array();
}
$this->db->query("UPDATE " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_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']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "', newsletter = '" . (int)$data['newsletter'] . "', status = '" . (int)$data['status'] . "', approved = '" . (int)$data['approved'] . "', safe = '" . (int)$data['safe'] . "' WHERE customer_id = '" . (int)$customer_id . "'");
if ($data['password']) {
$this->db->query("UPDATE " . DB_PREFIX . "customer SET salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "' WHERE customer_id = '" . (int)$customer_id . "'");
}
$this->db->query("DELETE FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
if (isset($data['address'])) {
foreach ($data['address'] as $address) {
if (!isset($address['custom_field'])) {
$address['custom_field'] = array();
}
$this->db->query("INSERT INTO " . DB_PREFIX . "address SET address_id = '" . (int)$address['address_id'] . "', customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($address['firstname']) . "', lastname = '" . $this->db->escape($address['lastname']) . "', company = '" . $this->db->escape($address['company']) . "', address_1 = '" . $this->db->escape($address['address_1']) . "', address_2 = '" . $this->db->escape($address['address_2']) . "', city = '" . $this->db->escape($address['city']) . "', postcode = '" . $this->db->escape($address['postcode']) . "', country_id = '" . (int)$address['country_id'] . "', zone_id = '" . (int)$address['zone_id'] . "', custom_field = '" . $this->db->escape(isset($address['custom_field']) ? serialize($address['custom_field']) : '') . "'");
if (isset($address['default'])) {
$address_id = $this->db->getLastId();
$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
}
}
}
return $this->getCustomer($customer_id);
}
public function deleteCustomer($customer_id)
{
$this->db->query("DELETE FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "customer_credit WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("DELETE FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
}
public function getCustomers($data = array())
{
$sql = "SELECT *, CONCAT(c.firstname, ' ', c.lastname) AS name, cgd.name AS customer_group FROM " . DB_PREFIX . "customer c LEFT JOIN " . DB_PREFIX . "customer_group_description cgd ON (c.customer_group_id = cgd.customer_group_id)";
$conditions = $this->getExtraConditions($data);
if (empty($conditions)) {
$conditions = " WHERE cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
} else {
$conditions .= " AND cgd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
}
$sql .= $conditions;
$sort_data = array(
'name',
'c.email',
'customer_group',
'c.status',
'c.approved',
'c.ip',
'c.date_added'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY name";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
public function getAddress($address_id)
{
$address_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "address WHERE address_id = '" . (int)$address_id . "'");
if ($address_query->num_rows) {
$country_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "country` WHERE country_id = '" . (int)$address_query->row['country_id'] . "'");
if ($country_query->num_rows) {
$country = $country_query->row['name'];
$iso_code_2 = $country_query->row['iso_code_2'];
$iso_code_3 = $country_query->row['iso_code_3'];
$address_format = $country_query->row['address_format'];
} else {
$country = '';
$iso_code_2 = '';
$iso_code_3 = '';
$address_format = '';
}
$zone_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone` WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'");
if ($zone_query->num_rows) {
$zone = $zone_query->row['name'];
$zone_code = $zone_query->row['code'];
} else {
$zone = '';
$zone_code = '';
}
return array(
'address_id' => $address_query->row['address_id'],
'customer_id' => $address_query->row['customer_id'],
'firstname' => $address_query->row['firstname'],
'lastname' => $address_query->row['lastname'],
'company' => $address_query->row['company'],
'address_1' => $address_query->row['address_1'],
'address_2' => $address_query->row['address_2'],
'postcode' => $address_query->row['postcode'],
'city' => $address_query->row['city'],
'zone_id' => $address_query->row['zone_id'],
'zone' => $zone,
'zone_code' => $zone_code,
'country_id' => $address_query->row['country_id'],
'country' => $country,
'iso_code_2' => $iso_code_2,
'iso_code_3' => $iso_code_3,
'address_format' => $address_format,
'custom_field' => unserialize($address_query->row['custom_field'])
);
}
}
public function getAddresses($customer_id)
{
$address_data = array();
$query = $this->db->query("SELECT address_id FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$customer_id . "'");
foreach ($query->rows as $result) {
$address_info = $this->getAddress($result['address_id']);
if ($address_info) {
$address_data[] = $address_info;
}
}
return $address_data;
}
public function getTotals($data = array())
{
$sql = "SELECT COUNT(*) AS number FROM " . DB_PREFIX . "customer c";
$sql .= $this->getExtraConditions($data);
$query = $this->db->query($sql);
return $query->row;
}
private function getExtraConditions($data)
{
$sql = '';
$implode = array();
if (!empty($data['search'])) {
$implode[] = "(CONCAT(c.firstname, ' ', c.lastname) LIKE '%" . $this->db->escape($data['search']) . "%' OR c.email LIKE '" . $this->db->escape($data['search']) . "%')";
}
if (!empty($data['group'])) {
$implode[] = "c.customer_group_id = '" . (int)$data['group'] . "'";
}
if (!empty($data['ip'])) {
$implode[] = "c.customer_id IN (SELECT customer_id FROM " . DB_PREFIX . "customer_ip WHERE ip = '" . $this->db->escape($data['ip']) . "')";
}
if (isset($data['status']) && !is_null($data['status'])) {
$implode[] = "c.status = '" . (int)$data['status'] . "'";
}
if (isset($data['approved']) && !is_null($data['approved'])) {
$implode[] = "c.approved = '" . (int)$data['approved'] . "'";
}
if (!empty($data['date_from'])) {
$implode[] = "DATE(c.date_added) >= DATE('" . $this->db->escape($data['date_from']) . " 00:00:00')";
}
if (!empty($data['date_to'])) {
$implode[] = "DATE(c.date_added) <= DATE('" . $this->db->escape($data['date_to']) . " 23:59:59')";
}
if ($implode) {
$sql .= " WHERE " . implode(" AND ", $implode);
}
return $sql;
}
}