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

// setup page
define("PAGE_NAME", t("rewards", "Rewards"));
define("PAGE_DESCRIPTION", t("rewards_meta_description", "Rewards"));
define("PAGE_KEYWORDS", t("rewards_meta_keywords", "earn, money, rewards, cash, sales, affiliate, file, hosting, site"));
define("MONTHLY_UPGRADE_EXAMPLE", 20);

// load reward details
$rewardsConfig   = pluginHelper::pluginSpecificConfiguration('rewards');
$rewardsSettings = json_decode($rewardsConfig['data']['plugin_settings'], true);

// include header
require_once(DOC_ROOT . '/_header.php');
?>

<div class="contentPageWrapper">

    <!-- main section -->
    <div class="pageSectionMain ui-corner-all">
        <div class="pageSectionMainInternal">
            <?php
            if((int)$rewardsSettings['pps_enabled'] == 1)
            {
            ?>
            <div id="pageHeader">
                <h2><?php echo t("rewards", "Rewards"); ?></h2>
            </div>
            <div class="faq">
                <p>
                    Earn <?php echo $rewardsSettings['user_percentage']; ?>% of each sale you refer to this site! You can earn money from upgrading users in 2 ways:
                </p>
                <ol class="rewardsTextList">
                    <li>Using your unique affiliate id, simply link to the site via your existing website. You'll earn <?php echo $rewardsSettings['user_percentage']; ?>% for any users which register for an account and subsequently upgrade.</li>
                    <li>From users which upgrade to download files within your account.</li>
                </ol>
                <p><strong>How much can you earn?</strong></p>
                <p>Based on <?php echo MONTHLY_UPGRADE_EXAMPLE; ?> upgrades and each upgrade costing <?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL; ?><?php echo SITE_CONFIG_COST_FOR_30_DAYS_PREMIUM; ?> per month, you could earn the following:</p>
                <table width="100%" class="rewardsTable">
                    <tr>
                        <td style="width: 25%;">Month:</td>
                        <td style="width: 25%; text-align: center;">Referrals:</td>
                        <td style="width: 30%; text-align: center;">Total Referrals: (inc renewals)</td>
                        <td>Monthly Total:</td>
                    </tr>
                    <?php
                    $overallTotal = 0;
                    for ($i            = 1; $i <= 10; $i++)
                    {
                        $totalRef      = MONTHLY_UPGRADE_EXAMPLE * $i;
                        $totalEarnings = (($totalRef * SITE_CONFIG_COST_FOR_30_DAYS_PREMIUM) / 100) * $rewardsSettings['user_percentage'];
                        ?>
                        <tr>
                            <td><?php echo date('F Y', strtotime('+' . $i . ' month')); ?></td>
                            <td style="text-align: center;"><?php echo MONTHLY_UPGRADE_EXAMPLE; ?></td>
                            <td style="text-align: center;"><?php echo $totalRef; ?></td>
                            <td style="text-align: right;"><?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL; ?><?php echo number_format($totalEarnings, 2); ?></td>
                        </tr>
                        <?php
                        $overallTotal  = $overallTotal + $totalEarnings;
                    }
                    ?>
                    <tr>
                        <td colspan="3" style="text-align: right;">Total:</td>
                        <td style="text-align: right;"><?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL; ?><?php echo number_format($overallTotal, 2); ?></td>
                    </tr>
                </table>
            </div>
            <div class="clear" style='padding-bottom: 16px;'><!-- --></div>
            <?php
            }
            ?>
            
            <?php
            if((int)$rewardsSettings['ppd_enabled'] == 1)
            {
                $lowestValue = $db->getValue("SELECT payout_rate FROM plugin_reward_ppd_group ORDER BY payout_rate ASC LIMIT 1");
            ?>
            <div id="pageHeader">
                <h2><?php echo t("pay_per_download_rates", "Pay Per Download (PPD) Rates"); ?></h2>
            </div>
            <p>
                <?php echo t('upload_your_files_and_youll_be_paid_for_every', 'Upload your files and you\'ll be paid for every file downloaded on your account.'); ?> <?php if((int)$rewardsSettings['ppd_min_file_size'] > 0) echo t('files_above_x_will_count', 'Files above [[[FILE_SIZE]]]MB will count.', array('FILE_SIZE'=>(int)$rewardsSettings['ppd_min_file_size'])); ?> <?php echo t('see_the_payment_rates_below', 'See the payment rates below:'); ?>
            </p>
            <table class="ppdRateTable">
                <thead>
                    <td style="width: 100px; text-align: center;"><?php echo t('group_name', 'Group Name'); ?>:</td>
                    <td style="width: 160px; text-align: center;"><?php echo t('paid_per_1000_users', 'Paid Per 1,000 Downloads'); ?>:</td>
                    <td><?php echo t('downloading_country', 'Downloading Country'); ?>:</td>
                </thead>
                <tbody>
                <?php
                // get ppd groups
                $groups = $db->getRows('SELECT id, group_label, payout_rate FROM plugin_reward_ppd_group ORDER BY id');
                foreach($groups AS $group)
                {
                    $countryData = $db->getRows('SELECT plugin_reward_ppd_group_country.country_code, plugin_reward_country_list.name FROM plugin_reward_ppd_group_country LEFT JOIN plugin_reward_country_list ON plugin_reward_ppd_group_country.country_code = plugin_reward_country_list.iso_alpha2 WHERE group_id='.$group['id']);
                    $countries = array();
                    if(COUNT($countryData))
                    {
                        foreach($countryData AS $countryRow)
                        {
                            $countries[] = $countryRow['name'];
                        }
                    }
                    
                    // add 'other' label
                    if($group['payout_rate'] == $lowestValue)
                    {
                        $countries[] = UCWords(t('other', 'Other'));
                    }
                    
                    // for no countries
                    if(COUNT($countries) == 0)
                    {
                        $countries[] = '-';
                    }
                    
                    $countriesStr = implode(", ", $countries);
                    
                    ?>
                    <tr>
                        <td style="text-align: center;"><?php echo safeOutputToScreen($group['group_label']); ?></td>
                        <td style="text-align: center;"><?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL.safeOutputToScreen($group['payout_rate']); ?></td>
                        <td><?php echo safeOutputToScreen($countriesStr); ?></td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>

            <div class="clear"><!-- --></div>
            <?php
            }
            ?>
            
            <p class="rewardsTopPadding"><strong>How can I claim my rewards?</strong></p>
            <p>
                Any rewards will take <?php echo $rewardsSettings['payment_lead_time']; ?> days to clear within your account. Once your cleared rewards are over <?php echo SITE_CONFIG_COST_CURRENCY_SYMBOL; ?><?php echo number_format($rewardsSettings['payment_threshold'], 2); ?> you can request a payment via your account. Payments are made on the <?php echo date('jS', strtotime(date('Y-m-').$rewardsSettings['payment_date'])); ?> of every month via PayPal.
            </p>
            <p class="rewardsTopPadding"><strong>How do I get started?</strong></p>
            <p>
                Signup for an account on our <a href="<?php echo WEB_ROOT; ?>/register.<?php echo SITE_CONFIG_PAGE_EXTENSION; ?>">registration page</a>.
                <?php
                if((int)$rewardsSettings['pps_enabled'] == 1)
                {
                ?>
                Once you've completed your registration you'll find your affiliate id in the 'rewards' section of your account. Begin by uploading and sharing your files or by linking from your existing site using your affiliate id like this:
                <ul>
                    <li><a href="<?php echo WEB_ROOT; ?>/?aff=[AFFILIATE_ID]"><?php echo WEB_ROOT; ?>/?aff=[AFFILIATE_ID]</a></li>
                </ul>
                <?php
                }
                elseif((int)$rewardsSettings['ppd_enabled'] == 1)
                {
                ?>
                Once you've completed your registration just start uploading your files and sharing the download links with your family and friends.<br/><br/>You'll be paid for any downloads outside of your account. Note: We only count completed downloads and downloads from unique IP addresses.
                <?php
                }
                ?>
            </p>
        </div>
    </div>
    <?php include_once(DOC_ROOT . "/_bannerRightContent.inc.php"); ?>
    <div class="clear"><!-- --></div>
</div>

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