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

function rt_products_product_page() {
    global $wpulang, $current_user;
    echo "<div class=\"wrap\"><h2>".'Products'."</h2>";
    $dir_flag = true;

    if (!is_dir(RT_UPLOAD_DIR)) {        
        $dir_flag = @ mkdir(RT_UPLOAD_DIR , 0755, true);
        @ chmod(RT_UPLOAD_DIR, 0755);
    }elseif(is_dir(RT_UPLOAD_DIR)) {        
        $dir_flag = @ copy(__file__, RT_UPLOAD_DIR.basename(__file__));
        unlink(RT_UPLOAD_DIR.basename(__file__));
    }

    if (!$dir_flag) {
            echo '<div class="error"><p><h3>ERROR: File-system permission error!</h3></p>
                <p>Please make your download directory is writeable. If you are unsure please contact your host support to confirm what chmod permissions are required to make a directory writeable.</p></div>';
    }

    ?>

<p class="">
    <a href="?page=wp-sa-salesautomator&wpu=list" class="row-title">List Product</a> |
    <a href="?page=wp-sa-salesautomator&wpu=add" class="row-title">Add Product</a>
</p>
    <?php
    $wpu = $_GET['wpu'];
    switch($wpu) {
        case 'list':
            rt_products_list();
            break;
        case 'add':
            rt_products_add();
            break;
        case 'edit':
            rt_products_edit();
            break;
        case 'view':
            rt_products_view();
            break;
        case 'save':
            rt_products_save();
            break;
        case 'delete':
            rt_products_delete();
            break;
        default:
            rt_products_list();
            break;
    }
    echo "</div>";
}

function rt_products_list() {
    global $wpdb;
    $sql = "SELECT * FROM ".$wpdb->prefix."wpsa_product_details";
    $rows = $wpdb->get_results($sql);
    if(isset($_GET['msg'])) {
        echo '<div class="updated"><p><strong>'.$_GET['msg'].'</strong></p></div>';
    }
    ?>
<table class="widefat post fixed" id="messagelist">
    <thead>
        <tr class="tablemenu">
            <th width="18%">Name</th>
            <th width="55%">Description</th>
            <th width="5%">Price</th>
            <th width="5%">Edit</th>
            <th width="5%">Delete</th>
            <th width="12%">Short Code</th>
        </tr>
    </thead>
        <?php
        foreach($rows as $row) {
            ?>
    <tr class="read">
        <td><a title="View this product" href="?page=wp-sa-salesautomator&amp;wpu=edit&amp;pid=<?php echo $row->id;?>"><?php echo $row->name;?></a></td>
        <td><a title="View this product" href="?page=wp-sa-salesautomator&amp;wpu=edit&amp;pid=<?php echo $row->id;?>"><?php echo substr($row->description,0,100);?></a></td>
        <td>$<?php echo $row->price;?></td>
        <td><a href="?page=wp-sa-salesautomator&amp;wpu=edit&amp;pid=<?php echo $row->id;?>"><img title="Edit!" alt="Edit!" src="<?php echo get_settings('siteurl');?>/wp-content/plugins/<?php echo dirname(dirname(plugin_basename(__FILE__)));?>/img/icons/Edit.png" width="16px"/></a></td>
        <td><a href="?page=wp-sa-salesautomator&amp;wpu=delete&amp;pid=<?php echo $row->id;?>"><img title="Delete!" alt="Delete" src="<?php echo get_settings('siteurl');?>/wp-content/plugins/<?php echo dirname(dirname(plugin_basename(__FILE__)));?>/img/icons/delete.png" onclick="return confirm('Do you really want to delete it?');"/></a></td>
        <td><?php echo '[product_listing pid="'. $row->id.'"]';?></td>
    </tr>
            <?php
        }
        ?>
</table>

<p>&nbsp;</p>
<h2>Usage Instruction</h2>
<h3>Showing "Buy Now" Button</h3>
<p>Copy-paste codes given in "Short Code" column above to display that products details & paypal buy button on you check-out pages.</p>
<h3>Showing Reset Download Link form </h3>
<p>Paste <code>[reset_download_link]</code> short tag in any post/page to show reset download link 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>
    <?php
}

