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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
<?php
/**
* @version $Header: /cvsroot/bitweaver/_bit_contact/Address.php,v 1.5 2009/10/01 14:16:59 wjames5 Exp $
*
* Copyright ( c ) 2006 bitweaver.org
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
*
* @package contact
*/
/**
* required setup
*/
require_once( LIBERTY_PKG_PATH.'LibertyContent.php' ); // Address base class
/**
* @package contact
*/
class Address extends LibertyContent {
var $mAddressId;
function Address( $pAddressId ) {
LibertyContent::LibertyContent();
if( is_numeric( $pAddressId ) ) {
$this->mAddressId = $pAddressId;
}
}
/**
* Returns title of an Contact object
*
* @return string Text for the title description
*/
function isValid() {
return( !empty( $this->mAddressId ) && is_numeric( $this->mAddressId ) );
}
/**
* Load an Address content Item
*
* (Describe address object here )
*/
function load( $pAddressId, $pSecure = TRUE ) {
$ret = NULL;
if( is_numeric( $pAddressId ) && (!$pSecure || ($pSecure && $this->isValid())) ) {
$bindVars = array( $pAddressId );
$whereSql = '';
if( $pSecure ) {
$whereSql = " AND `customers_id`=?";
array_push( $bindVars, $this->mCustomerId );
}
$query = "SELECT * FROM `".BIT_DB_PREFIX."contact_address` WHERE `contact_address_id`=? $whereSql";
if( $rs = $this->mDb->query( $query, $bindVars ) ) {
$ret = $rs->fields;
}
}
return( $ret );
}
/**
* verify, clean up and prepare data to be stored
* @param $pParamHash all information that is being stored. will update $pParamHash by reference with fixed array of itmes
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
* @access private
**/
function verify( &$pParamHash, &$errorHash ) {
global $gBitUser;
if( empty( $pParamHash['customers_id'] ) || !is_numeric( $pParamHash['customers_id'] ) ) {
if( $this->isValid() ) {
$pParamHash['address_store']['customers_id'] = $this->mCustomerId;
} else {
$errorHash['customers_id'] = tra( 'Your must be registered to save addresses' );
}
} else {
$pParamHash['address_store']['customers_id'] = $pParamHash['customers_id'];
}
if( empty( $pParamHash['firstname'] ) || strlen( $pParamHash['firstname'] ) < ENTRY_FIRST_NAME_MIN_LENGTH ) {
$errorHash['firstname'] = tra( 'Your First Name must contain a minimum of ' . ENTRY_FIRST_NAME_MIN_LENGTH . ' characters.' );
} else {
$pParamHash['address_store']['entry_firstname'] = $pParamHash['firstname'];
}
if( empty( $pParamHash['lastname'] ) || strlen( $pParamHash['lastname'] ) < ENTRY_LAST_NAME_MIN_LENGTH ) {
$errorHash['lastname'] = tra( 'Your Last Name must contain a minimum of ' . ENTRY_LAST_NAME_MIN_LENGTH . ' characters.' );
} else {
$pParamHash['address_store']['entry_lastname'] = $pParamHash['lastname'];
}
if( empty( $pParamHash['street_address'] ) || strlen( $pParamHash['street_address'] ) < ENTRY_STREET_ADDRESS_MIN_LENGTH ) {
$errorHash['street_address'] = tra( 'Your Street Address must contain a minimum of ' . ENTRY_STREET_ADDRESS_MIN_LENGTH . ' characters.' );
} else {
$pParamHash['address_store']['entry_street_address'] = $pParamHash['street_address'];
}
if( empty( $pParamHash['postcode'] ) || strlen( $pParamHash['street_address'] ) < ENTRY_POSTCODE_MIN_LENGTH ) {
$errorHash['postcode'] = tra( 'Your Post Code must contain a minimum of ' . ENTRY_POSTCODE_MIN_LENGTH . ' characters.' );
} else {
$pParamHash['address_store']['entry_postcode'] = $pParamHash['postcode'];
}
if( empty( $pParamHash['city'] ) || strlen( $pParamHash['city'] ) < ENTRY_CITY_MIN_LENGTH ) {
$errorHash['city'] = tra( 'Your City must contain a minimum of ' . ENTRY_CITY_MIN_LENGTH . ' characters.' );
} else {
$pParamHash['address_store']['entry_city'] = $pParamHash['city'];
}
if( !empty( $pParamHash['telephone'] ) && strlen( $pParamHash['telephone'] ) < ENTRY_TELEPHONE_MIN_LENGTH ) {
$errorHash['telephone'] = tra( 'Your telephone number must contain a minimum of ' . ENTRY_TELEPHONE_MIN_LENGTH . ' characters.' );
} elseif( !empty( $pParamHash['telephone'] ) ) {
$pParamHash['address_store']['entry_telephone'] = $pParamHash['telephone'];
} else {
$pParamHash['address_store']['entry_telephone'] = NULL;
}
if( ACCOUNT_GENDER == 'true' && !empty( $pParamHash['gender'] ) ) {
$pParamHash['address_store']['entry_gender'] = $pParamHash['gender'];
}
if( ACCOUNT_COMPANY == 'true' && !empty( $pParamHash['company'] ) ) {
$pParamHash['address_store']['entry_company'] = $pParamHash['company'];
}
if( ACCOUNT_SUBURB == 'true' && !empty( $pParamHash['suburb'] ) ) {
$pParamHash['address_store']['entry_suburb'] = $pParamHash['suburb'];
}
if( empty( $pParamHash['country_id'] ) || !is_numeric( $pParamHash['country_id'] ) || ($pParamHash['country_id'] < 1) ) {
$errorHash['country_id'] = tra( 'You must select a country from the Countries pull down menu.' );
} else {
$pParamHash['address_store']['entry_country_id'] = $pParamHash['country_id'];
if (ACCOUNT_STATE == 'true') {
if( $this->getZoneCount( $pParamHash['country_id'] ) ) {
if( $zoneId = $this->getZoneId( $pParamHash['state'], $pParamHash['country_id'] ) ) {
$pParamHash['address_store']['entry_state'] = $pParamHash['state'];
$pParamHash['address_store']['entry_zone_id'] = $zoneId;
} else {
$errorHash['state'] = tra( 'Please select a state from the States pull down menu.' );
}
} elseif( empty( $pParamHash['state'] ) || strlen( $pParamHash['state'] ) < ENTRY_STATE_MIN_LENGTH ) {
$errorHash['state'] = tra( 'Your State must contain a minimum of ' . ENTRY_STATE_MIN_LENGTH . ' characters.' );
} else {
$pParamHash['address_store']['entry_state'] = $pParamHash['state'];
}
}
}
return( count( $errorHash ) == 0 );
}
/**
* Store an address
* @param $pParamHash contains all data to store the address
* @param $pParamHash[title] title for the address
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
**/
function store( &$pParamHash ) {
global $current_page_base, $language_page_directory, $template;
$directory_array = $template->get_template_part($language_page_directory, '/^'.$current_page_base . '/');
while(list ($key, $value) = each($directory_array)) {
require_once($language_page_directory . $value);
}
if( $this->verify( $pParamHash, $this->mErrors ) ) {
$process = true;
if( empty( $pParamHash['address'] ) ) {
$pParamHash['address'] = $this->mDb->GenID( 'contact_id_seq');
$this->mDb->associateInsert(`".BIT_DB_PREFIX."contact_address`, $pParamHash['address_store']);
} else {
if( !empty( $pParamHash['force_history'] ) || ( empty( $pParamHash['minor'] ) && !empty( $this->mInfo['version'] ) && $pParamHash['field_changed'] )) {
if( empty( $pParamHash['has_no_history'] ) ) {
$query = "insert into `".BIT_DB_PREFIX."tiki_history`( `page_id`, `version`, `last_modified`, `user_id`, `ip`, `comment`, `data`, `description`, `format_guid`) values(?,?,?,?,?,?,?,?,?)";
$result = $this->mDb->query( $query, array( $this->mPageId, (int)$this->mInfo['version'], (int)$this->mInfo['last_modified'] , $this->mInfo['modifier_user_id'], $this->mInfo['ip'], $this->mInfo['comment'], $this->mInfo['data'], $this->mInfo['description'], $this->mInfo['format_guid'] ) );
}
// $action = "Created";
// $mailEvents = 'wiki_page_changes';
}
$this->mDb->associateUpdate(`".BIT_DB_PREFIX."contact_address`, $pParamHash['address_store'], array( 'contact_address_id'=>$pParamHash['address'] ) );
}
if( !$this->getDefaultAddress() || !empty( $pParamHash['primary'] ) ) {
$this->setDefaultAddress( $pParamHash['address'] );
}
// process the selected shipping destination
}
return( count( $this->mErrors ) == 0 );
}
/* function getDefaultAddress() {
$ret = NULL;
if( $this->isValid() ) {
if( empty( $this->mInfo ) ) {
$this->load();
}
if( !empty( $this->mInfo['customers_default_address_id'] ) && $this->addressExists( $this->mInfo['customers_default_address_id'] ) ) {
$ret = $this->mInfo['customers_default_address_id'];
} elseif( !empty( $this->mInfo['customers_default_address_id'] ) ) {
// somehow we lost our default address - let's be sure to clean this up
$this->setDefaultAddress( NULL );
unset( $this->mInfo['customers_default_address_id'] );
}
}
return( $ret );
}
function setDefaultAddress( $pAddressId ) {
$ret = NULL;
if( $this->isValid() && ( is_numeric( $pAddressId ) || is_null( $pAddressId ) ) ) {
$query = "UPDATE " . TABLE_CUSTOMERS . " SET `customers_default_address_id`=? WHERE `customers_id`=?";
$this->mDb->query( $query, array( $pAddressId, $this->mCustomerId ) );
$this->mInfo['customers_default_address_id'] = $pAddressId;
$ret = TRUE;
}
return( $ret );
}
*/
/**
* Returns list of contract entries
*
* @param integer AddressId
* @return string Text for the title description
*/
function addressExists( $pAddressId ) {
global $gBitDb;
$ret = FALSE;
if( is_numeric( $pAddressId ) ) {
$query = "SELECT count(*) FROM `".BIT_DB_PREFIX."contact_address` WHERE `contact_address_id`=?";
$ret = $gBitDb->GetOne( $query, array( $pAddressId ) );
}
return $ret;
}
/**
* Returns list of contract entries
*
* @param integer AddressId
* @return string Text for the title description
*/
function isValidAddress( $pAddressId ) {
$ret = FALSE;
$errors = array();
if( !($ret = $this->verifyAddress( $pAddressId, $errors ) ) ) {
unset( $errors['customers_id'] );
unset( $errors['gender'] );
if( !count( $errors ) ) {
$ret = TRUE;
}
}
return $ret;
}
/**
* Returns list of contract entries
*
* @param integer AddressId
* @return string Text for the title description
*/
function isAddressOwner( $pAddressId ) {
$ret = FALSE;
if( is_numeric( $pAddressId ) ) {
$query = "select count(*) as `total` from `".BIT_DB_PREFIX."contact_address`
where `customers_id` = ? and `contact_address_id` = ?";
$ret = $this->mDb->getOne( $query, array( $this->mCustomerId, $pAddressId ) );
}
return $ret;
}
/**
* Returns list of addresses
*
* @param integer CustomerId
* @return array List of addresses for the supplied CustomerId
*/
function getAddresses( $pCustomerId ) {
$ret = NULL;
if( is_numeric( $pCustomerId ) ) {
$query = "select `contact_address_id`, `entry_firstname` as `firstname`, `entry_lastname` as `lastname`,
`entry_company` as `company`, `entry_street_address` as `street_address`,
`entry_suburb` as `suburb`, `entry_city` as `city`, `entry_postcode` as `postcode`,
`entry_state` as `state`, `entry_zone_id` as `zone_id`,
`entry_country_id` as `country_id`, c.*
from `".BIT_DB_PREFIX."contact_address` ab INNER JOIN `".BIT_DB_PREFIX."countries` c ON( ab.`entry_country_id`=c.`countries_id` )
where `customers_id` = ?";
if( $rs = $this->mDb->query( $query, array( $pCustomerId ) ) ) {
$ret = $rs->GetRows();
}
}
return $ret;
}
/**
* Returns list of zones for a selected CountryId
*
* @param integer CountryId
* @return array List of zone names
*/
function getCountryZones( $pCountryId ) {
global $gBitDb;
$ret = array();
if( is_numeric( $pCountryId ) ) {
$query = "SELECT `zone_name` from `".BIT_DB_PREFIX."zones` WHERE `zone_country_id` = ? ORDER BY `zone_name`";
if( $rs = $this->mDb->query($query, array( $pCountryId ) ) ) {
while (!$rs->EOF) {
$ret[] = array('id' => $rs->fields['zone_name'], 'text' => $rs->fields['zone_name']);
$rs->MoveNext();
}
}
}
return( $ret );
}
/**
* Returns list of zones for a selected CountryId
*
* @param integer CountryId
* @return integer Number of zones defined for the supplied CountryId
*/
function getZoneCount( $pCountryId ) {
$query = "SELECT count(*) as `total` from `".BIT_DB_PREFIX."zones` WHERE `zone_country_id` = ?";
return( $this->mDb->getOne( $query, array( $pCountryId ) ) );
}
/**
* Returns list of zones for a selected CountryId
*
* @param string Zone Name
* @param integer CountryId
* @return integer ZoneId for the zone name
*/
function getZoneId( $pZone, $pCountryId ) {
$zone_query = "SELECT distinct `zone_id`
FROM `".BIT_DB_PREFIX."zones`
WHERE `zone_country_id` = ? AND (UPPER(`zone_name`) = ? OR UPPER(`zone_code`) = ?)";
return( $this->mDb->getOne( $zone_query, array( $pCountryId, strtoupper( $pZone ), strtoupper( $pZone ) ) ) );
}
// ********* History functions for the address ********** //
/**
* Get count of the number of historic records for the page
* @return count
*/
function getHistoryCount() {
$ret = NULL;
if( $this->isValid() ) {
$query = "SELECT COUNT(*) AS `count`
FROM `".BIT_DB_PREFIX."tiki_history`
WHERE `page_id` = ?";
$rs = $this->mDb->query($query, array($this->mPageId));
$ret = $rs->fields['count'];
}
return $ret;
}
/**
* Get complete set of historical data in order to display a given wiki page version
* @param pExistsHash the hash that was returned by LibertyContent::pageExists
* @return array of mInfo data
*/
function getHistory( $pVersion=NULL, $pUserId=NULL, $pOffset = 0, $maxRecords = -1 ) {
$ret = NULL;
if( $this->isValid() ) {
global $gBitSystem;
$versionSql = '';
if( @BitBase::verifyId( $pUserId ) ) {
$bindVars = array( $pUserId );
$whereSql = ' th.`user_id`=? ';
} else {
$bindVars = array( $this->mPageId );
$whereSql = ' th.`page_id`=? ';
}
if( !empty( $pVersion ) ) {
array_push( $bindVars, $pVersion );
$versionSql = ' AND th.`version`=? ';
}
$query = "SELECT tc.`title`, th.*,
uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name,
uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name
FROM `".BIT_DB_PREFIX."tiki_history` th INNER JOIN `".BIT_DB_PREFIX."tiki_pages` tp ON (tp.`page_id` = th.`page_id`) INNER JOIN `".BIT_DB_PREFIX."tiki_content` tc ON (tc.`content_id` = tp.`content_id`)
LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON (uue.`user_id` = th.`user_id`)
LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON (uuc.`user_id` = tc.`user_id`)
WHERE $whereSql $versionSql order by th.`version` desc";
$result = $this->mDb->query( $query, $bindVars, $maxRecords, $pOffset );
$ret = array();
while( !$result->EOF ) {
$aux = $result->fields;
$aux['creator'] = (isset( $aux['creator_real_name'] ) ? $aux['creator_real_name'] : $aux['creator_user'] );
$aux['editor'] = (isset( $aux['modifier_real_name'] ) ? $aux['modifier_real_name'] : $aux['modifier_user'] );
array_push( $ret, $aux );
$result->MoveNext();
}
}
return $ret;
}
/**
* Roll back to a specific version of a page
* @param pVersion Version number to roll back to
* @param comment Comment text to be added to the action log
* @return TRUE if completed successfully
*/
function rollbackVersion( $pVersion, $comment = '' ) {
$ret = FALSE;
if( $this->isValid() ) {
global $gBitUser,$gBitSystem;
$this->mDb->StartTrans();
// JHT - cache invalidation appears to be handled by store function - so don't need to do it here
$query = "select *, `user_id` AS modifier_user_id, `data` AS `edit` from `".BIT_DB_PREFIX."tiki_history` where `page_id`=? and `version`=?";
$result = $this->mDb->query($query,array( $this->mPageId, $pVersion ) );
if( $result->numRows() ) {
$res = $result->fetchRow();
$res['comment'] = 'Rollback to version '.$pVersion.' by '.$gBitUser->getDisplayName();
// JHT 2005-06-19_15:22:18
// set ['force_history'] to
// make sure we don't destory current content without leaving a copy in history
// if rollback can destroy the current page version, it can be used
// maliciously
$res['force_history'] = 1;
// JHT 2005-10-16_22:21:10
// title must be set or store fails
// we use current page name
$res['title'] = $this->mPageName;
if( $this->store( $res ) ) {
$action = "Changed actual version to $pVersion";
$t = $gBitSystem->getUTCTime();
$query = "insert into `".BIT_DB_PREFIX."tiki_actionlog`(`action`,`page_id`,`last_modified`,`user_id`,`ip`,`comment`) values(?,?,?,?,?,?)";
$result = $this->mDb->query($query,array($action,$this->mPageId,$t,ROOT_USER_ID,$_SERVER["REMOTE_ADDR"],$comment));
$ret = TRUE;
}
$this->mDb->CompleteTrans();
} else {
$this->mDb->RollbackTrans();
}
}
return $ret;
}
/**
* Removes a specific version of a page
* @param pVersion Version number to roll back to
* @param comment Comment text to be added to the action log
* @return TRUE if completed successfully
*/
function expungeVersion( $pVersion=NULL, $comment = '' ) {
$ret = FALSE;
if( $this->isValid() ) {
$this->mDb->StartTrans();
$bindVars = array( $this->mPageId );
$versionSql = '';
if( $pVersion ) {
$versionSql = " and `version`=? ";
array_push( $bindVars, $pVersion );
}
$hasRows = $this->mDb->getOne( "SELECT COUNT(`version`) FROM `".BIT_DB_PREFIX."tiki_history` WHERE `page_id`=? $versionSql ", $bindVars );
$query = "delete from `".BIT_DB_PREFIX."tiki_history` where `page_id`=? $versionSql ";
$result = $this->mDb->query( $query, $bindVars );
if( $hasRows ) {
global $gBitSystem;
$action = "Removed version $pVersion";
$t = $gBitSystem->getUTCTime();
$query = "insert into `".BIT_DB_PREFIX."tiki_actionlog`(`action`,`page_id`,`last_modified`,`user_id`,`ip`,`comment`) values(?,?,?,?,?,?)";
$result = $this->mDb->query($query,array($action,$this->mPageId,$t,ROOT_USER_ID,$_SERVER["REMOTE_ADDR"],$comment));
$ret = TRUE;
}
$this->mDb->CompleteTrans();
}
return $ret;
}
}
?>
|