function printCustomField($field, $label = NULL, $object = NULL)
{
    $detail = NULL;
    $text = getCustomField($field, $object, $detail);
    if (is_null($label)) {
        $label = $detail['desc'] . ': ';
    }
    if (!empty($text)) {
        echo html_encodeTagged($label . $text);
    }
}
function _wpr_subscriber_profile($subscriber)
{
    global $wpdb;
    $sid = $subscriber->id;
    if (isset($_POST['followupunsub'])) {
        $aid = (int) $_POST['aid'];
        $query = "DELETE FROM " . $wpdb->prefix . "wpr_followup_subscriptions where id={$aid}";
        $wpdb->query($query);
        ?>
		<script>window.location='admin.php?page=wpresponder/subscribers.php&action=profile&sid=<?php 
        echo $subscriber->id;
        ?>
';</script>
        <?php 
        exit;
    }
    if (isset($_POST['customfielddata'])) {
        //Asume that all the custom fields are in the post data.
        $nid = $_POST['custom_field_newsletter'];
        $query = "SELECT * FROM " . $wpdb->prefix . "wpr_custom_fields where nid = {$nid};";
        $results = $wpdb->get_results($query);
        $theSubscriberId = $_POST['custom_field_sid'];
        $formData = array();
        foreach ($_POST as $name => $value) {
            $formData[trim($name)] = trim($value);
        }
        foreach ($results as $cfield) {
            $fieldName = trim('newsletter-' . $nid . '-cfield-' . $cfield->id);
            $value = $_POST[$fieldName];
            $cid = $cfield->id;
            $query = "DELETE FROM " . $wpdb->prefix . "wpr_custom_fields_values where sid = {$theSubscriberId} and cid={$cid};";
            $wpdb->query($query);
            if (empty($value)) {
                continue;
            }
            $query = "INSERT INTO " . $wpdb->prefix . "wpr_custom_fields_values (nid,sid,cid,value) VALUES ('{$nid}','{$theSubscriberId}','{$cid}','{$value}')";
            $wpdb->query($query);
            $wpdb->print_error();
        }
        ?>
			<script> window.location='admin.php?page=wpresponder/subscribers.php&action=profile&sid=<?php 
        echo $sid;
        ?>
';
			</script>
			<?php 
        exit;
    }
    if (isset($_POST['unsubscription_form'])) {
        $sid = $_POST['sid'];
        $query = "UPDATE " . $wpdb->prefix . "wpr_subscribers set active=0 where id={$sid}";
        $wpdb->query($query);
        $query = "DELETE FROM " . $wpdb->prefix . "wpr_followup_subscriptions where sid={$sid}";
        $wpdb->query($query);
        $query = "DELETE FROM " . $wpdb->prefix . "wpr_custom_fields_values where sid={$sid}";
        $wpdb->query($query);
    }
    if (isset($_POST['subs_action'])) {
        switch ($_POST['subs_action']) {
            case 'delete':
                $sid = $_POST['sid'];
                $subscriber = _wpr_subscriber_get($sid);
                $theEmail = $subscriber->email;
                $query = "SELECT id from " . $wpdb->prefix . "wpr_subscribers where email='{$theEmail}';";
                $subscriptions = $wpdb->get_results($query);
                foreach ($subscriptions as $theSubscription) {
                    $currentSid = $theSubscription->id;
                    $deleteBlogSubscriptions = "DELETE FROM " . $wpdb->prefix . "wpr_blog_subscription where sid={$currentSid}";
                    $wpdb->query($deleteBlogSubscriptions);
                    $deleteFollowupSubscriptions = "DELETE FROM " . $wpdb->prefix . "wpr_followup_subscriptions where sid={$currentSid}";
                    $wpdb->query($deleteFollowupSubscriptions);
                    $deleteCustomFieldValues = "DELETE FROM " . $wpdb->prefix . "wpr_custom_field_values where sid={$currentSid}";
                    $wpdb->query($deleteCustomFieldValues);
                    $deleteSubscriber = "DELETE FROM " . $wpdb->prefix . "wpr_subscribers where id={$currentSid}";
                    $wpdb->query($deleteSubscriber);
                }
                ?>
<script> window.location='admin.php?page=wpresponder/subscribers.php';</script><?php 
                return;
                break;
            case 'unsubscribe':
                $newsletters = $_POST['newsletters'];
                foreach ($newsletters as $newsletter) {
                    $query = "update " . $wpdb->prefix . "wpr_subscribers set active=0 where nid=" . $newsletter . " and email='" . $subscriber->email . "'";
                    $wpdb->query($query);
                }
                ?>
            <script>window.history.go(-2);</script>
            <?php 
                return;
                break;
        }
    }
    ?>
<div class="wrap"><h2>Profile</h2></div>
<table>
  <tr>
    <td width="300">Name: </td>
    <td><?php 
    $query = "select DISTINCT name from " . $wpdb->prefix . "wpr_subscribers where email='" . $subscriber->email . "' order by active desc";
    $results = $wpdb->get_results($query);
    $names = array();
    foreach ($results as $name) {
        array_push($names, $name->name);
    }
    $theName = implode(", ", $names);
    echo $theName;
    ?>
                  </td>
                  </tr>
                  <tr>
                    <td>E-Mail Address: </td>
                    <td><?php 
    echo $subscriber->email;
    ?>
                    </td>
                    </tr>
                    </table>
                    <p></p>
                    <form action="<?php 
    echo $_SERVER['REQUEST_URI'];
    ?>
" method="post">
<input type="hidden" name="subs_action" value="delete" />
<input type="hidden" name="sid" value="<?php 
    echo $subscriber->id;
    ?>
" />
<input type="submit" onclick="return window.confirm('Are you sure you want to delete this subscriber?'); " value="Delete This Subscriber"  class="button-primary" />
</form>

<div style="clear:both"></div>

<h3>Current Newsletter Subscriptions:</h3>
<?php 
    $query = "select distinct a.id id, a.name name from " . $wpdb->prefix . "wpr_newsletters a, " . $wpdb->prefix . "wpr_subscribers b where a.id=b.nid and b.email='" . $subscriber->email . "' and b.active in(1,2)";
    $subscribedNewsletters = $wpdb->get_results($query);
    foreach ($subscribedNewsletters as $newsletter) {
        $nid = $newsletter->id;
        $email = $subscriber->email;
        $query = "SELECT * FROM " . $wpdb->prefix . "wpr_subscribers where nid='" . $newsletter->id . "' and email='" . $subscriber->email . "';";
        $results = $wpdb->get_results($query);
        $theSubscriberObject = $results[0];
        $sid = $theSubscriberObject->id;
        ?>
    <fieldset style="border: 1px solid #000; width:1000px; padding: 15px; margin-bottom: 10px;"><legend><span style="font-family: Arial;font-size: 15px; margin: 10px; font-weight:bold"><?php 
        echo $newsletter->name;
        ?>
</span></legend>
    <table width="300" style="margin: 10px;">
       <tr>
          <td>Name: </td>
          <td><?php 
        echo $theSubscriberObject->name;
        ?>
		  </td>
        </tr>
        <tr>
          <td>Subscribed on:</td>
          <td><?php 
        echo date("g:ia d F Y", $theSubscriberObject->date);
        ?>
          </td>        
        </tr>
     </table><br />

     
   
     <?php 
        $query = "SELECT * FROM " . $wpdb->prefix . "wpr_followup_subscriptions where sid={$sid} and type='autoresponder';";
        $autoresponderSubscriptions = $wpdb->get_results($query);
        ?>

     <?php 
        if (count($autoresponderSubscriptions)) {
            ?>
              <h3>Follow-up Autoresponders Subscriptions</h3>
                   <table class="widefat">
     <tr>
       <th>Name Of Autoresponder</th>
       <th width="150">Currently Receiving?</th>
       <th>Progress in Autoresponder</th>
       <th>Date Of Subscription</th>
       <th>Stop</th>
    </tr>
     <?php 
            foreach ($autoresponderSubscriptions as $followup) {
                ?>
			 <tr id="autores-<?php 
                echo $followup->eid;
                ?>
-row">
				<td><?php 
                $query = "SELECT * FROM " . $wpdb->prefix . "wpr_autoresponders where id=" . $followup->eid . ";";
                $theAutoresponder = $wpdb->get_results($query);
                echo $theAutoresponder[0]->name;
                ?>
				</td>
				<td>
				<?php 
                if (isAutoresponderSeriesActive($followup->eid)) {
                    echo "Receiving Follow-up Message.";
                } else {
                    echo "Has Received All Messages.";
                }
                ?>
				</td>
				<td>Has Received <?php 
                echo $followup->sequence + 1;
                ?>
 Messages.</td>
				<td>
				   <?php 
                echo date("g:ia d F Y", $followup->doc);
                ?>
				</td>
				<td>
				<?php 
                if (isAutoresponderSeriesActive($followup->eid)) {
                    ?>
					<form action="admin.php?page=wpresponder/subscribers.php&action=profile&sid=<?php 
                    echo $sid;
                    ?>
&aresid=<?php 
                    echo $followup->eid;
                    ?>
&subaction=delete" method="post">
					<input type="hidden" name="aid" value="<?php 
                    echo $followup->id;
                    ?>
" />
					<input type="submit" class="button-primary" name="followupunsub" value="Stop" onclick="return confirm('Are you sure you want to stop this autoresponder sequence for this subscriber?');" />
					</form>
				<?php 
                } else {
                    ?>
<center>Finished</center>
					<?php 
                }
                ?>
				</td>
			 </tr>
				<?php 
            }
            ?>
 </table>
     <?php 
        }
        ?>
    
     
     
     <h3>Custom Field Values</h3>     
     <?php 
        //fetch the custom fields of this newsletter
        $query = "SELECT * FROM " . $wpdb->prefix . "wpr_custom_fields where nid={$nid}";
        $customFieldList = $wpdb->get_results($query);
        if (count($customFieldList)) {
            ?>
			 <form name="newsletter-<?php 
            echo $nid;
            ?>
-customfields" method="post">
			 <input type="hidden" name="customfielddata" value="1" />
                         <input type="hidden" name="custom_field_sid" value="<?php 
            echo $theSubscriberObject->id;
            ?>
">
			 <input type="hidden" name="custom_field_newsletter" value="<?php 
            echo $nid;
            ?>
" />
			 <table width="800">
			 <?php 
            foreach ($customFieldList as $formfield) {
                $cid = $formfield->id;
                ?>
				 <tr> 
					 <td><?php 
                echo $formfield->label;
                ?>
</td>
					 <td><?php 
                $query = "SELECT value from " . $wpdb->prefix . "wpr_custom_fields_values where sid={$sid} and cid={$cid}";
                $valueSet = $wpdb->get_results($query);
                $value = $valueSet[0]->value;
                if ($formfield->type != "hidden") {
                    echo getCustomField($formfield->id, "newsletter-{$nid}-cfield-" . $formfield->id, $value);
                } else {
                    ?>
<input type="text" name="<?php 
                    echo "newsletter-{$nid}-cfield-" . $formfield->id;
                    ?>
" value="<?php 
                    echo $value;
                    ?>
" />(hidden type)<?php 
                }
                ?>
</td>
				 </tr>
				 <?php 
            }
            ?>
		</table>
		<input type="submit" class="button" value="Save Custom Field Information" style="display:block" /><br />
        </form>
        <?php 
        } else {
            ?>
No custom fields defined for this newsletter. <?php 
        }
        ?>
<br />



<?php 
        if ($theSubscriberObject->active == 1) {
            ?>
<strong>Subscription Status: </strong> Subscribed<p></p>
<form action="admin.php?page=wpresponder/subscribers.php&action=profile&sid=<?php 
            echo $sid;
            ?>
" method="post">
<input type="hidden" name="sid" value="<?php 
            echo $sid;
            ?>
" />
<input type="hidden" name="unsubscription_form" value="1" />
<input type="submit" name="submit" onclick="return window.confirm('Are you sure you want to unsusbcribe this reader from this newsletter?');" value="Unsubscribe from this newsletter" class="button-primary" /> 
</form>
<?php 
        } else {
            if ($theSubscriberObject->active == 2) {
                ?>
<strong>Subscription Status:</strong> Transfered. The subscriber's subscription to this newsletter was deactivated in accordance
with a <a href="admin.php?page=wpresponder/actions.php">transfer rule</a>.
    <?php 
            } else {
                ?>
User has Unsubscribed<?php 
            }
        }
        ?>
    
    </fieldset>
    <?php 
    }
    ?>
 
</form><br />
<a href="admin.php?page=wpresponder/subscribers.php" class="button">&laquo; Back</a>
    <?php 
}