<?php 

/* Add meta box */
global $post;
		
function get_all_posts_type(){
	global $wpdb;
	
	$post_types = $wpdb->get_results("SELECT DISTINCT `post_type` FROM `".$wpdb->prefix."posts`", ARRAY_A);
	return $post_types;
}

add_action( 'add_meta_boxes', 'add_meta_box_protected_content' );
function add_meta_box_protected_content()
{
	$all_posts_type = get_all_posts_type();
	foreach($all_posts_type as $post_type){
		$posttype = $post_type['post_type'];
		add_meta_box( 'protected-content-meta-box-id', 'DAP Content Protection', 'add_protected_content', $posttype , 'side', 'high' );
	}
	add_action( 'admin_enqueue_scripts', 'pc_enqueue_custom_js_style' );
} 

function pc_enqueue_custom_js_style() {
		
	wp_register_script('dap-protected-content-js', plugin_dir_url(__FILE__). 'includes/js/dap-protected-content.js');
	wp_enqueue_script( 'dap-protected-content-js' );
	wp_register_style( 'dap-protected-content-css', plugin_dir_url(__FILE__). 'includes/css/dap-protected-content.css', false, '1.0.0' );
	wp_enqueue_style( 'dap-protected-content-css' );
	wp_register_style( 'dap-protected-font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' );
	wp_enqueue_style( 'dap-protected-font-awesome' );
	wp_register_style( 'jquery-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', false, '1.0.0' );
	wp_enqueue_style( 'jquery-ui' );
	wp_register_script('jquery', '//code.jquery.com/jquery-1.11.1.js');
	wp_enqueue_script( 'jquery' );
	wp_register_script('jqueryui', '//code.jquery.com/ui/1.11.4/jquery-ui.js');
	wp_enqueue_script( 'jqueryui' ); 
	
}



