/[drupal]/contributions/uc_affiliate.admin.inc
ViewVC logotype

Contents of /contributions/uc_affiliate.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed Nov 4 23:44:21 2009 UTC (2 weeks, 6 days ago) by neochief
Branch: MAIN
File MIME type: text/x-php
Initial commit.
1 <?php
2
3 /**
4 * @file
5 * UC Affiliate administration forms and functionality.
6 */
7
8 /**
9 * Define the admin settings form.
10 */
11 function uc_affiliate_admin_settings($form_state) {
12 $form['uc_affiliate_commission'] = array(
13 '#type' => 'textfield',
14 '#title' => t('Default affiliate commission'),
15 '#description' => t('Sets the default commission structure which can be overridden on a per user base. This should be a comma separated list of number with the first value corresponding to the first level in the hierarchy and each successive value corresponding to the next level in the hierarchy. Example: <i>30; 2%; 1%</i>.'),
16 '#default_value' => uc_affiliate_commission2str(variable_get('uc_affiliate_commission', uc_affiliate_default_commission())),
17 '#size' => 10,
18 '#element_validate' => array('uc_affiliate_validate_commission'),
19 );
20 $form['uc_affiliate_ceil_commission'] = array(
21 '#type' => 'checkbox',
22 '#title' => t('Ceil affiliate commission'),
23 '#description' => t('If checked, affiliate commission will be ceiled to neighbour integer value (e.g. 19.477 => 20)'),
24 '#default_value' => variable_get('uc_affiliate_ceil_commission', TRUE),
25 '#size' => 10,
26 );
27 $form['uc_affiliate_allow_products_by_default'] = array(
28 '#type' => 'checkbox',
29 '#title' => t('Allow products by default'),
30 '#description' => t('This option will affect on default availability products for affiliates.'),
31 '#default_value' => variable_get('uc_affiliate_allow_products_by_default', TRUE),
32 '#size' => 10,
33 );
34
35 foreach (uc_order_status_list('general') as $status) {
36 $general_statuses[$status['id']] = $status['title'];
37 }
38 foreach (uc_order_status_list() as $status) {
39 $all_statuses[$status['id']] = $status['title'];
40 }
41 $form['uc_affiliate_commission_order_status'] = array(
42 '#type' => 'select',
43 '#multiple' => TRUE,
44 '#title' => t('Order status'),
45 '#default_value' => variable_get('affiliate_commission_order_status', 'completed'),
46 '#description' => t('Where in the order routine commissions will be assigned.'),
47 '#options' => $general_statuses,
48 );
49 $form['uc_affiliate_commission_return_status'] = array(
50 '#type' => 'select',
51 '#multiple' => TRUE,
52 '#title' => t('Return status'),
53 '#default_value' => variable_get('affiliate_commission_return_status', 'canceled'),
54 '#description' => t('If order will get one of selected statuses, it will appear in stats in "Returns" column and all of it\'s natural and referral commissions will be subtracted from affiliates\' balances.'),
55 '#options' => $all_statuses,
56 );
57 $form['uc_affiliate_invalid_redirect'] = array(
58 '#type' => 'textfield',
59 '#title' => t('On an invalid affiliate URL, redirect to'),
60 '#description' => t('When an invalid username is used as a subdomain, the user will be redirected to this URL.'),
61 '#default_value' => variable_get('affiliate_invalid_redirect', '')
62 );
63
64 $form = system_settings_form($form);
65 $form['#submit'][] = 'uc_affiliate_admin_settings_submit';
66
67 return $form;
68 }
69
70 /**
71 * Validate admin settings form.
72 */
73 function uc_affiliate_admin_settings_validate($form, &$form_state) {
74 $url = $form_state['values']['uc_affiliate_invalid_redirect'];
75 if (($url)&&($url != check_url($url) || strpos($url, '://') === false)) {
76 form_set_error('uc_affiliate_invalid_redirect', t('Please enter a valid URL.'));
77 }
78 }
79
80 /**
81 * Submit admin settings form.
82 */
83 function uc_affiliate_admin_settings_submit($form, &$form_state) {
84 variable_set('uc_affiliate_commission', uc_affiliate_parse_commission($form_state['values']['uc_affiliate_commission']));
85 }
86
87 /**
88 * Define the affiliates listing.
89 */
90 function uc_affiliate_admin() {
91 // Setup the table.
92 $header = array(
93 array('data' => t('Username'), 'field' => 'u.name'),
94 array('data' => t('Status'), 'field' => 'u.status'),
95 array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
96 array('data' => t('Last access'), 'field' => 'u.access'),
97 array('data' => t('Balance'), 'field' => 'balance'),
98 array('data' => ''),
99 );
100
101 // Define the query.
102 $sql = "SELECT u.uid, u.name, u.status, u.created, u.access, ac.time, SUM(ac.visits) AS visits, SUM(ac.users) AS users, SUM(ac.sales) AS sales, SUM(ac.returns) AS returns, SUM(ac.commission) AS commission, SUM(ap.paid) AS paid FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid LEFT OUTER JOIN {uc_affiliate_counts} ac ON u.uid = ac.affid LEFT JOIN {uc_affiliate_payments} ap ON ap.affid = ac.affid WHERE ur.rid IN (SELECT r.rid from {role} r INNER JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%%act as affiliate%%') GROUP BY u.uid";
103 $sql .= tablesort_sql($header);
104 $sql_count = "SELECT count(*) FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid IN (SELECT r.rid from {role} r INNER JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%%act as affiliate%%')";
105 $result = pager_query($sql, 50, 0, $sql_count);
106
107 $status = array(t('blocked'), t('active'));
108 $context = array(
109 'revision' => 'formatted-original',
110 'location' => 'affiliate-balance',
111 );
112
113 while ($affiliate = db_fetch_object($result)) {
114 $rows[] = array(
115 l($affiliate->name, 'user/'. $affiliate->uid),
116 $status[$affiliate->status],
117 format_interval(time() - $affiliate->created),
118 $affiliate->access ? t('@time ago', array('@time' => format_interval(time() - $affiliate->access))) : t('never'),
119 uc_price(($affiliate->commission ? $affiliate->commission : '0') - ($affiliate->paid ? $affiliate->paid : '0'), $context),
120 '',
121 );
122 }
123 if (count($rows)) {
124 $output = '<p>' . t("Click on an affiliate user to view their monthly sales and commissions.") . '</p>';
125 $output .= theme('table', $header, $rows);
126 $output .= theme('pager', NULL, 50, 0);
127 } else {
128 $output = '<p>' . t("There are no affiliates yet.") . '</p>';
129 }
130 return $output;
131 }
132
133
134 /**
135 * Daily affiliate stats.
136 */
137 function uc_affiliate_admin_daily_reports($affiliate = NULL, $only_stats = FALSE) {
138 $context = array(
139 'revision' => 'formatted-original',
140 'location' => 'affiliate-commission',
141 );
142 if ($affiliate) {
143 $add_where = ' AND affid = %d';
144 $admin = FALSE;
145 }
146
147 // Setup the table.
148 $header = array(
149 array('data' => t('Date'), 'field' => 'time'),
150 array('data' => t('Visits'), 'field' => 'visits'),
151 array('data' => t('Sales'), 'field' => 'sales'),
152 array('data' => t('Returns'), 'field' => 'returns'),
153 array('data' => t('Commission'), 'field' => 'commission'),
154 array('data' => t('Profit'), 'field' => 'profit'),
155 );
156
157 // Define the query.
158 $sql = 'SELECT * FROM {uc_affiliate_counts} WHERE (time between %d AND %d)'. $add_where .' GROUP BY time';
159 $sql .= tablesort_sql($header);
160 $sql_count = 'SELECT count(*) FROM {uc_affiliate_counts} WHERE (time between %d AND %d)'. $add_where;
161
162 $dates = _get_query_dates();
163
164 $result = pager_query($sql, 100, 0, $sql_count, $dates['start'], $dates['end'], $affiliate->uid);
165
166 $total = array(t('Total') .':', 'visits' => 0, 'sales' => 0, 'returns' => 0, 'commission' => 0);
167 while ($row = db_fetch_object($result)) {
168 $date = date('Y-m-d', $row->time);
169 if ($admin) {
170 $date = l($date, 'admin/store/affiliate/reports/affiliate', array('query' => check_url('stdate='. $date .'&endate='. $date)));
171 }
172 $rows[] = array(
173 $date,
174 $row->visits ? $row->visits : '0',
175 $row->sales ? $row->sales : '0',
176 $row->returns ? $row->returns : '0',
177 uc_price($row->commission ? $row->commission : 0, $context),
178 uc_price($row->profit ? $row->profit : 0, $context),
179 );
180 $total['visits'] += $row->visits;
181 $total['sales'] += $row->sales;
182 $total['returns'] += $row->returns;
183 $total['commission'] += $row->commission;
184 $total['profit'] += $row->profit;
185 }
186
187 $output .= $only_stats ? '' : drupal_get_form('uc_affiliate_date_selection_form');
188
189 if (!empty($rows)){
190 $total['commission'] = uc_price($total['commission'], $context);
191 $total['profit'] = uc_price($total['profit'], $context);
192
193 // If it's watched by affiliate, remove Profit column.
194 if (!$admin) {
195 unset($header[5]);
196 unset($total['profit']);
197 foreach($rows as &$row){
198 unset($row[5]);
199 }
200 }
201 // If no returns, we should not show this column to user at all.
202 if ($total['returns'] == 0) {
203 unset($header[3]);
204 unset($total['returns']);
205 foreach($rows as &$row){
206 unset($row[3]);
207 }
208 }
209
210 // Adding total row at the very bottom of rows list.
211 $rows[] = $total;
212
213 if ($only_stats) {
214 foreach ($header as $key => $col) {
215 unset($header[$key]['field']);
216 }
217 }
218
219 $output .= theme('table', $header, $rows);
220 $output .= theme('pager', NULL, 50, 0);
221 }
222 else {
223 if (!$only_stats) {
224 $output .= t('Sorry, no data is available for selected period.');
225 }
226 }
227
228 return $output;
229 }
230
231 /**
232 * Per-affiliate stats.
233 */
234 function uc_affiliate_admin_user_stats() {
235 $context = array(
236 'revision' => 'formatted-original',
237 'location' => 'affiliate-commission',
238 );
239 $header = array(
240 array('data' => t('Affiliate'), 'field' => 'affid'),
241 array('data' => t('Visits'), 'field' => 'visits'),
242 array('data' => t('Sales'), 'field' => 'sales'),
243 array('data' => t('Returns'), 'field' => 'returns'),
244 array('data' => t('Commissions'), 'field' => 'commission'),
245 array('data' => t('Profit'), 'field' => 'profit'),
246 );
247
248 $sql = "SELECT u.uid, u.name, u.status, u.created, u.access, ac.time, SUM(ac.visits) AS visits, SUM(ac.users) AS users, SUM(ac.sales) AS sales, SUM(ac.returns) AS returns, SUM(ac.commission) AS commission, SUM(ac.profit) AS profit
249 FROM {users} u
250 INNER JOIN {users_roles} ur ON u.uid = ur.uid
251 INNER JOIN {uc_affiliate_counts} ac ON u.uid = ac.affid
252 WHERE ur.rid IN (SELECT r.rid from {role} r INNER JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%%act as affiliate%%') AND
253 time between %d AND %d
254 GROUP BY u.uid";
255 $sql .= tablesort_sql($header);
256
257 $sql_count = "SELECT COUNT(*) FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid INNER JOIN {uc_affiliate_counts} ac ON u.uid = ac.affid WHERE ur.rid IN (SELECT r.rid from {role} r INNER JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%%act as affiliate%%') AND time between %d AND %d";
258
259 $dates = _get_query_dates();
260 $result = pager_query($sql, 50, 0, $sql_count, $dates['start'], $dates['end']);
261
262 while ($row = db_fetch_object($result)) {
263
264 $user = user_load(array('uid' => $row->uid));
265
266 $params = array();
267 if (isset($_GET['stdate'])){
268 $params[] = 'stdate='. $_GET['stdate'];
269 }
270 if (isset($_GET['endate'])){
271 $params[] = 'endate='. $_GET['endate'];
272 }
273 if (isset($_GET['period'])){
274 $params[] = 'period='. $_GET['period'];
275 }
276 $params = implode('&', $params);
277
278 // generate record
279 $rows[] = array(
280 l($user->name,'user/'.$user->uid.'/affiliate/stats',array(),$params),
281 $row->visits ? $row->visits : '0',
282 $row->sales ? $row->sales : '0',
283 $row->returns ? $row->returns : '0',
284 uc_price($row->commission ? $row->commission : 0, $context),
285 uc_price($row->profit ? $row->profit : 0, $context),
286 );
287 }
288
289 $output .= drupal_get_form('uc_affiliate_date_selection_form');
290
291 if (count($rows)){
292 $output .= theme('table', $header, $rows);
293 $output .= theme('pager', NULL, 50, 0);
294 } else {
295 $output .= t('Sorry, no data available yet for this period.');
296 }
297 return $output;
298 }
299
300
301
302
303
304
305
306
307
308
309 /*************************
310 * Misc functions.
311 */
312
313
314 /**
315 * Period selection form.
316 */
317 function uc_affiliate_date_selection_form($form_state) {
318 // Fetch dates from GET.
319 $dates = _get_query_dates();
320 $dt_first = $dates['start'];
321 $dt_last = $dates['end'];
322
323 // Create the form.
324 $form = array();
325 $form['time_selector'] = array(
326 '#type' => 'fieldset',
327 '#collapsible' => TRUE,
328 '#collapsed' => FALSE,
329 );
330
331 $using_dates = isset($_GET['stdate']) && $_GET['stdate'] != 0;
332 if ($using_dates) {
333 $suffix = '<script>$("#uc-affiliate-date-selection-form .dates-predefined").fadeTo("fast",0.3);</script>';
334 }
335
336 $form['time_selector']['period'] = array(
337 '#title' => t('Select a predefined range'),
338 '#type' => 'select',
339 '#options' => predefined_range_options(),
340 '#default_value' => $dates['period'],
341 '#prefix' => '<div class="dates-predefined">',
342 '#suffix' => '</div>'. $suffix,
343 '#attributes' => array(),
344 );
345
346 $form['time_selector']['use_dates'] = array(
347 '#type' => 'checkbox',
348 '#title' => t('Search using custom date range.'),
349 '#default_value' => $using_dates,
350 );
351 if (!$using_dates) $addcl = ' hidden';
352 $form['time_selector']['begin_date'] = array(
353 '#prefix' => '<div class="dates'. $addcl .'"><div class="start-date" style="float:left;margin-right:15px;">',
354 '#suffix' => '</div>',
355 '#title' => t('Start Date'),
356 '#type' => 'date',
357 '#default_value' => array(
358 'month' => date('n', $dt_first),
359 'day' => date('j', $dt_first),
360 'year' => date('Y', $dt_first)
361 ),
362 '#required' => FALSE,
363 );
364 $form['time_selector']['end_date'] = array(
365 '#prefix' => '<div class="end-date" style="float:left;">',
366 '#suffix' => '</div><br class="clearfix"/>',
367 '#title' => t('End Date'),
368 '#type' => 'date',
369 '#default_value' => array(
370 'month' => date('n', $dt_last),
371 'day' => date('j', $dt_last),
372 'year' => date('Y', $dt_last),
373 ),
374 );
375 $form['time_selector']['submit'] = array(
376 '#type' => 'submit',
377 '#value' => t('Submit'),
378 '#suffix' => '</div>',
379 '#attributes' => array('style' => 'margin:25px 0 0 20px;'),
380 );
381
382 // Add JS calendar
383 $path = drupal_get_path('module', 'uc_affiliate');
384 drupal_add_js($path.'/scripts/jquery-ui-1.5.3.custom_datepicker.min.js', 'module');
385 drupal_add_js($path.'/scripts/uc_affiliate.js', 'module');
386 drupal_add_css($path.'/styles/uc_affiliate.css', 'module');
387 drupal_add_css($path.'/styles/dpcss/ui.all.css', 'module');
388
389 return $form;
390 }
391
392 /**
393 * Period selection form submit handler.
394 */
395 function uc_affiliate_date_selection_form_submit($form, &$form_state) {
396 if ($form_state['values']['use_dates']) {
397 $start_date = date('Y-m-d', mktime(0, 0, 0, $form_state['values']['begin_date']['month'], $form_state['values']['begin_date']['day'], $form_state['values']['begin_date']['year']));
398 $end_date = date('Y-m-d', mktime(23, 59, 59, $form_state['values']['end_date']['month'], $form_state['values']['end_date']['day'], $form_state['values']['end_date']['year']));
399 $params = 'stdate='. $start_date .'&endate='. $end_date;
400 }
401 else {
402 $params = 'period='. $form_state['values']['period'];
403 }
404
405 $form_state['redirect'] = array($_GET['q'], $params);
406 }
407
408 /**
409 * Array of predefined date ranges for period selection form.
410 */
411 function predefined_range_options() {
412 return array(
413 'this' => t('This Month'),
414 'last' => t('Last Month'),
415 'last30' => t('Last 30 days'),
416 'last60' => t('Last 60 Days'),
417 'last90' => t('Last 90 Days')
418 );
419 }
420
421 function _get_query_dates() {
422 $now = _user_time();
423 $dates = array();
424
425 $type = isset($_GET['period']) ? check_plain($_GET['period']) : 'this';
426 $dates['period'] = $type;
427
428 if (isset($_GET['stdate']) && isset($_GET['endate'])) {
429 $dates['start'] = mktime(0, 0, 0, date("m", strtotime(check_plain($_GET['stdate']))) , date("d", strtotime(check_plain($_GET['stdate']))), date("Y", strtotime(check_plain($_GET['stdate']))));
430 $dates['end'] = mktime(23, 59, 59, date("m", strtotime(check_plain($_GET['endate']))) , date("d", strtotime(check_plain($_GET['endate']))), date("Y", strtotime(check_plain($_GET['endate']))));
431 }
432 else {
433 switch ($type) {
434 case 'lastm':
435 $dates['start'] = mktime(0, 0, 0, date("m", $now)-1 , 1, date("Y", $now)) - date("Z", $now);
436 $dates['end'] = mktime(23, 59, 59, date("m", $now) , 0, date("Y", $now)) - date("Z", $now);
437 break;
438 case 'last7':
439 $start = strtotime("-7 days", $now);
440 $dates['start'] = mktime(0, 0, 0, date("m", $start) , date("d", $start), date("Y", $start)) - date("Z", $start);
441 $dates['end'] = mktime(23, 59, 59, date("m", $now) , date("d", $now), date("Y", $now)) - date("Z", $now);
442 break;
443 case 'last30':
444 $start = strtotime("-30 days", $now);
445 $dates['start'] = mktime(0, 0, 0, date("m", $start) , date("d", $start), date("Y", $start)) - date("Z", $start);
446 $dates['end'] = mktime(23, 59, 59, date("m", $now) , date("d", $now), date("Y", $now)) - date("Z", $now);
447 break;
448 case 'last60':
449 $start = strtotime("-60 days", $now);
450 $dates['start'] = mktime(0, 0, 0, date("m", $start) , date("d", $start), date("Y", $start)) - date("Z", $start);
451 $dates['end'] = mktime(23, 59, 59, date("m", $now) , date("d", $now), date("Y", $now)) - date("Z", $now);
452 break;
453 case 'last90':
454 $start = strtotime("-90 days", $now);
455 $dates['start'] = mktime(23, 59, 59, date("m", $start) , date("d", $start), date("Y", $start)) - date("Z", $start);
456 $dates['end'] = mktime(23, 59, 59, date("m", $now) , date("d", $now), date("Y", $now)) - date("Z", $now);
457 break;
458 case 'thism':
459 default:
460 $dates['start'] = mktime(0, 0, 0, date("m", $now) , 1, date("Y", $now)) - date("Z", $now);
461 $dates['end'] = mktime(23, 59, 59, date("m", $now)+1 , 0, date("Y", $now)) - date("Z", $now);
462 break;
463 }
464 }
465 return $dates;
466 }
467

  ViewVC Help
Powered by ViewVC 1.1.2