Mailer used in many places, we still need a good 'selling' title
[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  * 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 Claus left us some user-ids.
87                         // We can now send mails to them...
88                         foreach ($RECEIVERS as $key => $userid) {
89                                 // Load personal data
90                                 if (fetchUserData($userid)) {
91                                         // The final receiver does exists so we can continue...
92                                         //* DEBUG: */ print("OK!/L:".__LINE__."<br />");
93
94                                         // Mark this user as "spammed" ;-) And place a line for him...
95                                         if (removeReceiver($dummy, $key, $userid, $DATA['id'], $DATA['id'], true) == 'done') {
96                                                 // Replace text variables
97                                                 foreach ($GLOBALS['replacer'] as $key => $value) {
98                                                         if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
99                                                 } // END - foreach
100
101                                                 // Prepare content
102                                                 $content = array(
103                                                         'id'       => $DATA['id'],
104                                                         'url'      => $DATA['url'],
105                                                         'time'     => createFancyTime($DATA['time']),
106                                                         'points'   => translateComma($DATA['points']),
107                                                         'category' => getCategory($DATA['cat_id']),
108                                                         'text'     => $DATA['text']
109                                                 );
110
111                                                 // Prepare the mail
112                                                 $mailText = loadEmailTemplate('bonus-mail', $content, $userid);
113
114                                                 // Send mail away
115                                                 if (isset($DATA['html_msg'])) {
116                                                         // Send HTML?
117                                                         sendEmail(getUserData('email'), $DATA['subject'], $mailText, $DATA['html_msg']);
118                                                 } else {
119                                                         // No HTML mail!
120                                                         sendEmail(getUserData('email'), $DATA['subject'], $mailText);
121                                                 }
122
123                                                 // Count one up and remove entry from dummy array
124                                                 $GLOBALS['pool_cnt']++; unset($dummy[$key]);
125
126                                                 if (getExtensionVersion('user') >= '0.1.4') {
127                                                         // Update mails received for receiver
128                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_received=emails_received+1 WHERE `userid`=%s LIMIT 1",
129                                                         array(bigintval($userid)), __FILE__, __LINE__);
130                                                 } // END - if
131
132                                                 // Do we have send maximum mails?
133                                                 if (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || (countSelection($dummy) == '0')) {
134                                                         // Yes, we have
135                                                         //* DEBUG: */ print("*EXIT/L:".__LINE__."<br />");
136                                                         break;
137                                                 } // END - if
138                                         } // END - if
139                                 } // END - if
140                         } // END - foreach
141
142                         // Update mediadata if version is 0.0.4 or higher
143                         if (getExtensionVersion('mediadata') >= '0.0.4') {
144                                 // Update entry (or add missing
145                                 $P = $GLOBALS['pool_cnt'];
146                                 if (!empty($cnt2) && empty($GLOBALS['pool_cnt'])) $P = $cnt2;
147                                 //* DEBUG: */ print("+MEDIA/L:".__LINE__.'/'.$P."+<br />");
148                                 updateMediadataEntry(array("total_send", "bonus_send"), 'add', $P);
149                         } // END - if
150
151                         // Close sending system
152                         //* DEBUG: */ print("-L:".__LINE__.'/'.countSelection($dummy)."-<br />");
153                         if (countSelection($dummy) == '0') {
154                                 // Queue reached!
155                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
156                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
157                                 //* DEBUG: */ print("*L:".__LINE__."*<br />");
158
159                                 // Update mediadata if version is 0.0.4 or higher
160                                 if (getExtensionVersion('mediadata') >= '0.0.4') {
161                                         // Update entry (or add missing)
162                                         //* DEBUG: */ print("*MEDIA/L:".__LINE__."*<br />");
163                                         updateMediadataEntry(array("total_orders", "bonus_orders"), 'add', 1);
164                                 } // END - if
165                         } elseif ($GLOBALS['pool_cnt'] >= getConfig('max_send')) {
166                                 // Update bonus pool
167                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='NEW', `target_send`=%s, `receivers`='%s' WHERE `id`=%s LIMIT 1",
168                                 array(countSelection($dummy), implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
169                                 //* DEBUG: */ print("*L:".__LINE__."<pre>");
170                                 //* DEBUG: */ print(print_r($dummy, true));
171                                 //* DEBUG: */ print("</pre>\n!!!<br />");
172                                 break;
173                         }
174                 }
175         }
176
177         // Free memory
178         SQL_FREERESULT($result_bonus);
179
180         // Remove variable
181         unset($mailText);
182 }
183
184 //
185 ?>