-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.karmabankmodel.php
164 lines (142 loc) · 5.08 KB
/
class.karmabankmodel.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
<?php if(!defined('APPLICATION')) exit();
class KarmaBankModel extends VanillaModel{
private $UserID;
private static $FC;
public function __construct($UserID) {
parent::__construct('KarmaBank');
$this->UserID=$UserID;
if(!self::$FC){
if(Gdn::Cache()->Type()==Gdn_Cache::CACHE_TYPE_NULL){
self::$FC = new Gdn_Filecache();
}else{
self::$FC = Gdn::Cache();
}
}
//self::$FC->AddContainer(array(Gdn_Cache::CONTAINER_LOCATION=>PATH_CACHE.'/karmacache'));
if(Gdn::Cache()->Type()==Gdn_Cache::CACHE_TYPE_FILE || Gdn::Cache()->Type()==Gdn_Cache::CACHE_TYPE_NULL)
self::$FC->AddContainer(array(Gdn_Cache::CONTAINER_LOCATION=>'./cache/karmacache'));
}
public function CheckForCollissions($Type,$Amount,$Value=0){
$Exists = self::$FC->Get($this->UserID.'|'.$Type.'|'.$Amount.'|'.$Value);
return $Exists;
}
public function Transaction($Type,$Amount,$Value=0,$RuleID=0){
if(!is_numeric($Amount))
return;
//Using file cache to psuedo-lock
$Lock = $this->UserID.'|'.$Type.'|'.$Amount.'|'.$Value;
self::$FC->Store($Lock,1,array(Gdn_Cache::FEATURE_EXPIRY => C('Plugins.KarmaBank.CacheExpire',1000)));
$Amount=number_format($Amount,2,'.','');
$CurrentBalance = $this->SQL
->Select('kb.*')
->From('KarmaBankBalance kb')
->Where('kb.UserID',$this->UserID)
->Get()
->FirstRow();
$this->SQL->Insert('KarmaBankTrans',
array(
'UserID'=>$this->UserID,
'Date'=>Gdn_Format::ToDateTime(),
'Amount'=>$Amount,
'Type'=>$Type
)
);
$TransID = $this->SQL->Database->Connection()->lastInsertId();
if($RuleID && $Value){
$TransTally = $this->SQL
->Select('kbtt.*')
->From('KarmaBankTransTally kbtt')
->Where(
array(
'UserID'=>$this->UserID,
'RuleID'=>$RuleID,
)
)
->Get();
if(!$TransTally->NumRows()){
$this->SQL->Insert('KarmaBankTransTally',
array(
'UserID'=>$this->UserID,
'RuleID'=>$RuleID,
'LastTally'=>$Value
)
);
}else{;
$this->SQL->Update('KarmaBankTransTally',
array(
'LastTally'=>$Value
),
array(
'UserID'=>$this->UserID,
'RuleID'=>$RuleID
)
)
->Put();
}
}
if(!$CurrentBalance){
$this->SQL->Insert('KarmaBankBalance',
array(
'UserID'=>$this->UserID,
'Balance'=>$Amount
)
);
}else{
$this->SQL->Update('KarmaBankBalance',
array(
'Balance'=>number_format($CurrentBalance->Balance,2,'.','')+$Amount,
'TransCount'=>$CurrentBalance->TransCount+1
),
array(
'UserID'=>$this->UserID
)
)
->Put();
}
self::$FC->Store($Lock,1,array(Gdn_Cache::FEATURE_EXPIRY => C('Plugins.KarmaBank.CacheExpire',-1)));
self::$FC->Remove($Lock);
return $TransID;
}
public function GetTransactionList($Limit=5, $Offset=0,$Order='desc'){
return $this->SQL
->Select('kbt.*')
->From('KarmaBankTrans kbt')
->Where('kbt.UserID',$this->UserID)
->OrderBy('kbt.TransID', strtolower($Order)=='desc'?'desc':'asc')
->Limit($Limit, $Offset)
->Get()
->Result();
}
public static function GetTransaction($TransID){
return Gdn::SQL()
->Select('kbt.*')
->From('KarmaBankTrans kbt')
->Where('kbt.TransID',$TransID)
->Get()
->FirstRow();
}
public function GetLastTransactionTally(){
return $this->SQL
->Select('kbtt.*')
->From('KarmaBankTransTally kbtt')
->Where('kbtt.UserID',$this->UserID)
->Get()
->Result();
}
public function GetBalance(){
return $this->SQL
->Select('kb.*')
->From('KarmaBankBalance kb')
->Where('kb.UserID',$this->UserID)
->Get()
->FirstRow();
}
public function GetBalances($UserIDs){
return $this->SQL
->Select('kb.*')
->From('KarmaBankBalance kb')
->WhereIn('kb.UserID',$UserIDs)
->Get()
->Result();
}
}