a2b4e0f16d3682031b345ad5194a260312500c1c
[mailer.git] / inc / modules / admin / what-list_user.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 09/28/2003 *
4  * ===================                          Last change: 08/18/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_user.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : List all or partial members                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle oder nur bestimmte Mitglieder auflisten     *
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')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
45
46 // Init variables
47 $moreColumns = '';
48
49 // Add lock reason?
50 if (isExtensionInstalledAndNewer('user', '0.3.5')) {
51         // Add them...
52         $moreColumns .= ', UNIX_TIMESTAMP(`lock_timestamp`) AS `lock_timestamp`';
53 } // END - if
54
55 // Is the extension 'country' installed?
56 if (isExtensionActive('country')) {
57         // Add country code
58         $moreColumns .= ',`country_code`';
59 } // END - if
60
61 // Init unset data
62 if (!isGetRequestElementSet('letter')) { setGetRequestElement('letter', '');       }
63 if (!isGetRequestElementSet('sortby')) { setGetRequestElement('sortby', 'userid'); }
64 if (!isGetRequestElementSet('page'))   { setGetRequestElement('page'  , 1);        }
65
66 // Set base HTML
67 $base = '[<a href="{%url=modules.php?module=admin';
68
69 if (isGetRequestElementSet('userid')) {
70         // Secure the user id
71         $userid = bigintval(getRequestElement('userid'));
72
73         // Does the account exists?
74         $result_user = sqlQueryEscaped('SELECT
75         `userid`,
76         `country`,
77         `email`,
78         `birth_day`,
79         `birth_month`,
80         `birth_year`,
81         `last_online`,
82         `used_points`,
83         `emails_sent`,
84         `joined`,
85         `last_update`,
86         `last_profile_sent`,
87         `notified`,
88         `emails_received`,
89         `mails_confirmed`
90         ' . $moreColumns . '
91 FROM
92         `{?_MYSQL_PREFIX?}_user_data`
93 WHERE
94         `userid`=%s
95 LIMIT 1',
96                 array($userid), __FILE__, __LINE__);
97
98         // User found?
99         if (sqlNumRows($result_user) == 1) {
100                 // Account found
101                 $content  = sqlFetchArray($result_user);
102
103                 // Get count/sum of refs, selected categories, unconfirmed mails
104                 // @TODO Refacture these old link generation to functions
105                 $content['refs']  = countSumTotalData($userid, 'refsystem', 'counter');
106                 $content['cats']  = countSumTotalData($userid, 'user_cats', 'id', 'userid', TRUE);
107                 $content['links'] = getTotalUnconfirmedMails($userid);
108
109                 // Add links to the numbers
110                 if ($content['links'] > 0) {
111                         $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $userid . '%}">' . $content['links'] . '</a>]';
112                 } // END - if
113                 if ($content['refs']  > 0) {
114                         $content['refs']  = $base . '&amp;what=list_refs&amp;userid=' . $userid . '%}">{%pipe,translateComma=' . $content['refs'] . '%}</a>]';
115                 } // END - if
116                 if ($content['cats']  > 0) {
117                         $content['cats']  = $base . '&amp;what=list_user_cats&amp;userid=' . $userid . '%}">{%pipe,translateComma=' . $content['cats'] . '%}</a>]';
118                 } // END - if
119
120                 // Calculate timestamp for birthday
121                 // @TODO Refacture birth_foo to avoid uni* timetstamps as older members cannot be greeded correctly
122                 $stamp = mktime(0, 0, 0, $content['birth_month'], $content['birth_day'], $content['birth_year']);
123
124                 // Is this above zero?
125                 if ($stamp > 0) {
126                         // Then use it
127                         $content['birthday'] = generateDateTime($stamp, '2');
128                 } else {
129                         // Zero or below so set zero!
130                         $content['birthday'] = generateDateTime(0, '2');
131                 }
132
133                 // Prepare data for template
134                 $content['email_link']        = generateEmailLink($content['email'], 'user_data');
135                 $content['last_online']       = generateDateTime($content['last_online'], '2');
136                 $content['joined']            = generateDateTime($content['joined'], '2');
137                 $content['last_update']       = generateDateTime($content['last_update'], '2');
138                 $content['last_profile_sent'] = generateDateTime($content['last_profile_sent'], '2');
139                 $content['locked_points']     = countSumTotalData($content['userid'], 'user_points', 'locked_points') + countSumTotalData($content['userid'], 'user_points', 'locked_order_points');
140
141                 // Prepare array for filter
142                 $data = array(
143                         'userid' => $content['userid'],
144                         'points' => 0
145                 );
146
147                 // Run the filter chain
148                 $data = runFilterChain('get_own_points', $data);
149
150                 // Add more data
151                 $content['own_points']     = $data['points'];
152                 $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], '2');
153
154                 // Link to email_details if some are sent
155                 if ($content['emails_sent'] > 0) {
156                         $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['emails_sent'] . '%}</a>]';
157                 } // END - if
158
159                 // Clickrate
160                 $content['click_rate'] = calculatePercentageRate($content['mails_confirmed'], $content['emails_received']);
161
162                 // Is the extension 'country' installed?
163                 if (isExtensionActive('country')) {
164                         // Then overwrite country information
165                         $content['country'] = generateCountryInfo($content['country_code']);
166                 } elseif ($content['country'] == '0') {
167                         // Zero id???
168                         $content['country'] = '???';
169                 }
170
171                 // Add userid
172                 $content['userid'] = $userid;
173
174                 // Load user-details template
175                 loadTemplate('admin_user_details', FALSE, $content);
176         } else {
177                 // Account does not exist!
178                 displayMessage('{%message,ADMIN_MEMBER_404=' . $userid . '%}');
179         }
180
181         // Free the result
182         sqlFreeResult($result_user);
183 } else {
184         $whereStatement = '';
185         if (isGetRequestElementSet('letter')) {
186                 // List only persons w
187                 $whereStatement = " WHERE `family` LIKE '".getRequestElement('letter') . "%%'";
188         } // END - if
189
190         // Parse the status or mode parameter
191         if (isGetRequestElementSet('status')) {
192                 // Is a WHERE statement already there?
193                 if (!empty($whereStatement)) {
194                         // Then append the status column
195                         $whereStatement .= sprintf(" AND `status`='%s'", sqlEscapeString(strtoupper(getRequestElement('status'))));
196                 } else {
197                         // Start a new one
198                         $whereStatement = sprintf(" WHERE `status`='%s'", sqlEscapeString(strtoupper(getRequestElement('status'))));
199                 }
200
201                 // Exclude other users
202                 $whereStatement = runFilterChain('user_exclusion_sql', ' ' . $whereStatement);
203         } elseif (isGetRequestElementSet('do')) {
204                 // Choose what we need to list
205                 switch (getRequestElement('do')) {
206                         case 'norefs': // Users w/o refs
207                                 if (!empty($whereStatement)) {
208                                         // Add AND statement
209                                         $whereStatement .= ' AND (`refid`=0 OR `refid` IS NULL)';
210                                 } else {
211                                         // Add WHERE statement
212                                         $whereStatement = ' WHERE (`refid`=0 OR `refid` IS NULL)';
213                                 }
214                                 break;
215
216                         case 'random_refid': // Users available for random referral id
217                                 if (!empty($whereStatement)) {
218                                         // Add AND statement
219                                         $whereStatement .= ' AND `rand_confirmed` >= {?user_min_confirmed?}';
220                                 } else {
221                                         // Add WHERE statement
222                                         $whereStatement = ' WHERE `rand_confirmed` >= {?user_min_confirmed?}';
223                                 }
224                                 break;
225
226                         case 'testers': // Test accounts
227                                 // Add statement through filter
228                                 $whereStatement = runFilterChain('user_inclusion_sql', $whereStatement);
229                                 break;
230
231                         default: // Invalid list mode
232                                 reportBug(__FILE__, __LINE__, sprintf('Invalid do %s detected.', sqlEscapeString(getRequestElement('do'))));
233                                 break;
234                 } // END - switch
235
236                 // If it is not 'testers', add exclusion SQL
237                 if (getRequestElement('do') != 'testers') {
238                         // Exclude tester users
239                         $whereStatement = runFilterChain('user_exclusion_sql', ' ' . $whereStatement);
240                 } // END - if
241         } // END - if
242
243         // Generate master query string
244         $sql = sprintf('SELECT
245         `userid`,
246         `email`,
247         `emails_sent`,
248         `mails_confirmed`,
249         `emails_received`
250         ' . $moreColumns . '
251 FROM
252         `{?_MYSQL_PREFIX?}_user_data`
253 ' . $whereStatement . '
254 ORDER BY
255         `%s` ASC',
256                 getRequestElement('sortby')
257         );
258
259         // Prepare SQL and run it
260         $result_master = sqlQuery($sql, __FILE__, __LINE__);
261
262         // Is the user limit zero?
263         if (getUserLimit() == '0') {
264                 // Then set it to default ...
265                 setConfigEntry('user_limit', 100);
266
267                 // ... but output warning
268                 displayMessage('{--EXTENSION_WARNING_USER_LIMIT--}');
269         } // END - if
270
271         // Calculate page count (0.5 fixes a bug with page count)
272         $numPages = round(sqlNumRows($result_master) / getUserLimit() + 0.5);
273
274         if (!isGetRequestElementSet('page'))   setGetRequestElement('page'  , 1);
275         if (!isGetRequestElementSet('offset')) setGetRequestElement('offset', getUserLimit());
276
277         // Add limitation to SQL string and run him again
278         $sql .= ' LIMIT ' . (getRequestElement('offset') * getRequestElement('page') - getRequestElement('offset')) . ', ' . getRequestElement('offset');
279         $result = sqlQuery($sql, __FILE__, __LINE__);
280
281         // Are there some entries?
282         if (!ifSqlHasZeroNums($result_master)) {
283                 // Sorting links and number of pages
284                 $templateContent['sort_links'] = addSortLinks(getRequestElement('letter'), getRequestElement('sortby'));
285                 $templateContent['num_pages'] = $numPages;
286
287                 // Column with nickname when nickname extension is present
288                 // @TODO Rewrite this into a filter
289                 $templateContent['nickname_th'] = '';
290                 if (isExtensionActive('nickname')) {
291                         // Nickname extension found
292                         $templateContent['nickname_th'] = '<td class="header_column bottom" align="center">{--NICKNAME--}</td>';
293                 } // END - if
294
295                 // Load all users
296                 $OUT = '';
297                 while ($content = sqlFetchArray($result)) {
298                         // Merge more data in
299                         $content = merge_array($content, $templateContent);
300
301                         // Get number of unconfirmed mails
302                         $content['links'] = getTotalUnconfirmedMails($content['userid']);
303                         if ($content['links'] > 0) {
304                                 $content['links'] = $base . '&amp;what=list_links&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['links'] . '%}</a>]';
305                         } // END - if
306
307                         // Set link to sent mails if present
308                         if ($content['emails_sent'] > 0) {
309                                 $content['emails_sent'] = $base . '&amp;what=email_details&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['emails_sent'] . '%}</a>]';
310                         } // END - if
311
312                         // Click rate
313                         $content['click_rate'] = calculatePercentageRate($content['mails_confirmed'], $content['emails_received']);
314
315                         // Transfer data to array
316                         $content['email']          = '[<a href="' . generateEmailLink($content['email'], 'user_data') . '">' . $content['email'] . '</a>]';
317                         $content['lock_timestamp'] = generateDateTime($content['lock_timestamp'], '2');
318                         $content['refs']           = countSumTotalData($content['userid'], 'user_refs', 'id', 'userid', TRUE);
319
320                         // Prepare array for filter
321                         $data = array(
322                                 'userid' => $content['userid'],
323                                 'points' => 0
324                         );
325
326                         // Run the filter chain
327                         $data = runFilterChain('get_own_points', $data);
328
329                         // Copy points
330                         $content['own_points'] = $data['points'];
331
332                         // Prepare array for filter again
333                         $data = array(
334                                 'userid' => $content['userid'],
335                                 'points' => 0
336                         );
337
338                         // Run the filter chain
339                         $data = runFilterChain('get_locked_points', $data);
340
341                         // Copy points
342                         $content['locked_points']  = $data['points'];
343
344                         // If we have at least one referral, make it clickable to referral list
345                         if ($content['refs'] > 0) {
346                                 $content['refs'] = $base . '&amp;what=list_refs&amp;userid=' . $content['userid'] . '%}">{%pipe,translateComma=' . $content['refs'] . '%}</a>]';
347                         } // END - if
348
349                         // Is the extension 'country' installed?
350                         // @TODO Rewrite this into a filter
351                         if (isExtensionActive('country')) {
352                                 // Then overwrite country information
353                                 $content['country'] = generateCountryInfo($content['country_code']);
354                         } elseif ($content['country'] == '') {
355                                 // Zero id???
356                                 $content['country'] = '???';
357                         }
358
359                         // Load row template and switch colors
360                         $OUT .= loadTemplate('admin_list_user_row', TRUE, $content);
361                 } // END - while
362
363                 // Free memory
364                 sqlFreeResult($result);
365
366                 // Free some memory
367                 sqlFreeResult($result_master);
368
369                 // Remember all rows
370                 $content['rows'] = $OUT;
371
372                 // Merge more data again
373                 $content = merge_array($content, $templateContent);
374
375                 // Load main template
376                 loadTemplate('admin_list_user', FALSE, $content);
377         } else {
378                 // No one as registered so far! :-(
379                 displayMessage('{--ADMIN_NO_NONE_REGISTERED--}');
380         }
381
382         // Free memory
383         sqlFreeResult($result_master);
384 }
385
386 // [EOF]
387 ?>