| 1 | |||
| 2 | # the contents of this file are Copyright (c) 2004-2009 David Blood | ||
| 3 | # This program is free software; you can redistribute it and/or | ||
| 4 | # modify it under the terms of the GNU General Public License as | ||
| 5 | # published by the Free Software Foundation. | ||
| 6 | |||
| 7 | |||
| 8 | package DBR::Admin::Window::FieldRelationshipList; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use Class::Std; | ||
| 12 | use Data::Dumper; | ||
| 13 | use DBR::Config::Trans; | ||
| 14 | use DBR::Config::Field; | ||
| 15 | |||
| 16 | use lib '/drj/tools/perl-dbr/lib'; | ||
| 17 | use DBR::Admin::Window; | ||
| 18 | use DBR::Admin::Utility; | ||
| 19 | use DBR::Admin::Window::RelationshipList; | ||
| 20 | |||
| 21 | use vars qw($VERSION $PKG); | ||
| 22 | |||
| 23 | $VERSION = 1.0; | ||
| 24 | |||
| 25 | $PKG = __PACKAGE__; | ||
| 26 | |||
| 27 | use base qw(DBR::Admin::Window); | ||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 | { | ||
| 32 | my %fields_of : ATTR( :get<fields> :set<fields>); | ||
| 33 | my %field_listbox_of : ATTR( :get<field_listbox> :set<field_listbox>); | ||
| 34 | my %table_id_of : ATTR( :get<table_id> :set<table_id>); | ||
| 35 | my %schema_id_of : ATTR( :get<schema_id> :set<schema_id>); | ||
| 36 | |||
| 37 | #################### | ||
| 38 | sub BUILD { | ||
| 39 | |||
| 40 | my ($self, $ident, $_args) = @_; | ||
| 41 | |||
| 42 | $self->set_table_id($_args->{table_id}); | ||
| 43 | $self->set_schema_id($_args->{schema_id}); | ||
| 44 | |||
| 45 | my $new_relationship_button = $self->get_win->add( | ||
| 46 | 'newrelationship', 'Buttonbox', | ||
| 47 | -buttons => [ | ||
| 48 | { | ||
| 49 | -label => '< Add New Relationship >', | ||
| 50 | -value => 1, | ||
| 51 | -shortcut => 1 , | ||
| 52 | -onpress => sub { | ||
| 53 | DBR::Admin::Window::RelationshipList->new( | ||
| 54 | {id => 'relationships', | ||
| 55 | parent => $self->get_win, | ||
| 56 | parent_title => ucfirst($self->get_id), | ||
| 57 | field_id => shift->get, | ||
| 58 | schema_id => $self->get_schema_id(), | ||
| 59 | table_id => $self->get_table_id(), | ||
| 60 | })->add_edit_relationship(add => 1) | ||
| 61 | } | ||
| 62 | |||
| 63 | }, | ||
| 64 | ], | ||
| 65 | -x => 40, | ||
| 66 | -y => 1, | ||
| 67 | |||
| 68 | ); | ||
| 69 | |||
| 70 | $new_relationship_button->draw(); | ||
| 71 | |||
| 72 | my $listbox = $self->get_win->add( | ||
| 73 | 'fieldlistbox', 'Listbox', | ||
| 74 | -y => 2, | ||
| 75 | -width => 25, | ||
| 76 | -vscrollbar => 1, | ||
| 77 | -onchange => sub { | ||
| 78 | DBR::Admin::Window::RelationshipList->new( | ||
| 79 | {id => 'relationships', | ||
| 80 | parent => $self->get_win, | ||
| 81 | parent_title => ucfirst($self->get_id), | ||
| 82 | field_id => shift->get, | ||
| 83 | schema_id => $self->get_schema_id(), | ||
| 84 | table_id => $self->get_table_id(), | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | ); | ||
| 89 | |||
| 90 | $listbox->onFocus(sub {$listbox->clear_selection}); | ||
| 91 | $self->set_field_listbox($listbox); | ||
| 92 | $self->load_field_list(); | ||
| 93 | $self->get_field_listbox->layout(); | ||
| 94 | $self->get_field_listbox->focus(); | ||
| 95 | $self->get_win->set_focusorder('fieldlistbox','newrelationship', 'close'); | ||
| 96 | } | ||
| 97 | |||
| 98 | ####################### | ||
| 99 | # get the list from the database | ||
| 100 | sub get_field_list { | ||
| 101 | |||
| 102 | my ($self, %_args) = @_; | ||
| 103 | |||
| 104 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | my $data = $dbrh->select( | ||
| 109 | -table => { | ||
| 110 | 'r' => 'dbr_relationships', | ||
| 111 | 'f' => 'dbr_fields', | ||
| 112 | }, | ||
| 113 | -fields => 'f.table_id f.name f.field_id', | ||
| 114 | -where => { | ||
| 115 | 'f.field_id' => ['j', 'r.from_field_id'], | ||
| 116 | 'r.from_table_id' => $self->get_table_id() | ||
| 117 | } | ||
| 118 | |||
| 119 | ) or throw DBR::Admin::Exception( | ||
| 120 | message => "failed to select from dbr_relationship, dbr_fields $!", | ||
| 121 | root_window => $self->get_win->root() | ||
| 122 | ); | ||
| 123 | |||
| 124 | #print STDERR Dumper $data; | ||
| 125 | |||
| 126 | my %menu_list; | ||
| 127 | my %fields; | ||
| 128 | |||
| 129 | foreach my $e (@$data) { | ||
| 130 | $menu_list{$e->{field_id}} = $e->{name}; | ||
| 131 | $fields{$e->{field_id}} = $e; | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->set_fields(\%fields); | ||
| 135 | return \%menu_list; | ||
| 136 | } | ||
| 137 | |||
| 138 | ################# | ||
| 139 | # load the field list into the | ||
| 140 | # field listbox | ||
| 141 | sub load_field_list{ | ||
| 142 | |||
| 143 | my ($self, %_args) = @_; | ||
| 144 | |||
| 145 | my $field_listbox = $self->get_field_listbox(); | ||
| 146 | my $menu_list = $self->get_field_list(); | ||
| 147 | my @menu_values = keys %{$menu_list}; | ||
| 148 | $field_listbox->values(\@menu_values); | ||
| 149 | $field_listbox->labels($menu_list); | ||
| 150 | $self->set_field_listbox($field_listbox); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | 1; |