1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2013 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 | |
---|
24 | if (!defined('PHPWG_ROOT_PATH')) |
---|
25 | { |
---|
26 | die('Hacking attempt!'); |
---|
27 | } |
---|
28 | |
---|
29 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
30 | |
---|
31 | // +-----------------------------------------------------------------------+ |
---|
32 | // | Check Access and exit when user status is not ok | |
---|
33 | // +-----------------------------------------------------------------------+ |
---|
34 | check_status(ACCESS_ADMINISTRATOR); |
---|
35 | |
---|
36 | trigger_action('loc_begin_cat_list'); |
---|
37 | |
---|
38 | if (!empty($_POST) or isset($_GET['delete'])) |
---|
39 | { |
---|
40 | check_pwg_token(); |
---|
41 | } |
---|
42 | |
---|
43 | // +-----------------------------------------------------------------------+ |
---|
44 | // | functions | |
---|
45 | // +-----------------------------------------------------------------------+ |
---|
46 | |
---|
47 | /** |
---|
48 | * save the rank depending on given categories order |
---|
49 | * |
---|
50 | * The list of ordered categories id is supposed to be in the same parent |
---|
51 | * category |
---|
52 | * |
---|
53 | * @param array categories |
---|
54 | * @return void |
---|
55 | */ |
---|
56 | function save_categories_order($categories) |
---|
57 | { |
---|
58 | $current_rank_for_id_uppercat = array(); |
---|
59 | $current_rank = 0; |
---|
60 | |
---|
61 | $datas = array(); |
---|
62 | foreach ($categories as $category) |
---|
63 | { |
---|
64 | if (is_array($category)) |
---|
65 | { |
---|
66 | $id = $category['id']; |
---|
67 | $id_uppercat = $category['id_uppercat']; |
---|
68 | |
---|
69 | if (!isset($current_rank_for_id_uppercat[$id_uppercat])) |
---|
70 | { |
---|
71 | $current_rank_for_id_uppercat[$id_uppercat] = 0; |
---|
72 | } |
---|
73 | $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat]; |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | $id = $category; |
---|
78 | $current_rank++; |
---|
79 | } |
---|
80 | |
---|
81 | $datas[] = array('id' => $id, 'rank' => $current_rank); |
---|
82 | } |
---|
83 | $fields = array('primary' => array('id'), 'update' => array('rank')); |
---|
84 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
85 | |
---|
86 | update_global_rank(); |
---|
87 | } |
---|
88 | |
---|
89 | // +-----------------------------------------------------------------------+ |
---|
90 | // | initialization | |
---|
91 | // +-----------------------------------------------------------------------+ |
---|
92 | |
---|
93 | check_input_parameter('parent_id', $_GET, false, PATTERN_ID); |
---|
94 | |
---|
95 | $categories = array(); |
---|
96 | |
---|
97 | $base_url = get_root_url().'admin.php?page=cat_list'; |
---|
98 | $navigation = '<a href="'.$base_url.'">'; |
---|
99 | $navigation.= l10n('Home'); |
---|
100 | $navigation.= '</a>'; |
---|
101 | |
---|
102 | // +-----------------------------------------------------------------------+ |
---|
103 | // | tabs | |
---|
104 | // +-----------------------------------------------------------------------+ |
---|
105 | |
---|
106 | $page['tab'] = 'list'; |
---|
107 | include(PHPWG_ROOT_PATH.'admin/include/albums_tab.inc.php'); |
---|
108 | |
---|
109 | // +-----------------------------------------------------------------------+ |
---|
110 | // | virtual categories management | |
---|
111 | // +-----------------------------------------------------------------------+ |
---|
112 | // request to delete a virtual category |
---|
113 | if (isset($_GET['delete']) and is_numeric($_GET['delete'])) |
---|
114 | { |
---|
115 | delete_categories(array($_GET['delete'])); |
---|
116 | $_SESSION['page_infos'] = array(l10n('Virtual album deleted')); |
---|
117 | update_global_rank(); |
---|
118 | invalidate_user_cache(); |
---|
119 | |
---|
120 | $redirect_url = get_root_url().'admin.php?page=cat_list'; |
---|
121 | if (isset($_GET['parent_id'])) |
---|
122 | { |
---|
123 | $redirect_url.= '&parent_id='.$_GET['parent_id']; |
---|
124 | } |
---|
125 | redirect($redirect_url); |
---|
126 | } |
---|
127 | // request to add a virtual category |
---|
128 | elseif (isset($_POST['submitAdd'])) |
---|
129 | { |
---|
130 | $output_create = create_virtual_category( |
---|
131 | $_POST['virtual_name'], |
---|
132 | @$_GET['parent_id'] |
---|
133 | ); |
---|
134 | |
---|
135 | invalidate_user_cache(); |
---|
136 | if (isset($output_create['error'])) |
---|
137 | { |
---|
138 | $page['errors'][] = $output_create['error']; |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | $page['infos'][] = $output_create['info']; |
---|
143 | } |
---|
144 | } |
---|
145 | // save manual category ordering |
---|
146 | elseif (isset($_POST['submitManualOrder'])) |
---|
147 | { |
---|
148 | asort($_POST['catOrd'], SORT_NUMERIC); |
---|
149 | save_categories_order(array_keys($_POST['catOrd'])); |
---|
150 | |
---|
151 | $page['infos'][] = l10n('Album manual order was saved'); |
---|
152 | } |
---|
153 | elseif (isset($_POST['submitAutoOrder'])) |
---|
154 | { |
---|
155 | $query = ' |
---|
156 | SELECT id |
---|
157 | FROM '.CATEGORIES_TABLE.' |
---|
158 | WHERE id_uppercat '. |
---|
159 | (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).' |
---|
160 | ;'; |
---|
161 | $category_ids = array_from_query($query, 'id'); |
---|
162 | |
---|
163 | if (isset($_POST['recursive'])) |
---|
164 | { |
---|
165 | $category_ids = get_subcat_ids($category_ids); |
---|
166 | } |
---|
167 | |
---|
168 | $categories = array(); |
---|
169 | $names = array(); |
---|
170 | $id_uppercats = array(); |
---|
171 | |
---|
172 | $query = ' |
---|
173 | SELECT id, name, id_uppercat |
---|
174 | FROM '.CATEGORIES_TABLE.' |
---|
175 | WHERE id IN ('.implode(',', $category_ids).') |
---|
176 | ;'; |
---|
177 | $result = pwg_query($query); |
---|
178 | while ($row = pwg_db_fetch_assoc($result)) |
---|
179 | { |
---|
180 | $categories[] = array( |
---|
181 | 'id' => $row['id'], |
---|
182 | 'id_uppercat' => $row['id_uppercat'], |
---|
183 | ); |
---|
184 | $names[] = $row['name']; |
---|
185 | } |
---|
186 | |
---|
187 | array_multisort( |
---|
188 | $names, |
---|
189 | SORT_REGULAR, |
---|
190 | 'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC, |
---|
191 | $categories |
---|
192 | ); |
---|
193 | save_categories_order($categories); |
---|
194 | |
---|
195 | $page['infos'][] = l10n('Albums automatically sorted'); |
---|
196 | } |
---|
197 | |
---|
198 | // +-----------------------------------------------------------------------+ |
---|
199 | // | Navigation path | |
---|
200 | // +-----------------------------------------------------------------------+ |
---|
201 | |
---|
202 | if (isset($_GET['parent_id'])) |
---|
203 | { |
---|
204 | $navigation.= $conf['level_separator']; |
---|
205 | |
---|
206 | $navigation.= get_cat_display_name_from_id( |
---|
207 | $_GET['parent_id'], |
---|
208 | $base_url.'&parent_id=' |
---|
209 | ); |
---|
210 | } |
---|
211 | // +-----------------------------------------------------------------------+ |
---|
212 | // | template initialization | |
---|
213 | // +-----------------------------------------------------------------------+ |
---|
214 | $template->set_filename('categories', 'cat_list.tpl'); |
---|
215 | |
---|
216 | $form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list'; |
---|
217 | if (isset($_GET['parent_id'])) |
---|
218 | { |
---|
219 | $form_action.= '&parent_id='.$_GET['parent_id']; |
---|
220 | } |
---|
221 | |
---|
222 | $template->assign(array( |
---|
223 | 'CATEGORIES_NAV'=>$navigation, |
---|
224 | 'F_ACTION'=>$form_action, |
---|
225 | 'PWG_TOKEN' => get_pwg_token(), |
---|
226 | )); |
---|
227 | |
---|
228 | // +-----------------------------------------------------------------------+ |
---|
229 | // | Categories display | |
---|
230 | // +-----------------------------------------------------------------------+ |
---|
231 | |
---|
232 | $categories = array(); |
---|
233 | |
---|
234 | $query = ' |
---|
235 | SELECT id, name, permalink, dir, rank, status |
---|
236 | FROM '.CATEGORIES_TABLE; |
---|
237 | if (!isset($_GET['parent_id'])) |
---|
238 | { |
---|
239 | $query.= ' |
---|
240 | WHERE id_uppercat IS NULL'; |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | $query.= ' |
---|
245 | WHERE id_uppercat = '.$_GET['parent_id']; |
---|
246 | } |
---|
247 | $query.= ' |
---|
248 | ORDER BY rank ASC |
---|
249 | ;'; |
---|
250 | $categories = hash_from_query($query, 'id'); |
---|
251 | |
---|
252 | // get the categories containing images directly |
---|
253 | $categories_with_images = array(); |
---|
254 | if (count($categories)) |
---|
255 | { |
---|
256 | $query = ' |
---|
257 | SELECT |
---|
258 | category_id, |
---|
259 | COUNT(*) AS nb_photos |
---|
260 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
261 | GROUP BY category_id |
---|
262 | ;'; |
---|
263 | // WHERE category_id IN ('.implode(',', array_keys($categories)).') |
---|
264 | |
---|
265 | $nb_photos_in = query2array($query, 'category_id', 'nb_photos'); |
---|
266 | |
---|
267 | $query = ' |
---|
268 | SELECT |
---|
269 | id, |
---|
270 | uppercats |
---|
271 | FROM '.CATEGORIES_TABLE.' |
---|
272 | ;'; |
---|
273 | $all_categories = query2array($query, 'id', 'uppercats'); |
---|
274 | $subcats_of = array(); |
---|
275 | |
---|
276 | foreach (array_keys($categories) as $cat_id) |
---|
277 | { |
---|
278 | foreach ($all_categories as $id => $uppercats) |
---|
279 | { |
---|
280 | if (preg_match('/(^|,)'.$cat_id.',/', $uppercats)) |
---|
281 | { |
---|
282 | @$subcats_of[$cat_id][] = $id; |
---|
283 | } |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | $nb_sub_photos = array(); |
---|
288 | foreach ($subcats_of as $cat_id => $subcat_ids) |
---|
289 | { |
---|
290 | $nb_photos = 0; |
---|
291 | foreach ($subcat_ids as $id) |
---|
292 | { |
---|
293 | if (isset($nb_photos_in[$id])) |
---|
294 | { |
---|
295 | $nb_photos+= $nb_photos_in[$id]; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | $nb_sub_photos[$cat_id] = $nb_photos; |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | $template->assign('categories', array()); |
---|
304 | $base_url = get_root_url().'admin.php?page='; |
---|
305 | |
---|
306 | if (isset($_GET['parent_id'])) |
---|
307 | { |
---|
308 | $template->assign( |
---|
309 | 'PARENT_EDIT', |
---|
310 | $base_url.'album-'.$_GET['parent_id'] |
---|
311 | ); |
---|
312 | } |
---|
313 | |
---|
314 | foreach ($categories as $category) |
---|
315 | { |
---|
316 | $cat_list_url = $base_url.'cat_list'; |
---|
317 | |
---|
318 | $self_url = $cat_list_url; |
---|
319 | if (isset($_GET['parent_id'])) |
---|
320 | { |
---|
321 | $self_url.= '&parent_id='.$_GET['parent_id']; |
---|
322 | } |
---|
323 | |
---|
324 | $tpl_cat = |
---|
325 | array( |
---|
326 | 'NAME' => |
---|
327 | trigger_event( |
---|
328 | 'render_category_name', |
---|
329 | $category['name'], |
---|
330 | 'admin_cat_list' |
---|
331 | ), |
---|
332 | 'NB_PHOTOS' => isset($nb_photos_in[$category['id']]) ? $nb_photos_in[$category['id']] : 0, |
---|
333 | 'NB_SUB_PHOTOS' => isset($nb_sub_photos[$category['id']]) ? $nb_sub_photos[$category['id']] : 0, |
---|
334 | 'NB_SUB_ALBUMS' => isset($subcats_of[$category['id']]) ? count($subcats_of[$category['id']]) : 0, |
---|
335 | 'ID' => $category['id'], |
---|
336 | 'RANK' => $category['rank']*10, |
---|
337 | |
---|
338 | 'U_JUMPTO' => make_index_url( |
---|
339 | array( |
---|
340 | 'category' => $category |
---|
341 | ) |
---|
342 | ), |
---|
343 | |
---|
344 | 'U_CHILDREN' => $cat_list_url.'&parent_id='.$category['id'], |
---|
345 | 'U_EDIT' => $base_url.'album-'.$category['id'], |
---|
346 | |
---|
347 | 'IS_VIRTUAL' => empty($category['dir']) |
---|
348 | ); |
---|
349 | |
---|
350 | if (empty($category['dir'])) |
---|
351 | { |
---|
352 | $tpl_cat['U_DELETE'] = $self_url.'&delete='.$category['id']; |
---|
353 | $tpl_cat['U_DELETE'].= '&pwg_token='.get_pwg_token(); |
---|
354 | } |
---|
355 | else |
---|
356 | { |
---|
357 | if ($conf['enable_synchronization']) |
---|
358 | { |
---|
359 | $tpl_cat['U_SYNC'] = $base_url.'site_update&site=1&cat_id='.$category['id']; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | $template->append('categories', $tpl_cat); |
---|
364 | } |
---|
365 | |
---|
366 | trigger_action('loc_end_cat_list'); |
---|
367 | |
---|
368 | // +-----------------------------------------------------------------------+ |
---|
369 | // | sending html code | |
---|
370 | // +-----------------------------------------------------------------------+ |
---|
371 | $template->assign_var_from_handle('ADMIN_CONTENT', 'categories'); |
---|
372 | ?> |
---|