7a3653dd3ab9e065342c4fe3e164ba4432a7fd16
[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 - 2015 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 } // END - if
42
43 // Don't run on daily reset
44 if ((isResetModeEnabled()) || (!isHtmlOutputMode())) {
45         // Skip here
46         return FALSE;
47 } elseif ((!isExtensionActive('bonus')) || (!isExtensionActive('other'))) {
48         // Abort if extension bonus/other are not active
49         return FALSE;
50 }
51
52 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
53 if ($GLOBALS['pool_cnt'] < getMaxSend()) {
54         // Default is ext-html_mail is gone
55         $add = '';
56
57         // Send out bonus mails?
58         if (isExtensionActive('html_mail')) {
59                 //  With HTML support
60                 $add = ', `html_msg`';
61         } // END - if
62
63         // Run query
64         $result_main = sqlQuery("SELECT
65         `id`,
66         `subject`,
67         `text`,
68         `receivers`,
69         `points`,
70         `time`,
71         `data_type`,
72         `timestamp`,
73         `url`,
74         `cat_id`,
75         `target_send`
76         " . $add . "
77 FROM
78         `{?_MYSQL_PREFIX?}_bonus`
79 WHERE
80         `data_type`='NEW'
81 ORDER BY
82         `timestamp` DESC", __FILE__, __LINE__);
83
84         // Some mails left?
85         if (!ifSqlHasZeroNums($result_main)) {
86                 // Init SQLs here
87                 initSqls();
88
89                 // Send these mails away...
90                 $count2 = '';
91                 while ($mailData = sqlFetchArray($result_main)) {
92                         // Message is active in queue
93                         sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
94                                 array(bigintval($mailData['id'])), __FILE__, __LINE__);
95
96                         // Default is no users left
97                         $receivers = array();
98
99                         // "Explode" all receivers into an array
100                         if (isInString(';', $mailData['receivers'])) {
101                                 // There's more than one receiver in the list...
102                                 $receivers = explode(';', $mailData['receivers']);
103                         } elseif (!empty($mailData['points'])) {
104                                 // Only one user left
105                                 $receivers = array($mailData['receivers']);
106                         }
107                         $temporaryReceivers = $receivers;
108
109                         /*
110                          * Now, if we are good little boys and girls Santa Claus left us
111                          * some user-ids and send out mails to them.
112                          */
113                         foreach ($receivers as $key => $userid) {
114                                 // Load personal data
115                                 if (fetchUserData($userid)) {
116                                         // The final receiver does exists so we can continue...
117                                         //* DEBUG: */ debugOutput('OK!/L:'.__LINE__);
118
119                                         // Remove receiver from list
120                                         $status = removeReceiver($temporaryReceivers, $key, $userid, $mailData['id'], $mailData['id'], TRUE);
121
122                                         // Did it work?
123                                         switch ($status) {
124                                                 case 'done': // Done!
125                                                         // Prepare the mail
126                                                         $mailText = loadEmailTemplate('member_bonus_pool_normal', $mailData, $userid);
127
128                                                         // Send mail away
129                                                         if (isset($mailData['html_msg'])) {
130                                                                 // Send HTML?
131                                                                 sendEmail(getUserData('userid'), $mailData['subject'], $mailText, $mailData['html_msg']);
132                                                         } else {
133                                                                 // No HTML mail!
134                                                                 sendEmail(getUserData('userid'), $mailData['subject'], $mailText);
135                                                         }
136
137                                                         // Count one up and remove entry from dummy array
138                                                         $GLOBALS['pool_cnt']++; unset($temporaryReceivers[$key]);
139
140                                                         // Is ext-user installed?
141                                                         if (isExtensionInstalledAndNewer('user', '0.1.4')) {
142                                                                 // Debug message
143                                                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',mailData[id]=' . $mailData['id']);
144                                                                 // Update mails received for receiver
145                                                                 addSql(sprintf("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1", bigintval($userid)));
146                                                         } // END - if
147
148                                                         // Is the size of pool reached or nothing to sent left?
149                                                         if (($GLOBALS['pool_cnt'] >= getMaxSend()) || (countSelection($temporaryReceivers) == 0)) {
150                                                                 // Yes, it is. So abort here
151                                                                 break;
152                                                         } // END - if
153                                                         break;
154
155                                                 default: // Unknown return type
156                                                         logDebugMessage(__FILE__, __LINE__, 'Unknown status ' . $status . ' detected. pool_id=' . $mailData['id'] . ',userid=' . $mailData['userid'] . ',stats_id=' . $stats_id);
157                                                         break;
158                                         } // END - switch
159                                 } // END - if
160                         } // END - foreach
161
162                         // Run all SQLs
163                         runFilterChain('run_sqls');
164
165                         // Update mediadata if version is 0.0.4 or higher
166                         if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
167                                 // Update entry (or add missing
168                                 $P = $GLOBALS['pool_cnt'];
169                                 if (!empty($count2) && empty($GLOBALS['pool_cnt'])) $P = $count2;
170                                 //* DEBUG: */ debugOutput('+MEDIA/L:'.__LINE__.'/'.$P.'+');
171                                 updateMediadataEntry(array('total_send', 'bonus_send'), 'add', $P);
172                         } // END - if
173
174                         // Close sending system
175                         //* DEBUG: */ debugOutput('-L:'.__LINE__.'/'.countSelection($temporaryReceivers).'-');
176                         if (countSelection($temporaryReceivers) == 0) {
177                                 // Queue reached!
178                                 sqlQueryEscaped("UPDATE
179         `{?_MYSQL_PREFIX?}_bonus`
180 SET
181         `data_type`='SEND',
182         `target_send`=0,
183         `receivers`=''
184 WHERE
185         `id`=%s
186 LIMIT 1",
187                                         array(bigintval($mailData['id'])), __FILE__, __LINE__);
188                                 //* DEBUG: */ debugOutput('*L:'.__LINE__.'*');
189
190                                 // Update mediadata if version is 0.0.4 or higher
191                                 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
192                                         // Update entry (or add missing)
193                                         //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
194                                         updateMediadataEntry(array('total_orders', 'bonus_orders'), 'add', 1);
195                                 } // END - if
196                         } elseif ($GLOBALS['pool_cnt'] >= getMaxSend()) {
197                                 // Update bonus pool
198                                 sqlQueryEscaped("UPDATE
199         `{?_MYSQL_PREFIX?}_bonus`
200 SET
201         `data_type`='NEW',
202         `target_send`=%s,
203         `receivers`='%s'
204 WHERE
205         `id`=%s
206 LIMIT 1",
207                                         array(
208                                                 countSelection($temporaryReceivers),
209                                                 implode(';', $temporaryReceivers),
210                                                 bigintval($mailData['id'])
211                                         ), __FILE__, __LINE__);
212                                 //* DEBUG: */ debugOutput('*L:'.__LINE__.'<pre>'.print_r($temporaryReceivers, TRUE).'</pre>!!!');
213
214                                 // Run any SQLs
215                                 runFilterChain('run_sqls');
216                                 break;
217                         }
218                 } // END - while
219         } // END - if
220
221         // Free memory
222         sqlFreeResult($result_main);
223
224         // Remove variable
225         unset($mailText);
226 } // END - if
227
228 // [EOF]
229 ?>