Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Form Templatization #3897

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 46 additions & 94 deletions cgi/user.pl
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,28 @@
use Storable qw/dclone/;
use Log::Any qw($log);

use Template;
use Data::Dumper;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need it in here?


my $type = param('type') || 'add';
my $action = param('action') || 'display';


# Initialize the Template module
my $tt = Template->new({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the initialisation of $tt and $template_data_ref is now done several times in Display.pm and some .pl files in the same way. Would it make sense to introduce a new ProductOpener::Template module with methods that know how to do just that? It could reduce duplicated code and avoid errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hangy : maybe we can just make the $tt in Display.pm an exported variable. Then it's initialize once at module startup, and that's it.

INCLUDE_PATH => $data_root . '/templates',
INTERPOLATE => 1,
EVAL_PERL => 1,
STAT_TTL => 60, # cache templates in memory for 1 min before checking if the source changed
COMPILE_EXT => '.ttc', # compile templates to Perl code for much faster reload
COMPILE_DIR => $data_root . '/tmp/templates',
});

# Passing values to the template
my $template_data_ref = {
lang => \&lang,
};

# If the "Create user" form was submitted from the product edit page
# save the password parameter and unset it so that the ProductOpener::Display::init()
# function does not try to authenticate the user (which does not exist yet) with that password
Expand Down Expand Up @@ -108,21 +127,10 @@
}
}

if (($action eq "display") or ($action eq "none")) {
$template_data_ref->{action} = $action;

if ($#errors >= 0) {
$html .= "
<div class='alert-box alert'>
<p>
<b>$Lang{correct_the_following_errors}{$lang}</b>
</p>
";
foreach my $error (@errors) {
$html .= "$error<br />";
}
$html .= '</div>';
}
}
$template_data_ref->{error_count} = $#errors;
$template_data_ref->{errors} = \@errors;

if ($action eq 'display') {

Expand Down Expand Up @@ -150,24 +158,11 @@
}
}

$html .= start_form()
. "<table>";
$template_data_ref->{display_user_form} = ProductOpener::Users::display_user_form($user_ref,\$scripts);
$template_data_ref->{display_user_form_optional} = ProductOpener::Users::display_user_form_optional($user_ref);
$template_data_ref->{display_user_form_admin_only} = ProductOpener::Users::display_user_form_admin_only($user_ref);

$html .= ProductOpener::Users::display_user_form($user_ref,\$scripts);
$html .= ProductOpener::Users::display_user_form_optional($user_ref);
$html .= ProductOpener::Users::display_user_form_admin_only($user_ref);

if ($admin) {
$html .= "\n<tr><td colspan=\"2\">" . checkbox(-name=>'delete', -label=>lang("delete_user")) . "</td></tr>";
}

$html .= "\n<tr><td>"
. hidden(-name=>'action', -value=>'process', -override=>1)
. hidden(-name=>'type', -value=>$type, -override=>1)
. hidden(-name=>'userid', -value=>$userid, -override=>1)
. submit(-class=>'button')
. "</td></tr>\n</table>"
. end_form();
$template_data_ref->{admin} = $admin;

}
elsif ($action eq 'process') {
Expand All @@ -178,77 +173,31 @@
elsif ($type eq 'delete') {
ProductOpener::Users::delete_user($user_ref);
}

$html .= "<p>" . lang($type . '_user_result') . "</p>";
$template_data_ref->{type} = $type;

if ($type eq 'add') {

# Show different messages depending on whether it is a pro account
# and whether we are on the public platform or the pro platform

if (defined $user_ref->{requested_org}) {

# Pro account, but the requested org already exists

my $requested_org_ref = retrieve_org($user_ref->{requested_org});

$html .= "<div id=\"existing_org_warning\">"
. "<p>" . sprintf(lang("add_user_existing_org"), org_name($requested_org_ref)) . "</p>"
. "<p>" . lang("add_user_existing_org_pending") . "</p>"
. "<p>" .lang("please_email_producers") . "</p>"
. "</div>";
}
elsif (defined $user_ref->{org}) {

# Pro-account, with a newly created org

if (defined $server_options{producers_platform}) {

# We are on the producers platform
# Suggest next steps:
# - import product data

$html .= "<p>" . lang("add_user_you_can_edit_pro") . "</p>";
$html .= "<p>&rarr; <a href=\"/cgi/import_file_upload.pl\">" . lang("import_product_data") . "</a></p>";
}
else {

# We are on the public platform, link to the producers platform

my $pro_url = "https://" . $subdomain . ".pro." . $server_domain . "/";
$html .= "<p>" . sprintf(lang("add_user_you_can_edit_pro_promo"), $pro_url) . "</p>";
}
}
else {
# Personal account

# Suggest next steps:
# - add or edit products on the web site or through the app
# - join us on Slack

$html .= "<p>" . sprintf(lang("add_user_you_can_edit"), lang("get_the_app_link")) . "</p>";

$html .= "<p>" . sprintf(lang("add_user_join_the_project"), lang("site_name")) . "</p>";

$html .= "<p>" . lang("add_user_join_us_on_slack") . "</p>";
$html .= "<p>&rarr; <a href=\"https://slack.openfoodfacts.org\">" . lang("join_us_on_slack") . "</a></p>";
}
}

if (($type eq 'add') or ($type eq 'edit')) {
$template_data_ref->{user_requested_org} = $user_ref->{requested_org};

my $requested_org_ref = retrieve_org($user_ref->{requested_org});
$template_data_ref->{add_user_existing_org} = sprintf(lang("add_user_existing_org"), org_name($requested_org_ref));

# Do not display donate link on producers platform
if (not $server_options{producers_platform}) {
$html .= "<h3>" . lang("you_can_also_help_us") . "</h3>\n";
$html .= "<p>" . lang("bottom_content") . "</p>\n";
}
$template_data_ref->{user_org} = $user_ref->{org};

$template_data_ref->{server_options_producers_platform} = $server_options{producers_platform};

my $pro_url = "https://" . $subdomain . ".pro." . $server_domain . "/";
$template_data_ref->{add_user_pro_url} = sprintf(lang("add_user_you_can_edit_pro_promo"), $pro_url);

$template_data_ref->{add_user_you_can_edit} = sprintf(lang("add_user_you_can_edit"), lang("get_the_app_link"));
$template_data_ref->{add_user_join_the_project} = sprintf(lang("add_user_join_the_project"), lang("site_name"));
}
}

