1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Reset manual order |
---|
4 | Version: auto |
---|
5 | Description: In an album, reset manual order with the current automatic order |
---|
6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=620 |
---|
7 | Author: plg |
---|
8 | Author URI: http://piwigo.wordpress.com |
---|
9 | */ |
---|
10 | |
---|
11 | if (!defined('PHPWG_ROOT_PATH')) |
---|
12 | { |
---|
13 | die('Hacking attempt!'); |
---|
14 | } |
---|
15 | |
---|
16 | load_language('plugin.lang', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/'); |
---|
17 | |
---|
18 | add_event_handler('loc_begin_admin', 'reset_manual_order_add_link'); |
---|
19 | function reset_manual_order_add_link() |
---|
20 | { |
---|
21 | global $template; |
---|
22 | |
---|
23 | // this is a trick: instead of directly filling $_SESSION['page_infos'] in |
---|
24 | // function reset_manual_order_process I had to use a temporary variable |
---|
25 | // $_SESSION['reset_manual_order'] because the message with a quote (in French) |
---|
26 | // was not correctly registered in the database. |
---|
27 | if (isset($_SESSION['reset_manual_order'])) |
---|
28 | { |
---|
29 | $_SESSION['page_infos'] = array(l10n('Images manual order was saved')); |
---|
30 | unset($_SESSION['reset_manual_order']); |
---|
31 | } |
---|
32 | |
---|
33 | $template->set_prefilter('element_set_ranks', 'reset_manual_order_add_link_prefilter'); |
---|
34 | } |
---|
35 | |
---|
36 | function reset_manual_order_add_link_prefilter($content, &$smarty) |
---|
37 | { |
---|
38 | $search = "#<legend>\{'Manual order'\|@translate\}</legend>#"; |
---|
39 | $replacement = '<legend>{\'Manual order\'|@translate}</legend> |
---|
40 | <a href="{$F_ACTION}&reset_manual_order=1">{\'Reset manual order with current automatic order\'|@translate}</a> |
---|
41 | '; |
---|
42 | |
---|
43 | return preg_replace($search, $replacement, $content); |
---|
44 | } |
---|
45 | |
---|
46 | add_event_handler('loc_begin_admin_page', 'reset_manual_order_process'); |
---|
47 | function reset_manual_order_process() |
---|
48 | { |
---|
49 | global $page, $template, $conf; |
---|
50 | |
---|
51 | if ('album' == $page['page'] and 'sort_order' == @$_GET['tab'] and isset($_GET['reset_manual_order'])) |
---|
52 | { |
---|
53 | $query = ' |
---|
54 | SELECT * |
---|
55 | FROM '.CATEGORIES_TABLE.' |
---|
56 | WHERE id = '.$_GET['cat_id'].' |
---|
57 | ;'; |
---|
58 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
59 | |
---|
60 | $order_by = $conf['order_by_inside_category']; |
---|
61 | if (!empty($row['image_order']) and 'rank' != $row['image_order']) |
---|
62 | { |
---|
63 | $order_by = ' ORDER BY '.$row['image_order']; |
---|
64 | } |
---|
65 | |
---|
66 | $query = ' |
---|
67 | SELECT |
---|
68 | image_id |
---|
69 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
70 | JOIN '.IMAGES_TABLE.' ON id = image_id |
---|
71 | WHERE category_id = '.$_GET['cat_id'].' |
---|
72 | '.$order_by.' |
---|
73 | ;'; |
---|
74 | $result = pwg_query($query); |
---|
75 | |
---|
76 | $ordered_image_ids = array(); |
---|
77 | while ($row = pwg_db_fetch_assoc($result)) |
---|
78 | { |
---|
79 | $ordered_image_ids[] = $row['image_id']; |
---|
80 | } |
---|
81 | |
---|
82 | $current_rank = 0; |
---|
83 | $datas = array(); |
---|
84 | foreach ($ordered_image_ids as $id) |
---|
85 | { |
---|
86 | array_push( |
---|
87 | $datas, |
---|
88 | array( |
---|
89 | 'category_id' => $_GET['cat_id'], |
---|
90 | 'image_id' => $id, |
---|
91 | 'rank' => ++$current_rank, |
---|
92 | ) |
---|
93 | ); |
---|
94 | } |
---|
95 | |
---|
96 | $fields = array( |
---|
97 | 'primary' => array('image_id', 'category_id'), |
---|
98 | 'update' => array('rank') |
---|
99 | ); |
---|
100 | |
---|
101 | mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas); |
---|
102 | |
---|
103 | $_SESSION['reset_manual_order'] = true; |
---|
104 | redirect(get_root_url().'admin.php?page=album-'.$_GET['cat_id'].'-sort_order'); |
---|
105 | } |
---|
106 | } |
---|
107 | ?> |
---|