Skip to content

Commit

Permalink
refactor: global variables removal (#10474)
Browse files Browse the repository at this point in the history
* $admin global var removed

* $styles global var removed

* $script global var removed

* $initjs global var removed

* $header global var removed

* $cc global var removed

* $bodyabout global var removed
  • Loading branch information
4nt0ineB authored Jul 2, 2024
1 parent b99b313 commit d9d9a70
Show file tree
Hide file tree
Showing 37 changed files with 362 additions and 323 deletions.
2 changes: 1 addition & 1 deletion cgi/display_org_table.pl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 401);
}

if ((not $admin) or (not $User{pro_moderator})) {
if ((not $request_ref->{admin}) or (not $User{pro_moderator})) {
$log->debug("user does not have permission to view organisation list", {User_id => $User_id}) if $log->is_debug();
display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403);
}
Expand Down
9 changes: 5 additions & 4 deletions cgi/export_products.pl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
$template_data_ref->{allow_submit} = 1;
}

process_template('web/pages/export_products/export_products.tt.html', $template_data_ref, \$html)
process_template('web/pages/export_products/export_products.tt.html', $template_data_ref, \$html, $request_ref)
|| ($html .= 'template error: ' . $tt->error());
}

Expand Down Expand Up @@ -150,7 +150,7 @@
$args_ref->{query}{states_tags} = 'en:to-be-exported';
}

