-
Notifications
You must be signed in to change notification settings - Fork 0
/
Citizen.php
645 lines (581 loc) · 24.8 KB
/
Citizen.php
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
<?php
/**
* @version $Header$
*
* 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 citizen
*/
/**
* required setup
*/
require_once( LIBERTY_PKG_PATH.'LibertyContent.php' ); // Citizen base class
require_once(NLPG_PKG_PATH.'lib/phpcoord-2.3.php' );
define( 'CITIZEN_CONTENT_TYPE_GUID', 'citizen' );
/**
* @package citizen
*/
class Citizen extends LibertyContent {
var $mCitizenId;
var $mParentId;
/**
* Constructor
*
* Build a Citizen object based on LibertyContent
* @param integer Citizen Id identifer
* @param integer Base content_id identifier
*/
function Citizen( $pCitizenId = NULL, $pContentId = NULL ) {
parent::__construct();
$this->registerContentType( CITIZEN_CONTENT_TYPE_GUID, array(
'content_type_guid' => CITIZEN_CONTENT_TYPE_GUID,
'content_name' => 'Citizen Entry',
'handler_class' => 'Citizen',
'handler_package' => 'citizen',
'handler_file' => 'Citizen.php',
'maintainer_url' => 'http://lsces.co.uk'
) );
$this->mCitizenId = (int)$pCitizenId;
if ( $pContentId > 9000000000 ) $pContentId = $pContentId - 9000000000;
$this->mContentId = (int)$pContentId;
$this->mContentTypeGuid = CITIZEN_CONTENT_TYPE_GUID;
// Permission setup
$this->mViewContentPerm = 'p_citizen_view';
$this->mCreateContentPerm = 'p_citizen_create';
$this->mUpdateContentPerm = 'p_citizen_update';
$this->mAdminContentPerm = 'p_citizen_admin';
}
/**
* Load a Citizen content Item
*
* (Describe Citizen object here )
*/
function load($pContentId = NULL) {
if ( $pContentId ) $this->mContentId = (int)$pContentId;
if( $this->verifyId( $this->mContentId ) ) {
$query = "select ci.*, a.*, n.*, p.*, lc.*,
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."citizen` ci
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = ci.`content_id` )
LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON (uue.`user_id` = lc.`modifier_user_id`)
LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON (uuc.`user_id` = lc.`user_id`)
LEFT JOIN `".BIT_DB_PREFIX."citizen_address` a ON a.usn = ci.usn
LEFT JOIN `".BIT_DB_PREFIX."nlpg_blpu` n ON n.`uprn` = ci.`nlpg`
LEFT JOIN `".BIT_DB_PREFIX."nlpg_lpi` p ON p.`uprn` = ci.`nlpg` AND p.`language` = 'ENG' AND p.`logical_status` = 1
WHERE ci.`content_id`=?";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
if ( $result && $result->numRows() ) {
$this->mInfo = $result->fields;
$this->mContentId = (int)$result->fields['content_id'];
$this->mCitizenId = (int)$result->fields['usn'];
$this->mParentId = (int)$result->fields['usn'];
$this->mCitizenName = $result->fields['title'];
$this->mInfo['creator'] = (isset( $result->fields['creator_real_name'] ) ? $result->fields['creator_real_name'] : $result->fields['creator_user'] );
$this->mInfo['editor'] = (isset( $result->fields['modifier_real_name'] ) ? $result->fields['modifier_real_name'] : $result->fields['modifier_user'] );
$this->mInfo['display_url'] = $this->getDisplayUrl();
$os1 = new OSRef($this->mInfo['x_coordinate'], $this->mInfo['y_coordinate']);
$ll1 = $os1->toLatLng();
$this->mInfo['prop_lat'] = $ll1->lat;
$this->mInfo['prop_lng'] = $ll1->lng;
}
}
LibertyContent::load();
return;
}
/**
* 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 ) {
// make sure we're all loaded up if everything is valid
if( $this->isValid() && empty( $this->mInfo ) ) {
$this->load( TRUE );
}
// It is possible a derived class set this to something different
if( empty( $pParamHash['content_type_guid'] ) ) {
$pParamHash['content_type_guid'] = $this->mContentTypeGuid;
}
if( !empty( $this->mContentId ) ) {
$pParamHash['content_id'] = $this->mContentId;
} else {
unset( $pParamHash['content_id'] );
}
if ( empty( $pParamHash['parent_id'] ) )
$pParamHash['parent_id'] = $this->mContentId;
// content store
// check for name issues, first truncate length if too long
if( empty( $pParamHash['surname'] ) || empty( $pParamHash['forename'] ) ) {
$this->mErrors['names'] = 'You must enter a forename and surname for this citizen.';
} else {
$pParamHash['title'] = substr( $pParamHash['prefix'].' '.$pParamHash['forename'].' '.$pParamHash['surname'].' '.$pParamHash['suffix'], 0, 160 );
$pParamHash['content_store']['title'] = $pParamHash['title'];
}
// Secondary store entries
$pParamHash['citizen_store']['prefix'] = $pParamHash['prefix'];
$pParamHash['citizen_store']['forename'] = $pParamHash['forename'];
$pParamHash['citizen_store']['surname'] = $pParamHash['surname'];
$pParamHash['citizen_store']['suffix'] = $pParamHash['suffix'];
$pParamHash['citizen_store']['organisation'] = $pParamHash['organisation'];
if ( !empty( $pParamHash['nino'] ) ) $pParamHash['citizen_store']['nino'] = $pParamHash['nino'];
if ( !empty( $pParamHash['dob'] ) ) $pParamHash['citizen_store']['dob'] = $pParamHash['dob'];
if ( !empty( $pParamHash['eighteenth'] ) ) $pParamHash['citizen_store']['eighteenth'] = $pParamHash['eighteenth'];
if ( !empty( $pParamHash['dod'] ) ) $pParamHash['citizen_store']['dod'] = $pParamHash['dod'];
return( count( $this->mErrors ) == 0 );
}
/**
* Store citizen data
* @param $pParamHash contains all data to store the citizen
* @param $pParamHash[title] title of the new citizen
* @param $pParamHash[edit] description of the citizen
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
**/
function store( &$pParamHash ) {
if( $this->verify( $pParamHash ) ) {
// Start a transaction wrapping the whole insert into liberty
$this->mDb->StartTrans();
if ( LibertyContent::store( $pParamHash ) ) {
$table = BIT_DB_PREFIX."citizen";
// mContentId will not be set until the secondary data has commited
if( $this->verifyId( $this->mCitizenId ) ) {
if( !empty( $pParamHash['citizen_store'] ) ) {
$result = $this->mDb->associateUpdate( $table, $pParamHash['citizen_store'], array( "content_id" => $this->mContentId ) );
}
} else {
$pParamHash['citizen_store']['content_id'] = $pParamHash['content_id'];
$pParamHash['citizen_store']['usn'] = $pParamHash['content_id'];
if( isset( $pParamHash['citizen_id'] ) && is_numeric( $pParamHash['citizen_id'] ) ) {
$pParamHash['citizen_store']['usn'] = $pParamHash['citizen_id'];
} else {
$pParamHash['citizen_store']['usn'] = $this->mDb->GenID( 'citizen_id_seq');
}
$pParamHash['citizen_store']['parent_id'] = $pParamHash['citizen_store']['content_id'];
$this->mCitizenId = $pParamHash['citizen_store']['content_id'];
$this->mParentId = $pParamHash['citizen_store']['parent_id'];
$this->mContentId = $pParamHash['content_id'];
$result = $this->mDb->associateInsert( $table, $pParamHash['citizen_store'] );
}
// load before completing transaction as firebird isolates results
$this->load();
$this->mDb->CompleteTrans();
} else {
$this->mDb->RollbackTrans();
$this->mErrors['store'] = 'Failed to store this citizen.';
}
}
return( count( $this->mErrors ) == 0 );
}
/**
* Delete content object and all related records
*/
function expunge()
{
$ret = FALSE;
if ($this->isValid() ) {
$this->mDb->StartTrans();
$query = "DELETE FROM `".BIT_DB_PREFIX."citizen` WHERE `content_id` = ?";
$result = $this->mDb->query($query, array($this->mContentId ) );
$query = "DELETE FROM `".BIT_DB_PREFIX."citizen_type_map` WHERE `content_id` = ?";
$result = $this->mDb->query($query, array($this->mContentId ) );
if (LibertyContent::expunge() ) {
$ret = TRUE;
$this->mDb->CompleteTrans();
} else {
$this->mDb->RollbackTrans();
}
}
return $ret;
}
/**
* Returns Request_URI to a Citizen content object
*
* @param string name of
* @param array different possibilities depending on derived class
* @return string the link to display the page.
*/
public static function getDisplayUrlFromHash( $pHash ) {
$ret = NULL;
if( !empty( $pHash['content_id'] ) ) {
$ret = CITIZEN_PKG_URL.'index.php?content_id='.$pHash['content_id'];
}
return $ret;
}
/**
* Returns HTML link to display a Citizen object
*
* @param string Not used ( generated locally )
* @param array mInfo style array of content information
* @return the link to display the page.
*/
function getDisplayLink( $pText, $aux ) {
if ( $this->mContentId != $aux['content_id'] ) $this->load($aux['content_id']);
if (empty($this->mInfo['content_id']) ) {
$ret = '<a href="'.$this->getDisplayUrlFromHash( $aux ).'">'.$aux['title'].'</a>';
} else {
$ret = '<a href="'.$this->getDisplayUrlFromHash( $aux ).'">'."Citizen - ".$this->mInfo['title'].'</a>';
}
return $ret;
}
/**
* Returns title of an Citizen object
*
* @param array mInfo style array of content information
* @return string Text for the title description
*/
public static function getTitleFromHash( $pHash ) {
$ret = NULL;
if( !empty( $pHash['title'] ) ) {
$ret = "Citizen - ".$pHash['title'];
} elseif( !empty( $pHash['content_name'] ) ) {
$ret = $pHash['content_name'];
}
return $ret;
}
/**
* Returns list of contract entries
*
* @param integer
* @param integer
* @param integer
* @return string Text for the title description
*/
function getList( &$pListHash ) {
LibertyContent::prepGetList( $pListHash );
$whereSql = $joinSql = $selectSql = '';
$bindVars = array();
array_push( $bindVars, $this->mContentTypeGuid );
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
if ( isset($pListHash['find']) ) {
$findesc = '%' . strtoupper( $pListHash['find'] ) . '%';
$whereSql .= " AND (UPPER(con.`SURNAME`) like ? or UPPER(con.`FORENAME`) like ?) ";
array_push( $bindVars, $findesc );
}
if ( isset($pListHash['add_sql']) ) {
$whereSql .= " AND $add_sql ";
}
$query = "SELECT con.*, lc.*,
uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name,
uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name $selectSql
FROM `".BIT_DB_PREFIX."citizen` ci
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = ci.`content_id` )
LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON (uue.`user_id` = lc.`modifier_user_id`)
LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON (uuc.`user_id` = lc.`user_id`)
$joinSql
WHERE lc.`content_type_guid`=? $whereSql
order by ".$this->mDb->convertSortmode( $pListHash['sort_mode'] );
$query_cant = "SELECT COUNT(lc.`content_id`) FROM `".BIT_DB_PREFIX."citizen` ci
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = ci.`content_id` )
$joinSql
WHERE lc.`content_type_guid`=? $whereSql";
$ret = array();
$this->mDb->StartTrans();
$result = $this->mDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
$cant = $this->mDb->getOne( $query_cant, $bindVars );
$this->mDb->CompleteTrans();
while ($res = $result->fetchRow()) {
$res['citizen_url'] = $this->getDisplayUrlFromHash( $res );
$ret[] = $res;
}
$pListHash['cant'] = $cant;
LibertyContent::postGetList( $pListHash );
return $ret;
}
/**
* Returns titles of the citizen type table
*
* @return array List of citizen type names from the citizen mamanger in alphabetical order
*/
function getCitizenTypeList() {
$query = "SELECT `type_name` FROM `citizen_type`
ORDER BY `type_name`";
$result = $this->mDb->query($query);
$ret = array();
while ($res = $result->fetchRow()) {
$ret[] = trim($res["type_name"]);
}
return $ret;
}
/**
* GoldenRecordLoad( $data );
* Rother golden file import
*/
function GoldenRecordLoad( &$data ) {
$table = BIT_DB_PREFIX."citizen";
$atable = BIT_DB_PREFIX."citizen_address";
$usn = 9000000000 + $data[0];
$pDataHash['citizen_store']['content_id'] = $data[0];
$pDataHash['citizen_store']['usn'] = $usn;
$pDataHash['address_store']['content_id'] = $data[0];
$pDataHash['address_store']['usn'] = $usn;
$pDataHash['citizen_store']['uprn'] = $data[1];
$pDataHash['address_store']['uprn'] = $data[1];
$pDataHash['citizen_store']['prefix'] = $data[2];
$pDataHash['citizen_store']['forename'] = $data[3];
$pDataHash['citizen_store']['surname'] = $data[4];
$pDataHash['citizen_store']['organisation'] = $data[5];
$pDataHash['citizen_store']['opfl'] = $data[6];
$pDataHash['citizen_store']['nino'] = $data[7];
if ( $data[8] <> '' ) $date = substr($data[8], 6, 4 ).'-'.substr($data[8], 3, 2 ).'-'.substr($data[8], 0, 2 );
else $date = NULL;
$pDataHash['citizen_store']['eighteenth'] = $date;
if ( $data[9] <> '' ) $date = substr($data[9], 6, 4 ).'-'.substr($data[9], 3, 2 ).'-'.substr($data[9], 0, 2 );
else $date = NULL;
$pDataHash['citizen_store']['dob'] = $date;
$pDataHash['citizen_store']['ctax'] = $data[10];
$pDataHash['address_store']['organisation'] = $data[11];
$pDataHash['address_store']['sao'] = $data[12];
$pDataHash['address_store']['pao'] = $data[13];
$pDataHash['address_store']['number'] = $data[14];
$pDataHash['address_store']['street'] = $data[15];
$pDataHash['address_store']['locality'] = $data[16];
$pDataHash['address_store']['town'] = $data[17];
$pDataHash['address_store']['county'] = $data[18];
$pDataHash['address_store']['postcode'] = substr($data[19], 0, 4).' '.substr($data[19], 4, 3);
$pDataHash['citizen_store']['nlpg'] = $data[20];
if ( $data[21] <> '' ) $date = substr($data[21], 6, 4 ).'-'.substr($data[21], 3, 2 ).'-'.substr($data[21], 0, 2 );
else $date[21] = null;
$pDataHash['citizen_store']['last_update_date'] = $date;
$query_cant = "SELECT COUNT(ci.`content_id`) FROM `".BIT_DB_PREFIX."citizen` ci
WHERE ci.`usn`=?";
$cant = $this->mDb->getOne( $query_cant, Array( 9000000000 + $data[0] ) );
if ( $cant > 0 ) {
$query_cant = "SELECT * FROM `".BIT_DB_PREFIX."citizen` ci
WHERE con.`usn`=?";
$current = $this->mDb->query( $query_cant, Array( 9000000000 + $data[0] ) );
$cfields = $current->fetchRow();
$save = false;
if ( $pDataHash['citizen_store']['uprn'] <> $cfields['uprn'] ) $save = true;
if ( $pDataHash['citizen_store']['nlpg'] <> $cfields['nlpg'] ) $save = true;
if ( $pDataHash['citizen_store']['prefix'] <> $cfields['prefix'] ) $save = true;
if ( $pDataHash['citizen_store']['forename'] <> $cfields['forename'] ) $save = true;
if ( $pDataHash['citizen_store']['surname'] <> $cfields['surname'] ) $save = true;
if ( $pDataHash['citizen_store']['organisation'] <> $cfields['organisation'] ) $save = true;
if ( $pDataHash['citizen_store']['opfl'] <> $cfields['opfl'] ) $save = true;
if ( $pDataHash['citizen_store']['nino'] <> $cfields['nino'] ) $save = true;
if ( $pDataHash['citizen_store']['ctax'] <> $cfields['ctax'] ) $save = true;
if ( $pDataHash['citizen_store']['dob'] && $pDataHash['citizen_store']['dob'] <> $cfields['dob'] )
{ $save = true;
}
if ( $pDataHash['citizen_store']['eighteenth'] && $pDataHash['citizen_store']['eighteenth'] <> $cfields['eighteenth'] ) $save = true;
if ( $pDataHash['citizen_store']['last_update_date'] && $pDataHash['citizen_store']['last_update_date'] <> $cfields['last_update_date'] ) $save = true;
if ( $save ) {
// $pDataHash['citizen_store']['last_update_date'] = $this->mDb->now();
$result = $this->mDb->associateUpdate( $table, $pDataHash['citizen_store'], array( "usn" => $usn ) );
}
$save = false;
if ( $pDataHash['address_store']['uprn'] <> $cfields['uprn'] ) $save = true;
if ( $pDataHash['address_store']['organisation'] <> $cfields['organisation'] ) $save = true;
if ( $pDataHash['address_store']['sao'] <> $cfields['sao'] ) $save = true;
if ( $pDataHash['address_store']['pao'] <> $cfields['pao'] ) $save = true;
if ( $pDataHash['address_store']['number'] <> $cfields['number'] ) $save = true;
if ( $pDataHash['address_store']['street'] <> $cfields['street'] ) $save = true;
if ( $pDataHash['address_store']['locality'] <> $cfields['locality'] ) $save = true;
if ( $pDataHash['address_store']['town'] <> $cfields['town'] ) $save = true;
if ( $pDataHash['address_store']['county'] <> $cfields['county'] ) $save = true;
if ( $pDataHash['address_store']['postcode'] <> $cfields['postcode'] ) $save = true;
if ( $save ) {
$result = $this->mDb->associateUpdate( $atable, $pDataHash['address_store'], array( "usn" => $usn ) );
}
} else {
$this->mDb->StartTrans();
$this->mContentId = 0;
$pDataHash['content_id'] = 0;
if ( LibertyContent::store( $pDataHash ) ) {
$pDataHash['citizen_store']['content_id'] = $pDataHash['content_id'];
$pDataHash['address_store']['content_id'] = $pDataHash['content_id'];
$result = $this->mDb->associateInsert( $table, $pDataHash['citizen_store'] );
$result = $this->mDb->associateInsert( $atable, $pDataHash['address_store'] );
$this->mDb->CompleteTrans();
} else {
$this->mDb->RollbackTrans();
$this->mErrors['store'] = 'Failed to store this citizen.';
}
}
return( count( $this->mErrors ) == 0 );
}
/**
* GoldenXrefRecordLoad( $data );
* Rother golden xref file import
*/
function GoldenXrefRecordLoad( &$data ) {
$table = BIT_DB_PREFIX."citizen_xref";
$pDataHash['data_store']['content_id'] = $data[0];
$pDataHash['data_store']['xref_key'] = $data[0];
$pDataHash['data_store']['source'] = $data[1];
$pDataHash['data_store']['cross_reference'] = $data[2];
$pDataHash['data_store']['data'] = $data[3];
$pDataHash['data_store']['start_date'] = $this->mDb->NOW();
$pDataHash['data_store']['last_update_date'] = $this->mDb->NOW();
$pDataHash['data_store']['entry_date'] = $this->mDb->NOW();
$result = $this->mDb->associateInsert( $table, $pDataHash['data_store'] );
}
/**
* Delete golden object and all related records
*/
function GoldenExpunge()
{
$ret = FALSE;
$query = "DELETE FROM `".BIT_DB_PREFIX."citizen`";
$result = $this->mDb->query( $query );
$query = "DELETE FROM `".BIT_DB_PREFIX."citizen_address`";
$result = $this->mDb->query( $query );
$query = "DELETE FROM `".BIT_DB_PREFIX."citizen_xref`";
$result = $this->mDb->query( $query );
return $ret;
}
/**
* getCitizenList( &$pParamHash );
* Get list of citizen records
*/
function getCitizenList( &$pParamHash ) {
global $gBitSystem, $gBitUser;
if ( empty( $pParamHash['sort_mode'] ) ) {
if ( empty( $_REQUEST["sort_mode"] ) ) {
$pParamHash['sort_mode'] = 'surname_asc';
} else {
$pParamHash['sort_mode'] = $_REQUEST['sort_mode'];
}
}
LibertyContent::prepGetList( $pParamHash );
$findSql = '';
$selectSql = '';
$joinSql = '';
$whereSql = '';
$bindVars = array();
$type = 'surname';
// this will set $find, $sort_mode, $max_records and $offset
extract( $pParamHash );
if( isset( $find_org ) and is_string( $find_org ) and $find_org <> '' ) {
$whereSql .= " AND UPPER( ci.`organisation` ) like ? ";
$bindVars[] = '%' . strtoupper( $find_org ). '%';
$type = 'organisation';
$pParamHash["listInfo"]["ihash"]["find_org"] = $find_org;
}
if( isset( $find_name ) and is_string( $find_name ) and $find_name <> '' ) {
$split = preg_split('|[,. ]|', $find_name, 2);
$whereSql .= " AND UPPER( ci.`surname` ) STARTING ? ";
$bindVars[] = strtoupper( $split[0] );
if ( array_key_exists( 1, $split ) ) {
$split[1] = trim( $split[1] );
$whereSql .= " AND UPPER( ci.`forename` ) STARTING ? ";
$bindVars[] = strtoupper( $split[1] );
}
$pParamHash["listInfo"]["ihash"]["find_name"] = $find_name;
}
if( isset( $find_street ) and is_string( $find_street ) and $find_street <> '' ) {
$whereSql .= " AND UPPER( a.`street` ) like ? ";
$bindVars[] = '%' . strtoupper( $find_street ). '%';
$pParamHash["listInfo"]["ihash"]["find_street"] = $find_street;
}
if( isset( $find_org ) and is_string( $find_postcode ) and $find_postcode <> '' ) {
$whereSql .= " AND UPPER( `a.postcode` ) LIKE ? ";
$bindVars[] = '%' . strtoupper( $find_postcode ). '%';
$pParamHash["listInfo"]["ihash"]["find_postcode"] = $find_postcode;
}
$query = "SELECT ci.*, a.UPRN, a.POSTCODE, a.SAO, a.PAO, a.NUMBER, a.STREET, a.LOCALITY, a.TOWN, a.COUNTY, ci.parent_id as uprn,
(SELECT COUNT(*) FROM `".BIT_DB_PREFIX."citizen_xref` x WHERE x.content_id = ci.content_id ) AS links,
(SELECT COUNT(*) FROM `".BIT_DB_PREFIX."task_ticket` e WHERE e.usn = ci.usn ) AS enquiries $selectSql
FROM `".BIT_DB_PREFIX."citizen` ci
LEFT JOIN `".BIT_DB_PREFIX."citizen_address` a ON a.content_id = ci.content_id $findSql
$joinSql
WHERE ci.`".$type."` <> '' $whereSql ORDER BY ".$this->mDb->convertSortmode( $sort_mode );
$query_cant = "SELECT COUNT( * )
FROM `".BIT_DB_PREFIX."citizen` ci
LEFT JOIN `".BIT_DB_PREFIX."citizen_address` a ON a.content_id = ci.content_id $findSql
$joinSql WHERE ci.`".$type."` <> '' $whereSql ";
// INNER JOIN `".BIT_DB_PREFIX."citizen_address` a ON a.content_id = ci.content_id
$result = $this->mDb->query( $query, $bindVars, $max_records, $offset );
$ret = array();
while( $res = $result->fetchRow() ) {
if (!empty($parse_split)) {
$res = array_merge($this->parseSplit($res), $res);
}
$ret[] = $res;
}
$pParamHash["cant"] = $this->mDb->getOne( $query_cant, $bindVars );
LibertyContent::postGetList( $pParamHash );
return $ret;
}
/**
* loadCitizen( &$pParamHash );
* Get citizen record
*/
function loadCitizen( &$pParamHash = NULL ) {
if( $this->isValid() ) {
$sql = "SELECT ci.*, a.*, n.*, p.*
FROM `".BIT_DB_PREFIX."citizen` ci
LEFT JOIN `".BIT_DB_PREFIX."citizen_address` a ON a.usn = ci.usn
LEFT JOIN `".BIT_DB_PREFIX."nlpg_blpu` n ON n.`uprn` = ci.`nlpg`
LEFT JOIN `".BIT_DB_PREFIX."nlpg_lpi` p ON p.`uprn` = ci.`nlpg` AND p.`language` = 'ENG' AND p.`logical_status` = 1
WHERE ci.`content_id` = ?";
if( $rs = $this->mDb->query( $sql, array( $this->mContentId ) ) ) {
if( $this->mInfo = $rs->fields ) {
/* if( $this->mInfo['local_custodian_code'] == 0 ) {
global $gBitSystem;
$gBitSystem->fatalError( tra( 'You do not have permission to access this client record' ), 'error.tpl', tra( 'Permission denied.' ) );
}
*/
$sql = "SELECT x.`last_update_date`, x.`source`, x.`cross_reference`
FROM `".BIT_DB_PREFIX."citizen_xref` x
WHERE x.content_id = ?";
/* Link to legacy system
CASE
WHEN x.`source` = 'POSTFIELD' THEN (SELECT `USN` FROM `".BIT_DB_PREFIX."caller` c WHERE ci.`caller_id` = x.`cross_reference`)
ELSE '' END AS USN
*/
$result = $this->mDb->query( $sql, array( $this->mContentId ) );
while( $res = $result->fetchRow() ) {
$this->mInfo['xref'][] = $res;
if ( $res['source'] == 'POSTFIELD' ) $ticket[] = $res['cross_reference'];
}
if ( isset( $ticket ) )
{ $sql = "SELECT t.* FROM `".BIT_DB_PREFIX."task_ticket` t
WHERE t.caller_id IN(". implode(',', array_fill(0, count($ticket), '?')) ." )";
$result = $this->mDb->query( $sql, $ticket );
while( $res = $result->fetchRow() ) {
$this->mInfo['tickets'][] = $res;
}
}
$os1 = new OSRef($this->mInfo['x_coordinate'], $this->mInfo['y_coordinate']);
$ll1 = $os1->toLatLng();
$this->mInfo['prop_lat'] = $ll1->lat;
$this->mInfo['prop_lng'] = $ll1->lng;
// $this->mInfo['display_usrn'] = $this->getUsrnEntryUrl( $this->mInfo['usrn'] );
// $this->mInfo['display_uprn'] = $this->getUprnEntryUrl( $this->mInfo['uprn'] );
//vd($this->mInfo);
} else {
global $gBitSystem;
$gBitSystem->fatalError( tra( 'Client record does not exist' ), 'error.tpl', tra( 'Not found.' ) );
}
}
}
return( count( $this->mInfo ) );
}
/**
* getXrefList( &$pParamHash );
* Get list of xref records for this citizen record
*/
function loadXrefList() {
if( $this->isValid() && empty( $this->mInfo['xref'] ) ) {
$sql = "SELECT x.`last_update_date`, x.`source`, x.`cross_reference`
FROM `".BIT_DB_PREFIX."citizen_xref` x
WHERE x.content_id = ?";
$result = $this->mDb->query( $sql, array( $this->mContentId ) );
while( $res = $result->fetchRow() ) {
$this->mInfo['xref'][] = $res;
if ( $res['source'] == 'POSTFIELD' ) $caller[] = $res['cross_reference'];
}
$sql = "SELECT t.* FROM `".BIT_DB_PREFIX."task_ticket` t
WHERE t.usn = ?";
$result = $this->mDb->query( $sql, array( '9000000001' ) ); //$this->mCitizenId ) );
while( $res = $result->fetchRow() ) {
$this->mInfo['tickets'][] = $res;
}
}
}
}
?>