[2597] | 1 | # +-----------------------------------------------------------------------+ |
---|
| 2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
| 3 | # +-----------------------------------------------------------------------+ |
---|
| 4 | # | Copyright(C) 2008 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 | # +-----------------------------------------------------------------------+ |
---|
| 20 | package Uploader::PWG::Categories; |
---|
| 21 | use strict; |
---|
[2661] | 22 | use Data::Dumper; |
---|
[3159] | 23 | use Wx::Locale qw/:default/; |
---|
[2597] | 24 | use base qw/ |
---|
| 25 | Uploader::Object |
---|
| 26 | Class::Accessor::Fast |
---|
| 27 | /; |
---|
| 28 | |
---|
| 29 | # create a list of tree items |
---|
| 30 | sub 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 | map { |
---|
| 45 | my $child = $records->{$_}; |
---|
| 46 | my $parentid = _parentid($child); |
---|
| 47 | if(defined $parentid){ |
---|
| 48 | _addchild( |
---|
| 49 | $records->{$parentid}, |
---|
| 50 | $child |
---|
| 51 | ); |
---|
| 52 | delete $records->{$_}; |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | sort { $b <=> $a } |
---|
| 56 | keys %$records; |
---|
[2661] | 57 | |
---|
[2597] | 58 | return [ |
---|
| 59 | [ |
---|
[3287] | 60 | $self->branding->{Categories}, |
---|
[2597] | 61 | undef, |
---|
| 62 | -1, |
---|
[2661] | 63 | 0, |
---|
[2597] | 64 | -1, |
---|
| 65 | |
---|
| 66 | ], |
---|
[2661] | 67 | sort { $a->[6] <=> $b->[6] } values %$records, |
---|
[2597] | 68 | ]; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | sub _parentid { |
---|
| 72 | my ( $record ) = @_; |
---|
| 73 | |
---|
| 74 | $record->[5]; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | sub _addchild { |
---|
| 79 | my ( $record, $child ) = @_; |
---|
| 80 | |
---|
| 81 | $record->[1] ||= []; |
---|
| 82 | |
---|
| 83 | my $children = $record->[1]; |
---|
| 84 | |
---|
| 85 | push @$children, $child; |
---|
| 86 | |
---|
| 87 | # sort by rank |
---|
| 88 | @$children = sort { $a->[6] <=> $b->[6] } @$children; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | sub _parent_rank { |
---|
| 92 | my ( $category ) = @_; |
---|
| 93 | |
---|
| 94 | my @rank = split /\./, $category->{global_rank} ; |
---|
| 95 | my @ucats = split /,/, $category->{uppercats} ; |
---|
| 96 | |
---|
| 97 | my $parent; |
---|
| 98 | if( 1 < scalar @ucats ){ |
---|
| 99 | $parent = $ucats[scalar @rank - 2 ]; |
---|
| 100 | } |
---|
| 101 | my $rank = $rank[scalar @rank - 1 ]; |
---|
| 102 | |
---|
| 103 | return ($parent, $rank); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | sub _record { |
---|
| 109 | my ( $category ) = @_; |
---|
| 110 | |
---|
| 111 | my ( $parent, $rank ) = _parent_rank($category); |
---|
| 112 | return [ |
---|
| 113 | $category->{name}, |
---|
| 114 | undef, |
---|
| 115 | $category, |
---|
[2661] | 116 | 1, |
---|
[2597] | 117 | -1, |
---|
| 118 | $parent, |
---|
| 119 | $rank, |
---|
| 120 | ]; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | 1; |
---|