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