Renamed function so it might be more understandable
[mailer.git] / inc / modules / admin / what-admin_add.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 09/27/2003 *
4  * ===================                          Last change: 12/13/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-admin_add.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Add more entries to the admin menu               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Mehr Menueeintraege zum Admin-Bereich einfuegen  *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if ((!defined('__SECURITY')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
45
46 // Check if the admin has entered title and what-php file name...
47 if ((isFormSent()) && ((!isPostRequestParameterSet('title')) || (!isPostRequestParameterSet('menu')) || (!isPostRequestParameterSet('descr')))) {
48         unsetPostRequestParameter('ok');
49 } // END - if
50
51 if (!isFormSent()) {
52         // Create arrays
53         $menus = array(); $titles = array(); $below = array();
54
55         // Get all available main menus
56         $result = SQL_QUERY("SELECT `action`, `title`, `sort` FROM `{?_MYSQL_PREFIX?}_admin_menu` WHERE (`what`='' OR `what` IS NULL) ORDER BY `sort` ASC", __FILE__, __LINE__);
57         if (!SQL_HASZERONUMS($result)) {
58                 // Read menu structure
59                 // @TODO Cant this be rewritten?
60                 while ($content = SQL_FETCHARRAY($result)) {
61                         // Menu actions
62                         $menus[] = $content['action'];
63
64                         // Menu titles
65                         $titles[] = $content['title'];
66
67                         // Below this menu point should the new be added so we simply increase the sort value by 1 :-)
68                         $below[] = $content['sort'] + 1;
69                 } // END - while
70
71                 // Free memory
72                 SQL_FREERESULT($result);
73
74                 // Remove double eintries
75                 // @TODO This can be somehow rewritten to a function
76                 $prev = ''; $dmy = $menus; $dmy2 = $titles; $dmy3 = $below;
77                 foreach ($menus as $key => $value) {
78                         if ($value == $prev) {
79                                 unset($dmy[$key]);
80                                 unset($dmy2[$key]);
81                                 unset($dmy3[$key]);
82                         } else {
83                                 $prev = $value;
84                         }
85                 } // END - foreach
86
87                 // Write dummys back to our array
88                 $menus  = $dmy;  unset($dmy);
89                 $titles = $dmy2; unset($dmy2);
90                 $below  = $dmy3; unset($dmy3);
91
92                 // Load sub menus :)
93                 foreach ($menus as $key_main => $value_main) {
94                         $result = SQL_QUERY_ESC("SELECT `what`, `title`, `sort` FROM `{?_MYSQL_PREFIX?}_admin_menu` WHERE `action`='%s' AND `what` != '' AND `what` IS NOT NULL ORDER BY `sort` ASC",
95                                 array($value_main), __FILE__, __LINE__);
96                         if (!SQL_HASZERONUMS($result)) {
97                                 // Init arrays
98                                 $menus[$value_main] = array();
99                                 $titles[$value_main] = array();
100                                 $below[$value_main] = array();
101
102                                 // Read menu structure
103                                 while ($content = SQL_FETCHARRAY($result)) {
104                                         // Menu actions
105                                         $menus[$value_main][] = $content['what'];
106
107                                         // Menu titles
108                                         $titles[$value_main][] = $content['title'];
109
110                                         // Below this menu point should the new be added so we simply increase the sort value by 1 :-)
111                                         $below[$value_main][] = $content['sort'] + 1;
112                                 } // END - while
113
114                                 // Free memory
115                                 SQL_FREERESULT($result);
116
117                                 // Remove double eintries
118                                 // @TODO This can be somehow rewritten to a function
119                                 $prev = ''; $dmy = $menus[$value_main]; $dmy2 = $titles[$value_main]; $dmy3 = $below[$value_main];
120                                 foreach ($menus[$value_main] as $key => $value) {
121                                         if ($value == $prev) {
122                                                 unset($dmy[$key]);
123                                                 unset($dmy2[$key]);
124                                                 unset($dmy3[$key]);
125                                         } else {
126                                                 $prev = $value;
127                                         }
128                                 }
129                                 $menus[$value_main] = $dmy;
130                                 $titles[$value_main] = $dmy2;
131                                 $below[$value_main] = $dmy3;
132                         }
133                 }
134         }
135
136         $OUT = '    <select class="form_select" name="sort" size="1">
137       <option value="0">{--ADMIN_IS_FIRST_MENU--}</option>';
138         foreach ($below as $key => $m) {
139                 if (is_array($m)) {
140                         foreach ($m as $key2 => $m2) {
141                                 $OUT .= '      <option value="' . $m2 . '">' . $titles[$key][$key2];
142                                 foreach ($menus as $k => $v) {
143                                         if (($v == $key) && (!is_array($v))) {
144                                                 $OUT .= ' (' . $titles[$k] . ')';
145                                         } // END - if
146                                 } // END - foreach
147                                 $OUT .= '</option>';
148                         }
149                 } else {
150                         $OUT .= '      <option value="' . $m . '">' . $titles[$key] . '</option>';
151                 }
152         }
153         $OUT .= '</select>';
154
155         // Prepare selections for template
156         $content['below_selection']  = $OUT;
157         $content['what_selection']   = adminAddMenuSelectionBox('admin', 'what'  , 'name');
158         $content['action_selection'] = adminAddMenuSelectionBox('admin', 'action', 'menu');
159
160         // Display form
161         loadTemplate('admin_add_admin_menu', false, $content);
162 } elseif (!isDemoModeActive()) {
163         // Insert new menu entry
164         if (isPostRequestParameterSet('menu')) {
165                 // Add sub menu
166                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (`action`,`what`,`title`,`descr`,`sort`) VALUES ('%s','%s','%s','%s','%s')",
167                         array(
168                                 postRequestParameter('menu'),
169                                 postRequestParameter('name'),
170                                 postRequestParameter('title'),
171                                 postRequestParameter('descr'),
172                                 bigintval(postRequestParameter('sort')),
173                         ), __FILE__, __LINE__
174                 );
175         } else {
176                 // Add main menu
177                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_admin_menu` (action, title, descr, sort) VALUES ('%s','%s','%s','%s')",
178                         array(
179                                 postRequestParameter('name'),
180                                 postRequestParameter('title'),
181                                 postRequestParameter('descr'),
182                                 bigintval(postRequestParameter('sort')),
183                         ), __FILE__, __LINE__
184                 );
185         }
186         loadTemplate('admin_settings_saved', false, '{--SETTINGS_SAVED--}');
187 } else {
188         // Is demo login!
189         loadTemplate('admin_settings_saved', false, '{--SETTINGS_NOT_SAVED--}');
190 }
191
192 // [EOF]
193 ?>