<?php
/* setup includes */
require_once('includes/master.inc.php');

/* require login */
$Auth->requireUser('login.php');

/* expire url */
if (isset($_REQUEST['d']))
{
    $url = shortUrl::loadById($_REQUEST['d']);
    if ($url)
    {
        /* check user id */
        if ($url->userId == $Auth->id)
        {
            $url->expire();
        }
    }
}

/* setup page */
define("PAGE_NAME", t("account_home_page_name", "Account Home"));
define("PAGE_DESCRIPTION", t("account_home_meta_description", "Your Account Home"));
define("PAGE_KEYWORDS", t("account_home_meta_keywords", "account, home, shorturl, your, interface, short, url, site"));

require_once('_header.php');

// load all short urls for this account
$urls = shortUrl::loadAllByAccount($Auth->id);
?>

<script>
    oTable = null;
    $(document).ready(function() {
        oTable = $('#shortUrlData').dataTable( {
            "sPaginationType": "full_numbers",
            "aaSorting": [[ 1, "asc" ]],
            "aoColumns": [
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                { "asSorting": [ "asc", "desc" ] },
                null
            ],
            "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData[0];
                $(nRow).attr("id", id);
                cellContents = aData[4];
                if (cellContents.substr(0,7) == 'expired')
                {
                    $(nRow).addClass('rowUrlExpired');
                }
                return nRow;
            }
        } );
    } );
    
    $('#shortUrlData_filter').ready(function() {
        $('#shortUrlData_filter').append($('#additionalFilters').html());
        $('#additionalFilters').html('');
        oTable.fnFilter( $('#statusFilter').val(), 4 );
    });
</script>

<div class="contentPageWrapper">

    <!-- main section -->
    <div class="pageSectionMainFull ui-corner-all">
        <div class="pageSectionMainInternal">
            <div id="pageHeader">
                <h2><?php echo t("your_short_urls", "Your Short Urls"); ?></h2>
            </div>
            <div>
                <p class="introText">
                    <?php if (COUNT($urls))
                    { ?>
                        You have <?php echo COUNT($urls); ?> url<?php echo (COUNT($urls) != 1) ? 's' : ''; ?> within your account. Use the table below to navigate or search for short urls you've previously created. <a href="index.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>">Click here</a> to create a new short url.
                    <?php }
                    else
                    { ?>
                        You have no urls within your account. <a href="index.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>">Click here</a> to create a new short url.
<?php } ?>
                    <br/><br/>
                </p>
            </div>
            <div class="clear"><!-- --></div>

            <div>
                <p class="introText">
                    <?php
                    if ($urls)
                    {
                        echo '<table id="shortUrlData" class="shortUrlData" width="100%" cellpadding="3" cellspacing="0">';
                        echo '<thead>';
                        echo '<th style="width: 19px;" class="ui-state-default"></th>';
                        echo '<th class="ui-state-default">'.t('short_original_url', 'Short/Original Url:').'</th>';
                        echo '<th  style="width: 135px;"class="ui-state-default">'.t('created_last_visited', 'Created/Last Visited:').'</th>';
                        echo '<th style="width: 55px;" class="ui-state-default">'.t('visits', 'Visits:').'</th>';
                        echo '<th style="width: 65px;" class="ui-state-default">'.t('status', 'Status:').'</th>';
                        echo '<th style="width: 65px;" class="ui-state-default">'.t('options', 'Options:').'</th>';
                        echo '</thead>';
                        echo '<tbody>';
                        foreach ($urls AS $url)
                        {
                            echo '<tr>';
                            echo '<td class="txtCenter">';
                            if ($url['status'] == 'active')
                            {
                                echo '  <img src="shortUrlIcon.php?id=' . $url['id'] . '" width="16" height="16"/>';
                            }
                            echo '</td>';
                            echo '<td title="' . $url['originalUrl'] . '"><a href="http://' . _CONFIG_SITE_FULL_URL . '/' . $url['shortUrl'] . '" target="_blank">' . _CONFIG_SITE_FULL_URL . '/' . $url['shortUrl'] . '</a><br/>' . shortUrl::formatLongUrlForScreen($url['originalUrl']) . '</td>';
                            echo '<td class="txtCenter">' . dater($url['dateCreated']) . '<br/>' . dater($url['lastAccessed']) . '</td>';
                            echo '<td class="txtCenter">' . $url['visits'] . '</td>';
                            echo '<td class="txtCenter">' . $url['status'];
                            //echo '<br/><span class="privatePublicText">(' . ($url['isPrivate'] == 1 ? 'private' : 'public') . ')</span>';
                            echo '</td>';
                            $links = array();
                            if ($url['status'] == 'active')
                            {
                                $links[] = '<a href="http://' . _CONFIG_SITE_FULL_URL . '/account_home.' . SITE_CONFIG_PAGE_EXTENSION . '?d=' . $url['id'] . '" onClick="return confirm(\'Are you sure you want to expire this short url?\');"><img src="' . SITE_IMAGE_PATH . '/delete.png" width="16" height="16" title="expire shorl url"/></a>';
                            }
                            $links[] = '<a href="http://' . _CONFIG_SITE_FULL_URL . '/' . $url['shortUrl'] . '~s"><img src="' . SITE_IMAGE_PATH . '/chart_pie.png" width="16" height="16" title="short url stats" style="margin-left: 2px; margin-right: 2px;"/></a>';
                            if ($url['status'] == 'active')
                            {
                                $links[] = '<a href="index.html?u=' . $url['id'] . '&success=1"><img src="' . SITE_IMAGE_PATH . '/group.png" width="16" height="16" title="share"/></a>';
                            }
                            echo '<td class="txtCenter">' . implode("&nbsp;", $links) . '</td>';
                            echo '</tr>';
                        }
                        echo '</tbody>';
                        echo '</table>';
                    }
                    ?>
                </p>
                <div class="clear"></div>
            </div>
        </div>
    </div>
</div>

<div id="additionalFilters">
    &nbsp;&nbsp;<?php echo UCWords(t('status', 'status')); ?>
    <select name="statusFilter" id="statusFilter" onChange="oTable.fnFilter( $(this).val(), 4 );">
        <option value="">- All -</option>
        <option value="active" SELECTED>Active</option>
        <option value="expired">Expired</option>
        <option value="disabled">Disabled</option>
        <option value="spam">Spam</option>
    </select>
</div>

<?php
require_once('_footer.php');
?>