function add_protected_content( )
{	
	global $post;
	$site_url = get_site_url();
	$home_url = get_home_url();
	/*if($site_url == $home_url){	$url = $site_url; }else{ $url = $home_url; }*/
    $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'] : "http://".$_SERVER['SERVER_NAME'];

	$get_permalink = get_permalink($post->ID); 

    //If it begins with https...
    if (preg_match('/^https/', $get_permalink)) {
      //...then we'll set the $url_prefix variable to https://
      $url_prefix = 'https://';
    } else {
      //If it does not begin with https we'll use http
      $url_prefix = 'http://';
    }
    //Get rid of the http:// or https://
    $permalinknew = str_replace(array('http://','https://'), '', $get_permalink);

	$permalink = (!empty($_SERVER['HTTPS'])) ? "https://".$permalinknew : "http://".$permalinknew;
    $permalink_slug_old = str_replace($url ,"",$permalink);
	$permalink_slug =rtrim($permalink_slug_old,"/"); 

	$dap_dbh = Dap_Connection::getConnection();
	$sql = "SELECT * FROM dap_file_resources fr, dap_products_resources_jn prj WHERE fr.url = '".$permalink_slug."' AND fr.id = prj.resource_id"; 
	$stmt = $dap_dbh->prepare($sql);
	$stmt->execute();
	$rows = $stmt->fetchAll();

?>  
<input type="hidden" value="<?php echo $pid = $post->ID; ?>" id="p_id">
<input type="hidden" value="<?php if(!empty($rows)) { foreach($rows as $row){ echo $product_id = $row["product_id"]; echo "," ;} } ?>" id="all_p_id">
<input type="hidden" id="p_url" value="<?php echo $permalink_slug;?>" >
<input type="hidden" value="<?php echo get_post_status( $pid  ); ?>" id="post_status">
<input type="hidden" value="<?php echo get_edit_post_link($post->ID ); ?>" id="post_url">
<input type="hidden" value="<?php echo SITE_URL_DAP."/dap/admin/addEditProducts.php?productId=";?>" id="dap_prod_url">
<input type="hidden" value="<?php if(!empty($rows)) {  $fileName = $rows[0]["name"]; if(!empty($fileName)){echo $fileName; }else{ echo $permalink_slug; } }?>" id="postnameid">
<input type="hidden" value="<?php if(!empty($rows)) {  $resource_id = $rows[0]["resource_id"]; if(!empty($resource_id)){echo $resource_id; }else{  } }?>" id="resourceid">

<div class="protected_product_content" id="protected_product_content_id">
	<a class="add_protected_content" href="#pro_contentpopup">Click to Protect Content</a>
	<?php 	if(!empty($rows)) {  ?><a class="delete_protected_content" title="Delete this protected content from all products." data-id="<?php echo  $row["resource_id"]; ?>" href="javascript:void(0)"><i class="fa fa-trash" aria-hidden="true"></i></a><?php } ?>
</div>

<div class="protected_product_content_table <?php 	if(!empty($rows)) { echo "protected_table_border";}?>" id="protected_product_content_table_id" >
		<table class="protected_product_table_page_select">
		<thead>
		   <tr class="protected_product_head <?php if(!empty($rows)) { echo "show_div";}else {echo "hide_div"; } ?>">
			   <th>Product Name</th>
			   <th>Drip</th>
			   <th class="actions_head">Actions</th>
		   </tr>
		</thead>
	   <tbody>
		<?php
		if(!empty($rows)) { 
			foreach($rows as $row){
				$rowid = $row["id"];
				$resource_id = $row["resource_id"];
				$product_id = $row["product_id"];
				$start_day = $row["start_day"];
				$start_date = $row["start_date"];
				if(!empty($start_day)){
					$drip_day = $start_day;
				}
				if(!empty($start_date)){
					$drip_day = $start_date;
				}
				
				
		?> 
			<tr id="row_<?php echo $rowid; ?>" class="each_prod_row">
				<td valign="top" class="prod_class" id="pro_<?php echo $product_id ;?>"> 
					<?php  $productList = null;  $productList = Dap_Product::loadProducts("","A"); 
					foreach ($productList as $prod) { ?>
					<?php if($prod->getId() == $product_id){ $productname =  $prod->getName();if(strlen($productname) > 15){$product_name =  trim($productname); $product_name_short = substr($product_name, 0, 15)."...";}else{ $product_name_short  = $productname;} }?>
					 <?php } ?>
						<div class="tool-tip">
							<div class="tool-tip-outer">
								<a href="<?php echo SITE_URL_DAP."/dap/admin/addEditProducts.php?productId=".$product_id;  ?>" target="_blank">
									<?php echo $product_name_short;?>
								</a>
								<div class="toll-tip-desc">
									<a href="javascript:void(0)">
										<?php echo $productname;?>
									</a>
								</div>
							</div>
						</div>
				   
				</td>
				<td valign="top"><?php if(!empty($start_date)){ echo $drip_day = date("m-d-Y", strtotime($drip_day));;  } else{ ?><input value="<?php echo $drip_day; ?>" class="editablecls"> <a class="update-btn btn btn-xs" title="Click to update drip settings." onclick="" href="javascript:void(0)">
							<i aria-hidden="true" class="fa fa-refresh"></i>
						</a><?php } ?></td>
				<td class="editable-button text-right action_buttons">
					<div id="" name="select_action">
						<a class="edit-btn btn btn-primary btn-xs" title="Click to edit drip settings." id="edit_<?php echo $rowid; ?>" href="#pro_contentpopup<?php echo $rowid; ?>">
							<i class="fa fa-pencil" aria-hidden="true"></i>
						</a>
						<a class="del-btn btn btn-primary btn-xs" title="Delete this protected content from the selected product." data-id="<?php echo $resource_id; ?>" id="del_<?php echo $rowid; ?>" onclick="" href="javascript:void(0)">
							<i aria-hidden="true" class="fa fa-trash"></i>
						</a>
					</div>
				</td>
			</tr>

		<?php 
				}
			}
		?>
		</tbody>

	</table>
	<div class="col-sm-12 success_drip_msg alert alert-success" style="display: none;">Drip day is sucessfully updated.</div>
	
</div>
<div class="pro_contentoverlay_outer">
<?php
	if(!empty($rows)) { 
		foreach($rows as $row){
			$rowid = $row["id"];
			$resource_id = $row["resource_id"];
			$product_id = $row["product_id"];
			$display_in_list = $row["display_in_list"];
			$fileName = $row["name"];
			$url_perm = $row["url"];
			$start_day = $row["start_day"];
			$end_day = $row["end_day"];
			$start_date = $row["start_date"];
			$end_date = $row["end_date"];
			
?>        
<div id="pro_contentpopup<?php echo $rowid ;?>" class="pro_contentoverlay">

    <div class="popup"> <a class="close" href="#">&times;</a>
		<h2 class="head_memb ">DAP Content Protection</h2>
        <div class="pro_content">
            <div class="protected-product-outer">
                <div class="protected-product-div">
                    
                    <table width="100%" cellpadding="2" cellspacing="2" class="protected_table">
                        <tr class="protected_table_in">
                            <td valign="middle" width="100%">
                                <table width="100%" cellpadding="2" cellspacing="2">
									<tr>
                                        <td valign="top">Page/Post to be Protected</td>
                                        <td valign="top" width="50%">
                                            <input name="url" type="text" id="url" value="<?php echo $permalink;?>" size="60" readonly style="background: rgb(204, 204, 204);">
                                         </td>
                                    </tr>
                                    <tr>
                                        <td>Select Product(s)</td>
                                        <td valign="top" width="50%">
                                            <select style="" id="product_all" style=" width: 55% !important;" name="product_all" multiple="multiple" class="productall_ids">
                                                <?php $productList = null; $productList = Dap_Product::loadProducts("","A"); 
												foreach ($productList as $prod) { ?>
                                                <option value="<?php echo $prod->getId(); ?>"  <?=isset($product_id) && $product_id == $prod->getId() ? 'selected' : '' ?>><?php echo $prod->getName(); ?></option>
												<?php } ?>
											</select>
                                        </td>
                                    </tr>
                                   
                                    <tr>
                                        <td valign="top">Link Text</td>
                                        <td valign="top" width="50%">
                                            <input name="fileName" type="text" id="fileName" value="<?php if(!empty($fileName)){echo $fileName; }else{ echo $permalink_slug; }?>" size="20" maxlength="100"> </td>
                                    </tr>
                                    <tr>
                                         <td valign="top my-content-div"> 
											Display on "My Content" Page?
											<div class="tool-tip my-content-tooltip">
												<div class="tool-tip-outer">
														 <i class="fa fa-info-circle" aria-hidden="true"></i>
													<div class="toll-tip-desc">
														Select YES if you want this link to be displayed in the "available content" area of DAP MyContent page.
													</div>
												</div>
											</div>
										</td>
                                        <td valign="top" width="50%">
                                            <div align="left display_in_list_outer">
                                                <label><input class="display_in_list_val" name="display_in_list<?php echo $rowid ;?>" type="radio" value="Y" <?=isset($display_in_list) && $display_in_list == 'Y' ? "checked='checked'" : '' ?> /> Yes</label>                                                
                                                <label><input class="display_in_list_val" name="display_in_list<?php echo $rowid ;?>" type="radio" value="N" <?=isset($display_in_list) && $display_in_list == 'N' ? "checked='checked'" : '' ?> /> No</label>
											</div>	
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="protected_table_in">
                            <td valign="middle" width="100%">
							
							 <table width="100%" cellpadding="2" cellspacing="2">
                                     <tr class="drip_sett">
                                        <th> <span class="tittle">Drip Settings</span> </th>
                                    </tr>
                                    <tr>
                                        <td valign="top">
										
                                            <div id="days_div" class="table-outer days_div_cls">
                                                <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                                                    <tbody>
                                                        <tr>
                                                            <td valign="top">
                                                                <div align="left">Start Day #</div>
                                                            </td>
                                                            <td valign="top">
                                                                  <div align="left">
                                                                    <input name="start_day" type="text" id="start_day<?php echo $rowid ;?>" class="days_input start_day_input"  value="<?php if(!empty($start_day)){ echo $start_day; }else{ echo ""; }?>" size="4" maxlength="4"> <span class="plainLink">(eg., Day '7')</span>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td valign="top">
                                                                <div align="left">End Day #</div>
                                                            </td>
                                                            <td valign="top">
                                                                 <div align="left">
                                                                    <input name="end_day" type="text" id="end_day<?php echo $rowid ;?>" class="days_input end_day_input" value="<?php if(!empty($end_day)){echo $end_day; }else{ echo ""; }?>" size="4" maxlength="4"> <span class="plainLink"> (eg., Day '30')</span>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
											
                                            <div align="center" class="scriptheader or-title"><span>OR</span></div>
                                            
											<div class="table-outer date_div_cls" id="date_div">
												<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
													
														<tbody>
															<tr>
																<td>
																	<div align="left">Start Date</div>
																</td>
																<td>  
																	<div align="left">
																		<input id="start_date<?php echo $rowid ;?>" name="start_date" class="date_input" type="text" class="" value="<?php if(!empty($start_date)){echo $start_date = date("m-d-Y", strtotime($start_date)); }else{ echo ""; }?>" size="10" maxlength="10"> <span class="plainLink"><a href="javascript:void(0)"><img src="<?php  echo $img_url1 = plugin_dir_url(__FILE__)."includes/images/cal.gif"; ?>" width="16" height="16" border="0" class="get_datepicker" alt="Pick a date"></a> (mm-dd-yyyy)</span>
																	</div>
																</td>
															</tr>
															<tr>
																<td>
																	<div align="left">End Date</div>
																</td>
																<td>
																	<div align="left">
																		<input id="end_date<?php echo $rowid ;?>" name="end_date" class="date_input" type="text" value="<?php if(!empty($end_date)){echo $end_date = date("m-d-Y", strtotime($end_date)); }else{ echo ""; }?>" size="10" maxlength="10"> <span class="plainLink"><a href="javascript:void(0)"><img src="<?php  echo $img_url1 = plugin_dir_url(__FILE__)."includes/images/cal.gif"; ?>" width="16" height="16" class="get_datepicker1" border="0" alt="Pick a date"></a> (mm-dd-yyyy)</span>
																	</div>
																</td>
															</tr>															
														</tbody>
												
												</table>
											</div>
											
											<div class="protected_content_out">
												<div class="button-primary next-btn protect_content_cls" id="protected_content">Protect Content</div>
											</div>
											
                                        </td>
                                    </tr>
                                </table>
								
                            </td>
                        </tr>
                    </table>
                    
                </div>
            </div>
        </div>
    </div>
</div>
<?php 
		}
	} 
?>
</div>
<div id="pro_contentpopup" class="pro_contentoverlay">

    <div class="popup"> <a class="close" href="#">&times;</a>
		<h2 class="head_memb ">DAP Content Protection</h2>
        <div class="pro_content">
            <div class="protected-product-outer">
                <div class="protected-product-div">
                    
                    <table width="100%" cellpadding="2" cellspacing="2" class="protected_table">
                        <tr class="protected_table_in">
                            <td valign="middle" width="100%">
                                <table width="100%" cellpadding="2" cellspacing="2">
									<tr>
                                        <td valign="top">Page/Post to be Protected</td>
                                        <td valign="top" width="50%">
                                            <input name="url" type="text" id="url" value="<?php echo $permalink;?>" size="60" readonly style="background: rgb(204, 204, 204);"> </td>
                                    </tr>
                                    <tr>
                                        <td>Select Product(s)</td>
                                        <td valign="top" width="50%">
                                            <select style="" id="product_all" style=" width: 55% !important;" name="product_all" multiple="multiple" class="productall_ids">
                                                <!--option value="0" selected="selected">ALL</option-->
                                                <?php  $productList = null; 
												$productList = Dap_Product::loadProducts("","A");  
												foreach ($productList as $prod) { ?>
                                                <option value="<?php echo $prod->getId(); ?>"><?php echo $prod->getName(); ?></option>
                                                <?php } ?>
											</select>
                                        </td>
                                    </tr>
                                   
                                    <tr>
                                        <td valign="top">Link Text</td>
                                        <td valign="top" width="50%">
                                            <input name="fileName" type="text" id="fileName" value="<?php echo $permalink_slug;?>" size="20" maxlength="100"> </td>
                                    </tr>
                                    <tr>
                                        <td valign="top my-content-div"> 
											Display on "My Content" Page?
											<div class="tool-tip my-content-tooltip">
												<div class="tool-tip-outer">
														 <i class="fa fa-info-circle" aria-hidden="true"></i>
													<div class="toll-tip-desc">
														Select YES if you want this link to be displayed in the "available content" area of DAP MyContent page.
													</div>
												</div>
											</div>
										</td>
                                        <td valign="top" width="50%">
                                            <div align="left display_in_list_outer">
                                                <label><input class="display_in_list_val" name="display_in_list" type="radio" value="Y" checked> Yes</label>                                                
                                                <label><input class="display_in_list_val" name="display_in_list" type="radio" value="N"> No</label>
											</div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="protected_table_in">
                            <td valign="middle" width="100%">
                                <table width="100%" cellpadding="2" cellspacing="2">
                                     <tr class="drip_sett">
                                        <th> <span class="tittle">Drip Settings</span> </th>
                                    </tr>
                                    <tr>
                                        <td valign="top">
										
                                            <div id="days_div" class="table-outer days_div_cls">
                                                <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                                                    <tbody>
                                                        <tr>
                                                            <td valign="top">
                                                                <div align="left">Start Day #</div>
                                                            </td>
                                                            <td valign="top">
                                                                <div align="left">
                                                                    <input name="start_day" type="text" class="days_input start_day_input" id="start_day" value="1" size="4" maxlength="4"> <span class="plainLink">(eg., Day '7')</span>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td valign="top">
                                                                <div align="left">End Day #</div>
                                                            </td>
                                                            <td valign="top">
                                                                <div align="left">
                                                                    <input name="end_day" type="text" class="days_input end_day_input" id="end_day" value="9999" size="4" maxlength="4"> <span class="plainLink"> (eg., Day '30')</span>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
											
                                            <div align="center" class="scriptheader or-title"><span>OR</span></div>
                                            
											<div class="table-outer date_div_cls" id="date_div">
												<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
													
														<tbody>
															<tr>
																<td>
																	<div align="left">Start Date</div>
																</td>
																<td>  
																	<div align="left">
																		<input id="start_date" name="start_date" class="date_input" type="text" value="" size="10" maxlength="10"> <span class="plainLink"><a href="javascript:void(0)"><img src="<?php  echo $img_url1 = plugin_dir_url(__FILE__)."includes/images/cal.gif"; ?>" width="16" height="16" border="0" class="get_datepicker" alt="Pick a date"></a> (mm-dd-yyyy)</span> </div>
																</td>
															</tr>
															<tr>
																<td>
																	<div align="left">End Date</div>
																</td>
																<td>
																	<div align="left">
																		<input id="end_date" name="end_date" class="date_input" type="text" value="" size="10" maxlength="10"> <span class="plainLink"><a href="javascript:void(0)"><img src="<?php  echo $img_url1 = plugin_dir_url(__FILE__)."includes/images/cal.gif"; ?>" width="16" height="16" class="get_datepicker1" border="0" alt="Pick a date"></a> (mm-dd-yyyy)</span> </div>
																</td>
															</tr>															
														</tbody>
												
												</table>
											</div>
											
											<div class="protected_content_out">
												<div class="button-primary next-btn protect_content_cls" id="protected_content">Protect Content</div>
											</div>
											
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                    
                </div>
            </div>
        </div>
    </div>
</div>		

<?php        
}
