<?php

/**************************************************************
    ADMIN MENU
**************************************************************/
add_action("admin_menu", "wpsa_el_admin_menu", 14);
function wpsa_el_admin_menu() {
    add_submenu_page('wpsa-addon', 'WPSA Email2List', 'Email2List', 'manage_options', 'email2list_admin', 'wpsa_el_setting_page');
}

/**************************************************************
    Admin Side Setting Page
**************************************************************/
function wpsa_el_setting_page() {
    if (!function_exists('wpsa_install')) return ;
    
    global $wpdb;
    global $_wp_real_parent_file;
//    print_r($_wp_real_parent_file);
      
    if(isset($_POST["e2l_mailing_list_service"]) && $_POST["e2l_mailing_list_service"] != "") {
        update_option("e2l_mailing_list_service", $_POST["e2l_mailing_list_service"]);
        $e2l_mailing_list_service = $_POST["e2l_mailing_list_service"];
    }
    else {
        $e2l_mailing_list_service = get_option("e2l_mailing_list_service");
    }
	
  // Credentials Form Submit
  if(isset($_POST["e2l_save_options"])) {
    // Update Settings      
    update_option('e2l_settings', $_POST);

    $e2l_subscribe_message = $_POST['e2l_subscribe_message'];
    update_option('e2l_subscribe_message', $e2l_subscribe_message);
    
	$message = "Your information has been saved!";
    $e2l_settings = get_option('e2l_settings');
  }
  else {            
      $e2l_subscribe_message = get_option('e2l_subscribe_message');
      $e2l_settings = get_option('e2l_settings');
//      print_r($e2l_settings);
  }
  
   if ($message) { echo '<div id="message" class="updated fade"><p>'.$message.'</p></div>';}

   ?>
    <div id="dropmessage" class="updated" style="display:none;"></div>
    <div class="wrap">
        <h2><?php _e("WPSA Email2List", "email_2_list"); ?></h2>
        <form method="post" name="email_2_list_options" target="_self">
            <table class="form-table">                
                <tr>
                    <th><label for="e2l_mailing_list_service"><?php _e('Select Your Mailing List Service:'); ?></label></th>
                    <td>
                        <input name="e2l_mailing_list_service" type="radio" value="AWEBER" onclick="document.email_2_list_options.submit();" <?php echo ( ( get_option('e2l_mailing_list_service') == 'AWEBER' ) ? 'checked' : '' ); ?>  />AWeber
                        <input name="e2l_mailing_list_service" type="radio" value="GETRESPONSE" onclick="document.email_2_list_options.submit();" <?php echo ( ( get_option('e2l_mailing_list_service') == 'GETRESPONSE' ) ? 'checked' : '' ); ?> />GetResponse
                        <input name="e2l_mailing_list_service" type="radio" value="GENERIC" onclick="document.email_2_list_options.submit();" <?php echo ( ( get_option('e2l_mailing_list_service') == 'GENERIC' ) ? 'checked' : '' ); ?> />Generic
                    </td>
                </tr>                
            </table>

            <?php
            if(!empty($e2l_mailing_list_service)) {                
                if($e2l_mailing_list_service == 'AWEBER') {
                    ?>
                            <p>In order to integrate the <a href="http://www.aweber.com/">AWeber</a>
                            registration form with the WordPress registration process you need to enter in the information below. This
                            information can be found in the HTML form code provided to you in your
                            <a href="http://www.aweber.com">AWeber</a> control panel. After logging in to your account,
                            select Web Forms from the primary navigation, create the form, then select Get HTML for that form.
                            Select Raw HTML Version from the resulting popup window (also remove the checkbox from the "beautiful
                            form styles" checkbox to simplify the data) and  retrieve the required values as shown below.</p>
                            <?php
                }
                else if($e2l_mailing_list_service == 'GETRESPONSE') {
                    ?>
                          <p>In order to integrate the <a href="http://www.GetResponse.com/">GetResponse</a>
                            registration form with the WordPress registration process you need to enter in the information below. This
                            information is simply the name of the <em>campaign</em> that you'd like your members to subscribe to.</p>
                          <?php
                } else { ?>
                          <p>Use the Generic type for lists that take subscriptions by email.  Enter the email address that should be used to subscribe to the list.</p>
                            <?php
                }
                ?>

                  <table class="widefat form-table" >
                    <thead>
                        <tr class="thead">
                            <th class="manage-column" scope="col">Subscription</th>
                            <th class="manage-column" scope="col"><?php echo $e2l_mailing_list_service == 'AWEBER' ? 'List Name(<em>listname</em>)' : ($e2l_mailing_list_service == 'GETRESPONSE' ? 'Campaign Name' : 'Subscription Email Addr.');?></th>
                            <?php
                        if($e2l_mailing_list_service == 'AWEBER') { ?><th scope="col">Form ID(<em>meta_web_form_id</em>)</th><?php } ?>
                        </tr>
                   <thead>

                   <tbody>                                              
                        <?php
                      $sql = 'select * from '.$wpdb->prefix.'wpsa_subscriptions ';
                      $subscriptions = $wpdb->get_results($sql);

                      if($subscriptions){
                          foreach( $subscriptions as $sub ){
                        ?>
                            <tr class="">
                                <td><?php echo $sub->name; ?></td>
                                <?php 
                                if($e2l_mailing_list_service == 'AWEBER') { ?>
                                            <td><input type="text" class="regular-text" name="e2l_list_name[AWEBER][<?php echo $sub->id;?>]" value="<?php echo $e2l_settings['e2l_list_name']['AWEBER'][$sub->id]; ?>"> </td>
                                            <td><input type="text" class="regular-text" name="e2l_list_form[AWEBER][<?php echo $sub->id;?>]" value="<?php echo $e2l_settings['e2l_list_form']['AWEBER'][$sub->id]; ?>"></td>
                                            <?php
                                }
                                else if($e2l_mailing_list_service == 'GETRESPONSE'){
                                    ?><td><input type="text" class="regular-text" name="e2l_list_name[GETRESPONSE][<?php echo $sub->id;?>]" value="<?php echo $e2l_settings['e2l_list_name']['GETRESPONSE'][$sub->id]; ?>"> </td><?php
                                }
                                else {
                                    ?><td><input type="text" class="regular-text" name="e2l_list_name[GENERIC][<?php echo $sub->id;?>]" value="<?php echo $e2l_settings['e2l_list_name']['GENERIC'][$sub->id]; ?>"> </td><?php
                                }
                                ?>
                            </tr>
                        <?php
                         }
                      }
                      ?>
                        <tr class="">
<!--                            <td><label for="subsc_free">Select Category</label>
                                <select name="subsc_free">
                                <?php
//                                $categories = get_categories();
//                                if ( $categories ) {
//                                    foreach( $categories as $cat ) {
//                                        echo '<option value="' .$cat->cat_ID. '">' .$cat->name. '</option>';
//                                    }
//                                }
                         ?>
                                </select>
                            </td> -->
                            <td><strong>Free Subscription</strong> </td>
                            <?php
                            
                            if($e2l_mailing_list_service == 'AWEBER') { ?>
                                        <td><input type="text" class="regular-text" name="e2l_list_name[AWEBER][free]" value="<?php echo $e2l_settings['e2l_list_name']['AWEBER']['free']; ?>"> </td>
                                        <td><input type="text" class="regular-text" name="e2l_list_form[AWEBER][free]" value="<?php echo $e2l_settings['e2l_list_form']['AWEBER']['free']; ?>"></td>
                                        <?php
                            }
                            else if($e2l_mailing_list_service == 'GETRESPONSE'){
                                ?><td><input type="text" class="regular-text" name="e2l_list_name[GETRESPONSE][free]" value="<?php echo $e2l_settings['e2l_list_name']['GETRESPONSE']['free']; ?>"> </td><?php
                            }
                            else {
                                ?><td><input type="text" class="regular-text" name="e2l_list_name[GENERIC][free]" value="<?php echo $e2l_settings['e2l_list_name']['GENERIC']['free']; ?>"> </td><?php
                            }
                            ?>
                        </tr>
                    </tbody>
                    </table>
    <?php } ?>
   

        <h2><?php _e("Registration Form Setting", "email_2_list"); ?></h2>
        <table class="form-table">
            <tr>
                <th><label for="e2l_subscribe_message">Message</label></th>
                <td align="left" ><textarea name="e2l_subscribe_message" id="e2l_subscribe_message" rows="3" cols="20"><?php echo (empty($e2l_subscribe_message) ? 'Sign me up to receive the newsletter!' : stripslashes($e2l_subscribe_message)) ; ?></textarea></td>
            </tr>
            <tr>
                <th><label for="e2l_subscribe_message_show">Show Message</label></th>
                <td><input type="checkbox" class="regular-text" <?php if ($e2l_settings['e2l_subscribe_message_show']) echo 'checked';?>  value="checked" id="e2l_subscribe_message_show" name="e2l_subscribe_message_show"></td>
            </tr>

            <tr valign="top">
                <th scope="row"><label for="e2l_fname">Show First Name</label></th>
                <td><input type="checkbox" class="regular-text" <?php if ($e2l_settings['e2l_fname']) echo 'checked';?>  value="checked" id="e2l_fname" name="e2l_fname"></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="e2l_fname_required">First Name Required</label></th>
                <td><input type="checkbox" class="regular-text" <?php if ($e2l_settings['e2l_fname_required']) echo 'checked';?>  value="checked" id="e2l_fname_required" name="e2l_fname_required"></td>
            </tr>

            <tr valign="top">
                <th scope="row"><label for="e2l_lname">Show Last Name</label></th>
                <td><input type="checkbox" class="regular-text" <?php if ($e2l_settings['e2l_lname']) echo 'checked';?>  value="checked" id="e2l_lname" name="e2l_lname"></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="e2l_lname_required">Last Name Required</label></th>
                <td><input type="checkbox" class="regular-text" <?php if ($e2l_settings['e2l_lname_required']) echo 'checked';?>  value="checked" id="e2l_lname_required" name="e2l_lname_required"></td>
            </tr>

            <tr>
                <td colspan="2" align="left"><input class="button-primary" type="submit" name="e2l_save_options" value="<?php _e("Save Setting", "email_2_list")?>" /></td>
            </tr>
        </table>
     </form>
<p><br />
<div style="height:0px; overflow:hidden;">

    <h2><?php _e("Registered Users", "email_2_list"); ?></h2>
    <p>This is a list of the users who checked your subscribe box. This list will likely not be the same as your AWeber/GetResponse list(s) due to members who don't respond to the Aweber/GetResponse confirmation email.</p>
    <table class="widefat form-table" cellspacing="0">
      <thead>
        <tr class="thead">
	      <th scope="col" id="username" class="manage-column column-username" >Username</th>
	      <th scope="col" id="name" class="manage-column column-name">Display Name</th>
	      <th scope="col" id="email" class="manage-column column-email" >E-mail</th>
          <th scope="col" id="registered" class="manage-column column-name" >Registered On</th>
        </tr>
      </thead>
      <tbody id="users" class="list:user user-list">
        <?php        
		  $users = $wpdb->get_results("SELECT user_id FROM {$wpdb->prefix}wpsa_el_log WHERE send_subsc_mail = 'yes'");
		  
          if($users) {
		    foreach($users as $user) {
                $user = get_userdata($user->user_id);
                ?>
			  <tr>
                <td class="username column-username"><strong><?php echo $user->user_login; ?></strong></td>
                <td class="name column-name"><?php echo $user->nickname; ?></td>
                <td class="email column-email"><a href='mailto:<?php echo $user->user_email; ?>' title='e-mail: <?php echo $user->user_email; ?>'><?php echo $user->user_email; ?></a></td>
                <td class="name column-name"><?php echo date("F jS, Y", strtotime($user->user_registered)); ?></td>
              </tr>
			<?php } ?>
		  <?php }else{ ?>
		    <tr class="alternate">
              <td class="username column-username" colspan="4"><i>There are not currently any registered users.</i></td>
            </tr>
		  <?php } ?>
      </tbody>      
    </table></div>
  </div>
  
<?php } 


/**************************************************************
    SHOW NOTIC WHEN WPSA PLUGIN IS ABSENT OR INACTIVE
**************************************************************/
add_action( 'admin_notices', 'wpsa_notice' );

function wpsa_notice() {
	global $cache_enabled;
	if (!function_exists('wpsa_install'))
		echo '<div class="error"><p><strong>The WP SalesAutomator plugin must be installed to use the email2list addon.</strong></p></div>';
}
?>