lib/TWiki/Configure/Section.pm
changeset 0 414e01d06fd5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/Configure/Section.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +#
     1.5 +# TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.6 +#
     1.7 +# Copyright (C) 2000-2006 TWiki Contributors.
     1.8 +#
     1.9 +# This program is free software; you can redistribute it and/or
    1.10 +# modify it under the terms of the GNU General Public License
    1.11 +# as published by the Free Software Foundation; either version 2
    1.12 +# of the License, or (at your option) any later version. For
    1.13 +# more details read LICENSE in the root of this distribution.
    1.14 +#
    1.15 +# This program is distributed in the hope that it will be useful,
    1.16 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.18 +#
    1.19 +# As per the GPL, removal of this notice is prohibited.
    1.20 +#
    1.21 +# Collection of configuration items
    1.22 +package TWiki::Configure::Section;
    1.23 +
    1.24 +use base 'TWiki::Configure::Item';
    1.25 +
    1.26 +use strict;
    1.27 +
    1.28 +sub new {
    1.29 +    my ($class, $head) = @_;
    1.30 +
    1.31 +    # SMELL: What is the base object supposed to do with the UI class?
    1.32 +    my $this = $class->SUPER::new('TWiki::Configure::UIs::Section');
    1.33 +
    1.34 +    $this->{headline} = $head;
    1.35 +    @{$this->{children}} = ();
    1.36 +
    1.37 +    return $this;
    1.38 +}
    1.39 +
    1.40 +sub addChild {
    1.41 +    my ($this, $child) = @_;
    1.42 +    foreach my $kid (@{$this->{children}}) {
    1.43 +        Carp::confess if $child eq $kid;
    1.44 +    }
    1.45 +    $child->{parent} = $this;
    1.46 +    push(@{$this->{children}}, $child);
    1.47 +}
    1.48 +
    1.49 +sub isExpertsOnly {
    1.50 +    my $this = shift;
    1.51 +    if (!defined($this->{isExpert})) {
    1.52 +        $this->{isExpert} = 1;
    1.53 +        foreach my $kid (@{$this->{children}}) {
    1.54 +            if (!$kid->isExpertsOnly()) {
    1.55 +                $this->{isExpert} = 0;
    1.56 +                last;
    1.57 +            }
    1.58 +        }
    1.59 +    }
    1.60 +    return $this->{isExpert};
    1.61 +}
    1.62 +
    1.63 +sub visit {
    1.64 +    my ($this, $visitor) = @_;
    1.65 +    my %visited;
    1.66 +    return 0 unless $visitor->startVisit($this);
    1.67 +    foreach my $child (@{$this->{children}}) {
    1.68 +        if ($visited{$child}) {
    1.69 +            die join(' ',@{$this->{children}});
    1.70 +        }
    1.71 +        $visited{$child} = 1;
    1.72 +        return 0 unless $child->visit($visitor);
    1.73 +    }
    1.74 +    return 0 unless $visitor->endVisit($this);
    1.75 +    return 1;
    1.76 +}
    1.77 +
    1.78 +sub getDepth {
    1.79 +    my $depth = 0;
    1.80 +    my $mum = shift;
    1.81 +
    1.82 +    while ($mum) {
    1.83 +        $depth++;
    1.84 +        $mum = $mum->{parent};
    1.85 +    }
    1.86 +    return $depth;
    1.87 +}
    1.88 +
    1.89 +# Get the section object associated with the given headline and depth
    1.90 +sub getSectionObject {
    1.91 +    my ($this, $head, $depth) = @_;
    1.92 +    if ($this->{headline} eq $head && $this->getDepth() == $depth) {
    1.93 +        return $this;
    1.94 +    }
    1.95 +    foreach my $child (@{$this->{children}}) {
    1.96 +        my $cvo = $child->getSectionObject($head, $depth);
    1.97 +        return $cvo if $cvo;
    1.98 +    }
    1.99 +    return undef;
   1.100 +}
   1.101 +
   1.102 +# Get the value object associated with the given keys
   1.103 +sub getValueObject {
   1.104 +    my ($this, $keys) = @_;
   1.105 +    foreach my $child (@{$this->{children}}) {
   1.106 +        my $cvo = $child->getValueObject($keys);
   1.107 +        return $cvo if $cvo;
   1.108 +    }
   1.109 +    return undef;
   1.110 +}
   1.111 +
   1.112 +# See if this section is changed from the default values. Should
   1.113 +# return a count of changed values.
   1.114 +sub needsSaving {
   1.115 +    my ($this, $valuer) = @_;
   1.116 +    my $count = 0;
   1.117 +    foreach my $child (@{$this->{children}}) {
   1.118 +       $count += $child->needsSaving($valuer);
   1.119 +    }
   1.120 +    return $count;
   1.121 +}
   1.122 +
   1.123 +1;