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