Some language strings fixed, renamed. Copyright notice updated
[mailer.git] / inc / pool / pool-bonus.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 12/12/2008 *
4  * ===================                          Last change: 12/12/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : pool-bonus.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Sends queued bonus mails from the pool           *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sendet freigegebene Bonus-Mails aus dem Pool     *
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 } // END - if
42
43 // Don't run on daily reset
44 if ((isResetModeEnabled()) || (!isHtmlOutputMode())) {
45         // Skip here
46         return false;
47 } elseif (!isExtensionActive('bonus')) {
48         // Abort if extension bonus is not active
49         return false;
50 }
51
52 // Need this here
53 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
54 if ($GLOBALS['pool_cnt'] < getMaxSend()) {
55         // Do we need to send out bonus mails?
56         if (isExtensionActive('html_mail')) {
57                 //                                 0        1        2          3          4        5          6            7         8        9            10           11
58                 $result_bonus = SQL_QUERY("SELECT `id`, `subject`, `text`, `receivers`, `points`, `time`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `html_msg` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='NEW' ORDER BY `timestamp` DESC", __FILE__, __LINE__);
59         } else {
60                 //                                 0        1        2          3          4        5          6            7         8        9            10
61                 $result_bonus = SQL_QUERY("SELECT `id`, `subject`, `text`, `receivers`, `points`, `time`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='NEW' ORDER BY `timestamp` DESC", __FILE__, __LINE__);
62         }
63
64         if (!SQL_HASZERONUMS($result_bonus)) {
65                 // Send these mails away...
66                 $count2 = '';
67                 while ($DATA = SQL_FETCHARRAY($result_bonus)) {
68                         // Message is active in queue
69                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
70                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
71
72                         // "Explode" all receivers into an array
73                         if (isInString(';', $DATA['receivers'])) {
74                                 // There's more than one receiver in the list...
75                                 $receivers = explode(';', $DATA['receivers']);
76                         } elseif (!empty($DATA['points'])) {
77                                 // Only one user left
78                                 $receivers = array($DATA['receivers']);
79                         } else {
80                                 // No users left
81                                 $receivers = array(0);
82                         }
83                         $dummy = $receivers;
84
85                         // Now, if we are good little boys and girls Santa Claus left us some user-ids.
86                         // We can now send mails to them...
87                         foreach ($receivers as $key => $userid) {
88                                 // Load personal data
89                                 if (fetchUserData($userid)) {
90                                         // The final receiver does exists so we can continue...
91                                         //* DEBUG: */ debugOutput('OK!/L:'.__LINE__);
92
93                                         // Remove receiver from list
94                                         $status = removeReceiver($dummy, $key, $userid, $DATA['id'], $DATA['id'], true);
95
96                                         // Did it work?
97                                         switch ($status) {
98                                                 case 'done': // Done!
99                                                         // Prepare the mail
100                                                         $mailText = loadEmailTemplate('bonus-mail', $DATA, $userid);
101
102                                                         // Send mail away
103                                                         if (isset($DATA['html_msg'])) {
104                                                                 // Send HTML?
105                                                                 sendEmail(getUserData('email'), $DATA['subject'], $mailText, $DATA['html_msg']);
106                                                         } else {
107                                                                 // No HTML mail!
108                                                                 sendEmail(getUserData('email'), $DATA['subject'], $mailText);
109                                                         }
110
111                                                         // Count one up and remove entry from dummy array
112                                                         $GLOBALS['pool_cnt']++; unset($dummy[$key]);
113
114                                                         if (isExtensionInstalledAndNewer('user', '0.1.4')) {
115                                                                 // Update mails received for receiver
116                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
117                                                                 array(bigintval($userid)), __FILE__, __LINE__);
118                                                         } // END - if
119
120                                                         // Do we have send maximum mails?
121                                                         if (($GLOBALS['pool_cnt'] >= getMaxSend()) || (countSelection($dummy) == 0)) {
122                                                                 // Yes, we have
123                                                                 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__);
124                                                                 break;
125                                                         } // END - if
126                                                         break;
127
128                                                 default: // Unknown return type
129                                                         logDebugMessage(__FILE__, __LINE__, 'Unknown status ' . $status . ' detected. pool_id=' . $DATA['id'] . ',userid=' . $DATA['userid'] . ',stats_id=' . $stats_id);
130                                                         break;
131                                         } // END - switch
132                                 } // END - if
133                         } // END - foreach
134
135                         // Update mediadata if version is 0.0.4 or higher
136                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
137                                 // Update entry (or add missing
138                                 $P = $GLOBALS['pool_cnt'];
139                                 if (!empty($count2) && empty($GLOBALS['pool_cnt'])) $P = $count2;
140                                 //* DEBUG: */ debugOutput('+MEDIA/L:'.__LINE__.'/'.$P.'+');
141                                 updateMediadataEntry(array('total_send', 'bonus_send'), 'add', $P);
142                         } // END - if
143
144                         // Close sending system
145                         //* DEBUG: */ debugOutput('-L:'.__LINE__.'/'.countSelection($dummy).'-');
146                         if (countSelection($dummy) == 0) {
147                                 // Queue reached!
148                                 SQL_QUERY_ESC("UPDATE
149         `{?_MYSQL_PREFIX?}_bonus`
150 SET
151         `data_type`='SEND',
152         `target_send`=0,
153         `receivers`=''
154 WHERE
155         `id`=%s
156 LIMIT 1",
157                                         array(bigintval($DATA['id'])), __FILE__, __LINE__);
158                                 //* DEBUG: */ debugOutput('*L:'.__LINE__.'*');
159
160                                 // Update mediadata if version is 0.0.4 or higher
161                                 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
162                                         // Update entry (or add missing)
163                                         //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
164                                         updateMediadataEntry(array('total_orders', 'bonus_orders'), 'add', 1);
165                                 } // END - if
166                         } elseif ($GLOBALS['pool_cnt'] >= getMaxSend()) {
167                                 // Update bonus pool
168                                 SQL_QUERY_ESC("UPDATE
169         `{?_MYSQL_PREFIX?}_bonus`
170 SET
171         `data_type`='NEW',
172         `target_send`=%s,
173         `receivers`='%s'
174 WHERE
175         `id`=%s
176 LIMIT 1",
177                                         array(
178                                                 countSelection($dummy),
179                                                 implode(';', $dummy),
180                                                 bigintval($DATA['id'])
181                                         ), __FILE__, __LINE__);
182                                 //* DEBUG: */ debugOutput('*L:'.__LINE__.'<pre>'.print_r($dummy, true).'</pre>!!!');
183                                 break;
184                         }
185                 } // END - while
186         } // END - if
187
188         // Free memory
189         SQL_FREERESULT($result_bonus);
190
191         // Remove variable
192         unset($mailText);
193 } // END - if
194
195 // [EOF]
196 ?>