function rt_products_add() {
    global $wpdb;
    $limit_size=(int)ini_get('upload_max_filesize');
    if(isset($_POST['name'])) {
        $sql_insert_file_val = "";
        $file_upload_floag = false;
        $maxuplaodsize=false;
        if(isset($_FILES['product_file']) ) {            
            $file_upload_floag = true;
            $total_files = count($_FILES['product_file']['name']);
            for ($file_i=0;$file_i < $total_files; $file_i++) {
                if($_FILES['product_file']['name'][$file_i]!=''){
                    $uploadfile = RT_UPLOAD_DIR .$_POST['pid'].'_'.basename($_FILES['product_file']['name'][$file_i]);
                    $file_size=$_FILES['product_file']['size'][$file_i];
                    $limit_size_in_bytes = $limit_size*1024*1024;
                    if($file_size >= $limit_size_in_bytes) {
                        $maxuplaodsize=true;
                    }
                    else if (move_uploaded_file($_FILES['product_file']['tmp_name'][$file_i], $uploadfile) && $_REQUEST['select_type']=='1') {
                        echo '<div class="updated"><p><strong>File '.$_FILES['product_file']['name'][$file_i].' is valid, and was successfully uploaded.</strong></p></div>';
                    }
                    else if($_REQUEST['rad_product_file'] && $_REQUEST['select_type']=='2' ) {
                        $file_name .= implode(',',$_REQUEST['rad_product_file']);
                        ?><div class="updated"><p><strong>Product Added successfully.</strong></p></div><?php
                    }
                    else {
//                        echo '<div class="error"><p>Please select file type OR Inside your wordpress root directory find <strong>wp-content >> plungins >> wp-products >> upload</strong> directory.<br/> Please check the access permission of "upload" directory in "wp-product" directory and make it writeable, check with your host if unsure in most cases it\'s 755 or 777.<br /></p>
//                            <p><strong>If you have shell access, then run following commands. </strong><br /> mkdir '.RT_UPLOAD_DIR.' <br /> chmod -R 0777 '.RT_UPLOAD_DIR.'</p></div>';
                        echo '<div class="error"><p><h3>ERROR: File-system permission error!</h3></p>
                                <p>Please make your download directory is writeable. If you are unsure please contact your host support to confirm what chmod permissions are required to make a directory writeable.</p></div>';
                        $file_upload_floag = false;
                    }
                }
            }
        }
        if ($file_upload_floag != false) {
            $rt_message_template = array();
            $rt_message_template['from'] = $_POST['rt_from'];
            $rt_message_template['fromname'] = $_POST['rt_fromname'];
            $rt_message_template['subject'] = $_POST['rt_subject1'];
            $rt_message_template['msg'] = $_POST['rt_msg1'];
            $rt_message_template['ty_msg'] = $_POST['rt_ty_msg1'];
            $rt_message_template = serialize($rt_message_template);
            $sql = "INSERT INTO ".$wpdb->prefix."wpsa_product_details (name ,description ,price ,message_template ) VALUES ( '".$_POST['name']."', '".$_POST['description']."', '".$_POST['price']."', '".$rt_message_template."'  )";
            $wpdb->query($sql);
            if($_REQUEST['select_type']=='1') {
                $file_name = array();
                foreach ($_FILES['product_file']['name'] as $ufname) {
                    $file_name[] = $wpdb->insert_id.'_'.$ufname;
                }
                $file_name = implode(',', $file_name);
            }
            else if($_REQUEST['select_type']=='2') { 
                $file_name = implode(',', $_REQUEST['rad_product_file']);
                echo '<div class="updated"><p><strong>Product Added successfully.</strong></p></div>';
            }
            $sql = "UPDATE ".$wpdb->prefix."wpsa_product_details SET file_name = '{$file_name}' WHERE id = '$wpdb->insert_id'";
            $wpdb->query($sql);
            if($_REQUEST['select_type']=='1') {
                foreach ($_FILES['product_file']['name'] as $uufile) {
                    rename(RT_UPLOAD_DIR.'_'.$uufile, RT_UPLOAD_DIR.$wpdb->insert_id.'_'.$uufile);
                }
            }
        }
    }
    $rt_option = get_option('rt_option_products');
//    print_r($rt_option);
    ?>

<form name="product_form" id="product_form" class="omlist" method="post" enctype="multipart/form-data" action="<?php echo "?page=wp-sa-salesautomator&wpu=add"; ?>" enctype="multipart/form-data">
    <table width="100%" 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 class="label"><label id="lprice" for="price">Price (USD)</label><br/><small><i>(required)</i></small></td>
            <td class="field"><input id="price" name="price" type="text" value="" maxlength="10" /></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"><input type="radio" name="select_type" id="select_type" value="1"><label id="lproduct_file" for="product_file">Upload Product File</label><br/><small><i>(required)</i></small></td>
            <td class="field">
                <div class=" row_block">
                    <input id="product_file" name="product_file[]" type="file" value="" />
                    <a href="#" onClick="return selrem(this);" class="remove_row"><img src="<?php echo WP_PLUGIN_URL.'/'.dirname(dirname(plugin_basename(__FILE__)));
    ; ?>/img/removeBtn.gif"/></a>&nbsp;
                    <a href="#" onClick="return seladd(this);" class="add_row"><img src="<?php echo WP_PLUGIN_URL.'/'.dirname(dirname(plugin_basename(__FILE__)));
    ; ?>/img/addBtn.gif"/></a>
                </div><br />
                <?php  echo "<b>(Maximum upload limit ".$limit_size." MB)</b>"; ?>
            </td>
            <td class="status"><?php // echo "<b>(Maximum upload limit ".$limit_size." MB)</b>"; ?></td>
        </tr><?php  ?>
        <tr>
            <td class="label"><h3>OR</h3></td>
            <td class="field"><small><div class="size_error">For files larger than <?php echo $limit_size;?>MB, upload via FTP, to path:<br />
                <b><?php echo RT_UPLOAD_DIR; ?> </b><br />and select from the list below.</div></small></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label" valign="top"><input type="radio" name="select_type" id="select_type" value="2">Select File</td>
            <td class="field" valign="top">
                <table class="select_product_image_list">
                        <?php
                        if ($handle = opendir(RT_UPLOAD_DIR)) {
                            $i=1;
                            while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && !is_dir($file) && !strstr( $file,'htaccess' )) {
                ?>
                    <tr>
                        <td valign="top">
                            <input type="checkbox" id="rad_product_file" name="rad_product_file[]" value="<?php echo $file;  ?>" /><?php echo $file; ?>
                        </td>
                    </tr>
                                    <?php } $i

                                        ++;
        }
        closedir($handle);
    } ?>
                </table>
            </td>
                            <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
        </tr>
    </table>
    <h5><?php _e('Customize Download Link Email Notification', 'wp-products');?></h5>
    <table class="form-table">
        <tbody>
            <tr valign="top">
                <th scope="row"><label for="from"><?php _e('From Email Address', 'wp-products');?></label></th>
                <td><input type="text" name="rt_from" id="from" style="width:250px;" value="<?php echo $rt_option['from'];?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="fromname"><?php _e('From Name', 'wp-products');?></label></th>
                <td><input type="text" name="rt_fromname" id="fromname" style="width:250px;" value="<?php echo $rt_option['fromname'];?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="subject"><?php _e('Subject <br /> [for download link]', 'wp-products');?></label></th>
                <td><input type="text" name="rt_subject1" id="subject1" style="width:350px;" value="<?php echo $rt_option['subject1'];?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="msg1"><?php _e('Message Template <br /> [for download link only]', 'wp-products');?></label></th>
                <td>
                    <p><strong><?php _e('Replacement Keys', 'wp-products');?>:</strong><br />%user_email% | %blogname% | %product_name%  | %download_link% | %description% <br />%price%  |  %first_name%  |  %last_name%  |   %register_link%</p>
                    <textarea name="rt_msg1" id="msg1" style="width:80%;height:200px;"><?php echo $rt_option['msg1'];?></textarea>					</td>
            </tr>
        </tbody>
    </table>
    <h5><?php _e('Customize Thank You Message', 'wp-products');?></h5>
    <table class="form-table">
        <tr valign="top">
            <th scope="row"><label for="tymsg1"><?php _e('Message Template <br /> [for Thank You Page only]', 'wp-products');?></label></th>
            <td>
                <p><strong><?php _e('Replacement Keys', 'wp-products');?>:</strong><br />%user_email% | %blogname% | %product_name%  | %download_link% | %description%  <br />%price%  |  %first_name%  |  %last_name%  |   %register_link%</p>
                <textarea name="rt_ty_msg1" id="tymsg1" style="width:80%;height:200px;"><?php echo $rt_option['msg1'];?></textarea>					</td>
        </tr>
    </table>
    <div class="submit"><input type="submit" value="save" name="submit"/></div>

