Fixed category saving, added unique key, removed deprecated templates:
[mailer.git] / inc / modules / member / what-categories.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } elseif (!isMember()) {
42         redirectToIndexMemberOnlyModule();
43 }
44
45 // Add description as navigation point
46 addMenuDescription('member', __FILE__);
47
48 $whereStatement = " WHERE `visible`='Y'";
49 if (isAdmin()) $whereStatement = '';
50
51 // Get all categories
52 $result = SQL_QUERY("SELECT `id`, `cat` FROM `{?_MYSQL_PREFIX?}_cats`".$whereStatement." ORDER BY `sort` ASC", __FILE__, __LINE__);
53
54 // Do we have entries?
55 if (!SQL_HASZERONUMS($result)) {
56         $LEAST = false;
57         if (isFormSent()) {
58                 $count = '0';
59                 foreach (postRequestParameter('cat') as $cat => $joined) {
60                         if ($joined != 'Y') $count++;
61                 } // END - foreach
62
63                 if ((SQL_NUMROWS($result) - $count) < getLeastCats()) {
64                         unsetPostRequestParameter('ok');
65                         $LEAST = true;
66                 } // END - if
67         } // END - if
68
69         // Is the form sent?
70         if (isFormSent()) {
71                 // Start counting all
72                 $count = '0';
73
74                 // Go through all entries
75                 foreach (postRequestParameter('cat') as $cat => $joined) {
76                         // Ini sql here
77                         $sql = '';
78
79                         // Has the user joined on this category?
80                         switch ($joined) {
81                                 case 'Y':
82                                         $result_user = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1",
83                                                 array(getMemberId(), bigintval($cat)), __FILE__, __LINE__);
84
85                                         // Debug message
86                                         //* DEBUG: */ logDebugMessage(__FILE__, __LINE__, 'cat='.$cat.',joined='.$joined.',getMemberId()='.getMemberId().',SQL_HASZERONUMS()='.intval(SQL_HASZERONUMS($result_user)));
87
88                                         if (SQL_HASZERONUMS($result_user)) {
89                                                 $sql = "INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s,%s)";
90                                         } // END - if
91
92                                         // Free memory
93                                         SQL_FREERESULT($result_user);
94                                         break;
95
96                                 case 'N':
97                                         $sql = "DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1";
98                                         break;
99                         } // END - switch
100
101                         if (!empty($sql)) {
102                                 // Run SQL command
103                                 SQL_QUERY_ESC(trim($sql), array(getMemberId(), bigintval($cat)), __FILE__, __LINE__);
104
105                                 // Count this row
106                                 $count += SQL_AFFECTEDROWS();
107                         } // END - if
108                 } // END - foreach
109
110                 // Categories saved?
111                 if ($count > 0) {
112                         // Output message
113                         loadTemplate('admin_settings_saved', false, '{--MEMBER_CATEGORIES_SAVED--}');
114                 } else {
115                         // None save
116                         loadTemplate('admin_settings_saved', false, '{--MEMBER_CATEGORIES_NOT_SAVED--}');
117                 }
118         } else {
119                 if ($LEAST === true) {
120                         // Also here we have to secure it... :(
121                         loadTemplate('admin_settings_saved', false, '{--CHOOSE_MORE_CATEGORIES--}');
122                 } // END - if
123
124                 // Put some data into constants for the template
125                 $content['rows'] = (SQL_NUMROWS($result) * 2 + 4);
126
127                 // Load header template
128                 loadTemplate('member_cats_header', false, $content);
129
130                 // Start switching colors and load all visible categories
131                 // @TODO Rewrite this to use $OUT .= ...
132                 $OUT = '';
133                 while ($content = SQL_FETCHARRAY($result)) {
134                         // Default he has not joined
135                         $content['jn'] = ' checked="checked"';
136                         $content['jy'] = '';
137
138                         // When we found an entry don't read it, just change the jx elements
139                         if ((isFormSent()) && (isPostRequestParameterSet('cat'))) {
140                                 // Form sent?
141                                 if (postRequestParameter('cat', $content['id']) =='Y') {
142                                         $content['jy'] = ' checked="checked"';
143                                         $content['jn'] = '';
144                                 } // END - if
145                         } else {
146                                 // Check if he has an entry
147                                 if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', true, sprintf(" AND `cat_id`=%s", bigintval($content['id']))) == 1) {
148                                         $content['jn'] = '';
149                                         $content['jy'] = ' checked="checked"';
150                                 } // END - if
151                         }
152
153                         // Load row template and switch colors
154                         loadTemplate('member_cat_row', false, $content);
155                 } // END - while
156
157                 // Load footer template
158                 loadTemplate('member_cats_footer');
159         }
160 } else {
161         // No cateogries are defined yet
162         loadTemplate('admin_settings_saved', false, '{--MEMBER_NO_CATEGORIES--}');
163 }
164
165 // Free result
166 SQL_FREERESULT($result);
167
168 // [EOF]
169 ?>