Tons of rewrites (SQL queries), surfbar nearly finished (working: surfing with static...
[mailer.git] / inc / modules / admin / what-send_bonus.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/28/2003 *
4  * ===============                              Last change: 08/05/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-send_bonus.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Send out bonus mails to all members              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : An alle Mitglieder Bonus-Mails verschicken       *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!IS_ADMIN())) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!EXT_IS_ACTIVE("order")) {
39         ADD_FATAL(sprintf(EXTENSION_PROBLEM_NOT_INSTALLED, "order"));
40         return;
41 }
42
43 // Add description as navigation point
44 ADD_DESCR("admin", basename(__FILE__));
45
46 $whereStatement = " WHERE visible='Y'";
47 if (is_admin()) $whereStatement = "";
48 $EXT_HTML = EXT_IS_ACTIVE("html_mail");
49 if (empty($_GET['mode'])) $_GET['mode'] = "select";
50
51 if (isset($_POST['ok']))
52 {
53         // Check if category and number of receivers is okay
54         $CAT_TABS = "%s"; $CAT_WHERE = ""; $cat = "";
55         if ($_POST['cat'] > 0)
56         {
57                 // Select category
58                 $CAT_TABS  = "LEFT JOIN "._MYSQL_PREFIX."_user_cats AS c ON d.userid=c.userid";
59                 $cat = bigintval($_POST['cat']);
60                 $CAT_WHERE = " AND c.cat_id=%s";
61         }
62         if (GET_EXT_VERSION("holiday") >= "0.1.3")
63         {
64                 // Add something for the holiday extension
65                 $CAT_WHERE .= " AND d.holiday_active='N'";
66         }
67
68         if (($EXT_HTML) && ($_GET['mode'] == "html"))
69         {
70                 // Only include HTML receivers
71                 $result = SQL_QUERY_ESC("SELECT d.userid FROM "._MYSQL_PREFIX."_user_data AS d ".$CAT_TABS." WHERE d.status='CONFIRMED' AND d.html='Y'".$CAT_WHERE." ORDER BY d.%s %s LIMIT %s",
72                  array($cat, $_CONFIG['order_select'], $_CONFIG['order_mode'], bigintval($_POST['receiver'])), __FILE__, __LINE__);
73         }
74          else
75         {
76                 // Include all
77                 $result = SQL_QUERY_ESC("SELECT d.userid FROM "._MYSQL_PREFIX."_user_data AS d ".$CAT_TABS." WHERE d.status='CONFIRMED'".$CAT_WHERE." ORDER BY d.%s %s LIMIT %s",
78                  array($cat, $_CONFIG['order_select'], $_CONFIG['order_mode'], bigintval($_POST['receiver'])), __FILE__, __LINE__);
79         }
80         if ((SQL_NUMROWS($result) == $_POST['receiver']) && ($_POST['receiver'] > 0))
81         {
82                 $RECEIVER = "";
83                 while (list($REC) = SQL_FETCHROW($result))
84                 {
85                         // Add receiver when not empty
86                         if (!empty($REC)) $RECEIVER .= ";".$REC;
87                 }
88
89                 // Free memory
90                 SQL_FREERESULT($result);
91                 $RECEIVER = substr($RECEIVER, 1);
92
93                 // Gettings points is oka, so we can add $USED later from
94                 if ($EXT_HTML)
95                 {
96                         $HTML = "N";
97                         if ($_GET['mode'] == "html") $HTML = "Y";
98                         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_bonus
99 (subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, mails_sent, html_msg)
100 VALUES ('%s', '%s', '%s', '%s', '%s', 'NEW', UNIX_TIMESTAMP(), '%s', '%s', '%s', '%s', '%s')",
101  array(
102         $_POST['subject'],
103         addslashes($_POST['text']),
104         $RECEIVER,
105         $_POST['points'],
106         $_POST['seconds'],
107         $_POST['url'],
108         $cat,
109         SELECTION_COUNT(explode(";", $RECEIVER)),
110         bigintval($_POST['receiver']),
111         $HTML
112 ), __FILE__, __LINE__);
113                 }
114                  else
115                 {
116                         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_bonus
117 (subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, mails_sent)
118 VALUES ('%s', '%s', '%s', '%s', '%s', 'NEW', UNIX_TIMESTAMP(), '%s', '%s', '%s', '%s')",
119  array(
120         $_POST['subject'],
121         addslashes($_POST['text']),
122         $RECEIVER,
123         $_POST['points'],
124         $_POST['seconds'],
125         $_POST['url'],
126         $cat,
127         SELECTION_COUNT(explode(";", $RECEIVER)),
128         bigintval($_POST['receiver']),
129 ), __FILE__, __LINE__);
130                 }
131
132                 // Mail inserted into bonus pool
133                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_BONUS_SEND);
134         }
135          else
136         {
137                 // More entered than can be reached!
138                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_MORE_SELECTED);
139         }
140         if (!empty($URL))
141         {
142                 // Redirect to requested URL
143                 LOAD_URL($URL);
144         }
145 } else {
146         $result = SQL_QUERY("SELECT id, cat FROM "._MYSQL_PREFIX."_cats".$whereStatement." ORDER BY sort", __FILE__, __LINE__);
147         if (SQL_NUMROWS($result) > 0)
148         {
149                 $MORE = "";
150                 if ($_GET['mode'] == "html") $MORE = " AND html='Y'";
151                 if (GET_EXT_VERSION("holiday") >= "0.1.3")
152                 {
153                         // Add something for the holiday extension
154                         $MORE .= " AND holiday_active='N'";
155                 }
156
157                 $result_all = SQL_QUERY("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE status='CONFIRMED' AND receive_mails > 0".$MORE, __FILE__, __LINE__);
158                 define('__ALL_VALUE', SQL_NUMROWS($result_all));
159                 // Initialize array...
160                 $CATS = array(
161                         'id'   => array(),
162                         'name' => array(),
163                         'uids' => array()
164                 );
165                 // ... and begin loading stuff
166                 while (list($id, $cat) = SQL_FETCHROW($result))
167                 {
168                         $CATS['id'][]   = $id;
169                         $CATS['name'][] = $cat;
170                         $result_uids = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_cats WHERE cat_id=%s",
171                          array(bigintval($id)), __FILE__, __LINE__);
172                         $uid_cnt = "0";
173                         while (list($ucat) = SQL_FETCHROW($result_uids))
174                         {
175                                 $result_ver = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data
176 WHERE userid=%s AND status='CONFIRMED' AND receive_mails > 0".$MORE." LIMIT 1",
177  array(bigintval($ucat)), __FILE__, __LINE__);
178                                 $uid_cnt += SQL_NUMROWS($result_ver);
179
180                                 // Free memory
181                                 SQL_FREERESULT($result_ver);
182                         }
183
184                         // Free memory
185                         SQL_FREERESULT($result_uids);
186                         $CATS['uids'][] = $uid_cnt;
187                 }
188
189                 // Free memory
190                 SQL_FREERESULT($result);
191
192                 if (!empty($MSG))
193                 {
194                         // We got system message so we drop it out to the user
195                         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
196                 }
197                 if ($EXT_HTML)
198                 {
199                         // If HTML extension is active
200                         define('__HTML_SELECT',  LOAD_TEMPLATE("admin_send_bonus_html", true));
201                 }
202                  else
203                 {
204                         // And if not
205                         define('__HTML_SELECT', "");
206                 }
207
208                 // Generate options
209                 $OUT = "";
210                 foreach ($CATS['id'] as $key=>$value)
211                 {
212                         if (strlen($CATS['name'][$key]) > 20) $CATS['name'][$key] = substr($CATS['name'][$key], 0, 17)."...";
213                         $OUT .= "      <OPTION value=\"".$value."\">".$CATS['name'][$key]." (".$CATS['uids'][$key]." ".USER_IN_CAT.")</OPTION>\n";
214                 }
215                 define('__OPTION_LINES', $OUT);
216
217                 // Store send mode
218                 define('__MODE', $_GET['mode']);
219
220                 // Select template
221                 switch($_GET['mode'])
222                 {
223                 case "html":   // HTML mails
224                 case "normal": // Normal mails
225                         $template = "admin_send_bonus_form";
226                         break;
227
228                 case "select": // Selection
229                         $template = "admin_send_bonus_select";
230                         break;
231                 }
232
233                 // Load template
234                 LOAD_TEMPLATE($template);
235         }
236          else
237         {
238                 // No cateogries are defined yet
239                 OUTPUT_HTML("<STRONG><SPAN class=\"member_failed\">".MEMBER_NO_CATS."</SPAN></STRONG>");
240         }
241 }
242
243 //
244 ?>