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