Naming inconsistencies for userid fixed
[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 - 2009 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         die();
42 } elseif (!isMember()) {
43         redirectToIndexMemberOnlyModule();
44 }
45
46 // Add description as navigation point
47 addMenuDescription('member', __FILE__);
48
49 $UID = getMemberId();
50 $whereStatement = " WHERE `visible`='Y'";
51 if (isAdmin()) $whereStatement = '';
52
53 $result = SQL_QUERY("SELECT id, cat FROM `{?_MYSQL_PREFIX?}_cats`".$whereStatement." ORDER BY `sort`", __FILE__, __LINE__);
54 $cats = SQL_NUMROWS($result);
55
56 if ($cats > 0) {
57         $LEAST = false;
58         if (isFormSent()) {
59                 $cnt = 0;
60                 foreach (postRequestElement('cat') as $cat => $joined) {
61                         if ($joined != 'Y') $cnt++;
62                 } // END - foreach
63
64                 if (($cats - $cnt) < getConfig('least_cats')) {
65                         unsetPostRequestElement('ok');
66                         $LEAST = true;
67                 } // END - if
68         } // END - if
69
70         // Is the form sent?
71         if (isFormSent()) {
72                 // Start counting all
73                 $cnt = 0;
74
75                 // Go through all entries
76                 foreach (postRequestElement('cat') as $cat => $joined) {
77                         // Ini sql here
78                         $sql = '';
79
80                         // Has the user joined on this category?
81                         switch ($joined) {
82                                 case 'Y':
83                                         $result_user = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1",
84                                                 array($UID, bigintval($cat)), __FILE__, __LINE__);
85
86                                         if (SQL_NUMROWS($result_user) == 0) {
87                                                 $sql = "INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (userid, cat_id) VALUES ('%s','%s')";
88                                         } // END - if
89
90                                         // Free memory
91                                         SQL_FREERESULT($result_user);
92                                         break;
93
94                                 case 'N':
95                                         $sql = "DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1";
96                                         break;
97                         } // END - switch
98
99                         if (!empty($sql)) {
100                                 // Run SQL command
101                                 SQL_QUERY_ESC(trim($sql), array($UID, bigintval($cat)), __FILE__, __LINE__);
102
103                                 // Count this row
104                                 $cnt += SQL_AFFECTEDROWS();
105                         } // END - if
106                 } // END - foreach
107
108                 // Categories saved?
109                 if ($cnt > 0) {
110                         // Output message
111                         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_CATS_SAVED'));
112                 } else {
113                         // None save
114                         loadTemplate('admin_settings_saved', false, getMessage('MEMBER_CATS_NOT_SAVED'));
115                 }
116         } else {
117                 if ($LEAST) {
118                         // Also here we have to secure it... :(
119                         loadTemplate('admin_settings_saved', false, sprintf(getMessage('CATS_LEAST'), getConfig('least_cats')));
120                 } // END - if
121
122                 // Put some data into constants for the template
123                 $content['rows'] = ($cats*2+4);
124
125                 // Load header template
126                 loadTemplate('member_cats_header', false, $content);
127
128                 // Start switching colors and load all visible categories
129                 // @TODO Rewrite this to use $OUT .= ...
130                 $OUT = ''; $SW = 2;
131                 while ($content = SQL_FETCHARRAY($result)) {
132                         // Default he has not joined
133                         $content['jn'] = ' checked="checked"';
134                         $content['jy'] = '';
135
136                         // When we found an entry don't read it, just change the jx elements
137                         if (isPostRequestElementSet(('cat'))) {
138                                 // Form sent?
139                                 if (postRequestElement('cat', $content['id']) =='Y') {
140                                         $content['jy'] = ' checked="checked"';
141                                         $content['jn'] = '';
142                                 }
143                         } else {
144                                 // Check if he has an entry
145                                 if (countSumTotalData($UID, 'user_cats', 'id', 'userid', true, sprintf(" AND `cat_id`=%s", bigintval($content['id']))) == 1) {
146                                         $content['jn'] = '';
147                                         $content['jy'] = ' checked="checked"';
148                                 }
149                         }
150
151                         // Add some more
152                         $content['sw'] = $SW;
153
154                         // Load row template and switch colors
155                         loadTemplate('member_cat_row', false, $content);
156                         $SW = 3 - $SW;
157                 }
158
159                 // Load footer template
160                 loadTemplate('member_cats_footer');
161         }
162 } else {
163         // No cateogries are defined yet
164         loadTemplate('admin_settings_saved', true, getMessage('MEMBER_NO_CATS'));
165 }
166
167 // Free result
168 SQL_FREERESULT($result);
169
170 //
171 ?>