</form>
    <?php

}

function rt_products_edit() {
    global $wpdb;
    $limit_size=(int)ini_get('upload_max_filesize');
    if(isset($_POST['name'])) {
        $sql_update_file = "";
        $maxuplaodsize = false;
        $rt_message_template = array();
        $rt_message_template['from'] = $_POST['rt_from'];
        $rt_message_template['fromname'] = $_POST['rt_fromname'];
        $rt_message_template['subject'] = $_POST['rt_subject1'];
        $rt_message_template['msg'] = $_POST['rt_msg1'];
        $rt_message_template['ty_msg'] = $_POST['rt_ty_msg1'];
        $rt_message_template = serialize($rt_message_template);

        $no_file_error = true;
        
        if (isset($_FILES['product_file']['error'])) {
            foreach($_FILES['product_file']['error'] as $file_error_log) {
                if ($file_error_log) {
                    $no_file_error = false;
                }
            }
        }

        if(isset($_FILES['product_file']) && $no_file_error && $_REQUEST['select_type']=='1') {
            $total_files = count($_FILES['product_file']['name']);
            $ufiles = array();
            for ($file_i=0;$file_i < $total_files; $file_i++) {
                $uploadfile = RT_UPLOAD_DIR.$_POST['pid'].'_'. basename($_FILES['product_file']['name'][$file_i]);
                $sql_insert_file_val = basename($uploadfile);
                $file_size=$_FILES['product_file']['size'][$file_i];
                $limit_size_in_bytes = $limit_size*1024*1024;
                if($file_size >=  $limit_size_in_bytes) {
                    $maxuplaodsize=true;
                }else {
                    @move_uploaded_file($_FILES['product_file']['tmp_name'][$file_i], $uploadfile);
                    $ufiles[] = $sql_insert_file_val;

//                    $sql = "UPDATE ".$wpdb->prefix."wpsa_product_details SET name = '".$_POST['name']."', description = '".$_POST['description']."', price = '".$_POST['price']."', file_name = '".$sql_insert_file_val."', message_template = '".$rt_message_template."' WHERE id =". $_POST['pid'];
//
//                    $wpdb->query($sql);
                }
            }
            $ufiles = implode(',',$ufiles);
            $sql = "UPDATE ".$wpdb->prefix."wpsa_product_details SET name = '".$_POST['name']."', description = '".$_POST['description']."', price = '".$_POST['price']."', file_name = '".$ufiles."', message_template = '".$rt_message_template."' WHERE id =". $_POST['pid'];
            $wpdb->query($sql);
        }
        else if($_REQUEST['rad_product_file'] && $_REQUEST['select_type']=='2' ) {
            $file_name = implode(',',$_POST['rad_product_file']);
            $sql = "UPDATE ".$wpdb->prefix."wpsa_product_details SET name = '".$_POST['name']."', description = '".$_POST['description']."', price = '".$_POST['price']."', file_name = '".$file_name."', message_template = '".$rt_message_template."' WHERE id =". $_POST['pid'];
            $wpdb->query($sql);
        }else {
            $sql = "UPDATE ".$wpdb->prefix."wpsa_product_details SET name = '".$_POST['name']."', description = '".$_POST['description']."', price = '".$_POST['price']."', message_template = '".$rt_message_template."' WHERE id =". $_POST['pid'];
            $wpdb->query($sql);
        }
        echo '<div class="updated"><p><strong>Product edited successfully.</strong></p></div>';
        $_GET['pid'] = $_POST['pid'];
    }
    $sql = "SELECT * FROM ".$wpdb->prefix."wpsa_product_details WHERE id = ".$_GET['pid'];
    $row = $wpdb->get_row($sql);
    $rt_option = $row->message_template;
    $rt_option = unserialize($rt_option);
    $existing_file_names = explode(',', $row->file_name);
    ?>

<form name="product_form" id="product_form_edit" class="omlist" method="post" action="<?php echo "?page=wp-sa-salesautomator&wpu=edit"; ?>" enctype="multipart/form-data">
    <table class="form-table" border="0">
        <tr>
            <td class="label" width="20%"><label id="lname" for="name">Name</label></td>
            <td class="field" width="30%"><input id="name" name="name" type="text" value="<?php echo $row->name;?>" maxlength="200" /></td>
            <td class="status" width="50%"></td>
        </tr>
        <tr>
            <td class="label"><label id="ldescription" for="description">Description</label></td>
            <td class="field"><textarea id="description" name="description" cols="20" rows="5" ><?php echo $row->description;?></textarea> </td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label"><label id="lprice" for="price">Price (USD)</label></td>
            <td class="field"><input id="price" name="price" type="text" value="<?php echo $row->price;?>" maxlength="10" /></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"><input type="radio" name="select_type" id="select_type" value="1"><label id="lproduct_file" for="product_file">Upload Product File</label></td>
            <td class="field">
                <div class=" row_block">
                    <input id="product_file" name="product_file[]" type="file" value="" />
                    <a href="#" onClick="return selrem(this);" class="remove_row">[-]</a>&nbsp;
                    <a href="#" onClick="return seladd(this);" class="add_row">[+]</a>
                </div>
                <p><b>Old version:</b></p>
                <ol>
                        <?php
    foreach ($existing_file_names as $existing_file_name) {
        echo '<li>'.$existing_file_name.'</li>';
    }?>
                </ol>
            </td>
            <td class="status"><?php // echo "<b>(Maximum upload limit ".$limit_size." MB)</b>"; ?></td>
        </tr>
        <tr>
            <td  colspan="3"></td>
        </tr>
        <tr>
            <td class="label"><h3>OR</h3></td>
            <td class="field"><small><div class="size_error">For files(product) larger than <?php echo $limit_size;?>MB, upload file FTP, to path:<br /><b><?php echo RT_UPLOAD_DIR; ?></b><br />and select from the list.</div></small></td>
            <td class="status"></td>
        </tr>
        <tr>
            <td class="label" valign="top"><input type="radio" name="select_type" id="select_type" value="2">Select File</td>
            <td class="field" valign="top">
                <table>
                        <?php
                        if ($handle = opendir(RT_UPLOAD_DIR)) {
                            $i=1;
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && !is_dir($file) && !strstr( $file,'htaccess' )) {
                ?>
                    <tr>
                        <td valign="top">
                            <input type="checkbox" id="rad_product_file" name="rad_product_file[]" value="<?php echo $file;  ?>" /><?php echo $file;  ?>
                        </td>
                    </tr>
                                    <?php } $i

                    ++;
        }
        closedir($handle);
    } ?>
                </table>
            </td>
        </tr>
        <input type="hidden" name="action" value="<?php echo $_GET['wpu'];?>">
        <input type="hidden" name="pid" value="<?php echo $_GET['pid'];?>">
        <tr>
          <td height="3" colspan="2" valign="top"><hr size="1" /></td>
          <td></td>
  </tr>
    </table>
    <h5><?php _e('Customize Download Link Email Notification', 'wp-products');?></h5>
    <table class="form-table">
        <tbody>
            <tr valign="top">
                <th scope="row"><label for="from"><?php _e('From Email Address', 'wp-products');?></label></th>
                <td><input type="text" name="rt_from" id="from" value="<?php echo $rt_option['from'];?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="fromname"><?php _e('From Name', 'wp-products');?></label></th>
                <td><input type="text" name="rt_fromname" id="fromname" value="<?php echo $rt_option['fromname'];?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="subject"><?php _e('Subject <br /> [for download link]', 'wp-products');?></label></th>
                <td><input type="text" name="rt_subject1" id="subject1" value="<?php echo $rt_option['subject'];?>" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="msg1"><?php _e('Message Template <br /> [for download link only]', 'wp-products');?></label></th>
                <td>
                    <p><strong><?php _e('Replacement Keys', 'wp-products');?>:</strong><br />%user_email% | %blogname% | %product_name%  | %download_link% | %description%  <br />%price%  |  %first_name%  |  %last_name%  |  %register_link</p>
                    <textarea name="rt_msg1" id="msg1" cols="30" rows="8"><?php echo $rt_option['msg'];?></textarea>					</td>
            </tr>
        </tbody>
    </table>
    <h5><?php _e('Customize Thank You Message', 'wp-products');?></h5>
    <table class="form-table">
        <tr valign="top">
            <th scope="row"><label for="tymsg1"><?php _e('Message Template <br /> [for Thank You Page only]', 'wp-products');?></label></th>
            <td>
                <p><strong><?php _e('Replacement Keys', 'wp-products');?>:</strong><br />%user_email% | %blogname% | %product_name%  | %download_link% | %description%  <br />%price%  |  %first_name%  |  %last_name%  |  %register_link</p>
                <textarea name="rt_ty_msg1" id="tymsg1" cols="30" rows="8"><?php echo $rt_option['ty_msg'];?></textarea>					</td>
        </tr>
    </table>
    <div class="submit"><input type="submit" value="save" name="submit"/></div>

