<?php


function irpp_ui_statistics()
{
    global $irpp;

    echo '<h2>Link Statistics</h2>';

    if (!$irpp->Options->isStatisticsEnabled()) {
        echo '<h4>Statistics are not enabled. They can be enabled in the Settings Tab.</h4>';
    } else {
        echo '<p>These statistics are for links inserted by the plugin. Shortcode links are not counted.</p>';
        echo '<br/>';

        $irpp->Form->prefix = 'Statistics';
        $irpp->Form->helps  = false;
        $irpp->Form->formStarts();
        if ($irpp->Check->nonce('irp_statistics')) {
            $irpp->Options->setLinkStatistics([]);
        }

        $irpp->Form->nonce('irp_statistics');
        $irpp->Form->submit('Reset Statistics');
        $irpp->Form->formEnds();

        $linkStatistics = $irpp->Options->getLinkStatistics();

        if (empty($linkStatistics)) {
            echo '<h4>There are currently no statistics to display.</h4>';
        } else {
            ?>
            <br />
            <table class="irp-grid">
                <tr>
                    <th>Post</th>
                    <th>Displayed Link Boxes</th>
                    <th>Display Count</th>
                </tr>
                <?php
                foreach ($linkStatistics as $k => $e) {
                    $post = get_post($k);
                    if (!empty($post)) {
                        echo '<tr>';
                        $title = $post->post_title;
                        if (empty(trim($title))) {
                            $title = 'NO TITLE';
                        }

                        echo '<td>'.$title.'</td>';
                        echo '<td></td>';
                        echo '<td></td>';
                        echo '</tr>';
                        foreach ($e as $k2 => $e2) {
                            $post = get_post($k2);
                            if (!empty($post)) {
                                echo '<tr>';
                                echo '<td></td>';
                                $title = $post->post_title;
                                if (empty(trim($title))) {
                                    $title = 'NO TITLE';
                                }

                                echo '<td>'.$title.'</td>';
                                echo '<td>'.$e2.'</td>';
                                echo '</tr>';
                            }
                        }
                    }//end if
                }//end foreach
                ?>
            </table>
            <div style="clear:both"></div>
            <?php
        }//end if
    }//end if

}//end irp_ui_statistics()
