Encapsulation and fixes:
[mailer.git] / inc / modules / admin / what-admins_mails.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if ((!defined('__SECURITY')) || (!isAdmin())) {
42         die();
43 } // END - if
44
45 // Add description as navigation point
46 addMenuDescription('admin', __FILE__);
47
48 if (isFormSent('edit')) {
49         // Check if entires are checked
50         if (countPostSelection() > 0) {
51                 // Add option for events
52                 $adminsList = generateOptionList('admins', 'id', 'login', '', 'email');
53                 $SW = 2; $rows = '';
54                 foreach (postRequestParameter('sel') as $template => $sel) {
55                         // First of all load data from DB
56                         $result = SQL_QUERY_ESC("SELECT `admin_id`, `id` FROM `{?_MYSQL_PREFIX?}_admins_mails` WHERE `mail_template`='%s' ORDER BY `id` ASC",
57                                 array($template), __FILE__, __LINE__);
58                         $OUT = '';
59                         $adminId2 = '0'; $id = '0';
60                         while ($content = SQL_FETCHARRAY($result)) {
61                                 // @TODO Can this be rewritten???
62                                 $OUT .= "<select name=\"admin_id[".$content['id']."]\" size=\"1\" class=\"admin_select\">
63 <option value=\"0\"";
64                                 if ($content['admin_id'] == '0') $OUT .= ' selected="selected"';
65                                 $OUT .=">{--ADMINS_ALL_ADMINS--}</option>\n";
66                                 if (isExtensionActive('events')) {
67                                         $OUT .= "<option value=\"-1\"";
68                                         if ($content['admin_id'] == '-1') $OUT .= ' selected="selected"';
69                                         $OUT .= ">{--ADMINS_TO_USER_EVENTS--}</option>\n";
70                                 }
71                                 $OUT .= generateOptionList('admins', 'id', 'login', $content['admin_id'], 'email');
72                                 $OUT .= "</select>\n<br />\n";
73
74                                 // Transfer ids
75                                 $adminId2 = $content['admin_id'];
76                                 $id = $content['id'];
77                         } // END - while
78
79                         // Free memory
80                         SQL_FREERESULT($result);
81
82                         // Prepare content
83                         $content['id']            = $id;
84                         $content['sw']            = $SW;
85                         $content['mail_template'] = $template;
86                         $content['admins']        = $adminsList;
87
88                         if ($adminId2 > 0) {
89                                 // Add form for an additional admin
90                                 $OUT .= loadTemplate('admin_admins_mails_edit_form', true, $content);
91                         } // END - if
92
93                         // Save full rendered content
94                         $content['content'] = $OUT;
95
96                         // Start outputing line
97                         $rows .= loadTemplate('admin_admins_mails_edit_row', true, $content);
98                         $SW = 3 - $SW;
99                 }
100
101                 // Edit checked entries
102                 loadTemplate('admin_admins_mails_edit', false, $rows);
103         } else {
104                 // Nothing selected
105                 loadTemplate('admin_settings_saved', false, getMessage('ADMINS_NO_MAIL_CHECKED'));
106         }
107 } else {
108         // Load all assigned mails
109         $result = SQL_QUERY("SELECT
110         m.id, m.admin_id, a.login, m.mail_template
111 FROM
112         `{?_MYSQL_PREFIX?}_admins_mails` AS m
113 LEFT JOIN
114         `{?_MYSQL_PREFIX?}_admins` AS a
115 ON
116         m.admin_id=a.id
117 WHERE
118         (m.admin_id=a.id OR m.admin_id < 1) AND m.mail_template != ''
119 ORDER BY
120         m.admin_id ASC,
121         m.mail_template ASC", __FILE__, __LINE__);
122
123         if (SQL_NUMROWS($result) > 0) {
124                 // Shall I change entries?
125                 if (isFormSent('change')) {
126                         // Init SQLs
127                         initSqls();
128
129                         // Ok, update database
130                         foreach (postRequestParameter('admin_id') as $id => $content['admin_id']) {
131                                 // Secure ids
132                                 $id  = bigintval($id);
133                                 $content['admin_id'] = bigintval($content['admin_id']);
134
135                                 // Update entry
136                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_admins_mails` SET admin_id=%s WHERE `id`=%s ORDER BY `id` LIMIT 1",
137                                         array($content['admin_id'], $id), __FILE__, __LINE__, false));
138
139                                 if (($content['admin_id'] < 1) && (isPostRequestParameterSet('template', $id))) {
140                                         // Remove any other admin entries
141                                         addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_admins_mails` WHERE mail_template='%s' AND id != '%s'",
142                                         array(postRequestParameter('template', $id), $id), __FILE__, __LINE__, false));
143                                 } // END - if
144
145                                 if (postRequestParameter('admin_new', postRequestParameter('template', $id)) > 0) {
146                                         // Add new admin
147                                         addSql(SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_admins_mails` (admin_id, mail_template) VALUES ('%s','%s')",
148                                         array($content['admin_id'], postRequestParameter('template', $id)), __FILE__, __LINE__, false));
149                                 } // END - if
150                         } // END - foreach
151
152                         // Run all queries
153                         runFilterChain('run_sqls');
154
155                         // Query again...
156                         $result = SQL_QUERY("SELECT
157         m.id, m.admin_id, a.login, m.mail_template
158 FROM
159         `{?_MYSQL_PREFIX?}_admins_mails` AS m
160 LEFT JOIN
161         `{?_MYSQL_PREFIX?}_admins` AS a
162 ON
163         m.admin_id=a.id
164 WHERE
165         (m.admin_id=a.id OR m.admin_id < 1) AND m.mail_template != ''
166 ORDER BY
167         m.admin_id ASC,
168         m.mail_template ASC", __FILE__, __LINE__);
169                 } // END - if
170
171                 // List found entries
172                 $OUT = ''; $SW = 2;
173                 while ($content = SQL_FETCHARRAY($result)) {
174                         // Prepare content
175                         $content['sw']   = $SW;
176                         $content['alnk'] = generateAdminLink($content['admin_id']);
177
178                         // Load row template
179                         $OUT .= loadTemplate('admin_admins_mails_list_row', true, $content);
180                         $SW = 3 - $SW;
181                 } // END - while
182
183                 // Free result
184                 SQL_FREERESULT($result);
185
186                 // Load template
187                 loadTemplate('admin_admins_mails_list', false, $OUT);
188         } else {
189                 // No entries found
190                 loadTemplate('admin_settings_saved', false, getMessage('ADMINS_MAILS_NO_ENTRIES'));
191         }
192 }
193
194 // [EOF]
195 ?>