From 65b2be182c376185a40173340a56cf62b7ceb4a0 Mon Sep 17 00:00:00 2001 From: Matthew Wilson Date: Mon, 14 Feb 2005 21:15:12 +0000 Subject: [PATCH] Merging Asset::getLineage and getLineageWhere. No one shoot me if it breaks!! :) These comments are fun. --- lib/WebGUI/Asset.pm | 124 +------------------------------------- lib/WebGUI/Asset/Event.pm | 4 +- 2 files changed, 4 insertions(+), 124 deletions(-) diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 2f45d6413..2dda6e3c0 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -1120,135 +1120,15 @@ Returns an array reference of lineages of relatives based upon rules. =head3 relatives -An array reference of relatives to retrieve. Valid parameters are "siblings", "children", "ancestors", "self", "descendants", "pedigree" +An array reference of relatives to retrieve. Valid parameters are "siblings", "children", "ancestors", "self", "descendants", "pedigree". If you want to retrieve all assets in the tree, use getRoot->getLineage( =head3 rules -A hash reference comprising limits to relative listing. Variables to rules include endingLineageLength, assetToPedigree, excludeClasses, returnQuickReadObjects, returnObjects, invertTree, includeOnlyClasses. +A hash reference comprising limits to relative listing. Variables to rules include endingLineageLength, assetToPedigree, excludeClasses, returnQuickReadObjects, returnObjects, invertTree, includeOnlyClasses, joinClass, and whereClause. There is no real reason to use a joinClass without a whereClause, but it's trivial to use a whereClause if you don't use a joinClass. You will only be able to filter on the asset table, however. =cut sub getLineage { - my $self = shift; - my $relatives = shift; - my $rules = shift; - my $lineage = $self->get("lineage"); - my @whereModifiers; - # let's get those siblings - if (isIn("siblings",@{$relatives})) { - push(@whereModifiers, " (parentId=".quote($self->get("parentId"))." and assetId<>".quote($self->getId).")"); - } - # ancestors too - my @specificFamilyMembers = (); - if (isIn("ancestors",@{$relatives})) { - my @familyTree = ($lineage =~ /(.{6})/g); - while (pop(@familyTree)) { - push(@specificFamilyMembers,join("",@familyTree)) if (scalar(@familyTree)); - } - } - # let's add ourself to the list - if (isIn("self",@{$relatives})) { - push(@specificFamilyMembers,$self->get("lineage")); - } - if (scalar(@specificFamilyMembers) > 0) { - push(@whereModifiers,"(lineage in (".quoteAndJoin(\@specificFamilyMembers)."))"); - } - # we need to include descendants - if (isIn("descendants",@{$relatives})) { - my $mod = "(lineage like ".quote($lineage.'%')." and lineage<>".quote($lineage); - if (exists $rules->{endingLineageLength}) { - $mod .= " and length(lineage) <= ".($rules->{endingLineageLength}*6); - } - $mod .= ")"; - push(@whereModifiers,$mod); - } - # we need to include children - if (isIn("children",@{$relatives})) { - push(@whereModifiers,"(parentId=".quote($self->getId).")"); - } - # now lets add in all of the siblings in every level between ourself and the asset we wish to pedigree - if (isIn("pedigree",@{$relatives}) && exists $rules->{assetToPedigree}) { - my @mods; - my $lineage = $rules->{assetToPedigree}->get("lineage"); - my $length = $rules->{assetToPedigree}->getLineageLength; - for (my $i = $length; $i > 0; $i--) { - my $line = substr($lineage,0,$i*6); - push(@mods,"( lineage like ".quote($line.'%')." and length(lineage)=".(($i+1)*6).")"); - last if ($self->getLineageLength == $i); - } - push(@whereModifiers, "(".join(" or ",@mods).")"); - } - # formulate a where clause - my $where = "state='published'"; - if (exists $rules->{excludeClasses}) { # deal with exclusions - my @set; - foreach my $className (@{$rules->{excludeClasses}}) { - push(@set,"className <> ".quote($className)); - } - $where .= ' and ('.join(" and ",@set).')'; - } - if (exists $rules->{includeOnlyClasses}) { - $where .= ' and (className in ('.quoteAndJoin($rules->{includeOnlyClasses}).'))'; - } - $where .= " and ".join(" or ",@whereModifiers) if (scalar(@whereModifiers)); - # based upon all available criteria, let's get some assets - my $columns = "assetId, className, parentId"; - my $slavedb; - if ($rules->{returnQuickReadObjects}) { - $columns = "*"; - $slavedb = WebGUI::SQL->getSlave; - } - my $sortOrder = ($rules->{invertTree}) ? "desc" : "asc"; - my $sql = "select $columns from asset where $where order by lineage $sortOrder"; - my @lineage; - my %relativeCache; - my $sth = WebGUI::SQL->read($sql, $slavedb); - while (my $properties = $sth->hashRef) { - # create whatever type of object was requested - my $asset; - if ($rules->{returnObjects}) { - if ($self->getId eq $properties->{assetId}) { # possibly save ourselves a hit to the database - $asset = $self; - } else { - $asset = WebGUI::Asset->newByDynamicClass($properties->{assetId}, $properties->{className}); - } - } elsif ($rules->{returnQuickReadObjects}) { - $asset = WebGUI::Asset->newByPropertyHashRef($properties); - } else { - $asset = $properties->{assetId}; - } - # since we have the relatives info now, why not cache it - if ($rules->{returnObjects} || $rules->{returnQuickReadObjects}) { - my $parent = $relativeCache{$properties->{parentId}}; - $relativeCache{$properties->{assetId}} = $asset; - $asset->{_parent} = $parent; - $parent->{_firstChild} = $asset unless(exists $parent->{_firstChild}); - $parent->{_lastChild} = $asset; - } - push(@lineage,$asset); - } - $sth->finish; - return \@lineage; -} - - -#------------------------------------------------------------------- - -=head2 getLineageWhere ( relatives,rules ) - -Returns an array reference of lineages of relatives based upon rules. - -=head3 relatives - -An array reference of relatives to retrieve. Valid parameters are "siblings", "children", "ancestors", "self", "descendants", "pedigree" - -=head3 rules - -A hash reference comprising limits to relative listing. Variables to rules include endingLineageLength, assetToPedigree, excludeClasses, returnQuickReadObjects, returnObjects, invertTree, includeOnlyClasses, joinClass, and whereClause. - -=cut - -sub getLineageWhere { my $self = shift; my $relatives = shift; my $rules = shift; diff --git a/lib/WebGUI/Asset/Event.pm b/lib/WebGUI/Asset/Event.pm index 07e155591..c1f43378b 100644 --- a/lib/WebGUI/Asset/Event.pm +++ b/lib/WebGUI/Asset/Event.pm @@ -355,7 +355,7 @@ sub view { # $var{"next.url"} = WebGUI::URL::page("func=viewEvent&wid=".$self->get("wobjectId")."&eid=".$id) if ($id); $var{description} = $event->getValue("description"); # my $where = "eventscalendar.type=2"; -# my $kiddos = $self->getLineageWhere(["children"],{returnObjects=>1,joinClass=>"WebGUI::Asset::Wobject::EventsCalendar",whereClause=>$where}); +# my $kiddos = $self->getLineage(["children"],{returnObjects=>1,joinClass=>"WebGUI::Asset::Wobject::EventsCalendar",whereClause=>$where}); # my $tabform = WebGUI::TabForm->new(); # #let's try to create a template variable that is a tabform of agendas. # foreach my $agenda (@{$kiddos}) { @@ -393,7 +393,7 @@ sub www_deleteEventConfirm { return WebGUI::Privilege::insufficient() unless ($self->canEdit); if ($session{form}{rid} ne "") { my $where = "EventsCalendar_Event.EventsCalendar_recurringId=".quote($session{form}{rid}); - my $series = $self->getParent->getLineageWhere(["descendants"],{returnObjects=>1, + my $series = $self->getParent->getLineage(["descendants"],{returnObjects=>1, joinClass=>"WebGUI::Asset::Event",whereClause=>$where}); foreach my $trashedEvent (@{$series}) { $trashedEvent->trash;