Possible fix for #125, applied fixes from profi-concept's branch
[mailer.git] / inc / modules / admin / what-adminedit.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/05/2003 *
4  * ===============                              Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-adminedit.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Edit the admin menu                              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Das Admin-Menue editieren                        *
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 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 // Add description as navigation point
46 ADD_DESCR('admin', __FILE__);
47
48 // Do we edit/delete/change main menus or sub menus?
49 $AND = "(`what` = '' OR `what` IS NULL)"; $SUB = '';
50 if (REQUEST_ISSET_GET('sub')) {
51         $AND = sprintf("`action`='%s' AND `what` IS NOT NULL", REQUEST_GET('sub'));
52         $SUB = REQUEST_GET('sub');
53 } // END - if
54
55 // Get count of (maybe) selected menu points
56 $chk = 0;
57 if (REQUEST_ISSET_POST('sel')) $chk = countPostSelection();
58
59 // List all menu points and make them editable
60 if ((REQUEST_ISSET_POST('edit')) && ($chk > 0) && (!IS_DEMO())) {
61         // Edit menu entries
62         // @TODO Kill all constants in this file
63         define('__SUB_VALUE', $SUB);
64         define('__CHK_VALUE', $chk);
65         $cnt = 0; $SW = 2;
66         foreach (REQUEST_POST('sel') as $sel => $confirm) {
67                 if ($confirm == 1) {
68                         $cnt++;
69                         $result = SQL_QUERY_ESC("SELECT title, action, what, descr FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE ".$AND." AND id=%s LIMIT 1",
70                         array(bigintval($sel)), __FILE__, __LINE__);
71                         if (SQL_NUMROWS($result) == 1) {
72                                 // Entry found so we load the stuff...
73                                 list($menu, $act, $wht, $descr) = SQL_FETCHROW($result);
74                                 SQL_FREERESULT($result);
75
76                                 // Prepare data for the row template
77                                 $content = array(
78                                         'action' => ADMIN_MAKE_MENU_SELECTION('admin', 'action', 'sel_action[' . $sel . ']', $act),
79                                         'what'   => ADMIN_MAKE_MENU_SELECTION('admin', 'what', 'sel_what[' . $sel . ']', $wht),
80                                         'sw'     => $SW,
81                                         'sel'    => $sel,
82                                         'menu'   => $menu,
83                                         'descr'  => $descr,
84                                         'cnt'    => $cnt,
85                                 );
86                                 $OUT .= LOAD_TEMPLATE('admin_amenu_edit_row', true, $content);
87                         } else {
88                                 // Entry not found?
89                                 $content = array(
90                                         'sw'  => $SW,
91                                         'sel' => $sel
92                                 );
93                                 $OUT .= LOAD_TEMPLATE('admin_menu_404_row', true, $content);
94                         }
95                         $SW = 3 - $SW;
96                 } // END - if
97         } // END - foreach
98
99         define('__MENU_ROWS', $OUT);
100         define('__CNT_VALUE', $cnt);
101
102         // Load template
103         LOAD_TEMPLATE('admin_amenu_edit_form');
104 } elseif ((REQUEST_ISSET_POST('del')) && (!IS_DEMO())) {
105         define('__SUB_VALUE', $SUB);
106         define('__CHK_VALUE', $chk);
107         // Del menu entries with or without confirmation
108         $SW = 2; $cnt = 0; $OUT = '';
109         foreach (REQUEST_POST('sel') as $sel => $confirm) {
110                 if ($confirm == 1) {
111                         $cnt++;
112                         $result = SQL_QUERY_ESC("SELECT title FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE ".$AND." AND id=%s LIMIT 1",
113                         array(bigintval($sel)), __FILE__, __LINE__);
114                         if (SQL_NUMROWS($result) == 1) {
115                                 // Entry found so we load the stuff...
116                                 list($menu) = SQL_FETCHROW($result);
117                                 SQL_FREERESULT($result);
118
119                                 // Prepare data for the row template
120                                 $content = array(
121                                         'menu' => $menu,
122                                         'cnt'  => $cnt,
123                                         'sel'  => $sel,
124                                         'sw'   => $SW
125                                 );
126                                 $OUT .= LOAD_TEMPLATE('admin_amenu_delete_row', true, $content);
127                         } else {
128                                 // Entry not found?
129                                 $content = array(
130                                         'sw'  => $SW,
131                                         'sel' => $sel
132                                 );
133                                 $OUT .= LOAD_TEMPLATE('admin_menu_404_row', true, $content);
134                         }
135                         $SW = 3 - $SW;
136                 }
137         }
138         define('__MENU_ROWS', $OUT);
139         define('__CNT_VALUE', $cnt);
140
141         // Load template
142         LOAD_TEMPLATE('admin_amenu_delete');
143 } elseif ((isFormSent()) && (!IS_DEMO())) {
144         // An action is done...
145         switch (REQUEST_POST('ok')) {
146                 case 'edit': // Edit menu
147                         foreach (REQUEST_POST('sel') as $sel => $menu) {
148                                 // Secure ID
149                                 $sel = bigintval($sel);
150
151                                 // Update entry
152                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admin_menu` SET
153 title='%s',
154 `action`='%s',
155 `what`='%s',
156 descr='%s'
157 WHERE ".$AND." AND id=%s LIMIT 1",
158                                 array(
159                                 $menu,
160                                 REQUEST_POST('sel_action', $sel),
161                                 REQUEST_POST('sel_what', $sel),
162                                 REQUEST_POST('sel_desc', $sel),
163                                 $sel,
164                                 ), __FILE__, __LINE__);
165                         }
166
167                         // Purge admin menu cache
168                         cachePurgeAdminMenu(0, REQUEST_POST('sel_action', $sel), REQUEST_POST('sel_what', $sel));
169
170                         // Load template
171                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('SETTINGS_SAVED'));
172                         break;
173
174                 case 'del': // Delete menu
175                         foreach (REQUEST_POST('sel') as $sel => $menu) {
176                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE ".$AND." AND id=%s LIMIT 1",
177                                 array(bigintval($sel)), __FILE__, __LINE__);
178                                 cachePurgeAdminMenu(0, '', '', $AND);
179                         } // END - foreach
180
181                         // Load template
182                         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('SETTINGS_SAVED'));
183                         break;
184
185                 default: // Unexpected action
186                         DEBUG_LOG(__FILE__, __LINE__, sprintf("Unsupported action %s detected.", REQUEST_POST('ok')));
187                         define('__OK_VALUE', REQUEST_POST('ok'));
188                         LOAD_TEMPLATE("admin_menu_unknown_okay");
189                         break;
190         }
191 } else {
192         if ((REQUEST_ISSET_GET(('act'))) && (REQUEST_ISSET_GET(('tid'))) && (REQUEST_ISSET_GET(('fid')))) {
193                 // Get IDs
194                 if (REQUEST_ISSET_GET(('w'))) {
195                         // Sub menus selected
196                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE `action`='%s' AND `sort`='%s' LIMIT 1",
197                         array(REQUEST_GET('act'), bigintval(REQUEST_GET('tid'))), __FILE__, __LINE__);
198                         list($tid) = SQL_FETCHROW($result);
199                         SQL_FREERESULT($result);
200                         $result = SQL_QUERY("SELECT `id` FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE `action`='%s' AND `sort`='%s' LIMIT 1",
201                         array(REQUEST_GET('act'), bigintval(REQUEST_GET('fid'))), __FILE__, __LINE__);
202                         list($fid) = SQL_FETCHROW($result);
203                         SQL_FREERESULT($result);
204                 } else {
205                         // Main menu selected
206                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE (`what`='' OR `what` IS NULL) AND `sort`='%s' LIMIT 1",
207                         array(bigintval(REQUEST_GET('tid'))), __FILE__, __LINE__);
208                         list($tid) = SQL_FETCHROW($result);
209                         SQL_FREERESULT($result);
210                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE (`what`='' OR `what` IS NULL) AND `sort`='%s' LIMIT 1",
211                         array(bigintval(REQUEST_GET('fid'))), __FILE__, __LINE__);
212                         list($fid) = SQL_FETCHROW($result);
213                         SQL_FREERESULT($result);
214                 }
215
216                 if ((!empty($tid)) && (!empty($fid))) {
217                         // Sort menu
218                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admin_menu` SET `sort`='%s' WHERE ".$AND." AND id=%s LIMIT 1",
219                         array(bigintval(REQUEST_GET('tid')), bigintval($fid)), __FILE__, __LINE__);
220                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admin_menu` SET `sort`='%s' WHERE ".$AND." AND id=%s LIMIT 1",
221                         array(bigintval(REQUEST_GET('fid')), bigintval($tid)), __FILE__, __LINE__);
222                         cachePurgeAdminMenu(0, '', '', $AND);
223                 }
224         }
225
226         // By default list menus
227         if (!empty($SUB)) {
228                 // Sub menus of a main menu
229                 $result = SQL_QUERY_ESC("SELECT id, action, what, title, sort FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE `action`='%s' AND `what` != '' AND `what` IS NOT NULL ORDER BY `sort` ASC",
230                 array($SUB), __FILE__, __LINE__);
231         } else {
232                 // Main menus
233                 $result = SQL_QUERY("SELECT id, action, what, title, sort FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE (`what`='' OR `what` IS NULL) ORDER BY `sort` ASC", __FILE__, __LINE__);
234         }
235
236         $max = SQL_NUMROWS($result);
237         if ($max > 0) {
238                 // @TODO Rewrite this constant
239                 define('__SUB_VALUE', $SUB);
240
241                 // Init variables
242                 $OUT = ''; $SW  = 2; $cnt = 0;
243
244                 // Process all entries
245                 while ($content = SQL_FETCHARRAY($result)) {
246                         // Count this entry
247                         $cnt++;
248
249                         // Init navigation variable
250                         $content['navi'] = '';
251                         if (($content['sort'] == 0) || (($content['sort'] == 1) && (!empty($SUB)))) {
252                                 // Is highest position
253                                 $content['navi'] = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$content['action']."&amp;w=".$content['what']."&amp;tid=".($content['sort']+1)."&amp;fid=".$content['sort']."\">{--LOWER--}</a>";
254                         } elseif ($cnt == $max) {
255                                 // Is lowest position
256                                 $content['navi'] = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$content['action']."&amp;w=".$content['what']."&amp;tid=".($content['sort']-1)."&amp;fid=".$content['sort']."\">{--HIGHER--}</a>";
257                         } elseif ($content['sort'] > 0) {
258                                 // Anything else between highest and lowest
259                                 $content['navi'] = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$content['action']."&amp;w=".$content['what']."&amp;tid=".($content['sort']-1)."&amp;fid=".$content['sort']."\">{--HIGHER--}</a>/<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$content['action']."&amp;w=".$content['what']."&amp;tid=".($content['sort']+1)."&amp;fid=".$content['sort']."\">{--LOWER--}</a>";
260                         }
261
262                         // Fix empty elements for constant (fixes display bugs in Firefox)
263                         if (empty($content['action'])) $content['action'] = '&nbsp;';
264                         if (empty($content['what']))   $content['what']   = '&nbsp;';
265                         if (empty($content['title']))  $content['title']  = '&nbsp;';
266
267                         // Add more data to $content
268                         $content['sw']   = $SW;
269                         $content['mode'] = 'admin';
270
271                         // Load row template and switch colors
272                         $OUT .= LOAD_TEMPLATE('admin_menu_overview_row', true, $content);
273                         $SW = 3 - $SW;
274                 } // END - switch
275
276                 // Free memory
277                 SQL_FREERESULT($result);
278
279                 // Rewrite this constant
280                 define('__MENU_ROWS', $OUT);
281
282                 // Load template
283                 LOAD_TEMPLATE('admin_amenu_edit');
284         } else {
285                 // Menu entries are missing... (???)
286                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_NO_MENUS_FOUND'));
287         }
288 }
289
290 //
291 ?>