branch prepared
[mailer.git] / inc / modules / admin / what-config_cats.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/10/2003 *
4  * ===============                              Last change: 07/04/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-config_cats.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Add new categories and edit / delete existing    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Neue Kategorien hinzufuegen und bestehende       *
12  *                     editieren / loeschen                             *
13  * -------------------------------------------------------------------- *
14  *                                                                      *
15  * -------------------------------------------------------------------- *
16  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
17  * For more information visit: http://www.mxchange.org                  *
18  *                                                                      *
19  * This program is free software; you can redistribute it and/or modify *
20  * it under the terms of the GNU General Public License as published by *
21  * the Free Software Foundation; either version 2 of the License, or    *
22  * (at your option) any later version.                                  *
23  *                                                                      *
24  * This program is distributed in the hope that it will be useful,      *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
27  * GNU General Public License for more details.                         *
28  *                                                                      *
29  * You should have received a copy of the GNU General Public License    *
30  * along with this program; if not, write to the Free Software          *
31  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
32  * MA  02110-1301  USA                                                  *
33  ************************************************************************/
34
35 // Some security stuff...
36 if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!IS_ADMIN())) {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 // Add description as navigation point
42 ADD_DESCR("admin", basename(__FILE__));
43
44 // Init variable to avoid a notice
45 $CATS = "";
46
47 OPEN_TABLE("100%", "admin_content admin_content_align", "");
48 if (isset($_POST['add'])) {
49         // Add a new category
50         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_cats WHERE cat='%s' LIMIT 1",
51          array(addslashes($_POST['catname'])), __FILE__, __LINE__);
52         if (SQL_NUMROWS($result) == 0) {
53                 // Category does not exists, we simply add it...
54                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_cats (cat, visible, sort) VALUES ('%s', '%s', '%s')",
55                  array(addslashes($_POST['catname']), $_POST['visible'], bigintval($_POST['parent'] + 1)), __FILE__, __LINE__);
56                 $content = "<SPAN class=\"admin_done\">".CATEGORY_ADDED."</SPAN>";
57         } else {
58                 // Category does already exists
59                 $content = "<SPAN class=\"admin_failed\">".CATEGORY_ALREADY_EXISTS."</SPAN>";
60         }
61
62         // Free memory
63         SQL_FREERESULT($result);
64
65         // Display message
66         LOAD_TEMPLATE("admin_settings_saved", false, $content);
67 } elseif ((isset($_POST['ok'])) && (isset($_POST['id'])) && (is_array($_POST['id']))) {
68         // Change or delete categories...
69         $TEXT = "";
70         foreach ($_POST['id'] as $id=>$cat) {
71                 // Secure ID
72                 $id = bigintval($id);
73
74                 // Is the entry set?
75                 if (!empty($cat)) {
76                         switch ($_GET['do'])
77                         {
78                         case "edit": // Change categories
79                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_cats SET cat='%s', visible='%s', sort=%d WHERE id=%d LIMIT 1",
80                                  array($cat, $_POST['vis'][$id], $_POST['sort'][$id], $id), __FILE__, __LINE__);
81                                 $TEXT = CATEGORIES_SAVED;
82                                 break;
83
84                         case "del": // Delete categories
85                                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_cats WHERE id=%d LIMIT 1",
86                                  array($id), __FILE__, __LINE__);
87                                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_cats WHERE cat_id=%d",
88                                  array($id), __FILE__, __LINE__);
89                                 $TEXT = CATEGORIES_DELETED;
90                                 break;
91                         }
92                 } else {
93                         // Entry not saved
94                         $TEXT .= sprintf(CATEGORY_NOT_SAVED, $id);
95                 }
96         }
97
98         if (isset($TEXT)) {
99                 // Display message
100                 LOAD_TEMPLATE("admin_settings_saved", false, $TEXT);
101         }
102 }
103  elseif ((isset($_POST['del'])) && ((SELECTION_COUNT($_POST['sel']) > 0) || (isset($_POST['sel'][0]))))
104 {
105         // Delete categories
106         $SW = 2; $OUT = "";
107         foreach ($_POST['sel'] as $id=>$value)
108         {
109                 // Load data of category
110                 $result = SQL_QUERY_ESC("SELECT cat FROM "._MYSQL_PREFIX."_cats WHERE id=%d LIMIT 1",
111                  array(bigintval($id)), __FILE__, __LINE__);
112                 list($cat) = SQL_FETCHROW($result);
113                 SQL_FREERESULT($result);
114
115                 // Prepare data for the row template
116                 $content = array(
117                         'sw'  => $SW,
118                         'id'  => $id,
119                         'cat' => $cat,
120                 );
121
122                 // Load row template and switch colors
123                 $OUT .= LOAD_TEMPLATE("admin_del_cats_row", true, $content);
124                 $SW = 3 - $SW;
125         }
126         define('__CAT_ROWS', $OUT);
127
128         // Load main template
129         LOAD_TEMPLATE("admin_del_cats");
130 } elseif ((isset($_POST['edit'])) && ((SELECTION_COUNT($_POST['sel']) > 0) || (isset($_POST['sel'][0])))) {
131         // Edit categories
132         $SW = 2; $OUT = "";
133         foreach ($_POST['sel'] as $id=>$value)
134         {
135                 // Load data from the category
136                 $result = SQL_QUERY_ESC("SELECT cat, visible, sort FROM "._MYSQL_PREFIX."_cats WHERE id=%d LIMIT 1",
137                  array(bigintval($id)), __FILE__, __LINE__);
138                 list($cat, $vis, $sort) = SQL_FETCHROW($result);
139                 SQL_FREERESULT($result);
140
141                 // Prepare data for the row template
142                 $content = array(
143                         'sw'   => $SW,
144                         'id'   => $id,
145                         'cat'  => $cat,
146                         'vis'  => ADD_SELECTION("yn", $vis, "vis", $id),
147                         'sort' => $sort,
148                 );
149
150                 // Load row template and switch colors
151                 $OUT .= LOAD_TEMPLATE("admin_edit_cats_row", true, $content);
152                 $SW = 3 - $SW;
153         }
154         define('__CAT_ROWS', $OUT);
155
156         // Load main template
157         LOAD_TEMPLATE("admin_edit_cats");
158 }
159  else
160 {
161         // Load all categories
162         $result = SQL_QUERY("SELECT id, cat, visible, sort FROM "._MYSQL_PREFIX."_cats ORDER BY sort", __FILE__, __LINE__);
163         if (SQL_NUMROWS($result) > 0)
164         {
165                 // List already existing categories for editing
166                 $SW = 2; $OUT = ""; $CATS = "";
167                 while (list($id, $cat, $visible, $sort) = SQL_FETCHROW($result))
168                 {
169                         // Prepare data for the row template
170                         $content = array(
171                                 'sw'   => $SW,
172                                 'id'   => $id,
173                                 'cat'  => $cat,
174                                 'vis'  => TRANSLATE_YESNO($visible),
175                                 'sort' => $sort,
176                         );
177
178                         // Put cat descriptions into variable for the selection box
179                         if (strlen($cat) > 20) $cat = substr($cat, 0, 17)."...";
180                         $CATS .= "      <OPTION value=\"".$sort."\">".$cat."</OPTION>\n";
181
182                         // Load row template and switch color
183                         $OUT .= LOAD_TEMPLATE("admin_config_cats_row", true, $content);
184                         $SW = 3 - $SW;
185                 }
186
187                 // Free memory
188                 SQL_FREERESULT($result);
189                 define('__CAT_ROWS', $OUT);
190
191                 // Load main template
192                 LOAD_TEMPLATE("admin_config_cats");
193         }
194
195         define('CATS', $CATS);
196
197         // Form to add a new category
198         LOAD_TEMPLATE("admin_add_cat");
199 }
200 CLOSE_TABLE();
201 //
202 ?>