source: branches/2.6/admin/user_list_backend.php @ 27836

Last change on this file since 27836 was 27836, checked in by plg, 10 years ago

bug 3053 fixed: columns "groups" and "privacy level" come back in Piwigo 2.6
user manager (which still needs improvement on filtering options...)

File size: 6.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24define('PHPWG_ROOT_PATH','../');
25define('IN_ADMIN', true);
26
27include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
28
29check_status(ACCESS_ADMINISTRATOR);
30       
31/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
32 * Easy set variables
33 */
34       
35/* Array of database columns which should be read and sent back to DataTables. Use a space where
36 * you want to insert a non-database field (for example a counter or static image)
37 */
38$aColumns = array('id', 'username', 'status', 'mail_address', 'recent_period', 'level', 'registration_date');
39$aColumns = trigger_change('user_list_columns', $aColumns);
40       
41/* Indexed column (used for fast and accurate table cardinality) */
42$sIndexColumn = "id";
43       
44/* DB table to use */
45$sTable = USERS_TABLE.' INNER JOIN '.USER_INFOS_TABLE.' AS ui ON id = ui.user_id';
46
47/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
48 * If you just want to use the basic configuration for DataTables with PHP server-side, there is
49 * no need to edit below this line
50 */
51
52/*
53 * Paging
54 */
55$sLimit = "";
56if ( isset( $_REQUEST['iDisplayStart'] ) && $_REQUEST['iDisplayLength'] != '-1' )
57{
58  $sLimit = "LIMIT ".pwg_db_real_escape_string( $_REQUEST['iDisplayStart'] ).", ".
59    pwg_db_real_escape_string( $_REQUEST['iDisplayLength'] );
60}
61       
62       
63/*
64 * Ordering
65 */
66if ( isset( $_REQUEST['iSortCol_0'] ) )
67{
68  $sOrder = "ORDER BY  ";
69  for ( $i=0 ; $i<intval( $_REQUEST['iSortingCols'] ) ; $i++ )
70  {
71    if ( $_REQUEST[ 'bSortable_'.intval($_REQUEST['iSortCol_'.$i]) ] == "true" )
72    {
73      $sOrder .= $aColumns[ intval( $_REQUEST['iSortCol_'.$i] ) ]."
74                                        ".pwg_db_real_escape_string( $_REQUEST['sSortDir_'.$i] ) .", ";
75    }
76  }
77               
78  $sOrder = substr_replace( $sOrder, "", -2 );
79  if ( $sOrder == "ORDER BY" )
80  {
81    $sOrder = "";
82  }
83}
84       
85       
86/*
87 * Filtering
88 * NOTE this does not match the built-in DataTables filtering which does it
89 * word by word on any field. It's possible to do here, but concerned about efficiency
90 * on very large tables, and MySQL's regex functionality is very limited
91 */
92$sWhere = "";
93if ( $_REQUEST['sSearch'] != "" )
94{
95  $sWhere = "WHERE (";
96  for ( $i=0 ; $i<count($aColumns) ; $i++ )
97  {
98    $sWhere .= $aColumns[$i]." LIKE '%".pwg_db_real_escape_string( $_REQUEST['sSearch'] )."%' OR ";
99  }
100  $sWhere = substr_replace( $sWhere, "", -3 );
101  $sWhere .= ')';
102}
103       
104/* Individual column filtering */
105for ( $i=0 ; $i<count($aColumns) ; $i++ )
106{
107  if (isset($_REQUEST['bSearchable_'.$i]) && isset($_REQUEST['sSearch_'.$i])
108      &&$_REQUEST['bSearchable_'.$i] == "true" && $_REQUEST['sSearch_'.$i] != ''
109    )
110  {
111    if ( $sWhere == "" )
112    {
113      $sWhere = "WHERE ";
114    }
115    else
116    {
117      $sWhere .= " AND ";
118    }
119    $sWhere .= $aColumns[$i]." LIKE '%".pwg_db_real_escape_string($_REQUEST['sSearch_'.$i])."%' ";
120  }
121}
122       
123       
124/*
125 * SQL queries
126 * Get data to display
127 */
128$sQuery = "
129                SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
130                FROM   $sTable
131                $sWhere
132                $sOrder
133                $sLimit
134        ";
135$rResult = pwg_query($sQuery);
136       
137/* Data set length after filtering */
138$rResultFilterTotal = pwg_query('SELECT FOUND_ROWS();');
139list($iFilteredTotal) = pwg_db_fetch_row($rResultFilterTotal);
140       
141/* Total data set length */
142$sQuery = "
143                SELECT COUNT(".$sIndexColumn.")
144                FROM   $sTable
145        ";
146$rResultTotal = pwg_query($sQuery);
147$aResultTotal = pwg_db_fetch_array($rResultTotal);
148$iTotal = $aResultTotal[0];
149       
150       
151/*
152 * Output
153 */
154$output = array(
155  "sEcho" => intval($_REQUEST['sEcho']),
156  "iTotalRecords" => $iTotal,
157  "iTotalDisplayRecords" => $iFilteredTotal,
158  "aaData" => array()
159        );
160
161$user_ids = array();
162
163while ( $aRow = pwg_db_fetch_array( $rResult ) )
164{
165  $user_ids[] = $aRow['id'];
166 
167  $row = array();
168  for ( $i=0 ; $i<count($aColumns) ; $i++ )
169  {
170    if ( $aColumns[$i] == "status" )
171    {
172      $row[] = l10n('user_status_'.$aRow[ $aColumns[$i] ]);
173    }
174    else if ( $aColumns[$i] == "level" )
175    {
176      $row[] = $aRow[ $aColumns[$i] ] == 0 ? '' : l10n(sprintf('Level %d', $aRow[ $aColumns[$i] ]));
177    }
178    else if ( $aColumns[$i] != ' ' )
179    {
180      /* General output */
181      $row[] = $aRow[ $aColumns[$i] ];
182    }
183  }
184  $output['aaData'][] = $row;
185}
186
187// replace "recent_period" by the list of groups
188if (count($user_ids) > 0)
189{
190  $groups_of_user = array();
191 
192  $query = '
193SELECT
194    user_id,
195    GROUP_CONCAT(name ORDER BY name SEPARATOR ", ") AS groups
196  FROM '.USER_GROUP_TABLE.'
197    JOIN '.GROUPS_TABLE.' ON id = group_id
198  WHERE user_id IN ('.implode(',', $user_ids).')
199  GROUP BY user_id
200;';
201  $result = pwg_query($query);
202  while ($row = pwg_db_fetch_assoc($result))
203  {
204    $groups_of_user[ $row['user_id'] ] = $row['groups'];
205  }
206
207  $key_replace = array_search('recent_period', $aColumns);
208 
209  // replacement
210  foreach (array_keys($output['aaData']) as $idx)
211  {
212    $user_id = $output['aaData'][$idx][0];
213    $output['aaData'][$idx][$key_replace] = isset($groups_of_user[$user_id]) ? $groups_of_user[$user_id] : '';
214  }
215}
216
217$output = trigger_change('after_render_user_list', $output);
218       
219echo json_encode( $output );
220?>
Note: See TracBrowser for help on using the repository browser.