class-affiliatesexport.php
7.39 KB
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
<?php
/**
* class-affiliatesexport.php
*
* Copyright (c) Antonio Blanco http://www.blancoleon.com
*
* This code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Antonio Blanco
* @package affiliatesexport
* @since affiliatesexport 1.0.0
*/
/**
* Groups Mailchimp class
*/
class AffiliatesExport {
public static function init() {
}
public static function generate($charset = null ) {
global $affiliates_db;
header( 'Content-Description: File Transfer' );
if ( !empty( $charset ) ) {
header( 'Content-Type: text/plain; charset=' . $charset );
} else {
header( 'Content-Type: text/plain' );
}
$hora = date( 'Y-m-d-H-i-s', time() );
header( "Content-Disposition: attachment; filename=\"affiliates-export-$hora.txt\"" );
$from_date = get_option("affexp_from_date", null);
$thru_date = get_option("affexp_thru_date", null);
$aff_table = $affiliates_db->get_tablename( 'affiliates' );
$ref_table = $affiliates_db->get_tablename( 'referrals' );
$hits_table = $affiliates_db->get_tablename( 'hits' );
$status = "active";
$affiliates = $affiliates_db->get_objects( "SELECT * FROM " . $aff_table . " WHERE status='" . $status . "'");
if ( count($affiliates) ) {
$output = "";
$sep = "\t";
foreach ($affiliates as $affiliate) {
if ($affiliate->email !== null) {
// Referrals
$referrals = self::get_affiliate_referrals($affiliate->affiliate_id, $from_date, $thru_date);
$count = 0;
$amount = 0;
if ( count($referrals) > 0 ) {
foreach ( $referrals as $referral ) {
$count ++;
if ($referral->amount != null)
$amount += $referral->amount;
}
}
// Hits
$hits = self::get_affiliate_hits($affiliate->affiliate_id, $from_date, $thru_date);
$count_hits = 0;
if ( count($hits) > 0 ) {
$count_hits = $hits[0]->total;
}
$output .= $affiliate->affiliate_id . $sep . $affiliate->email . $sep;
$output .= $count . $sep . $amount . $sep . $count_hits;
$output .= "\n";
}
}
echo $output;
} else {
echo __("There isn't affiliates ", AFFILIATESEXPORT_DOMAIN);
}
}
/**
* Returns referrals for a given affiliate.
*
* @param int $affiliate_id the affiliate's id
* @param string $from_date optional from date
* @param string $thru_date optional thru date
* @return int number of hits
*/
public static function get_affiliate_referrals( $affiliate_id, $from_date = null , $thru_date = null, $order_by = 'datetime', $order = 'DESC', $limit = null ) {
global $wpdb;
$referrals_table = _affiliates_get_tablename( 'referrals' );
$where = " WHERE affiliate_id = %d";
$values = array( $affiliate_id );
switch( $order_by ) {
case 'date' :
$order_by = 'datetime';
break;
case 'amount' :
break;
default :
$order_by = 'datetime';
}
$order = strtoupper( $order );
switch( $order ) {
case 'ASC' :
case 'DESC' :
break;
default :
$order = 'DESC';
}
$order_query = ' ORDER BY ' . $order_by . ' ' . $order;
$limit_query = '';
if ( $limit !== null ) {
$limit = intval( $limit );
if ( $limit > 0 ) {
$limit_query = ' LIMIT ' . $limit;
}
}
if ( $from_date ) {
$from_date = date( 'Y-m-d', strtotime( $from_date ) );
}
if ( $thru_date ) {
$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
}
if ( $from_date && $thru_date ) {
$where .= " AND datetime >= %s AND datetime < %s ";
$values[] = $from_date;
$values[] = $thru_date;
} else if ( $from_date ) {
$where .= " AND datetime >= %s ";
$values[] = $from_date;
} else if ( $thru_date ) {
$where .= " AND datetime < %s ";
$values[] = $thru_date;
}
$status = get_option("affexp_status", "-");
if ( $status != "-" ) {
$where .= " AND status = %s ";
$values[] = $status;
} //else {
// $where .= " AND status IN ( %s, %s ) ";
// $values[] = AFFILIATES_REFERRAL_STATUS_ACCEPTED;
// $values[] = AFFILIATES_REFERRAL_STATUS_CLOSED;
//}
return $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM $referrals_table $where $order_query $limit_query",
$values
) );
}
public static function get_affiliate_hits( $affiliate_id, $from_date = null , $thru_date = null, $status = null, $order_by = 'datetime', $order = 'DESC', $limit = null ) {
global $wpdb;
$hits_table = _affiliates_get_tablename( 'hits' );
$where = " WHERE affiliate_id = " . $affiliate_id;
$values = array();
if ( $from_date ) {
$from_date = date( 'Y-m-d', strtotime( $from_date ) );
}
if ( $thru_date ) {
$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
}
if ( $from_date && $thru_date ) {
$where .= " AND datetime >= %s AND datetime < %s ";
$values[] = $from_date;
$values[] = $thru_date;
} else if ( $from_date ) {
$where .= " AND datetime >= %s ";
$values[] = $from_date;
} else if ( $thru_date ) {
$where .= " AND datetime < %s ";
$values[] = $thru_date;
}
//return $wpdb->get_results( "SELECT COUNT(*) as total FROM $hits_table $where");
return $wpdb->get_results( $wpdb->prepare(
"SELECT COUNT(*) as total FROM $hits_table $where",
$values
) );
}
/**
* Adjust from und until dates from UTZ to STZ and take into account the
* for option which will adjust the from date to that of the current
* day, the start of the week or the month, leaving the until date
* set to null.
*
* @param string $for "day", "week" or "month"
* @param string $from date/datetime
* @param string $until date/datetime
*/
private static function for_from_until( $for, &$from, &$until ) {
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-date-helper.php');
if ( $for === null ) {
if ( $from !== null ) {
$from = date( 'Y-m-d H:i:s', strtotime( DateHelper::u2s( $from ) ) );
}
if ( $until !== null ) {
$until = date( 'Y-m-d H:i:s', strtotime( DateHelper::u2s( $until ) ) );
}
} else {
$user_now = strtotime( DateHelper::s2u( date( 'Y-m-d H:i:s', time() ) ) );
$user_now_datetime = date( 'Y-m-d H:i:s', $user_now );
$user_daystart_datetime = date( 'Y-m-d', $user_now ) . ' 00:00:00';
$server_now_datetime = DateHelper::u2s( $user_now_datetime );
$server_user_daystart_datetime = DateHelper::u2s( $user_daystart_datetime );
$until = null;
switch ( strtolower( $for ) ) {
case 'day' :
$from = date( 'Y-m-d H:i:s', strtotime( $server_user_daystart_datetime ) );
break;
case 'week' :
$fdow = intval( get_option( 'start_of_week' ) );
$dow = intval( date( 'w', strtotime( $server_user_daystart_datetime ) ) );
$d = $dow - $fdow;
$from = date( 'Y-m-d H:i:s', mktime( 0, 0, 0, date( 'm', strtotime( $server_user_daystart_datetime ) ) , date( 'd', strtotime( $server_user_daystart_datetime ) )- $d, date( 'Y', strtotime( $server_user_daystart_datetime ) ) ) );
break;
case 'month' :
$from = date( 'Y-m', strtotime( $server_user_daystart_datetime ) ) . '-01 00:00:00';
break;
default :
$from = null;
}
}
}
}
AffiliatesExport::init();