Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[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 - 2015 by Mailer Developer Team                   *
20  * For more information visit: http://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         exit();
41 } elseif (!isMember()) {
42         redirectToIndexMemberOnlyModule();
43 }
44
45 // Add description as navigation point
46 addYouAreHereLink('member', __FILE__);
47
48 $whereStatement = " WHERE `visible`='Y'";
49 if (isAdmin()) $whereStatement = '';
50
51 // Get all categories
52 $result = sqlQuery('SELECT
53         `id`,
54         `cat`
55 FROM
56         `{?_MYSQL_PREFIX?}_cats`
57 ' . $whereStatement . '
58 ORDER BY
59         `sort` ASC', __FILE__, __LINE__);
60
61 // Are there entries?
62 if (!ifSqlHasZeroNumRows($result)) {
63         $LEAST = FALSE;
64         if (isFormSent()) {
65                 $count = '0';
66                 foreach (postRequestElement('cat') as $categoryId => $joined) {
67                         if ($joined != 'Y') $count++;
68                 } // END - foreach
69
70                 if ((sqlNumRows($result) - $count) < getLeastCats()) {
71                         unsetPostRequestElement('ok');
72                         $LEAST = TRUE;
73                 } // END - if
74         } // END - if
75
76         // Is the form sent?
77         if (isFormSent()) {
78                 // Start counting all
79                 $count = '0';
80
81                 // Ini SQLs here
82                 initSqls();
83
84                 // Go through all entries
85                 foreach (postRequestElement('cat') as $categoryId => $joined) {
86                         // Has the user joined on this category?
87                         switch ($joined) {
88                                 case 'Y':
89                                         // Check if this category has an entry
90                                         if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', TRUE, sprintf(' AND `cat_id`=%s', bigintval($categoryId))) == 0) {
91                                                 // No, so add it
92                                                 addSql(sprintf('INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s,%s)', getMemberId(), bigintval($categoryId)));
93                                         } // END - if
94                                         break;
95
96                                 case 'N':
97                                         addSql(sprintf('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1', getMemberId(), bigintval($categoryId)));
98                                         break;
99                         } // END - switch
100                 } // END - foreach
101
102                 // Are there entries?
103                 if (countSqls() > 0) {
104                         // Run SQL commands
105                         $count = runFilterChain('run_sqls');
106                 } // END - if
107
108                 // Categories saved?
109                 if ($count > 0) {
110                         // Output message
111                         displayMessage('{--MEMBER_CATEGORIES_SAVED--}');
112                 } else {
113                         // None save
114                         displayMessage('{--MEMBER_CATEGORIES_NOT_SAVED--}');
115                 }
116         } else {
117                 if ($LEAST === TRUE) {
118                         // Also here we have to secure it... :(
119                         displayMessage('{--CHOOSE_MORE_CATEGORIES--}');
120                 } // END - if
121
122                 // Load all categories of this member
123                 $OUT = '';
124                 while ($content = sqlFetchArray($result)) {
125                         // Default he has not joined, add color switch
126                         $content['jn'] = ' checked="checked"';
127                         $content['jy'] = '';
128
129                         // When we found an entry don't read it, just change the jx elements
130                         if ((isFormSent()) && (isPostRequestElementSet('cat'))) {
131                                 // Form sent?
132                                 if (postRequestElement('cat', $content['id']) =='Y') {
133                                         $content['jy'] = ' checked="checked"';
134                                         $content['jn'] = '';
135                                 } // END - if
136                         } else {
137                                 // Check if he has an entry
138                                 if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', TRUE, sprintf(' AND `cat_id`=%s', bigintval($content['id']))) == 1) {
139                                         $content['jn'] = '';
140                                         $content['jy'] = ' checked="checked"';
141                                 } // END - if
142                         }
143
144                         // Load row template
145                         $OUT .= loadTemplate('member_cats_row', TRUE, $content);
146                 } // END - while
147
148                 // Free result
149                 sqlFreeResult($result);
150
151                 // Load footer template
152                 loadTemplate('member_cats', FALSE, $OUT);
153         }
154 } else {
155         // No cateogries are defined yet
156         displayMessage('{--MEMBER_NO_CATEGORIES--}');
157 }
158
159 // Free result
160 sqlFreeResult($result);
161
162 // [EOF]
163 ?>