<?php
/*
Copyright 2012 Neil Harvey - wordpressLAB
*/

function rt_subscription_page() {
    global $wpdb;
    ?>
<div class="wrap">
    <h2>Subscriptions</h2>
    <p class="">
        <a href="?page=wp-sa-manage-subscription&wpu=list" class="row-title">List Subscription</a> |
        <a href="?page=wp-sa-manage-subscription&wpu=add" class="row-title">Add Subscription</a> <!-- | Umesh:removed link for trial subscription
		<a href="?page=wp-sa-manage-subscription&wpu=trial" class="row-title">Trial Settings</a>-->
    </p>
        <?php
        $wpu = $_GET['wpu'];
        switch($wpu) {
            case 'list':
                rt_subscription_list();
                rt_sa_display_help_for_subscription_list();
                break;

            case 'add':
                rt_subscription_add();
                break;

            case 'edit':
                rt_subscription_edit();
                break;

            case 'delete':
                rt_subscription_delete();
                break;
            case 'trial':
                rt_subscription_trial();
                break;

            default:
                rt_subscription_list();
                rt_sa_display_help_for_subscription_list();
                break;
        }
        ?>
</div>
<?php
}

function rt_sa_display_help_for_subscription_list() {
    ?>
<div>
    <p>&nbsp;</p>
    <h2>Usage Instruction</h2>
    <h3>Showing Subscription Form </h3>
    <p>Paste <code>[subscription_form]</code> short tag in any post/page to show subscription form.</p>
    <h3>Important:</h3>
    <p><b>Do not forget to <a href="<?php bloginfo('siteurl'); ?>/wp-admin/admin.php?page=wp-sa-setting">configure paypal settings</a> in-order to accept payments.</b></p>
</div>
<?php
}
function rt_subscription_list() {
    global $wpdb;
    $table_name = $wpdb->prefix . "wpsa_subscriptions";
    $qry = "SELECT * FROM {$table_name} WHERE 1";
    $subscriptions_list = $wpdb->get_results($qry);
    ?>
<table class="widefat post fixed" id="messagelist">
    <thead>
        <tr class="tablemenu">
            <th width="20%">Name</th>
            <th width="40%">Description</th>
            <th width="10%">Amount</th>
            <th width="10%">Duration</th>
       <!-- <th width="10%">Type</th>-->
            <th width="10%">Edit</th>
            <th width="10%">Delete</th>
        </tr>
    </thead>
    <tbody>
            <?php   if($subscriptions_list) {
                $k=0;
                foreach ($subscriptions_list as $subscription) {
                    if($k % 2)
                        $class = 'alternate';
                    else
                        $class = 'unread';
        //Edited to display Day(s), Week(s), ... instead of D, W, M, Y on Manage Subscription page : Nemi
                    $durationinterval = $subscription->duration_interval3;
                    switch($durationinterval) {
                        case 'D':
                            $durationinterval .= "ay(s)";
                            break;

                        case 'W':
                            $durationinterval .= "eek(s)";
                            break;

                        case 'M':
                            $durationinterval .= "onth(s)";
                            break;

                        case 'Y':
                            $durationinterval .= "ear(s)";
                            break;
                        default:
                            break;
                    }
                            ?>
        <tr class="read <?php echo $class; ?>">
            <td><?php echo $subscription->name; ?></td>
            <td><?php echo $subscription->description; ?></td>
            <td><?php echo $subscription->amount3; ?></td>
            <td><?php echo $subscription->duration3.' '.$durationinterval; ?></td>
      <!--  <td><?php //echo $subscription->type; ?></td>-->
            <td ><a href="?page=wp-sa-manage-subscription&wpu=edit&item_id=<?php echo $subscription->id; ?>" title="Edit this item"><img title="Edit!" alt="Edit!" src="<?php echo WP_PLUGIN_URL; ?>/wp-salesautomator/img/icons/Edit.png" width="16px"/></a></td>
            <td><a href="?page=wp-sa-manage-subscription&wpu=delete&item_id=<?php echo $subscription->id; ?>" title="Delete this item"><img title="Delete!" alt="Delete!" src="<?php echo WP_PLUGIN_URL; ?>/wp-salesautomator/img/icons/delete.png" onclick="return confirm('Do you really want to delete it?');"/></a></td>
        </tr>
                    <?php      
                        $k++;
                    }
                }else {
                    echo "<tr><td colspan=\"4\">No Subscription details added yet.</td></tr>";
                }
                ?>
    </tbody>
</table>
    <?php
    }

