A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / modules / admin / what-admins_mails.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/14/2004 *
4  * ================                             Last change: 10/22/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-admins_mails.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Control which admin shall receive which mail     *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Stellen Sie ein, welcher Admin welche Mail erh.  *
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 ((!defined('__SECURITY')) || (!IS_ADMIN())) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Add description as navigation point
41 ADD_DESCR("admin", __FILE__);
42
43 if (REQUEST_ISSET_POST('edit')) {
44         // Check if entires are checked
45         $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
46         if ($SEL > 0) {
47                 // Add option for events
48                 $GLOBALS['cache_array']['admins'] = ADD_OPTION_LINES("admins", "id", "login", "", "email");
49                 $SW = 2; $rowNameS = "";
50                 foreach (REQUEST_POST('sel') as $template => $sel) {
51                         // First of all load data from DB
52                         $result = SQL_QUERY_ESC("SELECT admin_id, id FROM `{!_MYSQL_PREFIX!}_admins_mails` WHERE mail_template='%s' ORDER BY `id`",
53                                 array($template), __FILE__, __LINE__);
54                         $OUT = ""; $aid2 = 0;
55                         while ($content = SQL_FETCHARRAY($result)) {
56                                 // @TODO Can this be rewritten???
57                                 $OUT .= "<select name=\"admin_id[".$content['id']."]\" size=\"1\" class=\"admin_select\">
58 <option value=\"0\"";
59                                 if ($content['admin_id'] == "0") $OUT .= " selected=\"selected\"";
60                                 $OUT .=">{--ADMINS_ALL_ADMINS--}</option>\n";
61                                 if (EXT_IS_ACTIVE("events")) {
62                                         $OUT .= "<option value=\"-1\"";
63                                         if ($content['admin_id'] == "-1") $OUT .= " selected=\"selected\"";
64                                         $OUT .= ">{--ADMINS_TO_USER_EVENTS--}</option>\n";
65                                 }
66                                 $OUT .= ADD_OPTION_LINES("admins", "id", "login", $content['admin_id'], "email");
67                                 $OUT .= "</select>\n<br />\n";
68                                 $aid2 = $content['admin_id']; $id2 = $content['id'];
69                         }
70
71                         // Free memory
72                         SQL_FREERESULT($result);
73
74                         $content = array(
75                                 'sw'      => $SW,
76                                 'id'      => $id,
77                                 'id2'     => $id2,
78                                 'tpl'     => $template,
79                                 'admins'  => $GLOBALS['cache_array']['admins']
80                         );
81
82                         if ($aid2 > 0) {
83                                 // Add form for an additional admin
84                                 $OUT .= LOAD_TEMPLATE("admin_admins_mails_edit_form", true, $content);
85                         }
86
87                         // Save full rendered content
88                         $content['content'] = $OUT;
89
90                         // Start outputing line
91                         $rowNameS .= LOAD_TEMPLATE("admin_admins_mails_edit_row", true, $content);
92                         $SW = 3 - $SW;
93                 }
94
95                 // @TODO Rewrite this constant
96                 define('__ADMINS_ROWS', $rowNameS);
97
98                 // Edit checked entries
99                 LOAD_TEMPLATE("admin_admins_mails_edit");
100         } else {
101                 // Nothing selected
102                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMINS_NO_MAIL_CHECKED'));
103         }
104 } else {
105         // Load all assigned mails
106         $result = SQL_QUERY("SELECT DISTINCT m.id, m.admin_id, a.login, m.mail_template
107 FROM `{!_MYSQL_PREFIX!}_admins_mails` AS m
108 LEFT JOIN `{!_MYSQL_PREFIX!}_admins` AS a
109 ON m.admin_id=a.id
110 WHERE (m.admin_id=a.id OR m.admin_id < 1) AND m.mail_template != ''
111 ORDER BY m.admin_id, m.mail_template", __FILE__, __LINE__);
112
113         if (SQL_NUMROWS($result) > 0) {
114                 // Shall I change entries?
115                 if (REQUEST_ISSET_POST(('change'))) {
116                         // Init SQLs
117                         INIT_SQLS();
118
119                         // Ok, update database
120                         foreach (REQUEST_POST('admin_id') as $id => $content['admin_id']) {
121                                 // Secure IDs
122                                 $id  = bigintval($id);
123                                 $content['admin_id'] = bigintval($content['admin_id']);
124
125                                 // Update entry
126                                 ADD_SQL(SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admins_mails` SET admin_id=%s WHERE id=%s ORDER BY `id` LIMIT 1",
127                                         array($content['admin_id'], $id), __FILE__, __LINE__, false));
128
129                                 if (($content['admin_id'] < 1) && (REQUEST_ISSET_POST(('template', $id)))) {
130                                         // Remove any other admin entries
131                                         ADD_SQL(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admins_mails` WHERE mail_template='%s' AND id != '%s'",
132                                                 array(REQUEST_POST('template', $id), $id), __FILE__, __LINE__, false));
133                                 }
134
135                                 if (REQUEST_POST('admin_new', REQUEST_POST('template', $id)) > 0) {
136                                         // Add new admin
137                                         ADD_SQL(SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_admins_mails` (admin_id, mail_template) VALUES ('%s','%s')",
138                                                 array($content['admin_id'], REQUEST_POST('template', $id)), __FILE__, __LINE__, false));
139                                 }
140                         }
141
142                         // Run all queries
143                         runFilterChain('run_sqls');
144
145                         // Query again...
146                         $result = SQL_QUERY("SELECT DISTINCT m.id, m.admin_id, a.login, m.mail_template FROM `{!_MYSQL_PREFIX!}_admins_mails` AS m, `{!_MYSQL_PREFIX!}_admins` AS a WHERE m.admin_id=a.id OR m.admin_id < 1 ORDER BY m.admin_id", __FILE__, __LINE__);
147                 }
148
149                 // List found entries
150                 $OUT = ""; $SW = 2;
151                 while ($data = SQL_FETCHARRAY($result)) {
152                         // Prepare content
153                         $content = array(
154                                 'sw'   => $SW,
155                                 'id'   => $data['id'],
156                                 'tpl'  => $data['mail_template'],
157                                 'alnk' => GENERATE_AID_LINK($data['admin_id'])
158                         );
159
160                         // Load row template
161                         $OUT .= LOAD_TEMPLATE("admin_admins_mails_list_row", true, $content);
162                         $SW = 3 - $SW;
163                 }
164
165                 // Free result
166                 SQL_FREERESULT($result);
167
168                 // @TODO Rewrite this constant
169                 define('__MAILS_ROWS', $OUT);
170
171                 // Load template
172                 LOAD_TEMPLATE("admin_admins_mails_list");
173         } else {
174                 // No entries found
175                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMINS_MAILS_NO_ENTRIES'));
176         }
177 }
178
179 //
180 ?>