Home / Archive / RE.N.A.P.A. – Modulo 2 (Form iscrizione PCS – Step 1)
Duplicate Snippet

Embed Snippet on Your Site

RE.N.A.P.A. – Modulo 2 (Form iscrizione PCS – Step 1)

Vito Antonio D'Ambrosio PRO
<10
Code Preview
php
<?php
<?php
// RE.N.A.P.A. – Modulo 2: Form iscrizione PCS (Step 1)
function renapa_render_iscrizione_pcs_form( $old = array(), $errors = array() ) {
    // Valori precompilati (in caso di errori)
    $first_name = isset( $old['first_name'] ) ? $old['first_name'] : '';
    $last_name  = isset( $old['last_name'] ) ? $old['last_name'] : '';
    $email      = isset( $old['email'] ) ? $old['email'] : '';
    $phone      = isset( $old['phone'] ) ? $old['phone'] : '';
    $privacy    = isset( $old['privacy'] ) && $old['privacy'] ? 'checked' : '';
    // Output HTML del form
    ?>
    <div class="renapa-iscrizione">
        <h2>Richiesta di iscrizione al Registro Nazionale PCSStep 1</h2>
        <p>Compila i tuoi dati di base. Questa è la prima fase del percorso di iscrizione al RE.N.A.P.A. – Registro Nazionale Assistenti Personali AutomotivePersonal Car Shopper.</p>
        <?php if ( ! empty( $errors ) ) : ?>
            <div class="renapa-errori" style="border:1px solid #e11; padding:10px; margin:10px 0; color:#b91c1c;">
                <ul>
                    <?php foreach ( $errors as $err ) : ?>
                        <li><?php echo esc_html( $err ); ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        <form method="post">
            <?php wp_nonce_field( 'renapa_iscrizione_submit', 'renapa_iscrizione_nonce' ); ?>
            <p>
                <label>Nome<br>
                    <input type="text" name="renapa_first_name" value="<?php echo esc_attr( $first_name ); ?>" required>
                </label>
            </p>
            <p>
                <label>Cognome<br>
                    <input type="text" name="renapa_last_name" value="<?php echo esc_attr( $last_name ); ?>" required>
                </label>
            </p>
            <p>
                <label>Email<br>
                    <input type="email" name="renapa_email" value="<?php echo esc_attr( $email ); ?>" required>
                </label>
            </p>
            <p>
                <label>Telefono cellulare<br>
                    <input type="text" name="renapa_phone" value="<?php echo esc_attr( $phone ); ?>" required>
                </label>
            </p>
            <p>
                <label>
                    <input type="checkbox" name="renapa_privacy" value="1" <?php echo $privacy; ?> required>
                    Acconsento al trattamento dei miei dati personali (GDPR) per la gestione della mia richiesta di iscrizione al Registro PCS.
                </label>
            </p>
            <p>
                <button type="submit" name="renapa_iscrizione_submit" value="1">
                    Invia la tua richiesta di iscrizione
                </button>
            </p>
        </form>
    </div>
    <?php
}
// Gestione invio del form e inserimento nel database
function renapa_handle_iscrizione_pcs() {
    if ( ! isset( $_POST['renapa_iscrizione_submit'] ) ) {
        // Nessun invio: non fa nulla
        return;
    }
    // Verifica nonce (sicurezza)
    if ( ! isset( $_POST['renapa_iscrizione_nonce'] ) || 
         ! wp_verify_nonce( $_POST['renapa_iscrizione_nonce'], 'renapa_iscrizione_submit' ) ) {
        add_filter( 'the_content', function( $content ) {
            $error_msg = '<p style="color:#b91c1c;">Si è verificato un problema di sicurezza. Riprova a inviare il modulo.</p>';
            return $error_msg . $content;
        } );
        return;
    }
    $first_name = isset( $_POST['renapa_first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['renapa_first_name'] ) ) : '';
    $last_name  = isset( $_POST['renapa_last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['renapa_last_name'] ) ) : '';
    $email      = isset( $_POST['renapa_email'] ) ? sanitize_email( wp_unslash( $_POST['renapa_email'] ) ) : '';
    $phone      = isset( $_POST['renapa_phone'] ) ? sanitize_text_field( wp_unslash( $_POST['renapa_phone'] ) ) : '';
    $privacy    = isset( $_POST['renapa_privacy'] ) ? 1 : 0;
    $errors = array();
    if ( empty( $first_name ) ) {
        $errors[] = 'Il nome è obbligatorio.';
    }
    if ( empty( $last_name ) ) {
        $errors[] = 'Il cognome è obbligatorio.';
    }
    if ( empty( $email ) || ! is_email( $email ) ) {
        $errors[] = 'L\'email non è valida.';
    }
    if ( empty( $phone ) ) {
        $errors[] = 'Il telefono è obbligatorio.';
    }
    if ( ! $privacy ) {
        $errors[] = 'Devi acconsentire al trattamento dei dati personali.';
    }
    $old = array(
        'first_name' => $first_name,
        'last_name'  => $last_name,
        'email'      => $email,
        'phone'      => $phone,
        'privacy'    => $privacy,
    );
    // Se ci sono errori, reiniettiamo il form con i messaggi
    if ( ! empty( $errors ) ) {
        add_filter( 'the_content', function( $content ) use ( $old, $errors ) {
            ob_start();
            renapa_render_iscrizione_pcs_form( $old, $errors );
            $form_html = ob_get_clean();
            return $form_html . $content;
        } );
        return;
    }
    // Inserimento nel database
    global $wpdb;
    $table_name = $wpdb->prefix . 'renapa_candidati';
    $wpdb->insert(
        $table_name,
        array(
            'first_name' => $first_name,
            'last_name'  => $last_name,
            'email'      => $email,
            'phone'      => $phone,
            'privacy'    => $privacy ? 'yes' : 'no',
        ),
        array( '%s', '%s', '%s', '%s', '%s' )
    );
    // Messaggio di conferma
    add_filter( 'the_content', function( $content ) {
        $msg  = '<div class="renapa-iscrizione-conferma" style="border:1px solid #16a34a; padding:10px; margin:10px 0; color:#166534;">';
        $msg .= '<strong>Richiesta inviata correttamente.</strong><br>';
        $msg .= 'La tua richiesta di iscrizione al Registro Nazionale Assistenti Personali Automotive – PCS è stata registrata. ';
        $msg .= 'Verrai ricontattato dalla segreteria FEPECS per completare il percorso (corso BYC, documentazione e valutazione del Comitato Tecnico Scientifico).';
        $msg .= '</div>';
        return $msg . $content;
    } );
}
add_action( 'init', 'renapa_handle_iscrizione_pcs' );
// Shortcode per mostrare il form
function renapa_iscrizione_pcs_shortcode() {
    ob_start();
    renapa_render_iscrizione_pcs_form();
    return ob_get_clean();
}
add_shortcode( 'renapa_iscrizione_pcs', 'renapa_iscrizione_pcs_shortcode' );

Comments

Add a Comment