1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('CustomerAddressWriterInterface');
13
14 15 16 17 18 19 20 21 22 23
24 class CustomerAddressWriter implements CustomerAddressWriterInterface
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 public function write(CustomerAddressInterface $address)
49 {
50 $id = $address->getId();
51 if(empty($id))
52 {
53 $this->_insert($address);
54 }
55 else
56 {
57 $this->_update($address);
58 }
59 }
60
61
62 63 64 65 66 67 68
69 protected function _insert(CustomerAddressInterface $address)
70 {
71
72 $countryId = ($address->getCountry() !== null) ? $address->getCountry()->getId() : null;
73 $zoneName = ($address->getCountryZone() !== null) ? $address->getCountryZone()->getName() : null;
74 $zoneId = ($address->getCountryZone() !== null) ? $address->getCountryZone()->getId() : null;
75
76 $addressDataArray = array(
77 'customers_id' => (int)(string)$address->getCustomerId(),
78 'entry_gender' => (string)$address->getGender(),
79 'entry_company' => (string)$address->getCompany(),
80 'entry_firstname' => (string)$address->getFirstname(),
81 'entry_lastname' => (string)$address->getLastname(),
82 'entry_street_address' => (string)$address->getStreet(),
83 'entry_suburb' => (string)$address->getSuburb(),
84 'entry_postcode' => (string)$address->getPostcode(),
85 'entry_city' => (string)$address->getCity(),
86 'entry_state' => (string)$zoneName,
87 'entry_country_id' => (int)$countryId,
88 'entry_zone_id' => (int)$zoneId,
89 'customer_b2b_status' => (int)(string)$address->getB2BStatus(),
90 'address_date_added' => date('Y-m-d'),
91 'address_last_modified' => date('Y-m-d')
92 );
93
94 $this->db->insert('address_book', $addressDataArray);
95 $address->setId(new IdType($this->db->insert_id()));
96
97 return $address;
98 }
99
100
101 102 103 104 105 106 107
108 protected function _update(CustomerAddressInterface $address)
109 {
110
111 $countryId = ($address->getCountry() !== null) ? $address->getCountry()->getId() : null;
112 $zoneName = ($address->getCountryZone() !== null) ? $address->getCountryZone()->getName() : null;
113 $zoneId = ($address->getCountryZone() !== null) ? $address->getCountryZone()->getId() : null;
114
115 $addressDataArray = array(
116 'customers_id' => (int)(string)$address->getCustomerId(),
117 'entry_gender' => (string)$address->getGender(),
118 'entry_company' => (string)$address->getCompany(),
119 'entry_firstname' => (string)$address->getFirstname(),
120 'entry_lastname' => (string)$address->getLastname(),
121 'entry_street_address' => (string)$address->getStreet(),
122 'entry_suburb' => (string)$address->getSuburb(),
123 'entry_postcode' => (string)$address->getPostcode(),
124 'entry_city' => (string)$address->getCity(),
125 'entry_state' => (string)$zoneName,
126 'entry_country_id' => (int)$countryId,
127 'entry_zone_id' => (int)$zoneId,
128 'customer_b2b_status' => (int)(string)$address->getB2BStatus(),
129 'address_last_modified' => date('Y-m-d')
130 );
131
132 $this->db->update('address_book', $addressDataArray,
133 array('address_book_id' => (int)(string)$address->getId()));
134
135 return $address;
136 }
137 }