function rt_subscription_add() {
        global $wpdb;
        $table_name = $wpdb->prefix . "wpsa_subscriptions";
        if('add' == $_POST['rtsub_action']) {
            $categories = $_POST['post_category'];
            $categories = serialize($categories);
            $qry = "INSERT INTO {$table_name} (`name`, `description`, `amount1`, `duration1`, `duration_interval1`, `amount2`, `duration2`, `duration_interval2`, `amount3`, `duration3`, `duration_interval3`, `categories`) values('{$_POST['name']}', '{$_POST['description']}', '{$_POST['amount1']}', '{$_POST['duration1']}', '{$_POST['duration_interval1']}', '{$_POST['amount2']}', '{$_POST['duration2']}', '{$_POST['duration_interval2']}', '{$_POST['amount3']}', '{$_POST['duration3']}', '{$_POST['duration_interval3']}', '{$categories}')";
            $wpdb->query($qry);
            ?>
<div class="updated fade" id="message">
    <p><strong>Subscription added.</strong></p>
</div>
        <?php
        }
        ?>
<form name="subscription_form" id="subscription_form" class="omlist" method="post" action="<?php echo "?page=wp-sa-manage-subscription&wpu=add"; ?>" enctype="multipart/form-data">
    <table class="form-table" border="0">
        <tr>
            <td class="label" width="10%"><label id="lname" for="name">Name</label><br/><small><i>(required)</i></small></td>
            <td class="field" width="30%"><input id="name" name="name" type="text" value="" maxlength="200" /></td>
            <td class="status" width="60%"></td>
        </tr>
        <tr>
            <td class="label"><label id="ldescription" for="description">Description</label><br/><small><i>(optional)</i></small></td>
            <td class="field"><textarea id="description" name="description" cols="40" ></textarea> </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="lprice" for="amount1">Trial 1 Payment Amount</label><br/><small><i>(optional)</i></small></td>
            <td class="field"><input id="price" name="amount1" type="text" value="" maxlength="10" /></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration1">Trial 1 Subscription duration</label><br/><small><i>(optional)</i></small></td>
            <td class="field">
                <input id="duration" name="duration1" type="text" size="4" maxlength="4" value=""  />
                <select size="1" id="duration_interval1" name="duration_interval1">
                    <option value="D">Day(s)</option>
                    <option value="W">Week(s)</option>
                    <option value="M">Month(s)</option>
                    <option value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 1 Month(s)</span>
                <span class="description"></span>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="lprice" for="amount2">Trial 2 Payment Amount</label><br/><small><i>(optional)</i></small></td>
            <td class="field"><input id="price" name="amount2" type="text" value="" maxlength="10" /></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration2">Trial 2 Subscription duration</label><br/><small><i>(optional)</i></small></td>
            <td class="field">
                <input id="duration" name="duration2" type="text" size="4" maxlength="4" value=""  />
                <select size="1" id="duration_interval2" name="duration_interval2">
                    <option value="D">Day(s)</option>
                    <option value="W">Week(s)</option>
                    <option value="M">Month(s)</option>
                    <option value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 1 Month(s)</span>
                <span class="description"></span>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="lprice" for="amount3">Regular Payment Amount</label><br/><small><i>(required)</i></small></td>
            <td class="field"><input id="price" name="amount3" type="text" value="" maxlength="10" /></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration3">Regular Subscription duration</label><br/><small><i>(required)</i></small></td>
            <td class="field">
                <input id="duration" name="duration3" type="text" size="4" maxlength="4" value=""  />
                <select size="1" id="duration_interval3" name="duration_interval3">
                    <option value="D">Day(s)</option>
                    <option value="W">Week(s)</option>
                    <option value="M">Month(s)</option>
                    <option value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 1 Month(s)</span>
                <span class="description"></span>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
<tr>
            <td class="label"><label id="sub_cat" for="sub_cat">Select Categories</label></td>
            <td> <ul><?php wp_category_checklist(); ?></ul> </td>
        </tr>
    </table>

    <div class="submit">
        <input type="hidden" value="add" name="rtsub_action"/>
        <input type="submit" value="Save Subscription" name="submit"/>

    </div>
</form>
<h4>Allowable Values for Subscription duration</h4>
<table>
    <tr>
        <td>Unit</td>
        <td>Definition</td>
        <td>Acceptable Values</td>
    </tr>
    <tr>
        <td>Day</td>
        <td>24 hours</td>
        <td>1-90</td>
    </tr>
    <tr>
        <td>Week</td>
        <td>7 days</td>
        <td>1-52</td>
    </tr>
    <tr>
        <td>Month</td>
        <td>1 calendar month (same day of the month)</td>
        <td>1-24</td>
    </tr>
    <tr>
        <td>Year</td>
        <td>1 calendar year (same day of the year)</td>
        <td>1-5</td>
    </tr>
</table>
    <?php
    }

