More rewrites, and output-mode fixed (we should documentate this)
[mailer.git] / inc / pool / pool-bonus.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  * 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 - 2009 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         die();
42 }
43
44 // Don't run on daily reset
45 if (isResetModeEnabled()) {
46         // Skip here
47         return false;
48 } elseif (!isExtensionActive('bonus')) {
49         // Abort if extension bonus is not active
50         return false;
51 }
52
53 // Need this here
54 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
55 if ($GLOBALS['pool_cnt'] < getConfig('max_send')) {
56         // Do we need to send out bonus mails?
57         if (isExtensionActive('html_mail')) {
58                 //                                 0        1        2          3          4        5          6            7         8        9            10           11
59                 $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__);
60         } else {
61                 //                                 0        1        2          3          4        5          6            7         8        9            10
62                 $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__);
63         }
64
65         if (SQL_NUMROWS($result_bonus) > 0) {
66                 // Send these mails away...
67                 $cnt2 = '';
68                 while ($DATA = SQL_FETCHARRAY($result_bonus)) {
69                         // Message is active in queue
70                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
71                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
72
73                         // "Explode" all receivers into an array
74                         if (ereg(';', $DATA['receivers'])) {
75                                 // There's more than one receiver in the list...
76                                 $RECEIVERS = explode(';', $DATA['receivers']);
77                         } elseif (!empty($DATA['points'])) {
78                                 // Only one user left
79                                 $RECEIVERS = array($DATA['receivers']);
80                         } else {
81                                 // No users left
82                                 $RECEIVERS = array(0);
83                         }
84                         $dummy = $RECEIVERS;
85
86                         // Now, if we are good little boys and girls Santa left us some user-ids.
87                         // We can now send mails to them...
88                         foreach ($RECEIVERS as $key => $userid) {
89                                 // Load personal data
90                                 //* DEBUG: */ outputHtml("*L:".__LINE__.'/'.$userid."*<br />");
91                                 $result_user = SQL_QUERY_ESC("SELECT `surname`, `family`, `email` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
92                                         array(bigintval($userid)), __FILE__, __LINE__);
93
94                                 // Is his data available?
95                                 if (SQL_NUMROWS($result_user) == 1) {
96                                         // The final receiver does exists so we can continue...
97                                         list($surname, $family, $email) = SQL_FETCHROW($result_user);
98                                         //* DEBUG: */ outputHtml("OK!/L:".__LINE__."<br />");
99
100                                         // Mark this user as "spammed" ;-) And place a line for him...
101                                         if (removeReceiver($dummy, $key, $userid, $DATA['id'], $DATA['id'], true) == 'done') {
102                                                 // Replace text variables
103                                                 foreach ($GLOBALS['replacer'] as $key => $value) {
104                                                         if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
105                                                 } // END - foreach
106
107                                                 // Prepare content
108                                                 $content = array(
109                                                         'id'       => $DATA['id'],
110                                                         'url'      => $DATA['url'],
111                                                         'time'     => createFancyTime($DATA['time']),
112                                                         'points'   => translateComma($DATA['points']),
113                                                         'category' => getCategory($DATA['cat_id']),
114                                                         'text'     => $DATA['text']
115                                                 );
116
117                                                 // Prepare the mail
118                                                 $mailText = loadEmailTemplate('bonus-mail', $content, $userid);
119
120                                                 // Send mail away
121                                                 if (isset($DATA['html_msg'])) {
122                                                         // Send HTML?
123                                                         sendEmail($email, $DATA['subject'], $mailText, $DATA['html_msg']);
124                                                 } else {
125                                                         // No HTML mail!
126                                                         sendEmail($email, $DATA['subject'], $mailText);
127                                                 }
128
129                                                 // Count one up and remove entry from dummy array
130                                                 $GLOBALS['pool_cnt']++; unset($dummy[$key]);
131
132                                                 if (getExtensionVersion('user') >= '0.1.4') {
133                                                         // Update mails received for receiver
134                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_received=emails_received+1 WHERE `userid`=%s LIMIT 1",
135                                                         array(bigintval($userid)), __FILE__, __LINE__);
136                                                 } // END - if
137
138                                                 // Do we have send maximum mails?
139                                                 if (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || (countSelection($dummy) == 0)) {
140                                                         // Yes, we have
141                                                         //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__."<br />");
142                                                         break;
143                                                 } // END - if
144                                         } // END - if
145                                 } // END - if
146
147                                 // Free some memory
148                                 SQL_FREERESULT($result_user);
149                         }
150
151                         // Update mediadata if version is 0.0.4 or higher
152                         if (getExtensionVersion('mediadata') >= '0.0.4') {
153                                 // Update entry (or add missing
154                                 $P = $GLOBALS['pool_cnt'];
155                                 if (!empty($cnt2) && empty($GLOBALS['pool_cnt'])) $P = $cnt2;
156                                 //* DEBUG: */ outputHtml("+MEDIA/L:".__LINE__.'/'.$P."+<br />");
157                                 updateMediadataEntry(array("total_send", "bonus_send"), 'add', $P);
158                         } // END - if
159
160                         // Close sending system
161                         //* DEBUG: */ outputHtml("-L:".__LINE__.'/'.countSelection($dummy)."-<br />");
162                         if (countSelection($dummy) == 0) {
163                                 // Queue reached!
164                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
165                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
166                                 //* DEBUG: */ outputHtml("*L:".__LINE__."*<br />");
167
168                                 // Update mediadata if version is 0.0.4 or higher
169                                 if (getExtensionVersion('mediadata') >= '0.0.4') {
170                                         // Update entry (or add missing)
171                                         //* DEBUG: */ outputHtml("*MEDIA/L:".__LINE__."*<br />");
172                                         updateMediadataEntry(array("total_orders", "bonus_orders"), 'add', 1);
173                                 } // END - if
174                         } elseif ($GLOBALS['pool_cnt'] >= getConfig('max_send')) {
175                                 // Update bonus pool
176                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='NEW', `target_send`=%s, `receivers`='%s' WHERE `id`=%s LIMIT 1",
177                                 array(countSelection($dummy), implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
178                                 //* DEBUG: */ outputHtml("*L:".__LINE__."<pre>");
179                                 //* DEBUG: */ outputHtml(print_r($dummy, true));
180                                 //* DEBUG: */ outputHtml("</pre>\n!!!<br />");
181                                 break;
182                         }
183                 }
184         }
185
186         // Free memory
187         SQL_FREERESULT($result_bonus);
188
189         // Remove variable
190         unset($mailText);
191 }
192
193 //
194 ?>