</form>
    <?php



}

function rt_products_delete() {
    global $wpdb;
    $sql = "SELECT file_name FROM ".$wpdb->prefix."wpsa_product_details WHERE id = ".$_GET['pid'];
    $row = $wpdb->get_row($sql);
    @unlink(RT_UPLOAD_DIR.$row->file_name);
    $sql = "DELETE FROM ".$wpdb->prefix."wpsa_product_details where id = ".$_GET['pid'];
    $wpdb->query($sql);
    echo '<script type="text/javascript" language="javascript"> location.href = "?page=wp-sa-salesautomator&wpu=list&msg=Product Removed Successfully!";</script>';

}

function rt_products_view() {
    global $wpdb;
    $sql = "SELECT * FROM ".$wpdb->prefix."wpsa_product_details WHERE id = ".$_GET['pid'];
    $row = $wpdb->get_row($sql);
    ?>

<table class="form-table">
    <tr valign="top">
        <th scope="row"><b>Name</b></th>
        <td><?php echo $row->name;?></td>
    </tr>
    <tr valign="top">
        <th scope="row"><b>Description</b></th>
        <td><?php echo $row->description;?></td>
    </tr>
    <tr valign="top">
        <th scope="row"><b>Price</b></th>
        <td>$<?php echo $row->price;?></td>
    </tr>
    <tr valign="top">
        <th scope="row"><b>Upload Code: </b></th>
        <td><a href="<?php echo RT_UPLOAD_URL.$row->file_name;?>"><?php echo $row->file_name;?></a></td>
    </tr>
    <input type="hidden" name="action" value="<?php echo $_GET['wpu'];?>">
    <input type="hidden" name="pid" value="<?php echo $_GET['pid'];?>">

</table>
    <?php

}

