lib/TWiki/Configure/Section.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
#
colas@0
     2
# TWiki Enterprise Collaboration Platform, http://TWiki.org/
colas@0
     3
#
colas@0
     4
# Copyright (C) 2000-2006 TWiki Contributors.
colas@0
     5
#
colas@0
     6
# This program is free software; you can redistribute it and/or
colas@0
     7
# modify it under the terms of the GNU General Public License
colas@0
     8
# as published by the Free Software Foundation; either version 2
colas@0
     9
# of the License, or (at your option) any later version. For
colas@0
    10
# more details read LICENSE in the root of this distribution.
colas@0
    11
#
colas@0
    12
# This program is distributed in the hope that it will be useful,
colas@0
    13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
colas@0
    14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
colas@0
    15
#
colas@0
    16
# As per the GPL, removal of this notice is prohibited.
colas@0
    17
#
colas@0
    18
# Collection of configuration items
colas@0
    19
package TWiki::Configure::Section;
colas@0
    20
colas@0
    21
use base 'TWiki::Configure::Item';
colas@0
    22
colas@0
    23
use strict;
colas@0
    24
colas@0
    25
sub new {
colas@0
    26
    my ($class, $head) = @_;
colas@0
    27
colas@0
    28
    # SMELL: What is the base object supposed to do with the UI class?
colas@0
    29
    my $this = $class->SUPER::new('TWiki::Configure::UIs::Section');
colas@0
    30
colas@0
    31
    $this->{headline} = $head;
colas@0
    32
    @{$this->{children}} = ();
colas@0
    33
colas@0
    34
    return $this;
colas@0
    35
}
colas@0
    36
colas@0
    37
sub addChild {
colas@0
    38
    my ($this, $child) = @_;
colas@0
    39
    foreach my $kid (@{$this->{children}}) {
colas@0
    40
        Carp::confess if $child eq $kid;
colas@0
    41
    }
colas@0
    42
    $child->{parent} = $this;
colas@0
    43
    push(@{$this->{children}}, $child);
colas@0
    44
}
colas@0
    45
colas@0
    46
sub isExpertsOnly {
colas@0
    47
    my $this = shift;
colas@0
    48
    if (!defined($this->{isExpert})) {
colas@0
    49
        $this->{isExpert} = 1;
colas@0
    50
        foreach my $kid (@{$this->{children}}) {
colas@0
    51
            if (!$kid->isExpertsOnly()) {
colas@0
    52
                $this->{isExpert} = 0;
colas@0
    53
                last;
colas@0
    54
            }
colas@0
    55
        }
colas@0
    56
    }
colas@0
    57
    return $this->{isExpert};
colas@0
    58
}
colas@0
    59
colas@0
    60
sub visit {
colas@0
    61
    my ($this, $visitor) = @_;
colas@0
    62
    my %visited;
colas@0
    63
    return 0 unless $visitor->startVisit($this);
colas@0
    64
    foreach my $child (@{$this->{children}}) {
colas@0
    65
        if ($visited{$child}) {
colas@0
    66
            die join(' ',@{$this->{children}});
colas@0
    67
        }
colas@0
    68
        $visited{$child} = 1;
colas@0
    69
        return 0 unless $child->visit($visitor);
colas@0
    70
    }
colas@0
    71
    return 0 unless $visitor->endVisit($this);
colas@0
    72
    return 1;
colas@0
    73
}
colas@0
    74
colas@0
    75
sub getDepth {
colas@0
    76
    my $depth = 0;
colas@0
    77
    my $mum = shift;
colas@0
    78
colas@0
    79
    while ($mum) {
colas@0
    80
        $depth++;
colas@0
    81
        $mum = $mum->{parent};
colas@0
    82
    }
colas@0
    83
    return $depth;
colas@0
    84
}
colas@0
    85
colas@0
    86
# Get the section object associated with the given headline and depth
colas@0
    87
sub getSectionObject {
colas@0
    88
    my ($this, $head, $depth) = @_;
colas@0
    89
    if ($this->{headline} eq $head && $this->getDepth() == $depth) {
colas@0
    90
        return $this;
colas@0
    91
    }
colas@0
    92
    foreach my $child (@{$this->{children}}) {
colas@0
    93
        my $cvo = $child->getSectionObject($head, $depth);
colas@0
    94
        return $cvo if $cvo;
colas@0
    95
    }
colas@0
    96
    return undef;
colas@0
    97
}
colas@0
    98
colas@0
    99
# Get the value object associated with the given keys
colas@0
   100
sub getValueObject {
colas@0
   101
    my ($this, $keys) = @_;
colas@0
   102
    foreach my $child (@{$this->{children}}) {
colas@0
   103
        my $cvo = $child->getValueObject($keys);
colas@0
   104
        return $cvo if $cvo;
colas@0
   105
    }
colas@0
   106
    return undef;
colas@0
   107
}
colas@0
   108
colas@0
   109
# See if this section is changed from the default values. Should
colas@0
   110
# return a count of changed values.
colas@0
   111
sub needsSaving {
colas@0
   112
    my ($this, $valuer) = @_;
colas@0
   113
    my $count = 0;
colas@0
   114
    foreach my $child (@{$this->{children}}) {
colas@0
   115
       $count += $child->needsSaving($valuer);
colas@0
   116
    }
colas@0
   117
    return $count;
colas@0
   118
}
colas@0
   119
colas@0
   120
1;