Some rewrites, TODOs handled:
[mailer.git] / inc / modules / admin / what-guest_add.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/28/2003 *
4  * ===============                              Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-guest_add.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Add a new guest menu                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Einen neuen Gastmenuepunkt einfuegen             *
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 // Check if the admin has entered title and what-php file name...
49 if (((!REQUEST_ISSET_POST('title')) || (!REQUEST_ISSET_POST(('menu')))) && (isFormSent())) {
50         // Abort adding the menu entry
51         REQUEST_UNSET_POST('ok');
52 } // END - if
53
54 if (!isFormSent()) {
55         // Create arrays
56         $menus = array(); $titles = array(); $below = array();
57
58         // Get all available main menus
59         $result = SQL_QUERY("SELECT action, title, sort FROM `{!_MYSQL_PREFIX!}_guest_menu` WHERE (`what`='' OR `what` IS NULL) ORDER BY `sort`", __FILE__, __LINE__);
60         if (SQL_NUMROWS($result) > 0) {
61                 // Read menu structure
62                 // @TODO Cant this be rewritten?
63                 while ($content = SQL_FETCHARRAY($result)) {
64                         // Menu actions
65                         $menus[] = $content['action'];
66
67                         // Menu titles
68                         $titles[] = $content['title'];
69
70                         // Below this menu point should the new be added so we simply increase the sort value by 1 :-)
71                         $below[] = $content['sort'] + 1;
72                 } // END - while
73
74                 // Free memory
75                 SQL_FREERESULT($result);
76
77                 // Remove double eintries
78                 // @TODO This can be somehow rewritten to a function
79                 $prev = ''; $dmy = $menus; $dmy2 = $titles; $dmy3 = $below;
80                 foreach ($menus as $key => $value) {
81                         if ($value == $prev) {
82                                 unset($dmy[$key]);
83                                 unset($dmy2[$key]);
84                                 unset($dmy3[$key]);
85                         } else {
86                                 $prev = $value;
87                         }
88                 }
89
90                 // Init variables
91                 $menus  = $dmy;
92                 $titles = $dmy2;
93                 $below  = $dmy3;
94
95                 // Load sub menus :)
96                 foreach ($menus as $key_main => $value_main) {
97                         $result = SQL_QUERY_ESC("SELECT what, title, sort
98 FROM `{!_MYSQL_PREFIX!}_guest_menu`
99 WHERE `action`='%s' AND `what` != '' AND `what` IS NOT NULL ORDER BY `sort`",
100                         array($value_main), __FILE__, __LINE__);
101                         if (SQL_NUMROWS($result) > 0) {
102                                 // Initialize arrays
103                                 $menus[$value_main] = array();
104                                 $titles[$value_main] = array();
105                                 $below[$value_main] = array();
106
107                                 // Read menu structure
108                                 while ($content = SQL_FETCHARRAY($result)) {
109                                         // Menu actions
110                                         $menus[$value_main][] = $content['action'];
111
112                                         // Menu titles
113                                         $titles[$value_main][] = $content['title'];
114
115                                         // Below this menu point should the new be added so we simply increase the sort value by 1 :-)
116                                         $below[$value_main][] = $content['sort'] + 1;
117                                 }
118
119                                 // Free memory
120                                 SQL_FREERESULT($result);
121
122                                 // Remove double eintries
123                                 // @TODO This can be somehow rewritten to a function
124                                 $prev = ''; $dmy = $menus[$value_main]; $dmy2 = $titles[$value_main]; $dmy3 = $below[$value_main];
125                                 foreach ($menus[$value_main] as $key => $value) {
126                                         if ($value == $prev) {
127                                                 unset($dmy[$key]);
128                                                 unset($dmy2[$key]);
129                                                 unset($dmy3[$key]);
130                                         } else {
131                                                 $prev = $value;
132                                         }
133                                 }
134
135                                 // Transfer dummy array
136                                 $menus[$value_main] = $dmy;
137                                 $titles[$value_main] = $dmy2;
138                                 $below[$value_main] = $dmy3;
139                         }
140                 }
141         }
142
143         $OUT = "    <select class=\"admin_select\" name=\"sort\" size=\"1\">
144       <option value=\"0\">".IS_FIRST_MENU."</option>";
145         foreach ($below as $key => $m) {
146                 if (is_array($m)) {
147                         foreach ($m as $key2 => $m2) {
148                                 $OUT .= "      <option value=\"".$m2."\">".$titles[$key][$key2];
149                                 foreach ($menus as $k => $v) {
150                                         if (($v == $key) && (!is_array($v))) {
151                                                 $OUT .= " (" . $titles[$k] . ')';
152                                         }
153                                 }
154                                 $OUT .= "</option>\n";
155                         }
156                 } else {
157                         $OUT .= "      <option value=\"".$m."\">".$titles[$key]."</option>\n";
158                 }
159         }
160         $OUT .= "</select>";
161
162         define('__BELOW_SELECTION' , $OUT);
163         define('__WHAT_SELECTION'  , ADMIN_MAKE_MENU_SELECTION('guest', "what", "name"));
164         define('__ACTION_SELECTION', ADMIN_MAKE_MENU_SELECTION('guest', "action", "menu"));
165
166         // Display form
167         LOAD_TEMPLATE("admin_guest_add");
168 } elseif (!IS_DEMO()) {
169         // Insert new menu entry
170         if (REQUEST_ISSET_POST(('menu'))) {
171                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_guest_menu` (`action`,`what`,`title`,`sort`,`visible`,`locked`) VALUES ('%s','%s','%s','%s','%s','%s')",
172                 array(
173                 REQUEST_POST('menu'),
174                 REQUEST_POST('name'),
175                 REQUEST_POST('title'),
176                 bigintval(REQUEST_POST('sort')),
177                 REQUEST_POST('visible'),
178                 REQUEST_POST('active'),
179                 ), __FILE__, __LINE__);
180         } else {
181                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_guest_menu` (action, title, sort, visible, locked) VALUES ('%s','%s','%s','%s','%s')",
182                 array(
183                 REQUEST_POST('name'),
184                 REQUEST_POST('title'),
185                 bigintval(REQUEST_POST('sort')),
186                 REQUEST_POST('visible'),
187                 REQUEST_POST('active'),
188                 ), __FILE__, __LINE__);
189         }
190         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('SETTINGS_SAVED'));
191 } else {
192         // Demo mode!
193         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('SETTINGS_NOT_SAVED'));
194 }
195
196 //
197 ?>