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