Extension ext-coupon basicly finished, no shortcuts in naming:
[mailer.git] / inc / mails / doubler_mails.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 02/16/2005 *
4  * ===================                          Last change: 01/21/2006 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : doubler_mails.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Send's out mails for doubled points              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sendet Mails bei vergueteter Verdoppelung aus    *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } elseif (!isExtensionActive('doubler')) {
42         // Do not execute when extension is absent
43         return;
44 } elseif (!isHtmlOutputMode()) {
45         // Do not execute when script is in CSS mode
46         return;
47 }
48
49 // Initialize variables
50 $jackpotPoints = '0';
51 $userPoints = '0';
52
53 // @TODO Can this be rewritten to a filter?
54 if (isExtensionActive('jackpot')) {
55         $jackpotPoints = getJackpotPoints();
56 } // END - if
57
58 // Get total points of the doubler itself
59 $DOUBLER_POINTS = getDoublerTotalPointsLeft();
60 if ($DOUBLER_POINTS == '0') {
61         // Exit here to prevent some SQL errors (SQL_QUERY_ESC doen't insert zeros! We need to fix this...)
62         return;
63 } // END - if
64
65 // If not currently doubled set it to zero
66 unsetGetRequestParameter('DOUBLER_USERID');
67 unsetPostRequestParameter('DOUBLER_USERID');
68 setSession('DOUBLER_USERID', '');
69 if (empty($GLOBALS['local_doubler_userid'])) $GLOBALS['local_doubler_userid'] = '0';
70
71 // Check for doubles which we can pay out
72 $result_total = SQL_QUERY_ESC("SELECT
73         d.id, d.userid, d.points, d.remote_ip, d.timemark
74 FROM
75         `{?_MYSQL_PREFIX?}_doubler` AS d
76 LEFT JOIN
77         `{?_MYSQL_PREFIX?}_user_data` AS u
78 ON
79         d.userid=u.userid
80 WHERE
81         u.`status`='CONFIRMED' AND
82         d.points <= %s AND
83         d.points >= ({?doubler_min?} * 2) AND
84         d.completed='N' AND
85         d.is_ref='N'
86 ORDER BY
87         d.timemark ASC",
88         array(
89                 $DOUBLER_POINTS
90         ), __FILE__, __LINE__);
91
92 // Check for accounts with limitation
93 $result_main = SQL_QUERY_ESC("SELECT
94         d.id, d.userid, d.points, d.remote_ip, d.timemark
95 FROM
96         `{?_MYSQL_PREFIX?}_doubler` AS d
97 LEFT JOIN
98         `{?_MYSQL_PREFIX?}_user_data` AS u
99 ON
100         d.userid=u.userid
101 WHERE
102         u.`status`='CONFIRMED' AND
103         d.points <= %s AND
104         d.points >= ({?doubler_min?} * 2) AND
105         d.completed='N' AND
106         d.is_ref='N'
107 ORDER BY
108         d.timemark ASC
109 LIMIT {?doubler_max_sent?}",
110         array(
111                 $DOUBLER_POINTS
112         ), __FILE__, __LINE__);
113
114 // Do we have entries found?
115 if (((!SQL_HASZERONUMS($result_total)) && (getConfig('doubler_sent_all') == 'Y')) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != 'Y'))) {
116         // Switch to matching SQL resource
117         $result_load = $result_main;
118         if ((!SQL_HASZERONUMS($result_total)) && (getConfig('doubler_sent_all') == 'Y')) {
119                 $result_load = $result_total;
120         } // END - if
121
122         // At least one account was found
123         while ($content = SQL_FETCHARRAY($result_load)) {
124                 // Only double when points are enougth!
125                 if ($DOUBLER_POINTS >= $content['points']) {
126                         // Check for his ref points
127                         $ref = countSumTotalData($content['userid'], 'doubler', 'points', 'refid', false, " AND `completed`='N' AND `is_ref`='Y'");
128
129                         // Zero refid when empty (might be helpful!)
130                         if (empty($ref)) $ref = '0';
131
132                         if (($ref > 0) && ($GLOBALS['local_doubler_userid'] == $content['userid']) && (!empty($ref))) {
133                                 // Referal points found so add them and set line(s) to completed='Y'
134                                 $content['points'] += $ref;
135                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `refid`=%s AND `completed`='N' AND `is_ref`='Y'",
136                                         array(bigintval($content['userid'])), __FILE__, __LINE__);
137                         } else {
138                                 // No referal points found
139                                 $ref = '0';
140                         }
141
142                         // Exclude webmaster from doubling...
143                         if ($content['userid'] != getConfig('doubler_userid')) {
144                                 // Add points
145                                 addPointsDirectly(sprintf("doubler_%s", strtolower(getConfig('doubler_send_mode'))), $content['userid'], $content['points']);
146                         } // END - if
147
148                         // Set entry as "payed"
149                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `id`=%s LIMIT 1",
150                                 array(bigintval($content['id'])), __FILE__, __LINE__);
151
152                         $okay = false;
153
154                         // Check for jackpot inclusion in doubling process
155                         if (($jackpotPoints > 0) && ($jackpotPoints >= $content['points']) && (getConfig('doubler_jackpot') == 'Y')) {
156                                 // Subtract points from jackpot
157                                 subtractPointsFromJackpot($content['points']);
158                                 $jackpotPoints -= $content['points'];
159
160                                 // Okay, done!
161                                 $okay = true;
162                         } // END - if
163
164                         // Exclude also webmaster's id in taking points from webmaster's account
165                         if (($userPoints > 0) && ($userPoints >= $content['points']) && ($okay === false) && (getConfig('doubler_userid') > 0) && ($content['userid'] != getConfig('doubler_userid'))) {
166                                 // Add points to used points
167                                 subtractPoints('doubler_payout', getConfig('doubler_userid'), $content['points']);
168
169                                 // Okay, done!
170                                 $okay = true;
171                         } // END - if
172
173                         // Update doubler's account only when others are not updated
174                         if ($okay === false) {
175                                 // Add points to used doubler points
176                                 updateConfiguration('doubler_used', $content['points'], '+');
177                         } // END - if
178
179                         // Update variables to prevent errors
180                         incrementConfigEntry('doubler_used', $content['points']);
181                         $DOUBLER_POINTS -= $content['points'];
182
183                         // Prepare array
184                         $content['timemark'] = generateDateTime($content['timemark'], 2);
185
186                         // Load mail template and send mail away...
187                         $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
188                         sendEmail($content['userid'], '{--MEMBER_DOUBLER_SUBJECT--}', $message);
189                 } // END - if
190         } // END - while
191 } // END - if
192
193 // Free memory
194 SQL_FREERESULT($result_total);
195 SQL_FREERESULT($result_main);
196
197 // [EOF]
198 ?>