function rt_subscription_edit() {
        if(!isset ($_GET['item_id']))
            return;
        global $wpdb;
        $table_name = $wpdb->prefix . "wpsa_subscriptions";
        if('edit' == $_POST['rtsub_action']) {
            $categories = $_POST['post_category'];
            if ($categories) {
                $categories = serialize($categories);
            }
            $qry = "UPDATE {$table_name} SET `name`='{$_POST['name']}', `description`='{$_POST['description']}', `amount1`='{$_POST['amount1']}', `duration1`='{$_POST['duration1']}', `duration_interval1`='{$_POST['duration_interval1']}', `amount2`='{$_POST['amount2']}', `duration2`='{$_POST['duration2']}', `duration_interval2`='{$_POST['duration_interval2']}', `amount3`='{$_POST['amount3']}', `duration3`='{$_POST['duration3']}', `duration_interval3`='{$_POST['duration_interval3']}', `categories`='{$categories}' WHERE id={$_GET['item_id']}";
            $wpdb->query($qry);
            ?>
<div class="updated fade" id="message">
    <p><strong>Subscription updated.</strong></p>
</div>
        <?php
        }
        $qry = "SELECT * FROM $table_name WHERE id={$_GET['item_id']}";
        $result = $wpdb->get_results($qry);
        $cats = $result[0]->categories;
        $cats = unserialize($cats);
        ?>
<form name="subscription_form" id="subscription_form" class="omlist" method="post" action="<?php echo "?page=wp-sa-manage-subscription&wpu=edit&item_id={$result[0]->id}"; ?>" enctype="multipart/form-data">
    <table class="form-table" border="0">
        <tr>
            <td class="label" width="10%"><label id="lname" for="name">Name</label><br/><small><i>(required)</i></small></td>
            <td class="field" width="30%"><input id="name" name="name" type="text" value="<?php echo $result[0]->name; ?>" maxlength="200" /></td>
            <td class="status" width="60%"></td>
        </tr>
        <tr>
            <td class="label"><label id="ldescription" for="description">Description</label><br/><small><i>(optional)</i></small></td>
            <td class="field"><textarea id="description" name="description" cols="40" ><?php echo $result[0]->description; ?></textarea> </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="lprice" for="amount1">Trial 1 Payment Amount</label><br/><small><i>(optional)</i></small></td>
            <td class="field"><input id="amount" name="amount1" type="text" value="<?php echo $result[0]->amount1; ?>" maxlength="10" /></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration1">Trial 1 Subscription duration</label><br/><small><i>(optional)</i></small></td>
            <td class="field">
                <input id="duration" name="duration1" type="text" size="4" maxlength="4" value="<?php echo $result[0]->duration1; ?>"  />
                <select size="1" id="duration_interval" name="duration_interval1">
                    <option <?php echo ( ( $result[0]->duration_interval1 == 'D' ) ? 'selected' : '' ); ?> value="D">Day(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval1 == 'W' ) ? 'selected' : '' ); ?> value="W">Week(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval1 == 'M' ) ? 'selected' : '' ); ?> value="M">Month(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval1 == 'Y' ) ? 'selected' : '' ); ?> value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 1 Month(s)</span>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="lprice" for="amount2">Trial 2 Payment Amount</label><br/><small><i>(optional)</i></small></td>
            <td class="field"><input id="amount" name="amount2" type="text" value="<?php echo $result[0]->amount2; ?>" maxlength="10" /></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration2">Trial 2 Subscription duration</label><br/><small><i>(optional)</i></small></td>
            <td class="field">
                <input id="duration" name="duration2" type="text" size="4" maxlength="4" value="<?php echo $result[0]->duration2; ?>"  />
                <select size="1" id="duration_interval" name="duration_interval2">
                    <option <?php echo ( ( $result[0]->duration_interval2 == 'D' ) ? 'selected' : '' ); ?> value="D">Day(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval2 == 'W' ) ? 'selected' : '' ); ?> value="W">Week(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval2 == 'M' ) ? 'selected' : '' ); ?> value="M">Month(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval2 == 'Y' ) ? 'selected' : '' ); ?> value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 1 Month(s)</span>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="lprice" for="amount3">Regular Payment Amount</label><br/><small><i>(required)</i></small></td>
            <td class="field"><input id="amount" name="amount3" type="text" value="<?php echo $result[0]->amount3; ?>" maxlength="10" /></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration3">Regular Subscription duration</label><br/><small><i>(required)</i></small></td>
            <td class="field">
                <input id="duration" name="duration3" type="text" size="4" maxlength="4" value="<?php echo $result[0]->duration3; ?>"  />
                <select size="1" id="duration_interval" name="duration_interval3">
                    <option <?php echo ( ( $result[0]->duration_interval3 == 'D' ) ? 'selected' : '' ); ?> value="D">Day(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval3 == 'W' ) ? 'selected' : '' ); ?> value="W">Week(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval3 == 'M' ) ? 'selected' : '' ); ?> value="M">Month(s)</option>
                    <option <?php echo ( ( $result[0]->duration_interval3 == 'Y' ) ? 'selected' : '' ); ?> value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 1 Month(s)</span>
            </td>
            <td class="status"></td>
        </tr>
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
        <tr>
            <td class="label"><label id="sub_cat" for="sub_cat">Select Categories</label></td>
            <td> <?php wp_category_checklist(0,0,$cats); ?> </td>
        </tr>
    </table>
    <div class="submit">
        <input type="hidden" value="edit" name="rtsub_action"/>
        <input type="submit" value="update" name="submit"/>
    </div>