if ($debug) {
$html .= "<p>type: $type action: $action userid: $userid</p>";
}

$template_data_ref->{debug} = $debug;
$template_data_ref->{userid} = $userid;

my $full_width = 1;
if ($action ne 'display') {
$full_width = 0;
Expand All @@ -265,6 +214,9 @@
else {

my $title = lang($type . '_user_' . $action);

$tt->process('user_form.tt.html', $template_data_ref, \$html);
$html .= "<p>" . $tt->error() . "</p>";

display_new( {
title=>$title,
Expand Down
113 changes: 113 additions & 0 deletions templates/user_form.tt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
[% IF action == 'display' || action == 'none' %]
[% IF error_count >= 0 %]
<div class='alert-box alert'>
<p>
<b>[% lang('correct_the_following_errors') %]</b>
</p>
[% FOREACH error IN errors %]
[% error %]<br />
[% END %]
</div>
[% END %]
[% END %]

[% IF action == 'display' %]

<!-- Start form -->

<form method="post" action="/cgi/user.pl" enctype="multipart/form-data">
<table>
[% display_user_form %]
[% display_user_form_optional %]
[% display_user_form_admin_only %]

[% IF admin %]
<tr><td colspan="2">
<input type="checkbox" name="delete" value="on" checked="checked" />[% lang("delete_user") %]
</td></tr>
[% END %]
<tr>
<td>
<input type="hidden" name="action" value="process" />
<input type="hidden" name="type" value="[% type %]" />
<input type="hidden" name="[% userid %]" value="1" />
<input type="submit" name=".submit" class="button" />
</td>
</tr>
</table>
</form>

<!-- End form -->

[% ELSIF action == 'process' %]
<p>[% lang('${type}_user_result') %]</p>

[% IF type == 'add' %]

<!-- Show different messages depending on whether it is a pro account
and whether we are on the public platform or the pro platform -->

[% IF user_requested_org.defined %]

<!-- Pro account, but the requested org already exists -->

<div id="existing_org_warning">
<p>[% add_user_existing_org %]</p>
<p>[% lang("add_user_existing_org_pending") %]</p>
<p>[% lang("please_email_producers") %]</p>
</div>

[% ELSIF user_org.defined %]

<!-- Pro-account, with a newly created org -->

[% IF server_options_producers_platform.defined %]

<!-- We are on the producers platform -->
<!-- Suggest next steps: -->
<!-- - import product data -->

<p>[% lang("add_user_you_can_edit_pro") %]</p>
<p>&rarr; <a href="/cgi/import_file_upload.pl">[% lang("import_product_data") %]</a></p>

[% ELSE %]

<!-- We are on the public platform, link to the producers platform -->

<p>[% add_user_pro_url %]</p>

[% END %]

[% ELSE %]

<!-- Personal account -->

<!-- Suggest next steps:
- add or edit products on the web site or through the app
- join us on Slack -->

<p>[% add_user_you_can_edit %]</p>
<p>[% add_user_join_the_project %]</p>
<p>[% lang("add_user_join_us_on_slack") %]</p>
<p>&rarr; <a href="https://slack.openfoodfacts.org">[% lang("join_us_on_slack") %]</a></p>

[% END %]

[% END %]

[% IF type == 'add' || type == 'edit' %]

<!-- Do not display donate link on producers platform -->

[% IF !(server_options_producers_platform) %]
<h3>[% lang("you_can_also_help_us") %]</h3>
<p>[% lang("bottom_content") %]</p>
[% END %]

[% END %]

[% END %]

[% IF debug %]
<p>type: [% type %] action: [% action %] userid: [% userid %]</p>
[% END %]