| 1 | package Crop::Algorithm::TreeSortedPID; | ||
| 2 | use base qw/ Crop::Object /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Algorithm::TreeSortedPID | ||
| 6 | Tree based on pid-n structure. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Crop::Debug; | ||
| 13 | use Crop::Error; | ||
| 14 | use Crop::Util 'load_class'; | ||
| 15 | use Crop::Object::Collection; | ||
| 16 | |||
| 17 | =begin nd | ||
| 18 | Constant: ROOT | ||
| 19 | ID of Root of directory tree | ||
| 20 | =cut | ||
| 21 | use constant { | ||
| 22 | ROOT => 0, # Root of directory tree | ||
| 23 | }; | ||
| 24 | |||
| 25 | =begin nd | ||
| 26 | Variable: our %Attributes | ||
| 27 | Class attributes: | ||
| 28 | |||
| 29 | item - is NOT a node, but an item with useful data | ||
| 30 | tree - the tree storage | ||
| 31 | =cut | ||
| 32 | our %Attributes = ( | ||
| 33 | item => undef, # My::Doc::Dir | ||
| 34 | tree => undef, # My::Doc::Tree::SortedPID | ||
| 35 | ); | ||
| 36 | |||
| 37 | =begin nd | ||
| 38 | Constructor: new ( ) | ||
| 39 | Load required classes for tree-class and item-class. | ||
| 40 | |||
| 41 | Returns: | ||
| 42 | $self - if ok | ||
| 43 | undef - otherwise | ||
| 44 | =cut | ||
| 45 | sub new { | ||
| 46 | my $class = shift; | ||
| 47 | |||
| 48 | my $self = $class->SUPER::new(@_); | ||
| 49 | |||
| 50 | load_class $self->{$_} or return warn "OBJECT|CRIT: Can not load class $self->{$_}" for qw/ tree item /; | ||
| 51 | |||
| 52 | $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | =begin nd | ||
| 56 | Method: childs ($parent) | ||
| 57 | Get the childs of $pid sorted by 'n' with item data. | ||
| 58 | |||
| 59 | Parameters: | ||
| 60 | $parent - parent node of tree; or ROOT unless exists | ||
| 61 | |||
| 62 | Returns: | ||
| 63 | Collection of nodes with items extended | ||
| 64 | =cut | ||
| 65 | sub childs { | ||
| 66 | my ($self, $parent) = @_; | ||
| 67 | my $pid = $parent ? $parent->id : ROOT; | ||
| 68 | |||
| 69 | my $dir = $self->{tree}->All(pid => $pid, SORT => ['n']); | ||
| 70 | |||
| 71 | my $node_method = $self->{tree}->node_attr->name; | ||
| 72 | |||
| 73 | my $itemdir = $self->{item}->All(id => [keys %{$dir->Hash($node_method)}]); | ||
| 74 | |||
| 75 | my $dir_sorted = Crop::Object::Collection->new($self->{item}); | ||
| 76 | for ($dir->List) { | ||
| 77 | my $item = $itemdir->First(id => $_->$node_method); | ||
| 78 | $dir_sorted->Push($item) if $item->visible; | ||
| 79 | } | ||
| 80 | |||
| 81 | $dir_sorted; | ||
| 82 | } | ||
| 83 | |||
| 84 | =begin nd | ||
| 85 | Method: path (@path) | ||
| 86 | Get List of tree items that present a @path. | ||
| 87 | |||
| 88 | Parameters: | ||
| 89 | @path - array of strings where string is a name of node (directory) | ||
| 90 | |||
| 91 | Returns: | ||
| 92 | list of objects that present path - if Ok | ||
| 93 | undef - in case of error | ||
| 94 | =cut | ||
| 95 | sub path { | ||
| 96 | my ($self, @path) = @_; | ||
| 97 | |||
| 98 | my $breadcrumbs = Crop::Object::Collection->new($self->{item}); | ||
| 99 | my $pid = ROOT; | ||
| 100 | for (@path) { | ||
| 101 | my $childs = $self->{tree}->All(pid => $pid, SORT => ['n']); | ||
| 102 | my $hash = $childs->Hash($self->{tree}->node_attr->name); | ||
| 103 | my @content_ids = keys %$hash; | ||
| 104 | my $inner_dirs = $self->{item}->All(id => \@content_ids, visible => 1); | ||
| 105 | my $next = $inner_dirs->First(href => $_) or return warn "NODIR: Document directory not found"; | ||
| 106 | |||
| 107 | $breadcrumbs->Push($next); | ||
| 108 | |||
| 109 | $pid = $next->id; | ||
| 110 | } | ||
| 111 | |||
| 112 | $breadcrumbs; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |