aisa/scripts/dialog/add_city.pl

200 lines
5.3 KiB
Perl
Raw Permalink Normal View History

2025-11-26 09:31:54 +00:00
#!/usr/bin/perl
BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
require cfg ;
# print "Content-type: text/html\n\n";
print header; # CGI.pm method
use POSIX;
#------------------------------------------------------------------------------------------
@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});
my $qaction = $ARGV[0] ;
my $selbox = $ARGV[1] ;
our ($q) = CGI -> new() ;
$iaction = $q -> param('iaction') || $qaction ;
our $dialog = 1 ;
#------------------------------------------------------------------------------------------
# our $savjqy = 1 ; # save via jquery
# our $addbkb = 1 ; # add back button
# our $testing = 1 ;
#------------------------------------------------------------------------------------------
&today ;
#-------- page opts --------------------------------------------------------------------------------------------------------------------------------------------------
our $table = 'cities' ;
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($iaction eq 'add'){
&load_vars ;
&common_min_add_screen;
&screen1 ;
}
if ($iaction eq 'save'){
&common_min_load_params ;
&validate ;
&insert ;
&screen2 ;
}
exit ;
#------------------------------------------------------------------------------------------
sub load_vars {
$sort_field{1} = 'name' ; $preferred_title{name} = "City Name" ;
# $sort_field{2} = 'province_id' ; $preferred_title{province_id} = "Region" ;
$sort_field{2} = 'region_id' ;
$sort_field{3} = 'lat' ;
$sort_field{4} = 'lng' ;
$select{region_id} = 1;
$allow_deselect{region_id} = 1;
# local $region_id_where = qq~~ ; local @abcd = () ;
# if ($usertype eq 'schools_manager') {
# &db_min_ro('users','1,region_ids',"`id`='$userid'",'','') ;
# foreach (split(",",$db{users}{1}{region_ids})) {
# push @abcd , "`id` = '$_'" ;
# }
# }
# $region_id_where = join (" OR ",@abcd) ;
my $sql_or = '' ;
if ($is_schools_manager || $is_operator) {
@report_sql_or = () ;
foreach $_reg_id (keys %{$glob_regids{$userid}}) {
push @report_sql_or, "id = '$_reg_id'"
}
$sql_or = join(' OR ',@report_sql_or) ;
}
&db_min_ro('regions','*',"$sql_or",'','') ;
foreach (sort {$db{regions}{$a}{name} cmp $db{regions}{$b}{name}} keys %{$db{regions}}) {
$opts{region_id} .= qq~<option value="$_">$db{regions}{$_}{name} [$db{regions}{$_}{code}]</option>~ ;
}
} #------------------------------------------------------------------------------------------
sub validate {
my $exists = '' ;
$i{name} = &common_fix_str($i{name}) ;
$i{city} = $i{name} ;
&db_min_ro($table,'*',"name = '$i{name}'",'name','') ;
foreach my $id (keys %{$db{$table}}) { if ($db{$table}{$id}{name} eq $i{name}) { $exists = 1 ; } }
if ($exists) { $alert = &common_min_alert('warning',"$ucpage ALREADY EXISTS!",'warning-sign') ; %col_name = (); &load_vars ; &common_min_add_screen; &screen1; exit ; }
} #------------------------------------------------------------------------------------------
sub insert {
&add_db_fields ;
# &db_min_ro('regions','id,name',"id='$i{region_id}'",'','') ;
# $i{province} = $db{regions}{$i{region_id}}{name} ;
$ignore{sel_field} = 1 ;
# $ignore{region_id} = 1 ;
&set_default_vals ;
$i{id} = &db_min_get_max($table,'id') ;
&db_min_insert('cities') ;
} #------------------------------------------------------------------------------------------
sub set_default_vals {
&db_min_ro('regions','id,name',"id = $i{region_id}",'','') ;
$i{province} = $db{regions}{$i{region_id}}{name};
} #------------------------------------------------------------------------------------------
sub add_db_fields {
&hidden_fields ;
$hidden{sel_field} = 2 ;
$db{$table}{''}{sel_field} = $selbox if $selbox ;
# $ignore{id} = 1 ;
} #------------------------------------------------------------------------------------------
sub hidden_fields {
$ignore{iaction} = 1 ;
$required{name} = 1 ;
# $required{province_id} = 1 ;
$required{region_id} = 1 ;
# $required{lat} = 1 ;
# $required{lng} = 1 ;
} #------------------------------------------------------------------------------------------
sub add_screen {
# called from common_add_screen
$boxtitle = 'Add' ;
} #------------------------------------------------------------------------------------------
sub screen1 {
$trigger_jquery_raw .= qq~
\$('#btn-close-customer').click(function (e) {
parent.BootstrapDialog.closeAll() ;
});~ ;
&common_min_dialog_load_screen ;
} #------------------------------------------------------------------------------------------
sub screen2 {
$trigger_jquery_raw .= qq~
parent.\$("#$i{sel_field}").append("<option SELECTED value='$new_row_id'>$i{name}</option>");
// parent.\$("#$i{sel_field}").val('$new_row_id').trigger('change'); // this must stay commented out for multi-selects, otherwise it removes existing selected options
parent.\$("#$i{sel_field}").trigger("chosen:updated");
parent.BootstrapDialog.closeAll() ;
~;
&common_min_dialog_save_screen ;
} #------------------------------------------------------------------------------------------
use db ;
use today ;
use common ;
use common_min ;
use dialog ;
1;