But only when it is not empty
[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 - 2013 by Mailer Developer Team                   *
20  * For more information visit: http://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 $totalPoints = getDoublerTotalPointsLeft();
60 if ($totalPoints == '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 unsetGetRequestElement('DOUBLER_USERID');
67 unsetPostRequestElement('DOUBLER_USERID');
68 setSession('DOUBLER_USERID', '');
69 if (empty($GLOBALS['local_doubler_userid'])) $GLOBALS['local_doubler_userid'] = '0';
70
71 // Init additional WHERE statement
72 $whereStatement = ' ';
73
74 // Is ext-holiday installed?
75 // @TODO Rewrite these if() blocks to a filter
76 if (isExtensionActive('holiday')) {
77         // Exclude those as well
78         $whereStatement .= " AND `d`.`holiday_active`='N'";
79 } // END - if
80
81 // Check for doubles which we can pay out
82 $result_total = SQL_QUERY_ESC("SELECT
83         COUNT(`do`.`id`) AS `cnt`,
84 FROM
85         `{?_MYSQL_PREFIX?}_doubler` AS `do`
86 INNER JOIN
87         `{?_MYSQL_PREFIX?}_user_data` AS `d`
88 ON
89         `do`.`userid`=`d`.`userid`
90 WHERE
91         `d`.`status`='CONFIRMED'
92         " . runFilterChain('user_exclusion_sql', $whereStatement) . " AND
93         `do`.`points` <= %s AND
94         `do`.`points` >= ({?doubler_min?} * 2) AND
95         `do`.`completed`='N' AND
96         `do`.`is_ref`='N'
97 ORDER BY
98         `do`.`timemark` ASC",
99         array(
100                 $totalPoints
101         ), __FILE__, __LINE__);
102
103 // Get total count
104 list($totalCount) = SQL_FETCHROW($result_total);
105
106 // Free memory
107 SQL_FREERESULT($result_total);
108
109 // Check for accounts with limitation
110 $result_main = SQL_QUERY_ESC("SELECT
111         `do`.`id`,
112         `do`.`userid`,
113         `do`.`points`,
114         `do`.`remote_ip`,
115         `do`.`timemark`
116 FROM
117         `{?_MYSQL_PREFIX?}_doubler` AS `do`
118 INNER JOIN
119         `{?_MYSQL_PREFIX?}_user_data` AS `d`
120 ON
121         `do`.`userid`=`d`.`userid`
122 WHERE
123         `d`.`status`='CONFIRMED'
124         " . runFilterChain('user_exclusion_sql', $whereStatement) . " AND
125         `do`.`points` <= %s AND
126         `do`.`points` >= ({?doubler_min?} * 2) AND
127         `do`.`completed`='N' AND
128         `do`.`is_ref`='N'
129 ORDER BY
130         `do`.`timemark` ASC
131 LIMIT {?doubler_max_sent?}",
132         array(
133                 $totalPoints
134         ), __FILE__, __LINE__);
135
136 // Are there entries found?
137 if ((($totalCount > 0) && (getConfig('doubler_sent_all') == 'Y')) || ((SQL_NUMROWS($result_main) == getConfig('doubler_group_sent')) && (getConfig('doubler_sent_all') != 'Y'))) {
138         // Switch to matching SQL resource
139         $result_load = $result_main;
140         if (($totalCount > 0) && (getConfig('doubler_sent_all') == 'Y')) {
141                 $result_load = $result_total;
142         } // END - if
143
144         // At least one account was found
145         while ($content = SQL_FETCHARRAY($result_load)) {
146                 // Only double when points are enougth!
147                 if ($totalPoints >= $content['points']) {
148                         // Check for his ref points
149                         $refPoints = countSumTotalData($content['userid'], 'doubler', 'points', 'refid', FALSE, " AND `completed`='N' AND `is_ref`='Y'");
150
151                         // Zero refid when empty (might be helpful!)
152                         if (empty($refPoints)) {
153                                 $refPoints = '0';
154                         } // END - if
155
156                         if (($refPoints > 0) && ($GLOBALS['local_doubler_userid'] == $content['userid']) && (!empty($refPoints))) {
157                                 // Referral points found so add them and set line(s) to completed='Y'
158                                 $content['points'] += $refPoints;
159                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `refid`=%s AND `completed`='N' AND `is_ref`='Y'",
160                                         array(bigintval($content['userid'])), __FILE__, __LINE__);
161                         } else {
162                                 // No referral points found
163                                 $refPoints = '0';
164                         }
165
166                         // Exclude webmaster from doubling...
167                         if ($content['userid'] != getDoublerUserid()) {
168                                 // Add points
169                                 initReferralSystem();
170                                 addPointsThroughReferralSystem(sprintf("doubler_%s", strtolower(getDoublerSendMode())), $content['userid'], $content['points']);
171                         } // END - if
172
173                         // Set entry as "payed"
174                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_doubler` SET `completed`='Y' WHERE `id`=%s LIMIT 1",
175                                 array(bigintval($content['id'])), __FILE__, __LINE__);
176
177                         $okay = FALSE;
178
179                         // Check for jackpot inclusion in doubling process
180                         if (($jackpotPoints > 0) && ($jackpotPoints >= $content['points']) && (getConfig('doubler_jackpot') == 'Y')) {
181                                 // Subtract points from jackpot
182                                 $okay = subtractPointsFromJackpot($content['points']);
183                                 $jackpotPoints -= $content['points'];
184                         } // END - if
185
186                         // Exclude also webmaster's id in taking points from webmaster's account
187                         if (($userPoints > 0) && ($userPoints >= $content['points']) && ($okay === FALSE) && (getDoublerUserid() > 0) && ($content['userid'] != getDoublerUserid())) {
188                                 // Subtract points
189                                 $okay = subtractPoints('doubler_payout', getDoublerUserid(), $content['points']);
190                         } // END - if
191
192                         // Update doubler's account only when others are not updated
193                         if ($okay === FALSE) {
194                                 // Add points to used doubler points
195                                 updateConfiguration('doubler_used', $content['points'], '+');
196                         } // END - if
197
198                         // Update variables to prevent errors
199                         incrementConfigEntry('doubler_used', $content['points']);
200                         $totalPoints -= $content['points'];
201
202                         // Prepare array
203                         $content['timemark'] = generateDateTime($content['timemark'], 2);
204
205                         // Load mail template and send mail away...
206                         $message = loadEmailTemplate('member_doubler', $content, $content['userid']);
207                         sendEmail($content['userid'], '{--MEMBER_DOUBLER_SUBJECT--}', $message);
208                 } // END - if
209         } // END - while
210 } // END - if
211
212 // Free memory
213 SQL_FREERESULT($result_total);
214 SQL_FREERESULT($result_main);
215
216 // [EOF]
217 ?>