add_shortcode('product_listing', 'rt_products_product_list');

function rt_products_product_list($atts) {
    global $wpdb, $buyNowImage;
    $wp_sa_setting = get_option( 'wp_sa_setting' );
    if($wp_sa_setting['wp_sa_product_button_image']!='')
        $buy_now_image = $wp_sa_setting['wp_sa_product_button_image'];
    else
        $buy_now_image = $buyNowImage;

    if(isset ($_GET['tx']) && $_GET['st'] == 'Completed') {
        echo str_replace( "\'", "'",$wp_sa_setting['sa_thankyou_msg']);
        return ;
    }
    else if(isset($_GET['tx']) && strcmp($_GET['st'],'Completed')!=0) {
        echo 'Transaction Unsuccessful please try again';
        return ;
    }
    ob_start();
    $sql = "SELECT * FROM ".$wpdb->prefix."wpsa_product_details where id IN({$atts['pid']})";
    $results = $wpdb->get_results($sql);
    ?>         
<div id="product_entry">
    <a name="emptyCart" id="emptyCart"></a>
    <table id='product_list' width="100%">
        <tr>
            <td width="5%"><img title="Select Option" alt="Select Option" src="<?php echo get_settings('siteurl');?>/wp-content/plugins/<?php echo dirname(dirname(plugin_basename(__FILE__)));?>/img/select_product.gif"/></td>
            <td align="left" width="30%"><strong>Product</strong></td>
            <td align="left" width="50%"><strong>Description</strong></td>
            <td align="center" width="20%"><strong>Price</strong></td>
            <?php do_action('wpsa_product_list_head') ; ?>
        </tr>
        <tbody>
    <?php
    foreach ($results as $result) {
        ?>
            <tr id="item_<?php echo $result->id;?>">
                <td><input type="checkbox" name="<?php echo $result->name;?>" value="<?php echo $result->id;?>"></td>
                <td align="left"><div class="pname">
                        <span class="item_name"><?php echo $result->name;?></span>
                    </div></td>
                <td align="left">
                    <span class="pdescription"><?php echo $result->description;?></span>
                </td>
                <td><span class="item_price"><?php echo '$'.$result->price;?></span>
                    <input type="hidden" name="pid" value="<?php echo $result->id;?>">
                </td>
                <?php do_action('wpsa_product_list_col',$result->id) ; ?>
    
            </tr>
                    <?php
                }
                echo "</table>";
                echo "<br><div align='center'><p id='total_amount'>Total Price: <b>$0</b></p>";
                echo '<p class="buybut"><input id="submit_paypal" type="image" name="submit" border="0" src="'.$buy_now_image.'" alt="PayPal - The safer, easier way to pay online"/></p></div>';
                echo '</div>';

                do_action('wpsa_product_list_end') ;
                return ob_get_clean();
            }

