473 lines
12 KiB
PHP
473 lines
12 KiB
PHP
<?php
|
|
/*
|
|
* @version $Id: index.php 10381 2008-06-01 03:35:53Z $
|
|
* @package Carteasy
|
|
* @copyright Copyright (C) 2005 - 2011 Wlanium
|
|
* @license proprietary
|
|
* @author Thomas Bartelt
|
|
* Carteasy is a web shop system
|
|
*/
|
|
|
|
include_once './core/database.class.php';
|
|
|
|
class CustomerAddress {
|
|
|
|
// table fields
|
|
private $table_fields = array(
|
|
'customer_id' => array('required' => true, 'type' => 'int'),
|
|
'company' => array('required' => false, 'type' => 'text'),
|
|
'honorific' => array('required' => true, 'type' => 'int'),
|
|
'firstname' => array('required' => false, 'type' => 'text'),
|
|
'surname' => array('required' => false, 'type' => 'text'),
|
|
'street' => array('required' => true, 'type' => 'text'),
|
|
'house_number' => array('required' => false, 'type' => 'text'),
|
|
'zip_code' => array('required' => true, 'type' => 'text'),
|
|
'city' => array('required' => true, 'type' => 'text'),
|
|
'country' => array('required' => true, 'type' => 'int'),
|
|
'orders' => array('required' => false, 'type' => 'int')
|
|
);
|
|
|
|
// generic
|
|
private $base_object;
|
|
private $db;
|
|
private $address_id;
|
|
private $customer_id;
|
|
private $error;
|
|
private $validation_errors;
|
|
|
|
public function __construct($base_object, $address_id = false, $customer_id = false) {
|
|
$this->base_object = $base_object;
|
|
$this->db = $base_object->db;
|
|
$this->address_id = $address_id;
|
|
$this->customer_id = $customer_id;
|
|
$this->error = '';
|
|
$this->validation_errors = array();
|
|
} // end constructor
|
|
|
|
public function set_address_id($address_id) {
|
|
$this->address_id = $address_id;
|
|
} // end set_id
|
|
|
|
public function get_id() {
|
|
return $this->address_id;
|
|
} // end get_id
|
|
|
|
public function set_customer_id($customer_id) {
|
|
$this->customer_id = $customer_id;
|
|
} // end set_customer_id
|
|
|
|
public function get_customer_id() {
|
|
return $this->customer_id;
|
|
} // end get_customer_id
|
|
|
|
public function get_customer_id_by_address_id($address_id = false) {
|
|
$sql = "SELECT customer_id FROM customer_addresses";
|
|
|
|
if ($address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($address_id);
|
|
$this->address_id = $address_id;
|
|
} else if ($this->address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($this->address_id);
|
|
} else {
|
|
$this->error = "no address id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result->num_rows > 0) {
|
|
return $result->fetch_object()->customer_id;
|
|
}
|
|
$this->error = "no result";
|
|
|
|
return false;
|
|
} // end get_customer_id_by_address_id
|
|
|
|
public function get_error() {
|
|
return $this->error;
|
|
} // end get_error
|
|
|
|
public function get_validation_errors() {
|
|
return $this->validation_errors;
|
|
} // end get_validation_errors
|
|
|
|
public function get_all($filter = false, $customer_id = false) {
|
|
$sql = "SELECT * FROM customer_addresses WHERE";
|
|
|
|
if ($customer_id) {
|
|
$sql .= " customer_id=".$this->db->real_escape_string($customer_id);
|
|
$this->customer_id = $customer_id;
|
|
} else if ($this->customer_id) {
|
|
$sql .= " customer_id=".$this->db->real_escape_string($this->customer_id);
|
|
} else {
|
|
$this->error = "no customer id";
|
|
|
|
return false;
|
|
}
|
|
|
|
// TODO: I need filter object, here for coutnries
|
|
if ($filter) {
|
|
if ($filter == 'WHATEVER') {
|
|
$sql .= 'WHERE something';
|
|
} else {
|
|
$this->error = "unknown filter";
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result->num_rows > 0) {
|
|
$return_data = array();
|
|
while ($obj = $result->fetch_object()) {
|
|
$return_data[$obj->id] = $this->enrich($obj);
|
|
}
|
|
|
|
return $return_data;
|
|
}
|
|
$this->error = "no result";
|
|
|
|
return false;
|
|
} // end get_all
|
|
|
|
public function get_data_by_address_id($address_id = false) {
|
|
$sql = "SELECT * FROM customer_addresses";
|
|
|
|
if ($address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($address_id);
|
|
$this->address_id = $address_id;
|
|
} else if ($this->address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($this->address_id);
|
|
} else {
|
|
$this->error = "no address id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result->num_rows > 0) {
|
|
return $this->enrich($result->fetch_object());
|
|
}
|
|
$this->error = "no result";
|
|
|
|
return false;
|
|
} // end get_data_by_address_id
|
|
|
|
public function get_data_by_customer_id($customer_id = false) {
|
|
$sql = "SELECT * FROM customer_addresses";
|
|
|
|
if ($customer_id) {
|
|
$sql .= " WHERE customer_id=".$this->db->real_escape_string($customer_id);
|
|
$this->customer_id = $customer_id;
|
|
} else if ($this->customer_id) {
|
|
$sql .= " WHERE customer_id=".$this->db->real_escape_string($customer_id);
|
|
} else {
|
|
$this->error = "no customer id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result->num_rows > 0) {
|
|
$return_data = array();
|
|
|
|
while ($obj = $result->fetch_object()) {
|
|
$return_data[$obj->id] = $this->enrich($obj);
|
|
}
|
|
|
|
return $return_data;
|
|
}
|
|
$this->error = "no result";
|
|
|
|
return false;
|
|
} // end get_data_by_customer_id
|
|
|
|
public function delete_by_address_id($address_id = false) {
|
|
$sql = "DELETE FROM customer_addresses";
|
|
|
|
if ($address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($address_id);
|
|
$this->address_id = $address_id;
|
|
} else if ($this->address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($this->address_id);
|
|
} else {
|
|
$this->error = "no address id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result) {
|
|
$this->address_id = false;
|
|
|
|
return true;
|
|
}
|
|
$this->error = "can't delete";
|
|
|
|
return false;
|
|
} // end delete_by_address_id
|
|
|
|
public function delete_by_customer_id($customer_id = false) {
|
|
$sql = "DELETE FROM customer_addresses";
|
|
|
|
if ($customer_id) {
|
|
$sql .= " WHERE customer_id=".$this->db->real_escape_string($customer_id);
|
|
$this->customer_id = $customer_id;
|
|
} else if ($this->customer_id) {
|
|
$sql .= " WHERE customer_id=".$this->db->real_escape_string($this->customer_id);
|
|
} else {
|
|
$this->error = "no customer id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result) {
|
|
$this->address_id = false;
|
|
$this->customer_id = false;
|
|
|
|
return true;
|
|
}
|
|
$this->error = "can't delte";
|
|
|
|
return false;
|
|
} // end delte_by_customer_id
|
|
|
|
public function create($data, $customer_id = false) {
|
|
if ($data) {
|
|
$is_valid = $this->validate($data);
|
|
|
|
if ($is_valid) {
|
|
if (!isset($data['customer_id'])) {
|
|
if ($customer_id) {
|
|
$data['customer_id'] = $customer_id;
|
|
$this->customer_id = $customer_id;
|
|
} else if ($this->customer_id) {
|
|
$data['customer_id'] = $this->customer_id;
|
|
} else {
|
|
$this->error = "no customer id";
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$sql = "INSERT INTO customer_addresses ";
|
|
$data_line = "";
|
|
$insert_line = "";
|
|
|
|
$first = true;
|
|
foreach ($data as $key => $value) {
|
|
if ($first) {
|
|
$first = false;
|
|
} else {
|
|
$data_line .= ", ";
|
|
$value_line .= ", ";
|
|
}
|
|
|
|
$data_line .= $key;
|
|
$value_line .= "'".$this->db->real_escape_string($value)."'";
|
|
}
|
|
|
|
$sql .= "(".$data_line.") ";
|
|
$sql .= " VALUES (".$value_line.")";
|
|
|
|
$result = $this->db->query($sql);
|
|
if ($this->db->error) {
|
|
$log_object = Logger::get_instance();
|
|
$log_object->info('Error in /core/customeraddress.php->create()', $this->db->error.', sql:'.$sql);
|
|
}
|
|
if ($result) {
|
|
$this->address_id = $this->db->insert_id;
|
|
|
|
return $this->address_id;
|
|
}
|
|
$this->error = "create failed";
|
|
|
|
return false;
|
|
}
|
|
$this->error = "invalid data";
|
|
|
|
return false;
|
|
}
|
|
$this->error = "no data";
|
|
|
|
return false;
|
|
} // end create
|
|
|
|
public function update($data, $address_id = false) {
|
|
if ($data) {
|
|
$is_valid = $this->validate($data);
|
|
|
|
if ($is_valid) {
|
|
if (isset($data['address'])) {
|
|
$this->address_id = $data['address'];
|
|
unset($data['address']);
|
|
}
|
|
|
|
$sql = "UPDATE customer_addresses SET ";
|
|
$size = count($data);
|
|
$i = 1;
|
|
|
|
foreach ($data as $key => $value) {
|
|
$sql .= $key."='".$this->db->real_escape_string($value)."'";
|
|
|
|
if ($i < $size) {
|
|
$sql .= ", ";
|
|
$i++;
|
|
}
|
|
}
|
|
|
|
if ($address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($address_id);
|
|
$this->address_id = $address_id;
|
|
} else if ($this->address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($this->address_id);
|
|
} else {
|
|
$this->error = "no address id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result) {
|
|
return $this->address_id;
|
|
}
|
|
$this->error = "update failed";
|
|
|
|
return false;
|
|
}
|
|
$this->error = "invalid data";
|
|
|
|
return false;
|
|
}
|
|
$this->error = "no data";
|
|
|
|
return false;
|
|
} // end update
|
|
|
|
public function enrich($obj) {
|
|
if ($obj->honorific == '0') {
|
|
$obj->honorific_text = 'Herr';
|
|
} else if ($obj->honorific == '1') {
|
|
$obj->honorific_text = 'Frau';
|
|
} else if ($obj->honorific == '2') {
|
|
$obj->honorific_text = 'Firma';
|
|
} else {
|
|
$obj->honorific_text = '';
|
|
}
|
|
|
|
return $obj;
|
|
} // end enrich
|
|
|
|
public function validate($data) {
|
|
$is_valid = true;
|
|
|
|
foreach ($this->table_fields as $field => $setting) {
|
|
if ($setting['required'] == true && (!isset($data[$field]) || $data[$field] == '')) {
|
|
$this->validation_errors[$field] = 'not_set';
|
|
$is_valid = false;
|
|
}
|
|
}
|
|
|
|
return $is_valid;
|
|
} // end validate
|
|
|
|
public function set_field_to($field, $value, $address_id = false) {
|
|
if (isset($this->table_fields[$field])) {
|
|
$sql = "UPDATE customer_addresses SET ".$field."='".$this->db->real_escape_string($value)."'";
|
|
|
|
if ($address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($address_id);
|
|
$this->address_id = $address_id;
|
|
} else if ($this->address_id) {
|
|
$sql .= " WHERE id=".$this->db->real_escape_string($this->address_id);
|
|
} else {
|
|
$this->error = "no address id";
|
|
|
|
return false;
|
|
}
|
|
|
|
$result = $this->db->query($sql);
|
|
|
|
if ($result) {
|
|
return $this->address_id;
|
|
}
|
|
$this->error = 'no result';
|
|
|
|
return false;
|
|
}
|
|
$this->error = 'unknown field';
|
|
|
|
return false;
|
|
} // end validate
|
|
|
|
public function check_data_diff($data, $address_id) {
|
|
// get old customer data
|
|
unset($data['customer_id']);
|
|
$address_data_old = $this->get_data_by_address_id($address_id);
|
|
|
|
include_once './core/main.class.php';
|
|
$country_object = new Country($this->base_object);
|
|
$countries = $country_object->get_all_names();
|
|
$field_data = array(
|
|
'company' => array('name' => 'Firma', 'values' => false),
|
|
'honorific' => array(
|
|
'name' => 'Anrede',
|
|
'values' => array(
|
|
'0' => 'Herr',
|
|
'1' => 'Frau',
|
|
'2' => 'Firma'
|
|
)
|
|
),
|
|
'firstname' => array('name' => 'Vorname', 'values' => false),
|
|
'surname' => array('name' => 'Nachname', 'values' => false),
|
|
'street' => array('name' => 'Strasse', 'values' => false),
|
|
'house_number' => array('name' => 'Hausnummer', 'values' => false),
|
|
'zip_code' => array('name' => 'PLZ', 'type' => false),
|
|
'city' => array('name' => 'Ort', 'values' => false),
|
|
'country' => array('name' => 'Land', 'values' => $countries)
|
|
);
|
|
|
|
// check new data
|
|
$diff_data = array();
|
|
foreach ($data as $key => $value) {
|
|
$value_old = $address_data_old->{$key};
|
|
if ($key != 'pass' && $value != $value_old) {
|
|
if ($field_data[$key]['values']) {
|
|
$diff_data[$field_data[$key]['name']] = array(
|
|
'old' => $field_data[$key]['values'][$value_old],
|
|
'new' => $field_data[$key]['values'][$value]
|
|
);
|
|
} else {
|
|
$diff_data[$field_data[$key]['name']] = array(
|
|
'old' => $value_old,
|
|
'new' => $value
|
|
);
|
|
}
|
|
//$log_object = Logger::get_instance();
|
|
//$log_object->info($key, $value.'---'.$value_old);
|
|
}
|
|
}
|
|
|
|
return $diff_data;
|
|
}
|
|
|
|
function make_diff_table($data) {
|
|
$html = '<table border="0" cellpadding="2" cellspacing="2">';
|
|
$html .= '<tr><th colspan="3">Adressenänderung</hd></tr><tr><th>Feld</th><th>Alter Wert</th><th>Neuer Wert</th></tr>';
|
|
foreach ($data as $key => $val) {
|
|
$html .= '<tr><td>'.$key.'</td><td>'.$val['old'].'</td><td>'.$val['new'].'</td></tr>';
|
|
}
|
|
$html .= '</table>';
|
|
return $html;
|
|
}
|
|
}
|
|
|
|
?>
|