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