if ($admin) {
if ($request_ref->{admin}) {
if ((defined single_param("overwrite_owner")) and (single_param("overwrite_owner"))) {
$args_ref->{overwrite_owner} = 1;
}
Expand All @@ -171,7 +171,7 @@
$html .= "<p>" . lang("export_job_import") . " - <span id=\"result2\"></span></p>";
$html .= "<p>" . lang("export_job_status_update") . " - <span id=\"result3\"></span></p>";

$initjs .= <<JS
$request_ref->{initjs} .= <<JS
var minion_status = {
"inactive" : "$Lang{minion_status_inactive}{$lc}",
Expand Down Expand Up @@ -296,7 +296,8 @@
send_email_to_producers_admin("Export to public database requested: user: $User_id - org: $Org_id",
$admin_mail_body);

process_template('web/pages/export_products_results/export_products_results.tt.html', $template_data_ref2, \$html)
process_template('web/pages/export_products_results/export_products_results.tt.html',
$template_data_ref2, \$html, $request_ref)
|| ($html .= 'template error: ' . $tt->error());

}
Expand Down
4 changes: 2 additions & 2 deletions cgi/generate_sample_import_file.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use CGI::Carp qw(fatalsToBrowser);

use ProductOpener::Config qw/:all/;
use ProductOpener::Display qw/init_request $admin/;
use ProductOpener::Display qw/init_request/;
use ProductOpener::Users qw/:all/;
use ProductOpener::Lang qw/lang/;
use ProductOpener::Mail qw/:all/;
Expand All @@ -49,7 +49,7 @@
my $request_ref = ProductOpener::Display::init_request();

# sync CRM
if (defined $Org_id and not $admin and not $User{moderator} and not $User{pro_moderator}) {
if (defined $Org_id and not $request_ref->{admin} and not $User{moderator} and not $User{pro_moderator}) {
update_template_download_date($Org_id);
}

Expand Down
10 changes: 5 additions & 5 deletions cgi/import_file_process.pl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
# Default values: use the language and country of the interface
my $default_values_ref = {
lc => $lc,
countries => $cc,
countries => $request_ref->{cc},
};

$results_ref = convert_file($default_values_ref, $file, $columns_fields_file, $converted_file);
Expand Down Expand Up @@ -208,12 +208,12 @@
$template_data_ref->{process_import_id} = $import_id;
$template_data_ref->{link} = "/cgi/import_file_job_status.pl?file_id=$file_id&import_id=$import_id";

process_template('web/pages/import_file_process/import_file_process.tt.html', $template_data_ref, \$html);
process_template('web/pages/import_file_process/import_file_process.tt.js', $template_data_ref, \$js);
process_template('web/pages/import_file_process/import_file_process.tt.html', $template_data_ref, \$html, $request_ref);
process_template('web/pages/import_file_process/import_file_process.tt.js', $template_data_ref, \$js, $request_ref);

$initjs .= $js;
$request_ref->{initjs} .= $js;

$scripts .= <<HTML
$request_ref->{scripts} .= <<HTML
<script type="text/javascript" src="/js/dist/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="/js/dist/jquery.fileupload.js"></script>
HTML
Expand Down
2 changes: 1 addition & 1 deletion cgi/import_file_select_format.pl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
process_template('web/pages/import_file_select_format/import_file_select_format.tt.html',
$template_data_ref, \$html);
process_template('web/pages/import_file_select_format/import_file_select_format.tt.js', $template_data_ref, \$js);
$initjs .= $js;
$request_ref->{initjs} .= $js;

$request_ref->{title} = $title;
$request_ref->{content_ref} = \$html;
Expand Down
9 changes: 5 additions & 4 deletions cgi/import_file_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@
url => "/cgi/import_file_upload.pl",
};

process_template('web/pages/import_file_upload/import_file_upload.tt.html', $template_data_ref, \$html);
process_template('web/pages/import_file_upload/import_file_upload.tt.js', $template_data_ref, \$js);
process_template('web/pages/import_file_upload/import_file_upload.tt.html',
$template_data_ref, \$html, $request_ref);
process_template('web/pages/import_file_upload/import_file_upload.tt.js', $template_data_ref, \$js, $request_ref);

$initjs .= $js;
$request_ref->{initjs} .= $js;

$scripts .= <<HTML
$request_ref->{scripts} .= <<HTML
<script type="text/javascript" src="/js/dist/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="/js/dist/jquery.fileupload.js"></script>
HTML
Expand Down
16 changes: 9 additions & 7 deletions cgi/import_photos_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
$template_data_ref->{i} = $i;
$template_data_ref->{add_fields_options} = \@add_fields_options;

$scripts .= <<JS
$request_ref->{scripts} .= <<JS
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{\% for (var i=0, file; file=o.files[i]; i++) { \%}
Expand Down Expand Up @@ -176,14 +176,16 @@
JS
;

process_template('web/pages/import_photos_upload/import_photos_upload.tt.html', $template_data_ref, \$html)
process_template('web/pages/import_photos_upload/import_photos_upload.tt.html',
$template_data_ref, \$html, $request_ref)
or $html = "<p>" . $tt->error() . "</p>";
process_template('web/pages/import_photos_upload/import_photos_upload.tt.js', $template_data_ref, \$js)
process_template('web/pages/import_photos_upload/import_photos_upload.tt.js',
$template_data_ref, \$js, $request_ref)
or $html = "<p>" . $tt->error() . "</p>";

$initjs .= $js;
$request_ref->{initjs} .= $js;

$header .= <<HTML
$request_ref->{header} .= <<HTML
<script>
// Keep track of codes that we have seen so that we can submit field values only once
var codes = {};
Expand Down Expand Up @@ -214,7 +216,7 @@
HTML
;

$initjs .= <<JS
$request_ref->{initjs} .= <<JS
/*
* jQuery File Upload Demo
Expand Down Expand Up @@ -340,7 +342,7 @@
JS
;

$styles .= <<CSS
$request_ref->{styles} .= <<CSS
<!-- CSS to style the file input field as button and adjust the Bootstrap progress bars -->
Expand Down
8 changes: 4 additions & 4 deletions cgi/import_products_categories_from_public_database.pl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

if ($action eq "display") {
process_template('web/pages/import_product_categories/import_product_categories_from_public_database.tt.html',
$template_data_ref, \$html)
$template_data_ref, \$html, $request_ref)
or $html = "<p>" . $tt->error() . "</p>";
}

Expand All @@ -89,12 +89,12 @@

process_template(
'web/pages/import_product_categories_process/import_product_categories_from_public_database_process.tt.html',
$template_data_ref, \$html)
$template_data_ref, \$html, $request_ref)
or $html = "<p>" . $tt->error() . "</p>";
process_template(
'web/pages/import_product_categories_process/import_product_categories_from_public_database_process.tt.js',
$template_data_ref, \$js);
$initjs .= $js;
$template_data_ref, \$js, $request_ref);
$request_ref->{initjs} .= $js;

}

Expand Down
8 changes: 4 additions & 4 deletions cgi/manifest.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use ProductOpener::Config qw/:all/;
use ProductOpener::Store qw/:all/;
use ProductOpener::Index qw/:all/;
use ProductOpener::Display qw/$cc $subdomain init_request/;
use ProductOpener::Display qw/$subdomain init_request/;
use ProductOpener::Users qw/:all/;
use ProductOpener::URL qw/format_subdomain/;
use ProductOpener::Lang qw/$lc lang/;
Expand All @@ -47,13 +47,13 @@
# https://stackoverflow.com/a/16533563/11963
$short_name =~ s/\b([A-Z])[a-z]+(?=\s+[A-Z][a-z])|\G(?!^)\s+([A-Z])[a-z]+/$1$2/g;

if ($cc eq 'world') {
if ($request_ref->{cc} eq 'world') {
$long_name .= " " . uc($lc);
$short_name .= " " . uc($lc);
}
else {
$long_name .= " " . uc($cc) . "/" . uc($lc);
$short_name .= " " . uc($cc) . "/" . uc($lc);
$long_name .= " " . uc($request_ref->{cc}) . "/" . uc($lc);
$short_name .= " " . uc($request_ref->{cc}) . "/" . uc($lc);
}

my %manifest = (
Expand Down
6 changes: 3 additions & 3 deletions cgi/opensearch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use ProductOpener::Config qw/:all/;
use ProductOpener::Store qw/:all/;
use ProductOpener::Index qw/:all/;
use ProductOpener::Display qw/$cc $subdomain init_request/;
use ProductOpener::Display qw/$subdomain init_request/;
use ProductOpener::HTTP qw/write_cors_headers/;
use ProductOpener::Users qw/:all/;
use ProductOpener::Products qw/:all/;
Expand All @@ -51,11 +51,11 @@
my $short_name = $options{site_name};
# Maximum of 48 characters
my $long_name = $short_name;
if ($cc eq 'world') {
if ($request_ref->{cc} eq 'world') {
$long_name .= " " . uc($lc);
}
else {
$long_name .= " " . uc($cc) . "/" . uc($lc);
$long_name .= " " . uc($request_ref->{cc}) . "/" . uc($lc);
}

my $description = lang("search_description_opensearch");
Expand Down
22 changes: 11 additions & 11 deletions cgi/org.pl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
if (not defined $org_ref) {
$log->debug("org does not exist", {orgid => $orgid}) if $log->is_debug();

if ($admin or $User{pro_moderator}) {
if ($request_ref->{admin} or $request_ref->{pro_moderator}) {
$template_data_ref->{org_does_not_exist} = 1;
}
else {
Expand All @@ -79,7 +79,7 @@

# Does the user have permission to edit the org profile?

if (not(is_user_in_org_group($org_ref, $User_id, "admins") or $admin or $User{pro_moderator})) {
if (not(is_user_in_org_group($org_ref, $User_id, "admins") or $request_ref->{admin} or $request_ref->{pro_moderator})) {
$log->debug("user does not have permission to edit org",
{orgid => $orgid, org_admins => $org_ref->{admins}, User_id => $User_id})
if $log->is_debug();
Expand All @@ -92,7 +92,7 @@

if ($type eq 'edit') {
if (single_param('delete') eq 'on') {
if ($admin) {
if ($request_ref->{admin}) {
$type = 'delete';
}
else {
Expand All @@ -103,7 +103,7 @@

# Administrator fields

if ($admin or $User{pro_moderator}) {
if ($request_ref->{admin} or $User{pro_moderator}) {

# If the org does not exist yet, create it
if (not defined $org_ref) {
Expand Down Expand Up @@ -193,15 +193,15 @@

if ($action eq 'display') {

$template_data_ref->{admin} = $admin;
$template_data_ref->{admin} = $request_ref->{admin};

# Create the list of sections and fields

$template_data_ref->{sections} = [];

# Admin

if ($admin or $User{pro_moderator}) {
if ($request_ref->{admin} or $User{pro_moderator}) {

my $admin_fields_ref = [];

Expand Down Expand Up @@ -378,7 +378,7 @@
}
elsif ($type eq 'user_delete') {

if (is_user_in_org_group($org_ref, $User_id, "admins") or $admin or $User{pro_moderator}) {
if (is_user_in_org_group($org_ref, $User_id, "admins") or $request_ref->{admin} or $User{pro_moderator}) {
remove_user_by_org_admin(single_param('orgid'), single_param('user_id'));
$template_data_ref->{result} = lang("edit_org_result");

Expand All @@ -389,7 +389,7 @@

}
elsif ($type eq 'add_users') {
if (is_user_in_org_group($org_ref, $User_id, "admins") or $admin or $User{pro_moderator}) {
if (is_user_in_org_group($org_ref, $User_id, "admins") or $request_ref->{admin} or $User{pro_moderator}) {
my $email_list = remove_tags_and_quote(single_param('email_list'));
my $email_ref = add_users_to_org_by_admin($orgid, $email_list);

Expand All @@ -401,7 +401,7 @@
}
}
elsif ($type eq 'change_main_contact') {
if (is_user_in_org_group($org_ref, $User_id, "admins") or $admin or $User{pro_moderator}) {
if (is_user_in_org_group($org_ref, $User_id, "admins") or $request_ref->{admin} or $User{pro_moderator}) {
my $main_contact = remove_tags_and_quote(single_param('main_contact'));
# check that the main contact is a member of the organization
if (not is_user_in_org_group($org_ref, $main_contact, 'members')) {
Expand All @@ -417,7 +417,7 @@

elsif ($type eq 'admin_status') {
# verify right to change status
if (is_user_in_org_group($org_ref, $User_id, "admins") or $admin or $User{pro_moderator}) {
if (is_user_in_org_group($org_ref, $User_id, "admins") or $request_ref->{admin} or $User{pro_moderator}) {
# inputs are in the form admin_status_<user_id>, get them among param and extract the user_id
my @user_ids = sort map {$_ =~ /^admin_status_/ ? $' : ()} param();
my @existing_admins = sort grep {is_user_in_org_group($org_ref, $_, "admins")} keys %{$org_ref->{members}};
Expand Down Expand Up @@ -470,7 +470,7 @@
$template_data_ref->{main_contact} = $org_ref->{main_contact};
$template_data_ref->{crm_company_url} = get_company_url($org_ref);

process_template('web/pages/org_form/org_form.tt.html', $template_data_ref, \$html)
process_template('web/pages/org_form/org_form.tt.html', $template_data_ref, \$html, $request_ref)
or $html = "<p>template error: " . $tt->error() . "</p>";

$request_ref->{title} = $title;
Expand Down
2 changes: 1 addition & 1 deletion cgi/product_image_move.pl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

my $request_ref = ProductOpener::Display::init_request();

$log->debug("parsing code", {user => $User_id, code => $code, cc => $cc, lc => $lc, ip => remote_addr()})
$log->debug("parsing code", {user => $User_id, code => $code, cc => $request_ref->{cc}, lc => $lc, ip => remote_addr()})
if $log->is_debug();

if ((not defined $code) or ($code eq '')) {
Expand Down
2 changes: 1 addition & 1 deletion cgi/product_image_upload.pl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
code => $code,
previous_code => $previous_code,
previous_imgid => $previous_imgid,
cc => $cc,
cc => $request_ref->{cc},
lc => $lc,
imagefield => $imagefield,
ip => remote_addr()
Expand Down
4 changes: 2 additions & 2 deletions cgi/product_jqm_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ =head1 DESCRIPTION
= qw(product_name generic_name quantity packaging brands categories labels origins manufacturing_places emb_codes link expiration_date purchase_places stores countries );

# admin field to set a creator
if ($admin) {
if ($request_ref->{admin}) {
push @app_fields, "creator";
}

if ($admin or ($User_id eq "ecoscore-impact-estimator")) {
if ($request_ref->{admin} or ($User_id eq "ecoscore-impact-estimator")) {
push @app_fields, ("ecoscore_extended_data", "ecoscore_extended_data_version");
}

Expand Down
Loading

0 comments on commit d9d9a70

Please sign in to comment.