add_shortcode('reset_download_link', 'rt_reset_download_link');

function rt_reset_download_link($atts) {
                global $wpdb;
                ob_start();
                if(isset($_POST['retrive-button'])) {
                    $qry = "SELECT * FROM ".$wpdb->prefix."wpsa_product_transaction WHERE product_id =".$_POST['product']." AND customer_email = '".$_POST['email']."' AND status = 'active'" ;
                    $row = $wpdb->get_row($qry);
                    if( $row->customer_email) {
                        rt_sa_send_download_link($row->product_id ,array('payer_email' => $row->customer_email), $resrt_flag = true);
                        echo '<br/><div style="font-family:Arial; font-size: 14px">An email has been sent to you with the download link!</div>';
                    }else {
                        echo '<br/><div style="font-family:Arial; font-size: 14px">No matching record found for email provided.</div>';
                    }
    }else {
        $sql = "SELECT id, name FROM ".$wpdb->prefix."wpsa_product_details ";
        $rows = $wpdb->get_results($sql);
        ?>
        <form name="wp-retrive" id="wp-retrive" action="" method="post">
            <table style="font-family:Arial; font-size: 14px" border="0" width="100%">
                <tr>
                    <td width="30%" align="left"><label id="lproduct" for="product">Download Reset:</label></td>
                    <td width="30%" class="field" >
                        <select id="product" name="product">
                            <option value="select" selected="selected">Select Product</option>
                                    <?php
                                    foreach($rows as $row) {
                                        ?>
                            <option value="<?php echo $row->id;?>"><?php echo $row->name;?></option>
            <?php
        }
        ?>
                        </select>
                    </td>
                    <td class="status"></td>
                </tr>
            </table>
            <table id="domain_block">
            </table>
            <p><div style="font-family:Arial; font-size: 12px"><strong>Note:</strong> Please select the product you want to reset and<br />
                then enter your paypal email.</div></p>
        </form>
                <?php
    }
    return ob_get_clean();
}
?>