Some more rewrites and fix for countPostSelection()
[mailer.git] / inc / modules / admin / what-memedit.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/28/2003 *
4  * ===============                              Last change: 05/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-memedit.php                                 *
8  * -------------------------------------------------------------------- *
9  * Short description : Edit member's menu                               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Editieren Sie das Menue fuer Ihre Mitglieder     *
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 } // END - if
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
50 if (isGetRequestElementSet('sub')) {
51         $AND = sprintf("`action`='%s' AND `what` IS NOT NULL", getRequestElement('sub'));
52         $SUB = getRequestElement('sub');
53 } // END - if
54
55 // List all menu points and make them editable
56 if ((isPostRequestElementSet('edit')) && (countPostSelection() > 0) && (!isDemoModeActive())) {
57         // Edit menu entries
58         $content['sub'] = $SUB;
59         $content['chk'] = countPostSelection();
60         $SW = 2; $cnt = '0'; $OUT = '';
61         foreach (postRequestElement('sel') as $sel => $confirm) {
62                 if ($confirm == 1) {
63                         $cnt++;
64                         $result = SQL_QUERY_ESC("SELECT `title`, `action`, `what` FROM `{?_MYSQL_PREFIX?}_member_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                                 $data = array(
70                                         'cnt'    => $cnt,
71                                         'sel'    => $sel,
72                                         'menu'   => $data['title'],
73                                         'sw'     => $SW,
74                                         'act'    => adminAddMenuSelectionBox('member', 'act' , 'sel_act['  . $sel . ']', $data['action']),
75                                         'what'   => adminAddMenuSelectionBox('member', 'what', 'sel_what[' . $sel . ']', $data['what']),
76                                 );
77
78                                 // Load template
79                                 $OUT .= loadTemplate('admin_mmenu_edit_row', true, $data);
80                         } else {
81                                 // Entry not found?
82                                 $data = array(
83                                         'sw'  => $SW,
84                                         'sel' => $sel
85                                 );
86
87                                 // Load template
88                                 $OUT .= loadTemplate('admin_menu_404_row', true, $data);
89                         }
90                         SQL_FREERESULT($result);
91                         $SW = 3 - $SW;
92                 }
93         }
94         $content['rows'] = $OUT;
95         $content['cnt'] = $cnt;
96
97         // Load template
98         loadTemplate('admin_mmenu_edit', false, $content);
99 } elseif ((isPostRequestElementSet('del')) && (countPostSelection() > 0) && (!isDemoModeActive())) {
100         // Del menu entries with or without confirmation
101         $content['sub'] = $SUB;
102         $content['chk'] = countPostSelection();
103         $SW = 2; $cnt = '0'; $OUT = '';
104         foreach (postRequestElement('sel') as $sel => $confirm) {
105                 if ($confirm == 1) {
106                         $cnt++;
107                         $result = SQL_QUERY_ESC("SELECT `title` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE ".$AND." AND `id`=%s LIMIT 1",
108                                 array(bigintval($sel)), __FILE__, __LINE__);
109                         if (SQL_NUMROWS($result) == 1) {
110                                 // Entry found so we load the stuff...
111                                 list($title) = SQL_FETCHROW($result);
112                                 $content = array(
113                                         'cnt'   => $cnt,
114                                         'sel'   => $sel,
115                                         'title' => $title
116                                 );
117
118                                 // Load template
119                                 $OUT .= loadTemplate('admin_mmenu_delete_row', true, $content);
120                         } else {
121                                 // Entry not found?
122                                 $content = array(
123                                         'sw'  => $SW,
124                                         'sel' => $sel
125                                 );
126
127                                 // Load template
128                                 $OUT .= loadTemplate('admin_menu_404_row', true, $content);
129                         }
130                         SQL_FREERESULT($result);
131                 } // END - if
132         } // END - foreach
133         $content['rows'] = $OUT;
134         $content['cnt'] = $cnt;
135
136         // Load template
137         loadTemplate('admin_mmenu_delete', false, $content);
138 } elseif ((isPostRequestElementSet('status')) && (countPostSelection() > 0) && (!isDemoModeActive())) {
139         // Change status (visible / locked)
140         $content['sub'] = $SUB;
141         $content['chk'] = countPostSelection();
142         $SW = 2; $cnt = '0'; $OUT = '';
143         foreach (postRequestElement('sel') as $sel => $confirm) {
144                 if ($confirm == 1) {
145                         $cnt++;
146                         $result = SQL_QUERY_ESC("SELECT `title`, `visible`, `locked` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE ".$AND." AND `id`=%s LIMIT 1",
147                                 array(bigintval($sel)), __FILE__, __LINE__);
148                         if (SQL_NUMROWS($result) == 1) {
149                                 // Entry found so we load the stuff...
150                                 $data = SQL_FETCHARRAY($result);
151                                 $data = array(
152                                         'cnt'     => $cnt,
153                                         'menu'    => $data['title'],
154                                         'sel'     => $sel,
155                                         'sw'      => $SW,
156                                         'visible' => addSelectionBox('yn', $data['visible'], 'visible', $sel),
157                                         'locked'  => addSelectionBox('yn', $data['locked'] , 'locked' , $sel),
158                                 );
159
160                                 // Load template
161                                 $OUT .= loadTemplate('admin_menu_status_row', true, $data);
162                         } else {
163                                 // Entry not found?
164                                 $data = array(
165                                         'sw'  => $SW,
166                                         'sel' => $sel
167                                 );
168
169                                 // Load template
170                                 $OUT .= loadTemplate('admin_menu_404_row', true, $data);
171                         }
172                         SQL_FREERESULT($result);
173                         $SW = 3 - $SW;
174                 } // END - if
175         } // END - foreach
176         $content['rows'] = $OUT;
177         $content['cnt'] = $cnt;
178
179         // Load template
180         loadTemplate('admin_mmenu_status', false, $content);
181 } elseif ((isFormSent()) && (!isDemoModeActive())) {
182         // An act is done...
183         foreach (postRequestElement('sel') as $sel => $menu) {
184                 $AND = "(`what` = '' OR `what` IS NULL)";
185                 $sel = bigintval($sel);
186                 if (!empty($SUB)) $AND = "`action`='".$SUB."'";
187                 switch (postRequestElement('ok')) {
188                         case 'edit': // Edit menu
189                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_member_menu` SET `title`='%s', `action`='%s', `what`='%s' WHERE ".$AND." AND `id`=%s LIMIT 1",
190                                         array($menu, postRequestElement('sel_act', $sel), postRequestElement('sel_what', $sel), $sel), __FILE__, __LINE__);
191                                 break;
192
193                         case 'del': // Delete menu
194                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE ".$AND." AND `id`=%s LIMIT 1",
195                                         array($sel), __FILE__, __LINE__);
196                                 break;
197
198                         case 'status': // Change status of menus
199                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_member_menu` SET `visible`='%s', `locked`='%s' WHERE ".$AND." AND `id`=%s LIMIT 1",
200                                         array(postRequestElement('visible', $sel), postRequestElement('locked', $sel), $sel), __FILE__, __LINE__);
201                                 break;
202                 } // END - switch
203                 break;
204         } // END - foreach
205
206         // Load template
207         loadTemplate('admin_settings_saved', false, getMessage('SETTINGS_SAVED'));
208 } else {
209         if ((isGetRequestElementSet('act')) && (isGetRequestElementSet('tid')) && (isGetRequestElementSet('fid'))) {
210                 // Init
211                 $tid = ''; $fid = '';
212
213                 // Get ids
214                 if (isGetRequestElementSet('w')) {
215                         // Sub menus selected
216                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE `action`='%s' AND `sort`='%s' LIMIT 1",
217                                 array(bigintval(getRequestElement('act')), bigintval(getRequestElement('tid'))), __FILE__, __LINE__);
218                         list($tid) = SQL_FETCHROW($result);
219                         SQL_FREERESULT($result);
220                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE `action`='%s' AND `sort`='%s' LIMIT 1",
221                                 array(bigintval(getRequestElement('act')), bigintval(getRequestElement('fid'))), __FILE__, __LINE__);
222                         list($fid) = SQL_FETCHROW($result);
223                         SQL_FREERESULT($result);
224                 } else {
225                         // Main menu selected
226                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE (`what`='' OR `what` IS NULL) AND `sort`='%s' LIMIT 1",
227                                 array(bigintval(getRequestElement('tid'))), __FILE__, __LINE__);
228                         list($tid) = SQL_FETCHROW($result);
229                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE (`what`='' OR `what` IS NULL) AND `sort`='%s' LIMIT 1",
230                                 array(bigintval(getRequestElement('fid'))), __FILE__, __LINE__);
231                         list($fid) = SQL_FETCHROW($result);
232                 }
233
234                 if ((!empty($tid)) && (!empty($fid))) {
235                         // Sort menu
236                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_member_menu` SET `sort`='%s' WHERE ".$AND." AND id=%s LIMIT 1",
237                                 array(bigintval(getRequestElement('tid')), bigintval($fid)), __FILE__, __LINE__);
238                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_member_menu` SET `sort`='%s' WHERE ".$AND." AND id=%s LIMIT 1",
239                                 array(bigintval(getRequestElement('fid')), bigintval($tid)), __FILE__, __LINE__);
240                 } // END - -fi
241         }
242
243         if (!empty($SUB)) {
244                 // Edit sub menus
245                 $result = SQL_QUERY("SELECT `id`, `action`, `what`, `title`, `sort` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE ".$AND." AND `what` != '' AND `what` IS NOT NULL ORDER BY `sort` ASC", __FILE__, __LINE__);
246         } else {
247                 // Edit main menus
248                 $result = SQL_QUERY("SELECT `id`, `action`, `what`, `title`, `sort` FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE (`what`='' OR `what` IS NULL) ORDER BY `sort` ASC", __FILE__, __LINE__);
249         }
250
251         // Get total menu entries
252         $max = SQL_NUMROWS($result);
253
254         // Some are there?
255         if ($max > 0) {
256                 // Set sub value
257                 $content['sub'] = $SUB;
258
259                 $SW = 2; $cnt = '0'; $OUT = '';
260                 while ($data = SQL_FETCHARRAY($result)) {
261                         // Init navigation
262                         $data['navi'] = '';
263                         $cnt++;
264                         if (($data['sort'] == '0') || (($data['sort'] == 1) && (!empty($SUB)))) {
265                                 // Is highest position
266                                 $data['navi'] = "<a href=\"{?URL?}/modules.php?module=admin&amp;what=memedit&amp;sub=".$content['sub']."&amp;act=".$data['action']."&amp;w=".$data['what']."&amp;tid=".($data['sort']+1)."&amp;fid=".$data['sort']."\">{--LOWER--}</a>";
267                         } elseif ($cnt == $max) {
268                                 // Is lowest position
269                                 $data['navi'] = "<a href=\"{?URL?}/modules.php?module=admin&amp;what=memedit&amp;sub=".$content['sub']."&amp;act=".$data['action']."&amp;w=".$data['what']."&amp;tid=".($data['sort']-1)."&amp;fid=".$data['sort']."\">{--HIGHER--}</a>";
270                         } elseif ($data['sort'] > 0) {
271                                 // Anything else between highest and lowest
272                                 $data['navi'] = "<a href=\"{?URL?}/modules.php?module=admin&amp;what=memedit&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=memedit&amp;sub=".$content['sub']."&amp;act=".$data['action']."&amp;w=".$data['what']."&amp;tid=".($data['sort']+1)."&amp;fid=".$data['sort']."\">{--LOWER--}</a>";
273                         }
274
275                         // Fix empty values for Firefox
276                         if (empty($data['action'])) $data['action'] = '&nbsp;';
277                         if (empty($data['what']))   $data['what']   = '&nbsp;';
278                         if (empty($data['title']))  $data['title']  = '&nbsp;';
279
280                         // Add more entries
281                         $data['sw']   = $SW;
282                         $data['mode'] = 'mem';
283
284                         // Load row template and switch color
285                         $OUT .= loadTemplate('admin_menu_overview_row', true, $data);
286                         $SW = 3 - $SW;
287                 }
288
289                 // Free memory
290                 SQL_FREERESULT($result);
291
292                 // Remember rows in array
293                 $content['rows'] = $OUT;
294
295                 // Load main template
296                 loadTemplate('admin_mmenu_overview', false, $content);
297         } else {
298                 // Menu entries are missing... (???)
299                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NO_MENUS_FOUND'));
300         }
301 }
302
303 // [EOF]
304 ?>