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