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