Skip to content

Instantly share code, notes, and snippets.

@scottwalters
Created August 24, 2010 18:03
Show Gist options
  • Save scottwalters/547996 to your computer and use it in GitHub Desktop.
Save scottwalters/547996 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use lib '/data/WebGUI/lib';
use lib '/data/WebGUI/t/lib';
use WebGUI::Test;
use WebGUI::Session;
use WebGUI::User;
use WebGUI::Asset;
use Data::Dumper;
my $session = WebGUI::Test->session;
$session->user( { userId => 3 } ); # become root
my $assets = WebGUI::Asset->getRoot($session)->getLineage(['descendants'], {returnObjects=>1});
my $tree = { };
for my $asset (@$assets) {
my $lineage = $asset->get('lineage');
my @parts = $lineage =~ m/(.{6})/g;
# warn "asset: $asset lineage: $lineage parts: @parts";
my $node = $tree;
while(@parts) {
my $part = shift @parts;
$node->{$part} ||= { };
$node = $node->{$part};
}
# $node->{_asset} = { id => $asset->getId, title => $asset->get('title'), class => ref $asset };
$node->{_asset} = [ $asset->getId, $asset->get('title'), ref $asset ];
}
my $show_tree; $show_tree = sub {
my $node = shift;
my @vertical_lines = @{ shift() || [] };
my $depth= shift || 0;
if( my $asset_stuff = $node->{_asset} ) {
$vertical_lines[-1] = '+-' if @vertical_lines;
print join ' ', join('', @vertical_lines), @$asset_stuff, "\n";
$vertical_lines[-1] = '| ' if @vertical_lines;
# print join ' ', map($_ ? '| ' : ' ', @vertical_lines), @$asset_stuff, "\n";
# $asset->getId, ref($asset), $asset->get('title');
}
my @child_nodes = sort { $a cmp $b } grep $_ ne '_asset', keys %$node;
while (@child_nodes) {
my $child_node = shift @child_nodes;
my $bar_continues_on_down = @child_nodes ? '| ' : ' ';
$show_tree->($node->{$child_node}, [ @vertical_lines, $bar_continues_on_down, ]);
}
};
$show_tree->($tree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment