34beb1f902228748529fdd1c96b815934bf88374
[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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 } elseif (!IS_MEMBER()) {
44         redirectToUrl('modules.php?module=index');
45 }
46
47 // Add description as navigation point
48 ADD_DESCR('member', __FILE__);
49
50 $UID = getUserId();
51 $whereStatement = " WHERE `visible`='Y'";
52 if (IS_ADMIN()) $whereStatement = '';
53
54 $result = SQL_QUERY("SELECT id, cat FROM `{!_MYSQL_PREFIX!}_cats`".$whereStatement." ORDER BY `sort`", __FILE__, __LINE__);
55 $cats = SQL_NUMROWS($result);
56
57 if ($cats > 0) {
58         $LEAST = false;
59         if (IS_FORM_SENT()) {
60                 $cnt = 0;
61                 foreach (REQUEST_POST('cat') as $cat => $joined) {
62                         if ($joined == 'N') $cnt++;
63                 }
64
65                 if (($cats - $cnt) < getConfig('least_cats')) {
66                         REQUEST_UNSET_POST('ok');
67                         $LEAST = true;
68                 }
69         }
70
71         if (IS_FORM_SENT()) {
72                 foreach (REQUEST_POST('cat') as $cat => $joined) {
73                         switch ($joined) {
74                                 case 'Y':
75                                         $sql = '';
76                                         $result_user = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE userid=%s AND cat_id=%s LIMIT 1",
77                                         array($UID, bigintval($cat)), __FILE__, __LINE__);
78
79                                         if (SQL_NUMROWS($result_user) == 0) {
80                                                 $sql = "INSERT INTO `{!_MYSQL_PREFIX!}_user_cats` (userid, cat_id) VALUES ('%s','%s')";
81                                         } else {
82                                                 // Free memory
83                                                 SQL_FREERESULT($result_user);
84                                         }
85                                         break;
86
87                                 case 'N':
88                                         $sql = "DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE userid=%s AND cat_id=%s LIMIT 1";
89                                         break;
90                         }
91
92                         if (!empty($sql)) {
93                                 // Run SQL command
94                                 $result = SQL_QUERY_ESC(trim($sql), array($UID, bigintval($cat)), __FILE__, __LINE__);
95                         }
96                 }
97
98                 // Categories saved...
99                 LOAD_TEMPLATE('admin_settings_saved', true, getMessage('MEMBER_CATS_SAVED'));
100         } else {
101                 if ($LEAST) {
102                         // Also here we have to secure it... :(
103                         LOAD_TEMPLATE('admin_settings_saved', false, sprintf(getMessage('CATS_LEAST'), getConfig('least_cats')));
104                 }
105                 // Put some data into constants for the template
106                 define('__ROWS', ($cats*2+4));
107
108                 // Load header template
109                 LOAD_TEMPLATE("member_cats_header");
110
111                 // Start switching colors and load all visible categories
112                 // @TODO Rewrite this to use $OUT .= ...
113                 $OUT = ''; $SW = 2;
114                 while ($content = SQL_FETCHARRAY($result)) {
115                         // Default he has not joined
116                         $content['jn'] = ' checked="checked"';
117                         $content['jy'] = '';
118
119                         // When we found an entry don't read it, just change the jx elements
120                         if (REQUEST_ISSET_POST(('cat'))) {
121                                 // Form sent?
122                                 if (REQUEST_POST('cat', $content['id']) =='Y') {
123                                         $content['jy'] = ' checked="checked"';
124                                         $content['jn'] = '';
125                                 }
126                         } else {
127                                 // Check if he has an entry
128                                 if (GET_TOTAL_DATA($UID, "user_cats", "id", 'userid', true, sprintf(" AND cat_id=%s", bigintval($content['id']))) == 1) {
129                                         $content['jn'] = '';
130                                         $content['jy'] = ' checked="checked"';
131                                 }
132                         }
133
134                         // Add some more
135                         $content['sw'] = $SW;
136
137                         // Load row template and switch colors
138                         LOAD_TEMPLATE("member_cat_row", false, $content);
139                         $SW = 3 - $SW;
140                 }
141
142                 // Load footer template
143                 LOAD_TEMPLATE("member_cats_footer");
144         }
145 } else {
146         // No cateogries are defined yet
147         LOAD_TEMPLATE('admin_settings_saved', true, getMessage('MEMBER_NO_CATS'));
148 }
149
150 // Free result
151 SQL_FREERESULT($result);
152
153 //
154 ?>