wernis extension is now alpha code (only listing in admin area is missing), naming...
[mailer.git] / inc / modules / member / what-categories.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/19/2003 *
4  * ===============                              Last change: 06/30/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-categories.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Select or unselect categories                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Kategorien ab- bzw. anwaehlen                    *
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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40  elseif (!IS_LOGGED_IN())
41 {
42         LOAD_URL("modules.php[13~?module=index");
43 }
44
45 // Add description as navigation point
46 ADD_DESCR("member", basename(__FILE__));
47
48 $UID = $GLOBALS['userid'];
49 $whereStatement = " WHERE visible='Y'";
50 if (IS_ADMIN()) $whereStatement = "";
51
52 $result = SQL_QUERY("SELECT id, cat FROM "._MYSQL_PREFIX."_cats".$whereStatement." ORDER BY sort", __FILE__, __LINE__);
53 $cats = SQL_NUMROWS($result);
54
55 if ($cats > 0)
56 {
57         $LEAST = false;
58         if (isset($_POST['ok']))
59         {
60                 $cnt = 0;
61                 foreach ($_POST['cat'] as $cat=>$joined)
62                 {
63                         if ($joined == 'N') $cnt++;
64                 }
65                 if (($cats - $cnt) < $_CONFIG['least_cats'])
66                 {
67                         unset($_POST['ok']);
68                         $LEAST = true;
69                 }
70         }
71         if (isset($_POST['ok']))
72         {
73                 foreach ($_POST['cat'] as $cat=>$joined)
74                 {
75                         switch ($joined)
76                         {
77                                 case 'Y':
78                                         $sql = "";
79                                         $result_user = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_cats WHERE userid=%d AND cat_id=%d LIMIT 1",
80                                          array($UID, bigintval($cat)), __FILE__, __LINE__);
81
82                                         if (SQL_NUMROWS($result_user) == 0)
83                                         {
84                                                 $sql = "INSERT INTO "._MYSQL_PREFIX."_user_cats (userid, cat_id) VALUES ('%s', '%s')";
85                                         }
86                                          else
87                                         {
88                                                 // Free memory
89                                                 SQL_FREERESULT($result_user);
90                                         }
91                                         break;
92
93                                 case 'N':
94                                         $sql = "DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_cats WHERE userid=%d AND cat_id=%d LIMIT 1";
95                                         break;
96                         }
97                         if (!empty($sql))
98                         {
99                                 // Run SQL command
100                                 $result = SQL_QUERY_ESC(trim($sql), array($UID, bigintval($cat)), __FILE__, __LINE__);
101                         }
102                 }
103                 // Categories saved...
104                 LOAD_TEMPLATE("admin_settings_saved", true, MEMBER_CATS_SAVED);
105         }
106          else
107         {
108                 if ($LEAST)
109                 {
110                         // Also here we have to secure it... :(
111                         LOAD_TEMPLATE("admin_settings_saved", true, CATS_LEAST.": ".$_CONFIG['least_cats']);
112                 }
113                 // Put some data into constants for the template
114                 define('__ROWS', ($cats*2+4));
115
116                 // Load header template
117                 LOAD_TEMPLATE("member_cats_header");
118
119                 // Start switching colors and load all visible categories
120                 $SW = 2;
121                 while (list($id, $cat) = SQL_FETCHROW($result))
122                 {
123                         // Default he has not joined
124                         $JOINED_N = " checked"; $JOINED_Y = "";
125
126                         // Check category selection
127                         $result_user = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_cats WHERE userid=%d AND cat_id=%d LIMIT 1",
128                          array($UID, bigintval($id)), __FILE__, __LINE__);
129
130                         // When we found an entry don't read it, just change the JOINED_x variables
131                         if (isset($_POST['cat']))
132                         {
133                                 if ($_POST['cat'][$id] =='Y') { $JOINED_Y = " checked"; $JOINED_N = ""; }
134                         }
135                          else
136                         {
137                                 if (SQL_NUMROWS($result_user) == 1)
138                                 {
139                                         $JOINED_Y = " checked"; $JOINED_N = "";
140                                 }
141
142                                 // Free memory
143                                 SQL_FREERESULT($result_user);
144                         }
145
146                         // Prepare data for the template
147                         $content = array(
148                                 'sw'  => $SW,
149                                 'id'  => $id,
150                                 'cat' => $cat,
151                                 'jy'  => $JOINED_Y,
152                                 'jn'  => $JOINED_N,
153                         );
154
155                         // Load row template and switch colors
156                         LOAD_TEMPLATE("member_cat_row", false, $content);
157                         $SW = 3 - $SW;
158                 }
159
160                 // Load footer template
161                 LOAD_TEMPLATE("member_cats_footer");
162         }
163 }
164  else
165 {
166         // No cateogries are defined yet
167         LOAD_TEMPLATE("admin_settings_saved", true, MEMBER_NO_CATS);
168 }
169
170 SQL_FREERESULT($result);
171
172 //
173 ?>