annotate Lib/Form/ValueItem/List.pm @ 1:3b418b134d8c

ORM in progress
author Sergey
date Fri, 17 Jul 2009 13:30:46 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package Form::ValueItem::List;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use base qw(Form::ValueItem);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 DeclareProperty ListValues => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 DeclareProperty CurrentItem => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 $this->SUPER::CTOR(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 $this->{$ListValues} = [];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 my $source = $this->Form->Bindings->{$this->Attributes->{source}};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 if (ref $source eq 'CODE') {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 $this->LoadList($source->());
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 } elsif (ref $source and (UNIVERSAL::isa($source,'HASH') or UNIVERSAL::isa($source,'ARRAY'))){
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 $this->LoadList($source);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 if (not $source) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 warn "a source isn't specified for the listvalue ".$this->Id->Canonical;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 warn "an unsupported source type ".(ref $source)." for the listvalue".$this->Id->Canonical;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 sub Value {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 if (@_) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 my $newValue = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 $this->{$CurrentItem}->{active} = 0 if $this->{$CurrentItem};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39 my ($item) = (defined $newValue ? grep {defined $_->{id} and $_->{id} eq $newValue} @{$this->{$ListValues}} : undef);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 if ($item) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42 $this->{$CurrentItem} = $item;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 $item->{active} = 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 return $this->SUPER::Value($newValue);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 undef $this->{$CurrentItem};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 return $this->SUPER::Value(undef);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 return $this->SUPER::Value;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 sub LoadList {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 my ($this,$refList) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 if (ref $refList and UNIVERSAL::isa($refList,'HASH')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 $this->{$CurrentItem} = undef;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 $this->{$ListValues} = [ sort { $a->{name} cmp $b->{name} } map { Form::ValueItem::List::Item->new($_,ref $refList->{$_} eq 'ARRAY' ? @{$refList->{$_}} : $refList->{$_})} keys %{$refList}];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 $this->SUPER::Value(undef);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61 } elsif (ref $refList and UNIVERSAL::isa($refList,'ARRAY')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 $this->{$CurrentItem} = undef;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 $this->{$ListValues} = [map { Form::ValueItem::List::Item->new(ref $_ eq 'ARRAY' ? @$_ : $_ )} @$refList];
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 $this->SUPER::Value(undef);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 die new Exception('An unexpected list type');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 package Form::ValueItem::List::Item;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 use fields qw(
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 id
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 description
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 name
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 active
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78 sub new {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 my ($class,$id,$name,$desc) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 my $this=fields::new($class);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 $this->{id} = $id;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 $this->{name} = $name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 $this->{description} = $desc;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86 return $this;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 #compatibility with TToolkit
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 sub Id {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 $_[0]->{id};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 sub Description {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 $_[0]->{description};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 sub Active {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 $_[0]->{active};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 sub Name {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 $_[0]->{name};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 1;