Quick Search:

View

Revision:

Diff

Diff from 1669 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/address_book.php

Annotated File View

hpdl
175
1 <?php
2 /*
3   $Id: address_book.php 166 2005-08-05 10:14:21 +0200 (Fr, 05 Aug 2005) hpdl $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1669
8   Copyright (c) 2007 osCommerce
hpdl
175
9
hpdl
1497
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
175
13 */
14
hpdl
1669
15 /**
16  * The osC_AddressBook class handles customer address book related functions
17  */
18
hpdl
175
19   class osC_AddressBook {
20
hpdl
1669
21 /**
22  * Returns the address book entries for the current customer
23  *
24  * @access public
25  * @return array
26  */
27
28     public static function &getListing() {
hpdl
175
29       global $osC_Database, $osC_Customer;
30
hpdl
829
31       $Qaddresses = $osC_Database->query('select ab.address_book_id, ab.entry_firstname as firstname, ab.entry_lastname as lastname, ab.entry_company as company, ab.entry_street_address as street_address, ab.entry_suburb as suburb, ab.entry_city as city, ab.entry_postcode as postcode, ab.entry_state as state, ab.entry_zone_id as zone_id, ab.entry_country_id as country_id, z.zone_code as zone_code, c.countries_name as country_title from :table_address_book ab left join :table_zones z on (ab.entry_zone_id = z.zone_id), :table_countries c where ab.customers_id = :customers_id and ab.entry_country_id = c.countries_id order by ab.entry_firstname, ab.entry_lastname');
hpdl
175
32       $Qaddresses->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
hpdl
829
33       $Qaddresses->bindTable(':table_zones', TABLE_ZONES);
34       $Qaddresses->bindTable(':table_countries', TABLE_COUNTRIES);
hpdl
184
35       $Qaddresses->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
36       $Qaddresses->execute();
37
38       return $Qaddresses;
39     }
40
hpdl
1669
41 /**
42  * Returns a specific address book entry for the current customer
43  *
44  * @param int $id The ID of the address book entry to return
45  * @access public
46  * @return array
47  */
48
49     public static function &getEntry($id) {
hpdl
175
50       global $osC_Database, $osC_Customer;
51
52       $Qentry = $osC_Database->query('select entry_gender, entry_company, entry_firstname, entry_lastname, entry_street_address, entry_suburb, entry_postcode, entry_city, entry_state, entry_zone_id, entry_country_id, entry_telephone, entry_fax from :table_address_book where address_book_id = :address_book_id and customers_id = :customers_id');
53       $Qentry->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
54       $Qentry->bindInt(':address_book_id', $id);
hpdl
184
55       $Qentry->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
56       $Qentry->execute();
57
58       return $Qentry;
59     }
60
hpdl
1669
61 /**
62  * Verify the address book entry belongs to the current customer
63  *
64  * @param int $id The ID of the address book entry to verify
65  * @access public
66  * @return boolean
67  */
68
69     public static function checkEntry($id) {
hpdl
175
70       global $osC_Database, $osC_Customer;
71
72       $Qentry = $osC_Database->query('select address_book_id from :table_address_book where address_book_id = :address_book_id and customers_id = :customers_id');
73       $Qentry->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
74       $Qentry->bindInt(':address_book_id', $id);
hpdl
184
75       $Qentry->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
76       $Qentry->execute();
77
hpdl
1669
78       return ( $Qentry->numberOfRows() === 1 );
hpdl
175
79     }
80
hpdl
1669
81 /**
82  * Return the number of address book entries the current customer has
83  *
84  * @access public
85  * @return integer
86  */
87
88     public static function numberOfEntries() {
hpdl
175
89       global $osC_Database, $osC_Customer;
hpdl
1422
90
hpdl
183
91       static $total_entries;
hpdl
175
92
hpdl
1669
93       if ( !isset($total_entries) ) {
hpdl
175
94         $Qaddresses = $osC_Database->query('select count(*) as total from :table_address_book where customers_id = :customers_id');
95         $Qaddresses->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
hpdl
184
96         $Qaddresses->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
97         $Qaddresses->execute();
98
99         $total_entries = $Qaddresses->valueInt('total');
100       }
101
102       return $total_entries;
103     }
104
hpdl
1669
105 /**
106  * Save an address book entry
107  *
108  * @param array $data An array containing the address book information
109  * @param int $id The ID of the address book entry to update (if this is not provided, a new address book entry is created)
110  * @access public
111  * @return boolean
112  */
113
114     public static function saveEntry($data, $id = '') {
hpdl
175
115       global $osC_Database, $osC_Customer;
116
117       $updated_record = false;
118
hpdl
1669
119       if ( is_numeric($id) ) {
hpdl
175
120         $Qab = $osC_Database->query('update :table_address_book set customers_id = :customers_id, entry_gender = :entry_gender, entry_company = :entry_company, entry_firstname = :entry_firstname, entry_lastname = :entry_lastname, entry_street_address = :entry_street_address, entry_suburb = :entry_suburb, entry_postcode = :entry_postcode, entry_city = :entry_city, entry_state = :entry_state, entry_country_id = :entry_country_id, entry_zone_id = :entry_zone_id, entry_telephone = :entry_telephone, entry_fax = :entry_fax where address_book_id = :address_book_id and customers_id = :customers_id');
121         $Qab->bindInt(':address_book_id', $id);
hpdl
184
122         $Qab->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
123       } else {
124         $Qab = $osC_Database->query('insert into :table_address_book (customers_id, entry_gender, entry_company, entry_firstname, entry_lastname, entry_street_address, entry_suburb, entry_postcode, entry_city, entry_state, entry_country_id, entry_zone_id, entry_telephone, entry_fax) values (:customers_id, :entry_gender, :entry_company, :entry_firstname, :entry_lastname, :entry_street_address, :entry_suburb, :entry_postcode, :entry_city, :entry_state, :entry_country_id, :entry_zone_id, :entry_telephone, :entry_fax)');
125       }
hpdl
1669
126
hpdl
175
127       $Qab->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
hpdl
184
128       $Qab->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
129       $Qab->bindValue(':entry_gender', ((ACCOUNT_GENDER > -1) && isset($data['gender']) && (($data['gender'] == 'm') || ($data['gender'] == 'f'))) ? $data['gender'] : '');
130       $Qab->bindValue(':entry_company', (ACCOUNT_COMPANY > -1) ? $data['company'] : '');
131       $Qab->bindValue(':entry_firstname', $data['firstname']);
132       $Qab->bindValue(':entry_lastname', $data['lastname']);
133       $Qab->bindValue(':entry_street_address', $data['street_address']);
134       $Qab->bindValue(':entry_suburb', (ACCOUNT_SUBURB > -1) ? $data['suburb'] : '');
hpdl
778
135       $Qab->bindValue(':entry_postcode', (ACCOUNT_POST_CODE > -1) ? $data['postcode'] : '');
hpdl
175
136       $Qab->bindValue(':entry_city', $data['city']);
137       $Qab->bindValue(':entry_state', (ACCOUNT_STATE > -1) ? ((isset($data['zone_id']) && ($data['zone_id'] > 0)) ? '' : $data['state']) : '');
138       $Qab->bindInt(':entry_country_id', $data['country']);
139       $Qab->bindInt(':entry_zone_id', (ACCOUNT_STATE > -1) ? ((isset($data['zone_id']) && ($data['zone_id'] > 0)) ? $data['zone_id'] : 0) : '');
140       $Qab->bindValue(':entry_telephone', (ACCOUNT_TELEPHONE > -1) ? $data['telephone'] : '');
141       $Qab->bindValue(':entry_fax', (ACCOUNT_FAX > -1) ? $data['fax'] : '');
142       $Qab->execute();
143
hpdl
1669
144       if ( $Qab->affectedRows() === 1 ) {
hpdl
175
145         $updated_record = true;
146       }
147
hpdl
1669
148       if ( isset($data['primary']) && ($data['primary'] === true) ) {
149         if ( !is_numeric($id) ) {
hpdl
175
150           $id = $osC_Database->nextID();
151         }
152
hpdl
1669
153         if ( osC_AddressBook::setPrimaryAddress($id) ) {
hpdl
175
154           $osC_Customer->setCountryID($data['country']);
155           $osC_Customer->setZoneID(($data['zone_id'] > 0) ? (int)$data['zone_id'] : '0');
156           $osC_Customer->setDefaultAddressID($id);
157
hpdl
1669
158           if ( $updated_record === false ) {
hpdl
175
159             $updated_record = true;
160           }
161         }
162       }
163
hpdl
1669
164       if ( $updated_record === true ) {
hpdl
175
165         return true;
166       }
167
168       return false;
169     }
170
hpdl
1669
171 /**
172  * Set the address book entry as the primary address for the current customer
173  *
174  * @param int $id The ID of the address book entry
175  * @access public
176  * @return boolean
177  */
178
179     public static function setPrimaryAddress($id) {
hpdl
175
180       global $osC_Database, $osC_Customer;
181
hpdl
1669
182       if ( is_numeric($id) && ($id > 0) ) {
hpdl
175
183         $Qupdate = $osC_Database->query('update :table_customers set customers_default_address_id = :customers_default_address_id where customers_id = :customers_id');
184         $Qupdate->bindTable(':table_customers', TABLE_CUSTOMERS);
185         $Qupdate->bindInt(':customers_default_address_id', $id);
hpdl
184
186         $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
187         $Qupdate->execute();
188
hpdl
1669
189         return ( $Qupdate->affectedRows() === 1 );
hpdl
175
190       }
191
192       return false;
193     }
194
hpdl
1669
195 /**
196  * Delete an address book entry
197  *
198  * @param int $id The ID of the address book entry to delete
199  * @access public
200  * @return boolean
201  */
202
203     public static function deleteEntry($id) {
hpdl
175
204       global $osC_Database, $osC_Customer;
205
206       $Qdelete = $osC_Database->query('delete from :table_address_book where address_book_id = :address_book_id and customers_id = :customers_id');
207       $Qdelete->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
208       $Qdelete->bindInt(':address_book_id', $id);
hpdl
184
209       $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
175
210       $Qdelete->execute();
211
hpdl
1669
212       return ( $Qdelete->affectedRows() === 1 );
hpdl
175
213     }
214   }
215 ?>