1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
<?php
MainFactory::load_class('CustomerWriterInterface');
class CustomerWriter implements CustomerWriterInterface
{
protected $db;
public function __construct(CI_DB_query_builder $dbQueryBuilder)
{
$this->db = $dbQueryBuilder;
}
public function write(CustomerInterface $customer)
{
if($customer->getId() == null)
{
$this->_insert($customer);
}
else
{
$this->_update($customer);
}
}
protected function _insert(CustomerInterface $customer)
{
$customerDataArray = array(
'account_type' => (int)$customer->isGuest(),
'customers_status' => (int)$customer->getStatusId(),
'customers_cid' => (string)$customer->getCustomerNumber(),
'customers_gender' => (string)$customer->getGender(),
'customers_firstname' => (string)$customer->getFirstname(),
'customers_lastname' => (string)$customer->getLastname(),
'customers_email_address' => (string)$customer->getEmail(),
'customers_password' => (string)$customer->getPassword(),
'customers_vat_id' => (string)$customer->getVatNumber(),
'customers_vat_id_status' => (string)$customer->getVatNumberStatus(),
'customers_dob' => (string)$customer->getDateOfBirth()->format('Y-m-d'),
'customers_telephone' => (string)$customer->getTelephoneNumber(),
'customers_fax' => (string)$customer->getFaxNumber(),
'customers_default_address_id' => (int)(string)$customer->getDefaultAddress()->getId(),
'customers_date_added' => date('Y-m-d H:i:s')
);
$this->db->insert('customers', $customerDataArray);
$customer->setId(new IdType($this->db->insert_id()));
$customerInfoDataArray = array(
'customers_info_id' => (string)$customer->getId(),
'customers_info_date_of_last_logon' => '1000-01-01 00:00:00',
'customers_info_number_of_logons' => '0',
'customers_info_date_account_created' => date('Y-m-d H:i:s'),
'customers_info_date_account_last_modified' => '1000-01-01 00:00:00'
);
$this->db->insert('customers_info', $customerInfoDataArray);
}
protected function _update(CustomerInterface $customer)
{
$customerId = $customer->getId();
$customerDataArray = array(
'account_type' => (int)$customer->isGuest(),
'customers_status' => (int)$customer->getStatusId(),
'customers_cid' => (string)$customer->getCustomerNumber(),
'customers_gender' => (string)$customer->getGender(),
'customers_firstname' => (string)$customer->getFirstname(),
'customers_lastname' => (string)$customer->getLastname(),
'customers_email_address' => (string)$customer->getEmail(),
'customers_password' => (string)$customer->getPassword(),
'customers_vat_id' => (string)$customer->getVatNumber(),
'customers_vat_id_status' => (string)$customer->getVatNumberStatus(),
'customers_dob' => (string)$customer->getDateOfBirth()->format('Y-m-d'),
'customers_telephone' => (string)$customer->getTelephoneNumber(),
'customers_fax' => (string)$customer->getFaxNumber(),
'customers_default_address_id' => (string)$customer->getDefaultAddress()->getId(),
'customers_last_modified' => date('Y-m-d H:i:s')
);
$this->db->update('customers', $customerDataArray, array('customers_id' => $customerId));
$customerInfoDataArray = array(
'customers_info_date_account_last_modified' => date('Y-m-d H:i:s')
);
$this->db->update('customers_info', $customerInfoDataArray, array('customers_info_id' => $customerId));
}
}