</form>
<h4>Allowable Values for Subscription duration</h4>
<table>
    <tr>
        <td>Unit</td>
        <td>Definition</td>
        <td>Acceptable Values</td>
    </tr>
    <tr>
        <td>Day</td>
        <td>24 hours</td>
        <td>1-90</td>
    </tr>
    <tr>
        <td>Week</td>
        <td>7 days</td>
        <td>1-52</td>
    </tr>
    <tr>
        <td>Month</td>
        <td>1 calendar month (same day of the month)</td>
        <td>1-24</td>
    </tr>
    <tr>
        <td>Year</td>
        <td>1 calendar year (same day of the year)</td>
        <td>1-5</td>
    </tr>
</table>
    <?php
    }

function rt_subscription_delete() {
        if(!isset ($_GET['item_id']))
            return ;
        global $wpdb;
        $table_name = $wpdb->prefix . "wpsa_subscriptions";
        $qry = "DELETE FROM $table_name WHERE id={$_GET['item_id']}";
        $wpdb->query($qry);
        ?>
<div class="updated fade" id="message">
    <p><strong>Subscription deleted.</strong></p>
</div>
<?php
rt_subscription_list();
rt_sa_display_help_for_subscription_list();
}

/*    function rt_subscription_trial() {

        if('save_trial' == $_POST['rtsub_action']) {

            update_option('rt_sa_trial_amount', $_POST['amount']);
            update_option('rt_sa_trial_duration', $_POST['duration']);
            update_option('rt_sa_trial_duration_interval', $_POST['duration_interval']);
            ?>
<div class="updated fade" id="message">
    <p><strong>Settings updated.</strong></p>
</div>

        <?php
        }
        $rt_sa_trial_amount = get_option('rt_sa_trial_amount');
        $rt_sa_trial_duration = get_option('rt_sa_trial_duration');
        $rt_sa_trial_duration_interval = get_option('rt_sa_trial_duration_interval');
        ?>
<form name="subscription_trial_form" id="subscription_trial_form" class="omlist" method="post" action="<?php echo "?page=wp-sa-manage-subscription&wpu=trial"; ?>" enctype="multipart/form-data">
    <table class="form-table" border="0">
        <tr>
            <td class="label"><label id="lprice" for="amount">Payment Amount</label><br/><small><i>(required)</i></small></td>
            <td class="field"><input id="price" name="amount" type="text" value="<?php echo $rt_sa_trial_amount; ?>" maxlength="10" /><br />
                <span class="description">Enter 0 to keep trial period free</span></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lduration" for="duration">Subscription duration</label><br/><small><i>(required)</i></small></td>
            <td class="field">
                <input id="duration" name="duration" type="text" size="4" maxlength="4" value="<?php echo $rt_sa_trial_duration?>"  />
                <select size="1" id="duration_interval" name="duration_interval">
                    <option <?php echo ( ( $rt_sa_trial_duration_interval == 'D' ) ? 'selected' : '' ); ?> value="D">Day(s)</option>
                    <option <?php echo ( ( $rt_sa_trial_duration_interval == 'W' ) ? 'selected' : '' ); ?> value="W">Week(s)</option>
                    <option <?php echo ( ( $rt_sa_trial_duration_interval == 'M' ) ? 'selected' : '' ); ?> value="M">Month(s)</option>
                    <option <?php echo ( ( $rt_sa_trial_duration_interval == 'Y' ) ? 'selected' : '' ); ?> value="Y">Year(s)</option>
                </select>
                <span class="description">The period between payments.<br/>Example: 2 Week(s)</span>
                <span class="description">radhe: here paypal provieds some spesific conditions for subscribtions, we neeed to check it as per geven in the book. </span>
            </td>
            <td class="status"></td>
        </tr>
    </table>

    <div class="submit">
        <input type="hidden" value="save_trial" name="rtsub_action"/>
        <input type="submit" value="Save Subscription" name="submit"/>

    </div>
</form>

    <?php
    }*/

?>