1abebd0849796287bc3758fb22226e1b074cbb3e
[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  *                                                                      *
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 // Do we edit/delete/change main menus or sub menus?
44 $AND = "(`what` = '' OR `what` IS NULL)"; $SUB = "";
45 if (REQUEST_ISSET_GET(('sub')))
46 {
47         $AND = sprintf("action='%s' AND `what` IS NOT NULL", REQUEST_GET(('sub')));
48         $SUB = REQUEST_GET(('sub'));
49 }
50
51 // Get count of (maybe) selected menu points
52 $chk = 0;
53 if (REQUEST_ISSET_POST('sel')) $chk = SELECTION_COUNT(REQUEST_POST('sel'));
54
55 // List all menu points and make them editable
56 if ((REQUEST_ISSET_POST('edit')) && ($chk > 0) && (!IS_DEMO())) {
57         // Edit menu entries
58         define('__SUB_VALUE', $SUB);
59         define('__CHK_VALUE', $chk);
60         $cnt = 0; $SW = 2;
61         foreach (REQUEST_POST('sel') as $sel => $confirm)
62         {
63                 if ($confirm == 1)
64                 {
65                         $cnt++;
66                         $result = SQL_QUERY_ESC("SELECT title, action, what, descr FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE ".$AND." AND id=%s LIMIT 1",
67                          array(bigintval($sel)), __FILE__, __LINE__);
68                         if (SQL_NUMROWS($result) == 1)
69                         {
70                                 // Entry found so we load the stuff...
71                                 list($menu, $act, $wht, $descr) = SQL_FETCHROW($result);
72                                 SQL_FREERESULT($result);
73
74                                 // Prepare data for the row template
75                                 $content = array(
76                                         'action' => ADMIN_MAKE_MENU_SELECTION("admin", "action", "sel_action[".$sel."]", $act),
77                                         'what'   => ADMIN_MAKE_MENU_SELECTION("admin", "what", "sel_what[".$sel."]", $wht),
78                                         'sw'     => $SW,
79                                         'sel'    => $sel,
80                                         'menu'   => $menu,
81                                         'descr'  => $descr,
82                                         'cnt'    => $cnt,
83                                 );
84                                 $OUT .= LOAD_TEMPLATE("admin_amenu_edit_row", true, $content);
85                         }
86                          else
87                         {
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                 }
97         }
98         define('__MENU_ROWS', $OUT);
99         define('__CNT_VALUE', $cnt);
100
101         // Load template
102         LOAD_TEMPLATE("admin_amenu_edit_form");
103 }
104  elseif ((REQUEST_ISSET_POST('del')) && (!IS_DEMO()))
105 {
106         define('__SUB_VALUE', $SUB);
107         define('__CHK_VALUE', $chk);
108         // Del menu entries with or without confirmation
109         $SW = 2; $cnt = 0; $OUT = "";
110         foreach (REQUEST_POST('sel') as $sel => $confirm)
111         {
112                 if ($confirm == 1)
113                 {
114                         $cnt++;
115                         $result = SQL_QUERY_ESC("SELECT title FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE ".$AND." AND id=%s LIMIT 1",
116                          array(bigintval($sel)), __FILE__, __LINE__);
117                         if (SQL_NUMROWS($result) == 1)
118                         {
119                                 // Entry found so we load the stuff...
120                                 list($menu) = SQL_FETCHROW($result);
121                                 SQL_FREERESULT($result);
122
123                                 // Prepare data for the row template
124                                 $content = array(
125                                         'menu' => $menu,
126                                         'cnt'  => $cnt,
127                                         'sel'  => $sel,
128                                         'sw'   => $SW
129                                 );
130                                 $OUT .= LOAD_TEMPLATE("admin_amenu_delete_row", true, $content);
131                         }
132                          else
133                         {
134                                 // Entry not found?
135                                 $content = array(
136                                         'sw'  => $SW,
137                                         'sel' => $sel
138                                 );
139                                 $OUT .= LOAD_TEMPLATE("admin_menu_404_row", true, $content);
140                         }
141                         $SW = 3 - $SW;
142                 }
143         }
144         define('__MENU_ROWS', $OUT);
145         define('__CNT_VALUE', $cnt);
146
147         // Load template
148         LOAD_TEMPLATE("admin_amenu_delete");
149 } elseif ((IS_FORM_SENT()) && (!IS_DEMO())) {
150         // An action is done...
151         switch (REQUEST_POST('ok'))
152         {
153         case "edit": // Edit menu
154                 foreach (REQUEST_POST('sel') as $sel => $menu) {
155                         // Secure ID
156                         $sel = bigintval($sel);
157
158                         // Update entry
159                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admin_menu` SET
160 title='%s',
161 action='%s',
162 what='%s',
163 descr='%s'
164 WHERE ".$AND." AND id=%s LIMIT 1",
165  array(
166         $menu,
167         REQUEST_POST('sel_action', $sel),
168         REQUEST_POST('sel_what', $sel),
169         REQUEST_POST('sel_desc', $sel),
170         $sel,
171 ), __FILE__, __LINE__);
172                 }
173
174                 // Purge admin menu cache
175                 CACHE_PURGE_ADMIN_MENU(0, REQUEST_POST('sel_action', $sel), REQUEST_POST('sel_what', $sel));
176
177                 // Load template
178                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('SETTINGS_SAVED'));
179                 break;
180
181         case "del": // Delete menu
182                 foreach (REQUEST_POST('sel') as $sel => $menu) {
183                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE ".$AND." AND id=%s LIMIT 1",
184                                 array(bigintval($sel)), __FILE__, __LINE__);
185                         CACHE_PURGE_ADMIN_MENU(0, "", "", $AND);
186                 } // END - foreach
187
188                 // Load template
189                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('SETTINGS_SAVED'));
190                 break;
191
192         default: // Unexpected action
193                 DEBUG_LOG(__FILE__, __LINE__, sprintf("Unsupported action %s detected.", REQUEST_POST('ok')));
194                 define('__OK_VALUE', REQUEST_POST('ok'));
195                 LOAD_TEMPLATE("admin_menu_unknown_okay");
196                 break;
197         }
198 } else {
199         if ((REQUEST_ISSET_GET(('act'))) && (REQUEST_ISSET_GET(('tid'))) && (REQUEST_ISSET_GET(('fid')))) {
200                 // Get IDs
201                 if (REQUEST_ISSET_GET(('w'))) {
202                         // Sub menus selected
203                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE `action`='%s' AND `sort`='%s' LIMIT 1",
204                                 array(REQUEST_GET('act'), bigintval(REQUEST_GET('tid'))), __FILE__, __LINE__);
205                         list($tid) = SQL_FETCHROW($result);
206                         SQL_FREERESULT($result);
207                         $result = SQL_QUERY("SELECT id FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE `action`='%s' AND `sort`='%s' LIMIT 1",
208                                 array(REQUEST_GET('act'), bigintval(REQUEST_GET('fid'))), __FILE__, __LINE__);
209                         list($fid) = SQL_FETCHROW($result);
210                         SQL_FREERESULT($result);
211                 } else {
212                         // Main menu selected
213                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE (what='' OR `what` IS NULL) AND `sort`='%s' LIMIT 1",
214                                 array(bigintval(REQUEST_GET('tid'))), __FILE__, __LINE__);
215                         list($tid) = SQL_FETCHROW($result);
216                         SQL_FREERESULT($result);
217                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_admin_menu` WHERE (what='' OR `what` IS NULL) AND `sort`='%s' LIMIT 1",
218                                 array(bigintval(REQUEST_GET('fid'))), __FILE__, __LINE__);
219                         list($fid) = SQL_FETCHROW($result);
220                         SQL_FREERESULT($result);
221                 }
222
223                 if ((!empty($tid)) && (!empty($fid))) {
224                         // Sort menu
225                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admin_menu` SET `sort`='%s' WHERE ".$AND." AND id=%s LIMIT 1",
226                                 array(bigintval(REQUEST_GET('tid')), bigintval($fid)), __FILE__, __LINE__);
227                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_admin_menu` SET `sort`='%s' WHERE ".$AND." AND id=%s LIMIT 1",
228                                 array(bigintval(REQUEST_GET('fid')), bigintval($tid)), __FILE__, __LINE__);
229                         CACHE_PURGE_ADMIN_MENU(0, "", "", $AND);
230                 }
231         }
232
233         // By default list menus
234         if (!empty($SUB)) {
235                 // Sub menus of a main menu
236                 $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",
237                  array($SUB), __FILE__, __LINE__);
238         } else {
239                 // Main menus
240                 $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__);
241         }
242
243         $max = SQL_NUMROWS($result);
244         if ($max > 0) {
245                 define('__SUB_VALUE', $SUB);
246                 $SW  = 2;
247                 $cnt = 0;
248                 $OUT = "";
249                 while (list($id, $act, $wht, $title, $sort) = SQL_FETCHROW($result)) {
250                         $cnt++;
251                         if (($sort == 0) || (($sort == 1) && (!empty($SUB)))) {
252                                 // Is highest position
253                                 $NAVI = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$act."&amp;w=".$wht."&amp;tid=".($sort+1)."&amp;fid=".$sort."\">{--LOWER--}</a>";
254                         } elseif ($cnt == $max) {
255                                 // Is lowest position
256                                 $NAVI = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$act."&amp;w=".$wht."&amp;tid=".($sort-1)."&amp;fid=".$sort."\">{--HIGHER--}</a>";
257                         } elseif ($sort > 0) {
258                                 // Anything else between highest and lowest
259                                 $NAVI = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$act."&amp;w=".$wht."&amp;tid=".($sort-1)."&amp;fid=".$sort."\">{--HIGHER--}</a>/<a href=\"{!URL!}/modules.php?module=admin&amp;what=adminedit&amp;sub={!__SUB_VALUE!}&amp;act=".$act."&amp;w=".$wht."&amp;tid=".($sort+1)."&amp;fid=".$sort."\">{--LOWER--}</a>";
260                         }
261
262                         if (empty($act))   $act   = "&nbsp;";
263                         if (empty($wht))   $wht   = "&nbsp;";
264                         if (empty($title)) $title = "&nbsp;";
265
266                         $content = array(
267                                 'sw'     => $SW,
268                                 'id'     => $id,
269                                 'action' => $act,
270                                 'what'   => $wht,
271                                 'title'  => $title,
272                                 'navi'   => $NAVI,
273                                 'mode'   => "admin",
274                         );
275
276                         $OUT .= LOAD_TEMPLATE("admin_menu_overview_row", true, $content);
277                         $SW = 3 - $SW;
278                 }
279
280                 // Free memory
281                 SQL_FREERESULT($result);
282                 define('__MENU_ROWS', $OUT);
283
284                 // Load template
285                 LOAD_TEMPLATE("admin_amenu_edit");
286         } else {
287                 // Menu entries are missing... (???)
288                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('ADMIN_NO_MENUS_FOUND'));
289         }
290 }
291
292 //
293 ?>