source: extensions/pLoader/trunk/src/Uploader/PWG/Categories.pm @ 5966

Last change on this file since 5966 was 5966, checked in by ronosman, 14 years ago

Bug 1623 fixed : sub categories with id lower than parent id are not displayed.

  • Property svn:eol-style set to LF
File size: 3.4 KB
Line 
1# +-----------------------------------------------------------------------+
2# | pLoader - a Perl photo uploader for Piwigo                            |
3# +-----------------------------------------------------------------------+
4# | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
5# +-----------------------------------------------------------------------+
6# | This program is free software; you can redistribute it and/or modify  |
7# | it under the terms of the GNU General Public License as published by  |
8# | the Free Software Foundation                                          |
9# |                                                                       |
10# | This program is distributed in the hope that it will be useful, but   |
11# | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13# | General Public License for more details.                              |
14# |                                                                       |
15# | You should have received a copy of the GNU General Public License     |
16# | along with this program; if not, write to the Free Software           |
17# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18# | USA.                                                                  |
19# +-----------------------------------------------------------------------+
20package Uploader::PWG::Categories;
21use strict;
22use Data::Dumper;
23use Wx::Locale qw/:default/;
24use base qw/
25           Uploader::Object
26           Class::Accessor::Fast
27           /;
28
29# create a list of tree items
30sub prepare_items {
31    my ( $self, $categories ) = @_;
32
33
34    my $records = {};   
35
36    # lookup by id
37    # every node is a parent
38    map {
39        $records->{$_->{id}} = _record( $_ );
40    }
41    @$categories;
42
43    # find the parent
44    # sort by global rank, descending
45    # to make sure children come before their parents
46    map {
47        my $child = $records->{$_};
48        my $parentid = _parentid($child);
49        if(defined $parentid){
50            _addchild( 
51                $records->{$parentid}, 
52                $child
53            );
54            delete $records->{$_};     
55        }
56    }
57    sort { $records->{$b}->[7] cmp $records->{$a}->[7] }
58    keys %$records;
59
60    return [
61        [
62            $self->branding->{Categories},
63            undef,
64            -1,
65            0,
66            -1,
67           
68        ],
69        sort { $a->[6] <=> $b->[6] } values %$records,
70    ];
71}
72
73sub _parentid {
74    my ( $record ) = @_;
75   
76    $record->[5];
77}
78
79
80sub _addchild {
81    my ( $record, $child ) = @_;
82   
83    $record->[1] ||= [];
84   
85    my $children = $record->[1];
86   
87    push @$children, $child;
88   
89    # sort by rank     
90    @$children = sort { $a->[6] <=> $b->[6] } @$children;
91}
92
93sub _parent_rank {
94   my ( $category ) = @_;
95
96   my @rank = split /\./, $category->{global_rank} ;
97   my @ucats = split /,/, $category->{uppercats} ;
98
99   my $parent;
100   if( 1 < scalar @ucats ){
101       $parent = $ucats[scalar @rank - 2 ];
102   }
103   my $rank = $rank[scalar @rank - 1 ];
104   
105   return ($parent, $rank);
106}
107
108
109
110sub _record {
111    my ( $category ) = @_;
112
113    my ( $parent, $rank ) = _parent_rank($category);
114    return [
115        $category->{name},
116        undef,
117        $category,
118        1,
119        -1,
120        $parent,
121        $rank,
122        $category->{global_rank},
123    ];   
124}
125
1261;
Note: See TracBrowser for help on using the repository browser.