<commit_before>
package OpenXPKI::Client::UI::Handle::Profile;

use Moose;
use Data::Dumper;
use English;
use OpenXPKI::Serialization::Simple;

sub render_profile_select {

    my $class = shift; # static call
    my $self = shift; # reference to the wrapping workflow/result
    my $args = shift;
    my $wf_action = shift;


    $self->logger()->trace( 'render_profile_select with args: ' . Dumper $args );

    my $wf_info = $args->{WF_INFO};

    # Get the list of profiles from the backend - return is a hash with id => hash
    my $profiles = $self->send_command( 'get_cert_profiles', {});
    # Transform hash into value/label list and sort it
    # Apply translation
    map { $profiles->{$_}->{label} = $profiles->{$_}->{label} } keys %{$profiles};
    # Sort
    my @profiles = sort { lc($a->{label}) cmp lc($b->{label}) } values %{$profiles};

    my $context = $wf_info->{WORKFLOW}->{CONTEXT};

    my $cert_profile = $context->{cert_profile} || '';

    # If the profile is preselected, we need to fetch the options
    my @styles;
    if ($cert_profile) {
        my $styles = $self->send_command( 'get_cert_subject_profiles', { PROFILE => $cert_profile });
        # TODO clean up API after Mason decomissioning
        # Transform hash into value/label list and sort it
        @styles = map { { value => $_, label => $styles->{$_}->{LABEL}, description => $styles->{$_}->{DESCRIPTION} } } keys %{$styles};
        @styles = sort { lc($a->{label}) cmp lc($b->{label}) } @styles;
    }

    my @fields;
    foreach my $field (@{$wf_info->{ACTIVITY}->{$wf_action}->{field}}) {
        my $name = $field->{name};
        my $item = $self->__render_input_field( $field, $context->{$name} );

        if ($name eq 'cert_profile') {
            $item = {
                %{$item},
                options => \@profiles,
                actionOnChange => 'profile!get_styles_for_profile',
                prompt => $item->{placeholder}, # todo - rename in UI
            };
        } elsif ($name eq 'cert_subject_style') {
            $item = {
                %{$item},
                options => \@styles,
                prompt => $item->{placeholder}, # todo - rename in UI
            };
        }

        push @fields, $item;
    }


    # record the workflow info in the session
    push @fields, $self->__register_wf_token($wf_info, {
        wf_action => $wf_action,
        wf_fields => \@fields,
    });

    $self->add_section({
        type => 'form',
        action => 'workflow',
        content => {
            submit_label => 'I18N_OPENXPKI_UI_WORKFLOW_SUBMIT_BUTTON',
            fields => \@fields
        }
    });

    return $self;

}


