More misc fixes and rewrites (sorry, lame description)
[mailer.git] / inc / monthly / monthly_bonus.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 11/14/2004 *
4  * ===============                              Last change: 11/19/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : monthly_bonus.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Monthly bonus from click-bonus                   *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Monatlicher Bonus von Klick-Verguetung           *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 } elseif (!EXT_IS_ACTIVE('bonus')) {
44         return;
45 }
46
47 // Do not execute when script is in CSS mode or no daily reset
48 if ((getOutputMode() == 1) || (!isResetModeEnabled())) return;
49 //* DEBUG: */ echo basename(__FILE__)."<br />\n";
50
51 // Get current month (2 digits)
52 $curr = date('m', time());
53
54 if (($curr != getConfig('last_month')) && (getConfig('bonus_ranks') > 0) && (getOutputMode() != 1)) {
55         // Extension 'autopurge' is inactive or purging of inactive accounts is deactivated
56         $whereStatement1 = "WHERE `status`='CONFIRMED'";
57         $whereStatement2 = getConfig('bonus_ranks');
58
59         // Shall I keep inactive members away from here? (mostly wanted in an "active-rallye" ...)
60         if (EXT_IS_ACTIVE('autopurge')) {
61                 // Use last online stamp only when autopurge for inactive members is activated
62                 if (getConfig('ap_inactive_since') > 0) {
63                         // Okay, include last online timestamp
64                         $whereStatement1 = sprintf("WHERE `status`='CONFIRMED' AND `last_online` >= (UNIX_TIMESTAMP() - %s)", getConfig('ap_inactive_since'));
65                         $whereStatement2 = getConfig('bonus_ranks');
66                 } // END - if
67         } // END - if
68
69         // Add more bonus points here
70         $add = '';
71         // @TODO Rewrite this to a filter
72         if (getConfig('bonus_click_yn') == 'Y') $add .= " + `turbo_bonus`";
73         if (getConfig('bonus_login_yn') == 'Y') $add .= " + `login_bonus`";
74         if (getConfig('bonus_order_yn') == 'Y') $add .= " + `bonus_order`";
75         if (getConfig('bonus_stats_yn') == 'Y') $add .= " + `bonus_stats`";
76         if (getConfig('bonus_ref_yn')   == 'Y') $add .= " + `bonus_ref`";
77
78         // Shall we add some entries?
79         if (!empty($add)) {
80                 $whereStatement1 .= " AND (0" . $add . ") > 0";
81         } // END - if
82
83         // Run SQL string to check for accounts
84         $result_main = SQL_QUERY_ESC("SELECT `userid`, `email`, `gender`, `surname`, `family`, (0".$add.") AS points
85 FROM
86         `{!_MYSQL_PREFIX!}_user_data`
87 ".$whereStatement1."".$add."
88 ORDER BY
89         `points` DESC,
90         `userid` ASC
91 LIMIT %s",
92         array($whereStatement2), __FILE__, __LINE__);
93
94         // Some entries were found?
95         if (SQL_NUMROWS($result_main) > 0) {
96                 // Load our winners...
97                 while ($content = SQL_FETCHARRAY($result_main)) {
98                         // Make sure zero points are not mailed
99                         if ($content['points'] > 0) {
100                                 // Add points to user's account directly
101                                 ADD_POINTS_REFSYSTEM_DIRECT('monthly_bonus', $content['uid'], $content['points']);
102
103                                 // Translate gender/points
104                                 $content['gender']       = translateGender($content['gender']);
105                                 $content['points'] = translateComma($content['points']);
106
107                                 // Load email template and email it away
108                                 $message = LOAD_EMAIL_TEMPLATE('member_bonus', $content['points'], bigintval($content['uid']));
109                                 sendEmail($content['email'], getMessage('BONUS_MONTHLY_ONLINE_BONUS'), $message);
110                         } // END - if
111                 } // END - while
112
113                 // Reset accounts
114                 $result = SQL_QUERY("UPDATE `{!_MYSQL_PREFIX!}_user_data`
115 SET turbo_bonus=0, login_bonus=0, bonus_order=0, bonus_stats=0, bonus_ref=0", __FILE__, __LINE__);
116         } // END - if
117
118         // Free memory
119         SQL_FREERESULT($result_main);
120 } // END - if
121
122 //
123 ?>