-
Notifications
You must be signed in to change notification settings - Fork 1
/
uc_member_pricing.api.inc
172 lines (136 loc) · 4.38 KB
/
uc_member_pricing.api.inc
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
<?php
//$Id: uc_member_pricing.api.inc,v 1.1 2009/11/30 17:23:19 awebb Exp $
//------------------------------------------------------------------------------
// Database manipulation functions
//------------------------------------------------------------------------------
function uc_member_pricing_select_rules( $conditions, $force_array = FALSE ) {
$where = array();
$where_args = array();
if ( is_array( $conditions ) && count( $conditions ) ) {
foreach ( $conditions as $key => $value ) {
switch ( $key ) {
case 'ppid':
case 'nid':
case 'xid':
$where[] = "$key = %d";
break;
case 'type':
case 'price':
$where[] = "$key = '%s'";
break;
default:
continue;
}
$where_args[] = $value;
}
}
$pricing_rules = array();
$results = db_query( 'SELECT * FROM {uc_member_prices}'
. ( count( $where ) ? ' WHERE ' . join( ' AND ', $where )
: '' ),
$where_args );
while ( $rule = db_fetch_array( $results ) ) {
$pricing_rules[] = $rule;
}
if ( ! $force_array && count( $pricing_rules ) <= 1 ) {
return ( count( $pricing_rules ) ? $pricing_rules[ 0 ] : FALSE );
}
usort( $pricing_rules, '_uc_member_pricing_sort_rules' );
return $pricing_rules;
}
//------------------------------------------------------------------------------
function uc_member_pricing_add_rule( $nid, $type, $xid, $price,
$skip_check = FALSE ) {
if ( ! $skip_check ) {
$pricing_rule = uc_member_pricing_select_rules(
array(
'nid' => $nid,
'type' => $type,
'xid' => $xid
)
);
if ( $pricing_rule ) {
$pricing_rule[ 'price' ] = $price;
return uc_member_pricing_save_rule( $pricing_rule, TRUE );
}
}
$pricing_rule = array(
'nid' => $nid,
'type' => $type,
'xid' => $xid,
'price' => $price
);
db_query(
'INSERT INTO {uc_member_prices}'
. ' ( nid, type, xid, price )'
. ' VALUES ( %d, "%s", %d, "%s" )',
$pricing_rule[ 'nid' ],
$pricing_rule[ 'type' ],
$pricing_rule[ 'xid' ],
$pricing_rule[ 'price' ]
);
$pricing_rule[ 'ppid' ] = db_last_insert_id( 'uc_member_prices', 'ppid' );
return $pricing_rule;
}
//------------------------------------------------------------------------------
function uc_member_pricing_save_rule( $pricing_rule, $skip_check = FALSE ) {
if ( ! $skip_check ) {
if ( $pricing_rule[ 'ppid' ] ) {
$price_rule = uc_member_pricing_select_rules(
array(
'ppid' => $pricing_rule[ 'ppid' ]
)
);
}
else {
$is_new = TRUE;
}
if ( $is_new || ! $price_rule ) {
return uc_member_pricing_add_rule(
$pricing_rule[ 'nid'],
$pricing_rule[ 'type' ],
$pricing_rule[ 'xid' ],
$pricing_rule[ 'price' ],
TRUE
);
}
}
db_query( 'UPDATE {uc_member_prices}'
. ' SET nid = %d, type = "%s", xid = %d, price = "%s"'
. ' WHERE ppid = %d',
$pricing_rule[ 'nid' ],
$pricing_rule[ 'type' ],
$pricing_rule[ 'xid' ],
$pricing_rule[ 'price' ],
$pricing_rule[ 'ppid' ]
);
return $pricing_rule;
}
//------------------------------------------------------------------------------
function uc_member_pricing_remove_rule( $pricing_rule ) {
if ( is_array( $pricing_rule ) ) {
$ppid = $pricing_rule[ 'ppid' ];
}
else {
$ppid = $pricing_rule;
}
db_query( 'DELETE FROM {uc_member_prices} WHERE ppid = %d', $ppid );
}
//------------------------------------------------------------------------------
function _uc_member_pricing_sort_rules( $rule1, $rule2 ) {
// First sort by node id.
if ( $rule1[ 'nid' ] != $rule2[ 'nid' ] ) {
return ( $rule1[ 'nid' ] < $rule2[ 'nid' ] ? -1 : 1 );
}
// Then sort by type.
if ( $rule1[ 'type' ] != $rule2[ 'type' ] ) {
return ( $rule1[ 'type' ] == UCMP_TYPE_ROLE ? -1 : 1 );
}
// Then sort by xid.
if ( $rule1[ 'xid' ] != $rule2[ 'xid' ] ) {
return ( $rule1[ 'xid' ] < $rule2[ 'xid' ] ? -1 : 1 );
}
// They are the same. ( This should not happen )
return 0;
}
//------------------------------------------------------------------------------