-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
625 lines (549 loc) · 18.2 KB
/
User.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
<?php
class Pandamp_Modules_Identity_User_Model_User extends Zend_Db_Table_Abstract
implements Pandamp_BeanContext_Decoratable
{
protected $_name = 'KutuUser';
protected $_rowClass = 'Pandamp_Modules_Identity_User_Model_Row_User';
protected $_dependentTables = array('Pandamp_Modules_Identity_User_Model_UserDetail');
protected $_referenceMap = array(
'UserDetail' => array(
'columns' => 'uid',
'refTableClass' => 'Pandamp_Modules_Identity_User_Model_UserDetail',
'refColumns' => 'id'
)
);
protected $_schema = 'hid';
protected function _setupDatabaseAdapter()
{
$this->_db = Zend_Registry::get('db2');
parent::_setupDatabaseAdapter();
}
/**
* Adds an user Twitter account for the specified user with the specified data.
*
* @param integer $userId The userid this account will belong to
* @param array $data an assoc array with keys as fields and values as data
* to add
*
* @return integer 0 if the data wasn't added, otherwise the id of the newly added
* row
*/
public function addUserTwitter(Array $data)
{
$whiteList = array(
'username',
'name',
'password',
'tw_uid',
'registered_on'
);
$addData = array();
foreach ($data as $key => $value) {
if (in_array($key, $whiteList)) {
$addData[$key] = $value;
}
}
if (empty($addData)) {
return 0;
}
$id = $this->insert($addData);
if ((int)$id == 0) {
return $id;
}
return $id;
}
/**
* Returns the number of users that share the same username,
* in case the index on the username field is missing.
*
* @param string $userName
*
* @return integer
*/
public function getUserNameCount($userName)
{
if ($userName == "") {
return 0;
}
$select = $this->select()
->from($this, array(
'COUNT(id) as count_id'
))
->where('username = ?', $userName);
$row = $this->fetchRow($select);
return ($row !== null) ? $row->count_id : 0;
}
/**
* Returns the number of email users that share the same email,
* in case the index on the email user field is missing.
*
* @param string $email
*
* @return integer
*/
public function getEmailCount($email)
{
if ($email == "") {
return 0;
}
$select = $this->select()
->from($this, array(
'COUNT(id) as count_id'
))
->where('email = ?', $email);
$row = $this->fetchRow($select);
return ($row !== null) ? $row->count_id : 0;
}
/**
* Returs the row that matches both the username and the password.
*
* @param string $userName
* @param string $password
*
* @return Zend_Db_Table_Row
*/
public function getUserForUserNameCredentials($userName, $password)
{
if ($userName == "" || $password == "") {
return null;
}
/**
* @todo make sure username and password is a unique index
*/
$select = $this->select()
->from($this)
->where('username = ?', $userName)
->where('password=?', $password);
// $select = $select->__toString();
// print_r($select);exit();
$row = $this->fetchRow($select);
return $row;
}
/**
* Looks up the row with the facebook key $fb_uid, fetches the data and
* creates a new instance of Pandamp_User out of it or returns the data
* as fetched from the table.
*
* @param integer $fb_uid
*
*
* @return Pandamp_User or null if no data with the facebook key $fb_uid exists.
*
* @throws Pandamp_BeanContext_Exception The method will throw an exception
* of the type Pandamp_BeanContext_Exception if the BeanContext-object
* Pandamp_User could not be created automatically.
*/
public function getUserFromFacebook($fb_uid)
{
$fb_uid = (int)$fb_uid;
if ($fb_uid <= 0) {
return null;
}
$row = $this->fetchRow($this->select()->where('fb_uid=?', $fb_uid));
return $row;
}
/**
* Looks up the row with the twitter key $tw_uid, fetches the data and
* creates a new instance of Pandamp_User out of it or returns the data
* as fetched from the table.
*
* @param integer $tw_uid
*
*
* @return Pandamp_User or null if no data with the twitter key $tw_uid exists.
*
* @throws Pandamp_BeanContext_Exception The method will throw an exception
* of the type Pandamp_BeanContext_Exception if the BeanContext-object
* Pandamp_User could not be created automatically.
*/
public function getUserFromTwitter($tw_uid)
{
$tw_uid = (int)$tw_uid;
if ($tw_uid <= 0) {
return null;
}
$row = $this->fetchRow($this->select()->where('tw_uid=?', $tw_uid));
return $row;
}
/**
* Gets a user given his id.
*
* The id can be an internal id, or the FriendConnect id.
*
* @param string $id The id of the user.
* @param bool $isFCId True, if this id represents a FriendConnect id.
* Default value is False.
* @return User a User object or a NULL if the user cannot be found.
*/
public function getUserFromGoogleFriend($gfcId)
{
if ($gfcId == "") {
return null;
}
$row = $this->fetchRow($this->select()->where("gfcId='$gfcId'"));
return $this->convertRowToGoogleUser($row);
}
/**
* Converts a mysql_result row into a User object.
*
* @param resource $row The mysql result row fetched using mysql_fetch_assoc,
* representing a user.
* @return User A User object.
*/
private function convertRowToGoogleUser($row) {
$user = new Pandamp_Core_Panda_GoogleAuth();
$user->setId($row["id"]);
$user->setName($row["name"]);
$user->setPassword($row["password"]);
$user->setFCId($row["gfcId"]);
return $user;
}
/**
* Adds a new user.
*
* @param User $user The user object to be added. Id field, if present, is
* ignored.
* @return User The user object, with the id field populated.
*/
public function addUserGoogle($user)
{
$whiteList = array(
'username' => 'Google_'.$user->getFCId(),
'name' =>$user->getName(),
'gfcId' =>$user->getFCId(),
'registered_on' => new Zend_Db_Expr('NOW()')
);
$id = $this->insert($whiteList);
$user->setId($id);
return $user;
}
function fetchUser($order,$start,$end, $fields, $selectedRows, $where, $startdt, $enddt)
{
$sql = $this->_db->select()
->from(array('ku' => 'KutuUser'))
// ->joinLeft(array('kude' => 'KutuUserDetail'),
// 'ku.guid = kude.uid',array('idUser'=>'id','uidUser'=>'uid','packageId','promotionId','educationId','expenseId','paymentId','businessTypeId','periodeId','activationDate','isEmailSent','createdDate','isActive'))
->joinLeft(array('gag' => 'gacl_aro_groups'),
'ku.packageId = gag.id')
->joinLeft(array('kui' => 'KutuUserInvoice'),
'ku.kopel = kui.uid', array('kui.invoiceId', 'kui.expirationDate','kui.isPaid','DaysLeft' => 'DATEDIFF(NOW(),kui.expirationDate)'))
->joinLeft(array('kus' => 'KutuUserStatus'),
'ku.periodeId = kus.accountStatusId','kus.status')
->where('gag.value=?',"$where");
if ($startdt && $enddt)
{
$sql = $sql->where("kui.expirationDate BETWEEN '$startdt' AND '$enddt'");
}
if (isset($selectedRows))
{
if (isset($fields))
{
$i = 1;
foreach ($selectedRows as $selrows)
{
switch ($selrows) {
case 'guid':
$selrows = 'ku.'.$selrows;
break;
case 'kopel':
$selrows = 'ku.'.$selrows;
break;
case 'fullName':
$selrows = 'ku.'.$selrows;
break;
case 'username':
$selrows = 'ku.'.$selrows;
break;
case 'company':
$selrows = 'ku.'.$selrows;
break;
case 'createdDate':
$selrows = 'ku.'.$selrows;
break;
case 'expirationDate':
$selrows = 'kui.'.$selrows;
break;
case 'packageId':
$selrows = 'gag.value';
break;
case 'periodeId':
$selrows = 'kus.status';
break;
case 'paymentId':
$selrows = 'ku.'.$selrows;
break;
}
if ($i > 1)
{
if (preg_match('/%/',$fields))
$sql = $sql->orWhere($selrows.' like ?', "$fields");
else
$sql = $sql->orWhere($selrows.'= ?', "$fields");
}
else
{
if (preg_match('/%/',$fields))
$sql = $sql->where($selrows.' like ?', "$fields");
else
$sql = $sql->where($selrows.'= ?', "$fields");
}
$i++;
}
}
}
$sql = $sql->group('kopel');
$sql = $sql->order(array($order,'invoiceId DESC'))->limit($end, $start);
// $sql = $sql->group('kopel');
// $sql = $sql->order(array($order))->limitPage($start, $end);
// $sql = $sql->__toString();
// print_r($sql);exit();
$db = $this->_db->query($sql);
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
$data = array(
'table' => $this,
'data' => $dataFetch,
'rowClass' => $this->_rowClass,
'stored' => true
);
Zend_Loader::loadClass($this->_rowsetClass);
return new $this->_rowsetClass($data);
}
function fetchUserGroupFree($order,$start,$end, $fields, $selectedRows)
{
$sql = $this->_db->select()
->from(array('ku' => 'KutuUser'))
->joinLeft(array('gag' => 'gacl_aro_groups'),
'ku.packageId = gag.id')
->joinLeft(array('kus' => 'KutuUserStatus'),
'ku.periodeId = kus.accountStatusId','status')
->where('gag.value=?','member_gratis');
if (isset($selectedRows))
{
if (isset($fields))
{
$i = 1;
foreach ($selectedRows as $selrows)
{
switch ($selrows) {
case 'guid':
$selrows = 'ku.'.$selrows;
break;
case 'kopel':
$selrows = 'ku.'.$selrows;
break;
case 'fullName':
$selrows = 'ku.'.$selrows;
break;
case 'username':
$selrows = 'ku.'.$selrows;
break;
case 'company':
$selrows = 'ku.'.$selrows;
break;
case 'createdDate':
$selrows = 'ku.'.$selrows;
break;
case 'periodeId':
$selrows = 'kus.status';
break;
}
if ($i > 1)
{
if (preg_match('%',$fields))
$sql = $sql->orWhere($selrows.' like ?', "$fields");
else
$sql = $sql->orWhere($selrows.'= ?', "$fields");
}
else
{
if (preg_match('%',$fields))
$sql = $sql->where($selrows.' like ?', "$fields");
else
$sql = $sql->where($selrows.'= ?', "$fields");
}
$i++;
}
}
}
// $sql = $sql->group('uidUser');
$sql = $sql->order(array($order))->limit($end, $start);
// $sql = $sql->order(array($order))->limitPage($start, $end);
// $sql = $sql->__toString();
// print_r($sql);exit();
$db = $this->_db->query($sql);
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
$data = array(
'table' => $this,
'data' => $dataFetch,
'rowClass' => $this->_rowClass,
'stored' => true
);
Zend_Loader::loadClass($this->_rowsetClass);
return new $this->_rowsetClass($data);
}
function fetchUserGroupOther($order,$start,$end, $fields, $selectedRows)
{
$sql = $this->_db->select()
->from(array('ku' => 'KutuUser'))
->joinLeft(array('gag' => 'gacl_aro_groups'),
'ku.packageId = gag.id')
->joinLeft(array('kus' => 'KutuUserStatus'),
'ku.periodeId = kus.accountStatusId','status')
->where('gag.value NOT IN ("member_gratis","member_corporate","member_individual","member_bonus")');
if (isset($selectedRows))
{
if (isset($fields))
{
$i = 1;
foreach ($selectedRows as $selrows)
{
switch ($selrows) {
case 'guid':
$selrows = 'ku.'.$selrows;
break;
case 'kopel':
$selrows = 'ku.'.$selrows;
break;
case 'fullName':
$selrows = 'ku.'.$selrows;
break;
case 'username':
$selrows = 'ku.'.$selrows;
break;
case 'company':
$selrows = 'ku.'.$selrows;
break;
case 'createdDate':
$selrows = 'ku.'.$selrows;
break;
case 'periodeId':
$selrows = 'kus.status';
break;
}
if ($i > 1)
{
if (preg_match('%',$fields))
$sql = $sql->orWhere($selrows.' like ?', "$fields");
else
$sql = $sql->orWhere($selrows.'= ?', "$fields");
}
else
{
if (preg_match('%',$fields))
$sql = $sql->where($selrows.' like ?', "$fields");
else
$sql = $sql->where($selrows.'= ?', "$fields");
}
$i++;
}
}
}
// $sql = $sql->group('uidUser');
$sql = $sql->order(array($order))->limit($end, $start);
// $sql = $sql->order(array($order))->limitPage($start, $end);
// $sql = $sql->__toString();
// print_r($sql);exit();
$db = $this->_db->query($sql);
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
$data = array(
'table' => $this,
'data' => $dataFetch,
'rowClass' => $this->_rowClass,
'stored' => true
);
Zend_Loader::loadClass($this->_rowsetClass);
return new $this->_rowsetClass($data);
}
function fetchUserHistory($guid, $order,$start,$end)
{
$id = $this->fetchUserGroupHistory($guid);
$sql = $this->_db->select()
->from(array('ku' => 'KutuUser'))
->joinLeft(array('kude' => 'KutuUserDetail'),
'ku.guid = kude.uid',array('idUser'=>'id','uidUser'=>'uid','packageId','promotionId','educationId','expenseId','paymentId','businessTypeId','periodeId','activationDate','isEmailSent','createdDate','isActive'))
->joinLeft(array('gag' => 'gacl_aro_groups'),
'kude.packageId = gag.id')
->joinLeft(array('kui' => 'KutuUserInvoice'),
'ku.kopel = kui.uid', array('expirationDate','DaysLeft' => 'DATEDIFF(NOW(),kui.expirationDate)'))
->joinLeft(array('kus' => 'KutuUserStatus'),
'kude.periodeId = kus.accountStatusId','status');
$sql = $sql->where('kude.uid=?',"$guid");
// $sql = $sql->where('kude.id NOT IN ("'.$id.'")');
// $sql = $sql1->group('uidUser');
$sql = $sql->order(array($order,'idUser DESC'))->limit($end, $start);
// $sq = $sql->order(array($order))->limitPage($start, $end);
// $sql = $sql->__toString();
// print_r($sql);exit();
$db = $this->_db->query($sql);
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
$data = array(
'table' => $this,
'data' => $dataFetch,
'rowClass' => $this->_rowClass,
'stored' => true
);
Zend_Loader::loadClass($this->_rowsetClass);
return new $this->_rowsetClass($data);
}
function fetchUserGroupHistory($guid)
{
$db = $this->_db->query("SELECT id from KutuUserDetail WHERE uid='$guid' GROUP BY uid ORDER BY id DESC");
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
if ($dataFetch) return ($dataFetch[0]['id']);
}
function countUserGroup($where, $startdt, $enddt)
{
$sql = $this->_db->select()
->from(array('ku' => 'KutuUser'),array('count'=>'COUNT(*)'))
->joinLeft(array('gag' => 'gacl_aro_groups'),'ku.packageId = gag.id')
->joinLeft(array('kui' => 'KutuUserInvoice'),'ku.kopel = kui.uid', array('kui.expirationDate','kui.isPaid'))
->where('gag.value=?',$where);
if ($startdt && $enddt)
{
$sql = $sql->where("kui.expirationDate BETWEEN '$startdt' AND '$enddt'");
}
// $db = $this->_db->query
// ("Select count(*) count From KutuUser,gacl_aro_groups where KutuUser.packageId=gacl_aro_groups.id AND gacl_aro_groups.value='$where'");
// $dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
$db = $this->_db->query($sql);
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
return ($dataFetch[0]['count']);
}
function countUserGroupOther()
{
$sql = $this->_db->select()
->from(array('ku' => 'KutuUser'),'count(*) as count')
->joinLeft(array('gag' => 'gacl_aro_groups'),'ku.packageId = gag.id')
->joinLeft(array('kus' => 'KutuUserStatus'),'ku.periodeId = kus.accountStatusId','status')
->where('gag.value NOT IN ("member_gratis","member_corporate","member_individual","member_bonus")');
$db = $this->_db->query($sql);
$dataFetch = $db->fetchAll(Zend_Db::FETCH_ASSOC);
return ($dataFetch[0]['count']);
}
public function getUserCount($guid)
{
if ($guid == "") {
return 0;
}
$select = $this->select()
->from($this, array(
'COUNT(guid) as count_id'
))
->where('guid = ?', $guid);
$row = $this->fetchRow($select);
return ($row !== null) ? $row->count_id : 0;
}
// -------- interface Pandamp_BeanContext_Decoratable
public function getRepresentedEntity()
{
return 'Pandamp_Modules_Identity_User';
}
public function getDecoratableMethods()
{
return array(
'getUserForUserNameCredentials',
'getUserFromFacebook',
'getUserFromTwitter'
);
}
}