Huge rewrite:
[mailer.git] / inc / modules / admin / what-del_email.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 02/12/2004 *
4  * ================                             Last change: 02/27/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-del_email.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Delete a bonus or normal mail                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Bonus- / Normal-Mail loeschen                    *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  *  Module by Robert Niedziela, Megacomputing                           *
15  *          web : http://mc-p.mcserver.de                               *
16  *                                                                      *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
19  * For more information visit: http://www.mxchange.org                  *
20  *                                                                      *
21  * This program is free software; you can redistribute it and/or modify *
22  * it under the terms of the GNU General Public License as published by *
23  * the Free Software Foundation; either version 2 of the License, or    *
24  * (at your option) any later version.                                  *
25  *                                                                      *
26  * This program is distributed in the hope that it will be useful,      *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
29  * GNU General Public License for more details.                         *
30  *                                                                      *
31  * You should have received a copy of the GNU General Public License    *
32  * along with this program; if not, write to the Free Software          *
33  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
34  * MA  02110-1301  USA                                                  *
35  ************************************************************************/
36
37 // Some security stuff...
38 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
39         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
40         require($INC);
41 }
42
43 // Add description as navigation point
44 ADD_DESCR('admin', __FILE__);
45
46 // Init counter for deleted mails
47 $cnt = 0;
48
49 if (REQUEST_ISSET_GET('mid')) {
50         // Load email data
51         $result = SQL_QUERY_ESC("SELECT `id`, `sender`, `subject`, `url`, `timestamp`, `payment_id` FROM `{!_MYSQL_PREFIX!}_pool` WHERE `id`=%s LIMIT 1",
52                 array(bigintval(REQUEST_GET('mid'))), __FILE__, __LINE__);
53
54         // Delete mail only once
55         if (SQL_NUMROWS($result) == 1) {
56                 // Load data
57                 list ($id, $sender, $subject, $url, $timestamp, $payId) = SQL_FETCHROW($result);
58                 SQL_FREERESULT($result);
59
60                 // Get points we shall pay back per mail
61                 $price = getPaymentPoints($payId, 'price');
62
63                 // Prepare data for the template
64                 define('__ID'     , $id);
65                 define('__SENDER' , generateUserProfileLink($sender));
66                 define('__SUBJECT', $subject);
67                 define('__URL'    , DEREFERER($url));
68                 define('__ORDERED', generateDateTime($timestamp, '0'));
69
70                 // Load template
71                 LOAD_TEMPLATE("admin_del_email_normal");
72
73                 // Transfer data to data array
74                 $DATA = array('url' => $url, 'subject' => $subject);
75
76                 // Load email template and send the email away
77                 $message_user = LOAD_EMAIL_TEMPLATE('order-deleted', array(), $sender);
78                 sendEmail($sender, getMessage('MEMBER_ORDER_DELETED'), $message_user);
79
80                 // Fetch right stats_id from pool
81                 $result_pool = SQL_QUERY_ESC("SELECT
82         s.id
83 FROM
84         `{!_MYSQL_PREFIX!}_user_stats` AS s
85 LEFT JOIN
86         `{!_MYSQL_PREFIX!}_pool` AS p
87 ON
88         s.pool_id=p.id
89 WHERE
90         s.pool_id=%s
91 LIMIT 1",
92                         array(bigintval(REQUEST_GET('mid'))), __FILE__, __LINE__);
93                 if (SQL_NUMROWS($result_pool) == 1) {
94                         // Fetch stats id
95                         list($stats_id) = SQL_FETCHROW($result_pool);
96
97                         // Get all user links
98                         $links = GET_TOTAL_DATA($stats_id, 'user_links', 'userid', 'stats_id', true);
99
100                         // Reset sent mails for recipient(s)
101                         reduceRecipientReceivedMails('stats_id', REQUEST_GET('mid'), $links);
102
103                         // Calc total points and pay them back
104                         $totalPoints = $links * $price;
105
106                         // Shall we pay the points back to the user?
107                         if (getConfig('repay_deleted_mails') != 'SHRED') {
108                                 //* DEBUG: */ echo $stats_id.':'.$totalPoints.'/'.$links.'/'.$price."<br />\n";
109                                 if ($totalPoints > 0) {
110                                         // Shall we payback to user or jackpot?
111                                         if (getConfig('repay_deleted_mails') == 'JACKPOT') {
112                                                 // Set jackpot
113                                                 $sender = 0;
114                                         } // END - if
115
116                                         // Pay back points
117                                         //* DEBUG: */ echo "PAYBACK:".$sender."<br />\n";
118                                         ADD_POINTS_REFSYSTEM('mail_deleted', $sender, $totalPoints, true, '0', false,'direct');
119
120                                         // Output message
121                                         if (getConfig('repay_deleted_mails') == 'REPAY') {
122                                                 // Repayed
123                                                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('ADMIN_POINTS_REPAYED'),
124                                                 translateComma($totalPoints)
125                                                 ));
126                                         } else {
127                                                 // To jackpot
128                                                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('ADMIN_POINTS_TO_JACKPOT'),
129                                                 translateComma($totalPoints)
130                                                 ));
131                                         }
132                                 } else {
133                                         // No points repayed!
134                                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_NO_POINTS_REPAYED'));
135                                 }
136                         } else {
137                                 // Points shredded!
138                                 LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('ADMIN_POINTS_SHREDDED'), $totalPoints));
139                         }
140
141                         // Remove links from DB
142                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_links` WHERE `stats_id`=%s",
143                         array(bigintval($stats_id)), __FILE__, __LINE__);
144                         $cnt += SQL_AFFECTEDROWS();
145
146                         // Load template for link
147                         LOAD_TEMPLATE('admin_settings_saved', false, "<a href=\"{!URL!}/modules.php?module=admin&amp;what=del_email&amp;pid=".bigintval(REQUEST_GET('mid'))."\">".ADMIN_REMOVE_STATS_ENTRY."</a>");
148                 } // END - if
149
150                 // Free the result
151                 SQL_FREERESULT($result_pool);
152
153                 // Delete mail from queue
154                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_pool` WHERE `id`=%s LIMIT 1",
155                         array(bigintval(REQUEST_GET('mid'))), __FILE__, __LINE__);
156                 $cnt += SQL_AFFECTEDROWS();
157
158                 // Output link for manually removing stats entry
159                 OUTPUT_HTML(sprintf(ADMIN_DELETED_MAILS_COUNT, $cnt));
160         } else {
161                 // Mail already deleted!
162                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_NORMAL_MAIL_ALREADY_DELETED'));
163         }
164 } elseif (REQUEST_ISSET_GET('pid')) {
165         // Remove stats entries
166         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_stats` WHERE `pool_id`=%s LIMIT 1",
167         array(bigintval(REQUEST_GET('pid'))), __FILE__, __LINE__);
168
169         // Output message
170         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_USER_STATS_REMOVED'));
171 } elseif ((REQUEST_ISSET_GET('bid')) && (EXT_IS_ACTIVE('bonus'))) {
172         // Load data from bonus mail
173         $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `url`, `timestamp`, `mails_sent` FROM `{!_MYSQL_PREFIX!}_bonus` WHERE `id`=%s",
174                 array(bigintval(REQUEST_GET('bid'))), __FILE__, __LINE__);
175
176         // Delete mail only once
177         if (SQL_NUMROWS($result) == 1) {
178                 // Load data
179                 list($id, $subject, $url, $timestamp, $sent) = SQL_FETCHROW($result);
180
181                 // Reset sent mails for recipient(s)
182                 reduceRecipientReceivedMails('bonus_id', REQUEST_GET('bid'), $sent);
183
184                 // Init counter for deleted mails
185                 $cnt = 0;
186
187                 // Delete bonus mail entirely from database
188                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_bonus` WHERE `id`=%s LIMIT 1",
189                         array(bigintval(REQUEST_GET('bid'))), __FILE__, __LINE__);
190                 $cnt += SQL_AFFECTEDROWS();
191                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_links` WHERE `bonus_id`=%s",
192                         array(bigintval(REQUEST_GET('bid'))), __FILE__, __LINE__);
193                 $cnt += SQL_AFFECTEDROWS();
194
195                 // Prepare data for the template
196                 define('__ID'     , $id);
197                 define('__SUBJECT', $subject);
198                 define('__URL'    , DEREFERER($url));
199                 define('__ORDERED', generateDateTime($timestamp, '0'));
200                 define('__CNT'    , $cnt);
201
202                 // Load template
203                 LOAD_TEMPLATE('admin_del_email_bonus');
204         } else {
205                 // Mail already deleted!
206                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_BONUS_MAIL_ALREADY_DELETED'));
207         }
208
209         // Free result
210         SQL_FREERESULT($result);
211 } elseif ((REQUEST_ISSET_GET('nid')) && (GET_EXT_VERSION('bonus') >= '0.8.7')) {
212         // Load data from bonus mail
213         $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `url`, `timestamp` FROM `{!_MYSQL_PREFIX!}_bonus` WHERE `id`=%s",
214                 array(bigintval(REQUEST_GET('nid'))), __FILE__, __LINE__);
215
216         // Delete mail only once
217         if (SQL_NUMROWS($result) == 1) {
218                 // Load data
219                 list ($id, $subject, $url, $timestamp) = SQL_FETCHROW($result);
220                 SQL_FREERESULT($result);
221
222                 // Init counter for deleted mails
223                 $cnt = 0;
224
225                 // Delete bonus mail entirely from database
226                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_bonus` WHERE `id`=%s LIMIT 1",
227                         array(bigintval(REQUEST_GET('nid'))), __FILE__, __LINE__);
228                 $cnt += SQL_AFFECTEDROWS();
229                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_links` WHERE `bonus_id`=%s",
230                         array(bigintval(REQUEST_GET('nid'))), __FILE__, __LINE__);
231                 $cnt += SQL_AFFECTEDROWS();
232
233                 // Prepare data for the template
234                 define('__ID'     , $id);
235                 define('__SUBJECT', $subject);
236                 define('__URL'    , DEREFERER($url));
237                 define('__ORDERED', generateDateTime($timestamp, '0'));
238                 define('__CNT'    , $cnt);
239
240                 // Load template
241                 LOAD_TEMPLATE('admin_del_email_notify');
242         } else {
243                 // Mail already deleted!
244                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_NOTIFY_MAIL_ALREADY_DELETED'));
245         }
246 } else {
247         // No mail orders fond
248         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_WRONG_CALL'));
249 }
250
251 //
252 ?>