sub render_subject_form {

    my $class = shift; # static call
    my $self = shift; # reference to the wrapping workflow/result
    my $args = shift;
    my $wf_action = shift;
    my $param = shift;

    my $section;
    my $mode = 'enroll';

    if ($param) {
        ($section, $mode) = split /!/, $param;
    }

    my $wf_info = $args->{WF_INFO};

    my $context = $wf_info->{WORKFLOW}->{CONTEXT};

    # get profile and style from the context
    my $cert_profile = $context->{'cert_profile'};
    my $cert_subject_style = $context->{'cert_subject_style'};

    my $is_renewal = ($mode eq 'renewal');

    # Parse out the field name and type, we assume that there is only one activity with one field
    $wf_action = (keys %{$wf_info->{ACTIVITY}})[0] unless($wf_action);
    my $field_name = $wf_info->{ACTIVITY}->{$wf_action}->{field}[0]->{name};
    my $field_type = $section ? $section : substr($wf_info->{ACTIVITY}->{$wf_action}->{field}[0]->{type}, 5);

    $self->logger()->debug( " Render subject for $field_name with type $field_type in $wf_action " );

    # Allowed types are cert_subject, cert_san, cert_info
    my $fields = $self->send_command_v2( 'get_field_definition',
        { profile => $cert_profile, style => $cert_subject_style, 'section' =>  $section }
    );

    $self->logger()->trace( 'Profile fields' . Dumper $fields );

    # Load preexisiting values from context
    my $values = {};
    if ($context->{$field_name}) {
        $values = $self->serializer()->deserialize( $context->{$field_name} );
    }


    my @fielddesc;
    foreach my $field (@{$fields}) {
        push @fielddesc, { label => $field->{LABEL}, value => $field->{DESCRIPTION}, format => 'raw' } if ($field->{DESCRIPTION});
    }

    $self->logger()->trace( 'Preset ' . Dumper $values );

    # Map the old notation for the new UI
    $fields = OpenXPKI::Client::UI::Handle::Profile::__translate_form_def( $fields, $field_name, $values, $is_renewal );

    $self->logger()->trace( 'Mapped fields' . Dumper $fields );<commit_msg>Fix profile rendering handler<commit_after>
package OpenXPKI::Client::UI::Handle::Profile;

use Moose;
use Data::Dumper;
use English;
use OpenXPKI::Serialization::Simple;

sub render_profile_select {

    my $class = shift; # static call
    my $self = shift; # reference to the wrapping workflow/result
    my $args = shift;
    my $wf_action = shift;


    $self->logger()->trace( 'render_profile_select with args: ' . Dumper $args );

    my $wf_info = $args->{WF_INFO};

    # Get the list of profiles from the backend - return is a hash with id => hash
    my $profiles = $self->send_command( 'get_cert_profiles', {});
    # Transform hash into value/label list and sort it
    # Apply translation
    map { $profiles->{$_}->{label} = $profiles->{$_}->{label} } keys %{$profiles};
    # Sort
    my @profiles = sort { lc($a->{label}) cmp lc($b->{label}) } values %{$profiles};

    my $context = $wf_info->{WORKFLOW}->{CONTEXT};

    my $cert_profile = $context->{cert_profile} || '';

    # If the profile is preselected, we need to fetch the options
    my @styles;
    if ($cert_profile) {
        my $styles = $self->send_command( 'get_cert_subject_profiles', { PROFILE => $cert_profile });
        # TODO clean up API after Mason decomissioning
        # Transform hash into value/label list and sort it
        @styles = map { { value => $_, label => $styles->{$_}->{LABEL}, description => $styles->{$_}->{DESCRIPTION} } } keys %{$styles};
        @styles = sort { lc($a->{label}) cmp lc($b->{label}) } @styles;
    }

    my @fields;
    foreach my $field (@{$wf_info->{ACTIVITY}->{$wf_action}->{field}}) {
        my $name = $field->{name};
        my $item = $self->__render_input_field( $field, $context->{$name} );

        if ($name eq 'cert_profile') {
            $item = {
                %{$item},
                options => \@profiles,
                actionOnChange => 'profile!get_styles_for_profile',
                prompt => $item->{placeholder}, # todo - rename in UI
            };
        } elsif ($name eq 'cert_subject_style') {
            $item = {
                %{$item},
                options => \@styles,
                prompt => $item->{placeholder}, # todo - rename in UI
            };
        }

        push @fields, $item;
    }


    # record the workflow info in the session
    push @fields, $self->__register_wf_token($wf_info, {
        wf_action => $wf_action,
        wf_fields => \@fields,
    });

    $self->add_section({
        type => 'form',
        action => 'workflow',
        content => {
            submit_label => 'I18N_OPENXPKI_UI_WORKFLOW_SUBMIT_BUTTON',
            fields => \@fields
        }
    });

    return $self;

}


sub render_subject_form {

    my $class = shift; # static call
    my $self = shift; # reference to the wrapping workflow/result
    my $args = shift;
    my $wf_action = shift;
    my $param = shift;

    my $section;
    my $mode = 'enroll';

    if ($param) {
        ($section, $mode) = split /!/, $param;
    }

    my $wf_info = $args->{WF_INFO};

    my $context = $wf_info->{WORKFLOW}->{CONTEXT};

    # get profile and style from the context
    my $cert_profile = $context->{'cert_profile'};
    my $cert_subject_style = $context->{'cert_subject_style'};

    my $is_renewal = ($mode eq 'renewal');

    # Parse out the field name and type, we assume that there is only one activity with one field
    $wf_action = (keys %{$wf_info->{ACTIVITY}})[0] unless($wf_action);
    my $field_name = $wf_info->{ACTIVITY}->{$wf_action}->{field}[0]->{name};

    $section = substr($wf_info->{ACTIVITY}->{$wf_action}->{field}[0]->{type}, 5) unless($section);

    $self->logger()->debug( " Render subject for $field_name, section $section in $wf_action" );

    # Allowed types are cert_subject, cert_san, cert_info
    my $fields = $self->send_command_v2( 'get_field_definition',
        { profile => $cert_profile, style => $cert_subject_style, 'section' =>  $section }
    );

    $self->logger()->trace( 'Profile fields' . Dumper $fields );

    # Load preexisiting values from context
    my $values = {};
    if ($context->{$field_name}) {
        $values = $self->serializer()->deserialize( $context->{$field_name} );
    }


    my @fielddesc;
    foreach my $field (@{$fields}) {
        push @fielddesc, { label => $field->{LABEL}, value => $field->{DESCRIPTION}, format => 'raw' } if ($field->{DESCRIPTION});
    }

    $self->logger()->trace( 'Preset ' . Dumper $values );

    # Map the old notation for the new UI
    $fields = OpenXPKI::Client::UI::Handle::Profile::__translate_form_def( $fields, $field_name, $values, $is_renewal );

    $self->logger()->trace( 'Mapped fields' . Dumper $fields );
{'fraction_non_alphanumeric': 0.16434865696995435, 'fraction_numerical': 0.001486357362777365, 'mean_word_length': 2.7634838194166997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 169, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4662293', 'n_tokens_mistral': 3116, 'n_tokens_neox': 2982, 'n_words': 772}