<?php
/**
 * Created by PhpStorm.
 * User: marco
 * Date: 23/07/18
 * Time: 18.09
 */
function c_f_html_metabox()
{
    global $post;
    $style = '
    <style>
        span.cf_shortcode { padding: 5px; background-color: #333333; margin-right: 10px; color: white; font-weight: bold;}
    </style>';
    $html = '<h3>Available short codes:</h3>';
    // <p><span class="cf_shortcode">[cf_mario]</span><span class="cf_shortcode">[cf_mario]</span><span class="cf_shortcode">[cf_mario]</span><span class="cf_shortcode">[cf_mario]</span></p>';
    if (have_rows('contact_form_rows')) :
        $html .= '<p>'
        ?>

        <?php while (have_rows('contact_form_rows')) : the_row(); ?>
        <?php if (have_rows('contact_form_columns')) : ?>
            <?php while (have_rows('contact_form_columns')) : the_row(); ?>
                <?php the_sub_field('width'); ?>
                <?php if (have_rows('contact_form_blocks')): ?>
                    <?php while (have_rows('contact_form_blocks')) : the_row(); ?>
                        <?php if (get_row_layout() == 'text_input') : ?>
                            <?php //the_sub_field( 'id' ); ?>
                            <?php //the_sub_field( 'label' ); ?>
                            <?php if (get_sub_field('required') == 1) {
                                // echo 'true';
                            } else {
                                // echo 'false';
                            }
                            $html .= '<span class="cf_shortcode">[cf_'.get_sub_field('id').']</span>'
                            ?>
                        <?php endif; ?>
                    <?php endwhile; ?>
                <?php else: ?>
                    <?php // no layouts found ?>
                <?php endif; ?>
            <?php endwhile; ?>
        <?php else : ?>
            <?php // no rows found ?>
        <?php endif; ?>
    <?php endwhile;

        $html .= '</p>'
        ?>
    <?php else : ?>
        <?php $html .= '<p>No form fields</p>' ?>
    <?php endif;

    echo $style.$html;

}

function add_metabox_to_contact_form()
{
    $screens = ['contact_form'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Contact Form Shortcodes',  // Box title
            'c_f_html_metabox',  // Content callback, must be of type callable
            $screen                   // Post type
        );
    }
}

add_action('add_meta_boxes', 'add_metabox_to_contact_form');