| 1 | # the contents of this file are Copyright (c) 2004-2009 David Blood | ||
| 2 | # This program is free software; you can redistribute it and/or | ||
| 3 | # modify it under the terms of the GNU General Public License as | ||
| 4 | # published by the Free Software Foundation. | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | package DBR::Admin::Window::RelationshipList; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use Class::Std; | ||
| 12 | use Data::Dumper; | ||
| 13 | use DBR::Config::Relation; | ||
| 14 | |||
| 15 | use lib '/drj/tools/perl-dbr/lib'; | ||
| 16 | use DBR::Admin::Window; | ||
| 17 | use DBR::Admin::Utility; | ||
| 18 | |||
| 19 | use vars qw($VERSION $PKG); | ||
| 20 | |||
| 21 | $VERSION = 1.0; | ||
| 22 | |||
| 23 | $PKG = __PACKAGE__; | ||
| 24 | |||
| 25 | use base qw(DBR::Admin::Window); | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | { | ||
| 30 | my %relationship_of : ATTR( :get<relationship> :set<relationship>); | ||
| 31 | my %relationship_listbox_of : ATTR( :get<relationship_listbox> :set<relationship_listbox>); | ||
| 32 | my %field_id_of : ATTR( :get<field_id> :set<field_id>); | ||
| 33 | my %table_id_of : ATTR( :get<table_id> :set<table_id>); | ||
| 34 | my %schema_id_of : ATTR( :get<schema_id> :set<schema_id>); | ||
| 35 | |||
| 36 | #################### | ||
| 37 | sub BUILD { | ||
| 38 | |||
| 39 | my ($self, $ident, $_args) = @_; | ||
| 40 | |||
| 41 | $self->set_field_id($_args->{field_id}); | ||
| 42 | $self->set_schema_id($_args->{schema_id}); | ||
| 43 | $self->set_table_id($_args->{table_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 {$self->add_edit_relationship(add => 1)} | ||
| 53 | |||
| 54 | }, | ||
| 55 | ], | ||
| 56 | -x => 40, | ||
| 57 | -y => 1, | ||
| 58 | |||
| 59 | ); | ||
| 60 | |||
| 61 | $new_relationship_button->draw(); | ||
| 62 | |||
| 63 | my $listbox = $self->get_win->add( | ||
| 64 | 'relationshiplistbox', 'Listbox', | ||
| 65 | -y => 2, | ||
| 66 | -vscrollbar => 1, | ||
| 67 | -onchange => sub {$self->add_edit_relationship(listbox => shift);} | ||
| 68 | ); | ||
| 69 | |||
| 70 | $listbox->onFocus(sub {$listbox->clear_selection}); | ||
| 71 | $self->set_relationship_listbox($listbox); | ||
| 72 | $self->load_relationship_list(); | ||
| 73 | $self->get_relationship_listbox->layout(); | ||
| 74 | $self->get_relationship_listbox->focus(); | ||
| 75 | $self->get_win->set_focusorder('relationshiplistbox', 'newrelationship', 'close'); | ||
| 76 | } | ||
| 77 | |||
| 78 | ####################### | ||
| 79 | # get the list from the database | ||
| 80 | sub get_relationship_list { | ||
| 81 | |||
| 82 | my ($self, %_args) = @_; | ||
| 83 | |||
| 84 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 85 | |||
| 86 | my $data = $dbrh->select( | ||
| 87 | -table => 'dbr_relationships', | ||
| 88 | -fields => 'relationship_id from_name from_table_id from_field_id to_name to_table_id to_field_id type', | ||
| 89 | # -where => [[{to_field_id => $self->get_field_id},{from_field_id => $self->get_field_id}]] | ||
| 90 | -where => {from_field_id => $self->get_field_id}, | ||
| 91 | |||
| 92 | ) or throw DBR::Admin::Exception( | ||
| 93 | message => "failed to select from dbr_relationships", | ||
| 94 | root_window => $self->get_win->root() | ||
| 95 | ); | ||
| 96 | |||
| 97 | #print STDERR Dumper $data; | ||
| 98 | |||
| 99 | my %menu_list; | ||
| 100 | my %relationship; | ||
| 101 | |||
| 102 | foreach my $e (@$data) { | ||
| 103 | $menu_list{$e->{relationship_id}} = $e->{from_name} . ' -> ' . $e->{to_name}; | ||
| 104 | $relationship{$e->{relationship_id}} = $e; | ||
| 105 | } | ||
| 106 | |||
| 107 | $self->set_relationship(\%relationship); | ||
| 108 | return \%menu_list; | ||
| 109 | } | ||
| 110 | |||
| 111 | ################# | ||
| 112 | # load the relationship list into the | ||
| 113 | # relationship listbox | ||
| 114 | sub load_relationship_list{ | ||
| 115 | |||
| 116 | my ($self, %_args) = @_; | ||
| 117 | |||
| 118 | my $relationship_listbox = $self->get_relationship_listbox(); | ||
| 119 | my $menu_list = $self->get_relationship_list(); | ||
| 120 | my @menu_values = keys %{$menu_list}; | ||
| 121 | $relationship_listbox->values(\@menu_values); | ||
| 122 | $relationship_listbox->labels($menu_list); | ||
| 123 | $self->set_relationship_listbox($relationship_listbox); | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | ##################### | ||
| 131 | sub add_edit_relationship { | ||
| 132 | |||
| 133 | my ($self, %_args) = @_; | ||
| 134 | |||
| 135 | my $edit_window = $self->get_win->add( | ||
| 136 | 'relationshipeditwindow', 'Window', | ||
| 137 | -border => 1, | ||
| 138 | -y => 1, | ||
| 139 | -bfg => 'blue', | ||
| 140 | -title => 'Edit Relationship', | ||
| 141 | -titlereverse => 0, | ||
| 142 | ); | ||
| 143 | |||
| 144 | $edit_window->focus(); | ||
| 145 | |||
| 146 | my $x = 5; | ||
| 147 | my $y = 1; | ||
| 148 | my $relationship_hash = {}; | ||
| 149 | if ($_args{add}) { | ||
| 150 | $relationship_hash = { | ||
| 151 | from_table_id => $self->get_table_id(), | ||
| 152 | from_field_id => $self->get_field_id(), | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | else { | ||
| 156 | $relationship_hash = $self->get_relationship->{$_args{listbox}->get()}; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | ################## | ||
| 162 | # from | ||
| 163 | |||
| 164 | my $label = $edit_window->add( | ||
| 165 | "from_name_label", 'Label', | ||
| 166 | -text => "from_name: ", | ||
| 167 | -x => $x, | ||
| 168 | -y => $y | ||
| 169 | ); | ||
| 170 | |||
| 171 | $label->draw; | ||
| 172 | |||
| 173 | my $from_text_box = $edit_window->add( | ||
| 174 | "from_name_text_box", 'TextEditor', | ||
| 175 | -sbborder => 1, | ||
| 176 | -y => $y, | ||
| 177 | -x => ($x + 16) , | ||
| 178 | -readonly => 0, | ||
| 179 | -singleline => 1, | ||
| 180 | -text => $relationship_hash->{from_name} | ||
| 181 | ); | ||
| 182 | $y += 1; | ||
| 183 | |||
| 184 | ###### | ||
| 185 | $label = $edit_window->add( | ||
| 186 | "from_table_id_label", 'Label', | ||
| 187 | -text => "from_table: ", | ||
| 188 | -x => $x, | ||
| 189 | -y => $y | ||
| 190 | ); | ||
| 191 | |||
| 192 | $label->draw; | ||
| 193 | |||
| 194 | my ($values, $labels, $selected) = _get_table_popup_menu_values($relationship_hash, $self->get_schema_id(), 'from_table_id', $self->get_win->root()); | ||
| 195 | my $from_field_id_popup; # need to declare this now and use it later | ||
| 196 | my $from_field_popup_x = $x+16; | ||
| 197 | my $from_field_popup_y = $y+1; | ||
| 198 | my $from_table_id_popup = $edit_window->add( | ||
| 199 | "from_table_id_popup", 'Popupmenu', | ||
| 200 | -y => $y, | ||
| 201 | -x => ($x + 16) , | ||
| 202 | -values => $values, | ||
| 203 | -labels => $labels, | ||
| 204 | -selected => $selected, | ||
| 205 | -onchange => sub {$from_field_id_popup = $self->reload_field_popup(popup => $from_field_id_popup, | ||
| 206 | relationship_hash => $relationship_hash, | ||
| 207 | key => 'from_field_id', | ||
| 208 | table_popup => shift, | ||
| 209 | y => $from_field_popup_y, | ||
| 210 | x => $from_field_popup_x, | ||
| 211 | name => 'from_field_id_popup', | ||
| 212 | edit_window => $edit_window, | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | ); | ||
| 216 | |||
| 217 | |||
| 218 | $from_table_id_popup->draw(); | ||
| 219 | |||
| 220 | $y += 1; | ||
| 221 | |||
| 222 | ###### | ||
| 223 | $label = $edit_window->add( | ||
| 224 | "from_field_label", 'Label', | ||
| 225 | -text => "from_field: ", | ||
| 226 | -x => $x, | ||
| 227 | -y => $y | ||
| 228 | ); | ||
| 229 | |||
| 230 | $label->draw; | ||
| 231 | |||
| 232 | |||
| 233 | my ($values, $labels, $selected) = _get_field_popup_menu_values($relationship_hash, | ||
| 234 | $from_table_id_popup->get(), | ||
| 235 | 'from_field_id', | ||
| 236 | $self->get_win->root() | ||
| 237 | ); | ||
| 238 | |||
| 239 | $from_field_id_popup = $edit_window->add( | ||
| 240 | "from_field_id_popup", 'Popupmenu', | ||
| 241 | -y => $y, | ||
| 242 | -x => ($x + 16) , | ||
| 243 | -values => $values, | ||
| 244 | -labels => $labels, | ||
| 245 | -selected => $selected, | ||
| 246 | ); | ||
| 247 | |||
| 248 | |||
| 249 | $from_field_id_popup->draw(); | ||
| 250 | |||
| 251 | |||
| 252 | $y += 1; | ||
| 253 | |||
| 254 | |||
| 255 | ################# | ||
| 256 | # to | ||
| 257 | my $label = $edit_window->add( | ||
| 258 | "to_name_label", 'Label', | ||
| 259 | -text => "to_name: ", | ||
| 260 | -x => $x, | ||
| 261 | -y => $y | ||
| 262 | ); | ||
| 263 | |||
| 264 | $label->draw; | ||
| 265 | |||
| 266 | my $to_text_box = $edit_window->add( | ||
| 267 | "to_name_text_box", 'TextEditor', | ||
| 268 | -sbborder => 1, | ||
| 269 | -y => $y, | ||
| 270 | -x => ($x + 16) , | ||
| 271 | -readonly => 0, | ||
| 272 | -singleline => 1, | ||
| 273 | -text => $relationship_hash->{to_name} | ||
| 274 | ); | ||
| 275 | $y += 1; | ||
| 276 | |||
| 277 | ###### | ||
| 278 | $label = $edit_window->add( | ||
| 279 | "to_table_id_label", 'Label', | ||
| 280 | -text => "to_table: ", | ||
| 281 | -x => $x, | ||
| 282 | -y => $y | ||
| 283 | ); | ||
| 284 | |||
| 285 | $label->draw; | ||
| 286 | |||
| 287 | |||
| 288 | my ($values, $labels, $selected) = _get_table_popup_menu_values($relationship_hash, $self->get_schema_id(), 'to_table_id', $self->get_win->root()); | ||
| 289 | my $to_field_id_popup; # need to declare this now and use it later | ||
| 290 | my $to_field_popup_x = $x+16; | ||
| 291 | my $to_field_popup_y = $y+1; | ||
| 292 | my $to_table_id_popup = $edit_window->add( | ||
| 293 | "to_table_id_popup", 'Popupmenu', | ||
| 294 | -y => $y, | ||
| 295 | -x => ($x + 16) , | ||
| 296 | -values => $values, | ||
| 297 | -labels => $labels, | ||
| 298 | -selected => $selected, | ||
| 299 | -onchange => sub { $to_field_id_popup = $self->reload_field_popup(popup => $to_field_id_popup, | ||
| 300 | relationship_hash => $relationship_hash, | ||
| 301 | key => 'to_field_id', | ||
| 302 | table_popup => shift, | ||
| 303 | y => $to_field_popup_y, | ||
| 304 | x => $to_field_popup_x, | ||
| 305 | name => 'to_field_id_popup', | ||
| 306 | edit_window => $edit_window, | ||
| 307 | ); | ||
| 308 | } | ||
| 309 | ); | ||
| 310 | |||
| 311 | |||
| 312 | $to_table_id_popup->draw(); | ||
| 313 | |||
| 314 | $y += 1; | ||
| 315 | |||
| 316 | ###### | ||
| 317 | $label = $edit_window->add( | ||
| 318 | "to_field_label", 'Label', | ||
| 319 | -text => "to_field: ", | ||
| 320 | -x => $x, | ||
| 321 | -y => $y | ||
| 322 | ); | ||
| 323 | |||
| 324 | $label->draw; | ||
| 325 | |||
| 326 | |||
| 327 | my ($values, $labels, $selected) = _get_field_popup_menu_values($relationship_hash, $to_table_id_popup->get(), 'to_field_id', $self->get_win->root()); | ||
| 328 | |||
| 329 | $to_field_id_popup = $edit_window->add( | ||
| 330 | "to_field_id_popup", 'Popupmenu', | ||
| 331 | -y => $y, | ||
| 332 | -x => ($x + 16) , | ||
| 333 | -values => $values, | ||
| 334 | -labels => $labels, | ||
| 335 | -selected => $selected, | ||
| 336 | ); | ||
| 337 | |||
| 338 | |||
| 339 | $to_field_id_popup->draw(); | ||
| 340 | |||
| 341 | $y += 1; | ||
| 342 | |||
| 343 | ############# | ||
| 344 | # type | ||
| 345 | $label = $edit_window->add( | ||
| 346 | "type_label", 'Label', | ||
| 347 | -text => "type: ", | ||
| 348 | -x => $x, | ||
| 349 | -y => $y | ||
| 350 | ); | ||
| 351 | |||
| 352 | $label->draw; | ||
| 353 | |||
| 354 | |||
| 355 | my ($values, $labels, $selected) = _get_popup_menu_values(DBR::Config::Relation::list_types(), $relationship_hash, 'type'); | ||
| 356 | |||
| 357 | my $type_popup = $edit_window->add( | ||
| 358 | "type_popup", 'Popupmenu', | ||
| 359 | -y => $y, | ||
| 360 | -x => ($x + 16) , | ||
| 361 | -values => $values, | ||
| 362 | -labels => $labels, | ||
| 363 | -selected => $selected, | ||
| 364 | ); | ||
| 365 | |||
| 366 | |||
| 367 | $to_field_id_popup->draw(); | ||
| 368 | |||
| 369 | $y += 1; | ||
| 370 | |||
| 371 | ##### | ||
| 372 | # buttons | ||
| 373 | my $submit_button = $edit_window->add( | ||
| 374 | 'submit', 'Buttonbox', | ||
| 375 | -buttons => [ | ||
| 376 | { | ||
| 377 | -label => '< Submit >', | ||
| 378 | -value => 1, | ||
| 379 | -shortcut => 1 , | ||
| 380 | -onpress => sub { | ||
| 381 | $self->submit_edit( | ||
| 382 | add => $_args{add}, | ||
| 383 | relationship_id => $_args{listbox} ? $_args{listbox}->get() : undef, | ||
| 384 | edit_window => $edit_window, | ||
| 385 | from_name => $from_text_box->get(), | ||
| 386 | from_table_id => $from_table_id_popup->get(), | ||
| 387 | from_field_id => $from_field_id_popup->get(), | ||
| 388 | to_name => $to_text_box->get(), | ||
| 389 | to_table_id => $to_table_id_popup->get(), | ||
| 390 | to_field_id => $to_field_id_popup->get(), | ||
| 391 | type => $type_popup->get(), | ||
| 392 | |||
| 393 | ); | ||
| 394 | } | ||
| 395 | }, | ||
| 396 | { | ||
| 397 | -label => '< Cancel >', | ||
| 398 | -value => 2, | ||
| 399 | -shortcut => 2 , | ||
| 400 | -onpress => sub {$self->close_edit_window(edit_window => $edit_window)} | ||
| 401 | } | ||
| 402 | |||
| 403 | ], | ||
| 404 | -x => 6, | ||
| 405 | -y => $y, | ||
| 406 | |||
| 407 | ); | ||
| 408 | $submit_button->draw(); | ||
| 409 | |||
| 410 | $from_text_box->focus(); | ||
| 411 | |||
| 412 | } | ||
| 413 | |||
| 414 | ####################### | ||
| 415 | # called when the submit button on the | ||
| 416 | # add/edit window is selected | ||
| 417 | sub submit_edit { | ||
| 418 | |||
| 419 | my ($self, %_args) = @_; | ||
| 420 | |||
| 421 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 422 | my $ret; | ||
| 423 | |||
| 424 | #print STDERR "submit\n"; | ||
| 425 | #print STDERR Dumper \%_args; | ||
| 426 | |||
| 427 | |||
| 428 | if ($_args{add}) { | ||
| 429 | $ret = $dbrh->insert( | ||
| 430 | -table => 'dbr_relationships', | ||
| 431 | -fields => { | ||
| 432 | from_name => $_args{from_name}, | ||
| 433 | from_table_id => $_args{from_table_id}, | ||
| 434 | from_field_id => $_args{from_field_id}, | ||
| 435 | to_name => $_args{to_name}, | ||
| 436 | to_table_id => $_args{to_table_id}, | ||
| 437 | to_field_id => $_args{to_field_id}, | ||
| 438 | type => $_args{type} | ||
| 439 | }, | ||
| 440 | ) or throw DBR::Admin::Exception( | ||
| 441 | message => "failed to insert into dbr_relationships", | ||
| 442 | root_window => $self->get_win->root() | ||
| 443 | ); | ||
| 444 | |||
| 445 | } else { | ||
| 446 | |||
| 447 | |||
| 448 | $ret = $dbrh->update( | ||
| 449 | -table => 'dbr_relationships', | ||
| 450 | -fields => { | ||
| 451 | from_name => $_args{from_name}, | ||
| 452 | from_table_id => $_args{from_table_id}, | ||
| 453 | from_field_id => $_args{from_field_id}, | ||
| 454 | to_name => $_args{to_name}, | ||
| 455 | to_table_id => $_args{to_table_id}, | ||
| 456 | to_field_id => $_args{to_field_id}, | ||
| 457 | type => $_args{type} | ||
| 458 | }, | ||
| 459 | -where => { relationship_id => $_args{relationship_id} } | ||
| 460 | ) or throw DBR::Admin::Exception( | ||
| 461 | message => "failed to update dbr_relationships", | ||
| 462 | root_window => $self->get_win->root() | ||
| 463 | ); | ||
| 464 | } | ||
| 465 | |||
| 466 | #success dialog | ||
| 467 | my $confirm = $self->get_win->root->dialog( | ||
| 468 | -message => $_args{add} ? "This relationship has been successfully added." : | ||
| 469 | "This relationship has been successfully updated.", | ||
| 470 | -title => "Success", | ||
| 471 | -buttons => [ | ||
| 472 | { | ||
| 473 | -label => '< OK >', | ||
| 474 | -value => 1, | ||
| 475 | -shortcut => 1 | ||
| 476 | } | ||
| 477 | ] | ||
| 478 | ); | ||
| 479 | |||
| 480 | # reset the relationship list | ||
| 481 | $self->load_relationship_list(); | ||
| 482 | |||
| 483 | # close window | ||
| 484 | $self->close_edit_window(%_args); | ||
| 485 | |||
| 486 | } | ||
| 487 | |||
| 488 | ####################### | ||
| 489 | sub close_edit_window { | ||
| 490 | |||
| 491 | my ($self, %_args) = @_; | ||
| 492 | |||
| 493 | $_args{edit_window}->parent->delete('relationshipeditwindow'); | ||
| 494 | $_args{edit_window}->parent->draw(); | ||
| 495 | $_args{edit_window}->parent->focus(); | ||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | ######################### | ||
| 500 | sub reload_field_popup{ | ||
| 501 | my ($self, %_args) = @_; | ||
| 502 | |||
| 503 | my ($values, $labels, $selected) = _get_field_popup_menu_values($_args{relationship_hash}, $_args{table_popup}->get(), $_args{key}, $self->get_win->root()); | ||
| 504 | |||
| 505 | |||
| 506 | # have to do it this way beacause Curses::UI::Popupmenu doesn't have values, labels and selected | ||
| 507 | # methods like listbox does. :( | ||
| 508 | |||
| 509 | $_args{edit_window}->delete($_args{name}); | ||
| 510 | |||
| 511 | $_args{popup} = $_args{edit_window}->add( | ||
| 512 | $_args{name}, 'Popupmenu', | ||
| 513 | -y => $_args{y}, | ||
| 514 | -x => $_args{x} , | ||
| 515 | -values => $values, | ||
| 516 | -labels => $labels, | ||
| 517 | -selected => $selected, | ||
| 518 | ); | ||
| 519 | |||
| 520 | $_args{popup}->draw(); | ||
| 521 | |||
| 522 | |||
| 523 | $_args{edit_window}->set_focusorder('from_name_text_box', | ||
| 524 | 'from_table_id_popup', | ||
| 525 | 'from_field_id_popup', | ||
| 526 | 'to_name_text_box', | ||
| 527 | 'to_table_id_popup', | ||
| 528 | 'to_field_id_popup', | ||
| 529 | 'type_popup', | ||
| 530 | 'submit'); | ||
| 531 | return $_args{popup}; | ||
| 532 | } | ||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | ############# | ||
| 537 | sub _get_table_popup_menu_values { | ||
| 538 | my ($relationship_hash, $schema_id, $key, $root) = @_; | ||
| 539 | |||
| 540 | |||
| 541 | |||
| 542 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 543 | |||
| 544 | my $data = $dbrh->select( | ||
| 545 | -table => 'dbr_tables', | ||
| 546 | -fields => 'table_id name display_name ', | ||
| 547 | -where => {schema_id => $schema_id}, | ||
| 548 | ) or throw DBR::Admin::Exception( | ||
| 549 | message => "failed to select from dbr_tables", | ||
| 550 | root_window => $root, | ||
| 551 | ); | ||
| 552 | |||
| 553 | |||
| 554 | my %values; | ||
| 555 | foreach my $t (@$data) { | ||
| 556 | $values{$t->{table_id}} = $t->{display_name} || $t->{name}; | ||
| 557 | } | ||
| 558 | |||
| 559 | my @vals = keys %values; | ||
| 560 | |||
| 561 | # find the index of the selected value in @vals | ||
| 562 | my $index = 0; | ||
| 563 | my $found = 0; | ||
| 564 | foreach my $v (@vals) { | ||
| 565 | if ($v == $relationship_hash->{$key}) { | ||
| 566 | $found = 1; | ||
| 567 | last; | ||
| 568 | } | ||
| 569 | $index++; | ||
| 570 | } | ||
| 571 | |||
| 572 | if (!$found) { | ||
| 573 | $index = undef; | ||
| 574 | } | ||
| 575 | |||
| 576 | return (\@vals, \%values, $index); | ||
| 577 | } | ||
| 578 | |||
| 579 | |||
| 580 | |||
| 581 | |||
| 582 | ############# | ||
| 583 | sub _get_field_popup_menu_values { | ||
| 584 | my ($relationship_hash, $table_id, $key, $root) = @_; | ||
| 585 | |||
| 586 | |||
| 587 | |||
| 588 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 589 | |||
| 590 | my $data = $dbrh->select( | ||
| 591 | -table => 'dbr_fields', | ||
| 592 | -fields => 'field_id name display_name ', | ||
| 593 | -where => {table_id => $table_id}, | ||
| 594 | ) or throw DBR::Admin::Exception( | ||
| 595 | message => "failed to select from dbr_fields", | ||
| 596 | root_window => $root, | ||
| 597 | ); | ||
| 598 | |||
| 599 | |||
| 600 | my %values; | ||
| 601 | foreach my $t (@$data) { | ||
| 602 | $values{$t->{field_id}} = $t->{display_name} || $t->{name}; | ||
| 603 | } | ||
| 604 | |||
| 605 | my @vals = keys %values; | ||
| 606 | |||
| 607 | |||
| 608 | # find the index of the selected value in @vals | ||
| 609 | my $index = 0; | ||
| 610 | my $found = 0; | ||
| 611 | foreach my $v (@vals) { | ||
| 612 | if ($v == $relationship_hash->{$key}) { | ||
| 613 | $found = 1; | ||
| 614 | last; | ||
| 615 | } | ||
| 616 | $index++; | ||
| 617 | } | ||
| 618 | |||
| 619 | if (!$found) { | ||
| 620 | $index = undef; | ||
| 621 | } | ||
| 622 | |||
| 623 | return (\@vals, \%values, $index); | ||
| 624 | } | ||
| 625 | |||
| 626 | ############# | ||
| 627 | sub _get_popup_menu_values { | ||
| 628 | my ($value_ref, $lookup, $key) = @_; | ||
| 629 | |||
| 630 | my %values; | ||
| 631 | foreach my $t (@$value_ref) { | ||
| 632 | $values{$t->{type_id}} = $t->{name} || $t->{handle}; | ||
| 633 | } | ||
| 634 | my @vals = keys %values; | ||
| 635 | # find the index of the selected value in @vals | ||
| 636 | my $index = 0; | ||
| 637 | my $found = 0; | ||
| 638 | foreach my $v (@vals) { | ||
| 639 | if ($v == $lookup->{$key}) { | ||
| 640 | $found = 1; | ||
| 641 | last; | ||
| 642 | } | ||
| 643 | $index++; | ||
| 644 | } | ||
| 645 | |||
| 646 | if (!$found) { | ||
| 647 | $index = undef; | ||
| 648 | } | ||
| 649 | |||
| 650 | return (\@vals, \%values, $index); | ||
| 651 | } | ||
| 652 | |||
| 653 | |||
| 654 | |||
| 655 | } | ||
| 656 | |||
| 657 | 1; |