 <?php  if(priv($_SESSION['role'],'view','posts',$connect)!=1){exit(); }?>
<?php  include("fckeditor.php") ; ?>



<?php  


$sqlQuery="select * from   servicesoperator   
inner join services on `Services_id`=ServicesOperator_servicesID
where `ServicesOperator_id`='".post($connect,$_REQUEST['soid'])."' and `ServicesOperator_servicesID`='".post($connect,$_REQUEST['sid'])."'
";

 if(Role($_SESSION['userID'],$connect)!=1){
	$sqlQuery=$sqlQuery." and compnayId='".getCompnay($connect,$_SESSION['userID'])."'";
 }

 $count=mysqli_query($connect,$sqlQuery);
 if(mysqli_num_rows($count)==0){exit();}








$services=Table("services",'Services_id',post($connect,$_REQUEST['sid']),$connect);
   $servicesOperator=Table("servicesoperator",'ServicesOperator_id',post($connect,$_REQUEST['soid']),$connect);  ?>










 <div>
    <ul class="breadcrumb">
        <li>
            <a href="index.php">Home</a>
        </li>
        <li>
            <a href="#"> Add Post</a>
        </li>
        
        <li>
            <a href="index.php?page=servicesForCompany"> <?php  echo($services['Services_name']); ?></a>
        </li>
         <li>
            <a href="index.php?page=ContentPosts&sid=<?php  echo($services['Services_id']); ?>&soid=<?php  echo($services['ServicesOperator_id']); ?>"> <?php  echo($servicesOperator['ServicesOperator_name']); ?></a>
        </li>
        
        
        
        
        
    </ul>
</div>



<div class="row">
        <div class="center col-md-12"><h1><?php  echo($servicesOperator['ServicesOperator_name']); ?></h1></div></div>

<div class="row">
        <div class="box col-md-12">
            <div class="box-inner">
                <div class="box-header well" data-original-title="">
                    <h2><i class=" glyphicon glyphicon-th-list"></i> Post</h2>

                    <div class="box-icon">
                        <a href="#" class="btn btn-setting btn-round btn-default"><i
                                class="glyphicon glyphicon-cog"></i></a>
                        <a href="#" class="btn btn-minimize btn-round btn-default"><i
                                class="glyphicon glyphicon-chevron-up"></i></a>
                        <a href="#" class="btn btn-close btn-round btn-default"><i
                                class="glyphicon glyphicon-remove"></i></a>
                    </div>
                </div>
                <div class="box-content">
                
                <br />

                
              <?php   
			  
			  if($_REQUEST['deleteva']==1){
				   
				  $selectPostforDeleteImage=mysqli_query($connect,"select * from post where post_id='".post($connect,$_REQUEST['id'])."'");
				  while($roPostfordeleteImage=mysqli_fetch_array($selectPostforDeleteImage)){
					   echo($roPostfordeleteImage['post_ved_adu']);
					if(unlink("../postFiles/".$roPostfordeleteImage['post_ved_adu'])){
						$deletimages=mysqli_query($connect,"update post set post_ved_adu=''
 where     post_id='".post($connect,$_REQUEST['id'])."'");
						
						} 
					  }
				  
				  
			  }
			  
			  
			   if($_REQUEST['deleteimg']==1){
				  
				  
				  $delteimage=mysqli_query($connect,"select * from postImages
 where  postImages_id='".post($connect,$_REQUEST['imgid'])."' and postImages_postid='".post($connect,$_REQUEST['id'])."'");
				 while($rowdeleteimage=mysqli_fetch_array($delteimage)) {
					// echo($rowdeleteimage['postImages_image']);
					if(unlink("../postFiles/".$rowdeleteimage['postImages_image'])){
						$deletimages=mysqli_query($connect,"delete from postImages
 where  postImages_id='".post($connect,$_REQUEST['imgid'])."' and postImages_postid='".post($connect,$_REQUEST['id'])."'");
						
						} 
					 
					 }
				  
				  
				  } ?>
                 
                  <?php  
				 if($_REQUEST['opt']==do_up){
					if($_REQUEST['post_title']==''
 
	){
		
		echo("<div style='color:red'> Please Fill All Parameters </div>");
		}else{
 





if($_FILES['post_ved_adu']['name']){
	
	 $selectPostforDeleteImage=mysqli_query($connect,"select * from post where post_id='".post($connect,$_REQUEST['id'])."'");
				  while($roPostfordeleteImage=mysqli_fetch_array($selectPostforDeleteImage)){
	
 $extva=Get_Extension_From_File($_FILES['post_ved_adu']['name']);
 $newNameva="va".strtotime(date("y-m-d G:i:s")).".".$extva;
  if(upload("../postFiles/",$newNameva  ,$_FILES['post_ved_adu']['tmp_name'])==1){
	  unlink("../postFiles/".$roPostfordeleteImage['post_ved_adu']);
	$addVedAduio=  " , `post_ved_adu`='".$newNameva."'";
  }

				  }

}






 $post_query="update post
set 
`post_date`=now(),
  `post_title`='".post($connect,$_REQUEST['post_title'])."',
   `post_parent`=0,
    `post_text`='".post($connect,$_REQUEST['post_text'])."',
   
	 `post_youtube`='".post($connect,$_REQUEST['post_youtube'])."'
	   
 ".$addVedAduio."
 where post_id='".post($connect,$_REQUEST['id'])."'
 
 
 ";
 
 
 
 $situation=mysqli_query($connect,$post_query);
 if($situation){ 
 
 $id=post($connect,$_REQUEST['id']);
 $postImages=$_FILES['post_images'];
  // print_r($postImages);
 
   
 for($i=0;$i<=sizeof($postImages);$i++){
 
 $ext=Get_Extension_From_File($postImages['name'][$i]);
 $newName=strtotime(date("y-m-d G:i:s")).$i.".".$ext;
  if(upload("../postFiles/",$newName  ,$postImages['tmp_name'][$i])==1){
 $insertpostImages=mysqli_query($connect,"insert into postImages
 set postImages_image='".$newName."'
 ,postImages_postid=".$id."");
  }
 }
// print_r(error_get_last());
 
// exit();
 
 
 
 
 
 echo("<div>post Added SucessFully</div>");}
}

go("index.php?page=ContentPosts&opt=up&id=".post($connect,$_REQUEST['id'])."&sid=".post($connect,$_REQUEST['sid'])."&soid=".post($connect,$_REQUEST['soid'])     );

} 
				 ?>
                 
                 
                
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 <?php  
				 if($_REQUEST['opt']==up){
					 
					 $post_q="select * from post where post_id=".post($connect,$_REQUEST['id']);
					$post_res=mysqli_query($connect,$post_q);
					if($post_res){
						$post_row=mysqli_fetch_assoc($post_res);
					
					 }
				 ?>
                 
                         
                   
                  <fieldset>
<legend>Update  Post</legend>      
                   
                 
                 
                 <form method="post"  class="login_form" enctype="multipart/form-data" >
<input type="hidden" name="opt" value="do_up" />
<input type="hidden" name="id" value="<?php echo($post_row['post_id']); ?>" />
 <input type="hidden" name="sid" value="<?php echo(post($connect,$_REQUEST['sid'])); ?>" />
  <input type="hidden" name="soid" value="<?php echo(post($connect,$_REQUEST['soid'])); ?>" />
<div class="form-group">
<div class="row ">
<div class="col-md-2"> Title </div>
<div class="col-md-4"><input type="text" name="post_title" id="post_title" class="form-control" 
 value='<?php echo($post_row['post_title']); ?>' /></div>
 

</div>
</div>

<div class="form-group">
<div class="row ">
<div class="col-md-1"> Text </div>
<div class="col-md-10"><?php /*?><textarea name="post_text"></textarea><?php */?>
<?php 
$sBasePath = $_SERVER['PHP_SELF'] ;
$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "_samples" ) ) ;
$oFCKeditor = new FCKeditor('post_text') ;
$oFCKeditor->BasePath	= $sBasePath ;
$oFCKeditor->Height	= '300' ;
$oFCKeditor->Value		= $post_row['post_text'] ;
$oFCKeditor->Create() ;  ?>


</div>
 

</div>
</div>






<div class="form-group">
<div class="row ">
<div class="col-md-2"> Images Multible</div>
<div class="col-md-4">
 
  <input type="file" name="post_images[]" id="post_images[]"  multiple>
  
  
  
  
  
  
  
  
</div>
 

</div>
<style >
.ddd{float:left; width:100px; border:1px #F00 solid
	}
</style>


<div class="form-group">
<div class="row ">
<div class="col-md-10"> 
 <?php    $selImages=mysqli_query($connect,"select * from postImages
 where postImages_postid=".$post_row['post_id']."");
 while($rowimg=mysqli_fetch_array($selImages)){ ?>
 <div style="width:100px !important; float:left; margin:20px">
<img src="../postFiles/<?php  echo($rowimg['postImages_image']); ?>" style="width:100px; height:100px"> <a href="index.php?page=ContentPosts&opt=up&id=<?php  echo($rowimg['postImages_postid']); ?>&deleteimg=1&imgid=<?php  echo($rowimg['postImages_id']); ?>&soid=<?php  echo(post($connect,$_REQUEST['soid'])); ?>&sid=<?php  echo(post($connect,$_REQUEST['sid'])); ?>" onClick="return confirm('are you sure');">delete</a></div> 

<?php }  ?>
</div>
  
  

 


</div>
</div>




 
<div class="form-group">
<div class="row ">
<div class="col-md-2"> Vedio or Audio </div>
<div class="col-md-4">
 
  <input type="file" name="post_ved_adu" id="post_ved_adu">
</div>
 <div class="col-md-4"><?php  if($post_row['post_ved_adu']!=''){?> <a href="../postFiles/<?php  echo($post_row['post_ved_adu']); ?>" ><?php  echo($post_row['post_ved_adu']); ?></a> <a onClick="return confirm('are you sure');" href="index.php?page=ContentPosts&opt=up&id=<?php  echo($post_row['post_id']); ?>&deleteva=1&soid=<?php  echo(post($connect,$_REQUEST['soid'])); ?>&sid=<?php  echo(post($connect,$_REQUEST['sid'])); ?>">delete</a><?php }  ?></div>

</div>
</div>
 
 
<div class="form-group">
<div class="row ">
<div class="col-md-2"> Youtube Link </div>
<div class="col-md-4">
 
  <input type="text" name="post_youtube" id="post_youtube" class="form-control"  value="<?php echo($post_row['post_youtube']); ?>">
</div>
 <div class="col-md-4"><?php  if($post_row['post_youtube']!=''){ ?><iframe width="200" height="200" src="https://www.youtube-nocookie.com/embed/<?php  echo(youtubeId($post_row['post_youtube'])); ?>" frameborder="0" allowfullscreen></iframe><?php } ?></div>

</div>
</div>
 


 

<div class="form-group">
<div class="row ">

<div class="col-md-12"><input type="submit" name="update_post" value="update post" class="action-button"  style="width:120px"/></div>
</div>
</div>

</form>
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
              


                       <br>
<br>
<br>
<br>






<br>
<br>   
                 
                 
                 
                 
              </fieldset>   
                 
                 
                 
                  <?php  
				 	}
				 ?>
                 
                 
                 
                
                 <!-----      end update post       -->
                 
                
               <!-----       add post       -->
                
                <?php 
				
			if($_REQUEST['opt']==do_add)
{
	
	if($_REQUEST['post_title']==''
 
	){
		
		echo("<div style='color:red'> Please Fill All Parameters </div>");
		}else{
 



 $extva=Get_Extension_From_File($_FILES['post_ved_adu']['name']);
 $newNameva="va".strtotime(date("y-m-d G:i:s")).".".$extva;
  if(upload("../postFiles/",$newNameva  ,$_FILES['post_ved_adu']['tmp_name'])==1){
	$addVedAduio=  " , `post_ved_adu`='".$newNameva."'";
  }






 $post_query="insert into post
set 
 `post_date`=now(),
  `post_title`='".post($connect,$_REQUEST['post_title'])."',
   `post_parent`=0,
    `post_text`='".post($connect,$_REQUEST['post_text'])."',    
	 `post_youtube`='".post($connect,$_REQUEST['post_youtube'])."',
	  `postLink`='".generateRandom(7)."'
,	  post_serviceOperID='".post($connect,$_REQUEST['soid'])."'
 ".$addVedAduio."
 ";
 
 
 
 $situation=mysqli_query($connect,$post_query);
 if($situation){ 
 $id=mysqli_insert_id($connect);
 
 $postImages=$_FILES['post_images'];
  
 // print_r($postImages);
  
  
 for($i=0;$i<=sizeof($postImages);$i++){
	// echo($postImages['name'][$i]);
	// exit();
 //upload files
 $ext=Get_Extension_From_File($postImages['name'][$i]);
 $newName=strtotime(date("y-m-d G:i:s")).$i.".".$ext;
  if(upload("../postFiles/",$newName  ,$postImages['tmp_name'][$i])==1){
 $insertpostImages=mysqli_query($connect,"insert into postImages
 set postImages_image='".$newName."'
 ,postImages_postid=".$id."");
  }
 }
 
 
 
 
 
 
 
 
 
 echo("<div>post Added SucessFully</div>");}
}
go("index.php?page=ContentPosts&opt=up&sid=".post($connect,$_REQUEST['sid'])."&soid=".post($connect,$_REQUEST['soid'])     );

}
			
			
			
				 ?>
                
                
                
                   <?php 
				   if($_REQUEST['opt']==add)
				   { 
				   ?>
                   
                   
                   
                   
                   
                   
                  <fieldset>
<legend>Add New Post</legend>      
                   
                   
                   
                   
                   
                   
                   
                       <form method="post" enctype="multipart/form-data"  class="login_form" >
<input type="hidden" name="opt" value="do_add" />
<input type="hidden" name="sid" value="<?php echo(post($connect,$_REQUEST['sid'])); ?>" />
  <input type="hidden" name="soid" value="<?php echo(post($connect,$_REQUEST['soid'])); ?>" />
<div class="form-group">
<div class="row ">
<div class="col-md-2"> Title </div>
<div class="col-md-4"><input type="text" name="post_title" id="post_title" class="form-control" placeholder="title" /></div>
 

</div>
</div>

<div class="form-group">
<div class="row ">
<div class="col-md-1"> Text </div>
<div class="col-md-10"><?php /*?><textarea name="post_text"></textarea><?php */?>
<?php 
$sBasePath = $_SERVER['PHP_SELF'] ;
$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "_samples" ) ) ;
$oFCKeditor = new FCKeditor('post_text') ;
$oFCKeditor->BasePath	= $sBasePath ;
$oFCKeditor->Height	= '300' ;
$oFCKeditor->Value		= $post_row['post_text'] ;
$oFCKeditor->Create() ;  ?>
</div>
 

</div>
</div>






<div class="form-group">
<div class="row ">
<div class="col-md-2"> Images Multible</div>
<div class="col-md-4">
 
  <input type="file" name="post_images[]" id="post_images[]"  multiple>
</div>
 

</div>
</div>





 
<div class="form-group">
<div class="row ">
<div class="col-md-2"> Vedio or Audio </div>
<div class="col-md-4">
 
  <input type="file" name="post_ved_adu" id="post_ved_adu">
</div>
 

</div>
</div>
 
 
<div class="form-group">
<div class="row ">
<div class="col-md-2"> Youtube Link </div>
<div class="col-md-4">
 
  <input type="text" name="post_youtube" id="post_youtube">
</div>
 

</div>
</div>
 

<div class="form-group">
<div class="row ">

<div class="col-md-12"><input type="submit" name="Add_post" value="Add post" class="action-button"  style="width:120px"/></div>
</div>
</div>

</form>







    </fieldset>












                       <br>
<br>
<br>
<br>






<br>
<br>

 <!-----      end  add post       -->
                       
                       <?php } ?>
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                      
                  <fieldset>
<legend>Search</legend>
                       
                       
                       <form method="post"  class="login_form"  action="index.php?page=ContentPosts">
<input type="hidden" name="optsearch" value="1" />
<input type="hidden" name="sid" value="<?php echo(post($connect,$_REQUEST['sid'])); ?>" />
  <input type="hidden" name="soid" value="<?php echo(post($connect,$_REQUEST['soid'])); ?>" />
 
<div class="form-group">
<div class="row ">
<div class="col-md-2">Name</div>
<div class="col-md-4"><input type="text" name="post_nameSearch" id="post_nameSearch" class="form-control" placeholder="post_name" value="<?php echo($_REQUEST['post_nameSearch']); ?>" /></div>
</div>
</div>

 
  

<div class="form-group">
<div class="row ">

<div class="col-md-12"><input type="submit" name="update_post" value="Search post" class="action-button"  style="width:120px"/></div>
</div>
</div>

</form>
                       
                       
                       
                       
                      
            </fieldset>           
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       <div class="row">
                       <div class="col-md-12">
                         <a href="index.php?page=ContentPosts&opt=add&sid=<?php echo(post($connect,$_REQUEST['sid'])); ?>&soid=<?php echo(post($connect,$_REQUEST['soid'])); ?>" ><i class="glyphicon glyphicon-plus icon-white"></i> Add new Post</a><br>

                         <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table table-striped table-bordered bootstrap-datatable datatable responsive">
                                         
                         
                         
                           <tr>
                             <td>Post Title </td>
                             <td align="center" valign="middle">Link</td>
                             <td align="center" valign="middle">Preview</td>
                             <td align="center" valign="middle">Operation </td>
                           </tr>
                           <?php  
						   
						$sqlQuery="select * from post inner join servicesoperator  on `post_serviceOperID`=`ServicesOperator_id`
inner join services on `Services_id`=ServicesOperator_servicesID
where `ServicesOperator_id`='".post($connect,$_REQUEST['soid'])."' 
and `ServicesOperator_servicesID`='".post($connect,$_REQUEST['sid'])."'
";
			 
			 
			 if(Role($_SESSION['userID'],$connect)!=1){
				 $sqlQuery=$sqlQuery." and compnayId='".getCompnay($connect,$_SESSION['userID'])."'";
				 
				 }
			 $query_post=$sqlQuery;
 
 
 
if($_REQUEST['post_nameSearch']!=''){
	$query_post=$query_post." and post_title like '%".$_REQUEST['post_nameSearch']."%'";
	 
	}
	
	
  //echo($query_post);
		 
			 $post_res=mysqli_query($connect,$query_post);
			
			 if($post_res){
				 while($row=mysqli_fetch_assoc($post_res))
				 {
				
			  ?>
				 
                           <tr>
                             <td><?php echo($row['post_title']); ?></td>
                            <td><?php echo($row['postLink']); ?></td>
                             <td align="center" valign="middle"><a href="/offersWeb/platform/c.php?link=<?php echo($row['postLink']); ?>" target="_blank">Preview</a></td>
                             <td align="center" valign="middle"><a href="index.php?page=ContentPosts&opt=up&id=<?php  echo($row['post_id']); ?>&sid=<?php echo(post($connect,$_REQUEST['sid'])); ?>&soid=<?php echo(post($connect,$_REQUEST['soid'])); ?>"  > 
                              <i class="glyphicon glyphicon-edit icon-white"></i> Update</a> | 
                              
                              <?php //  <i class="glyphicon glyphicon-trash icon-white"></i> Delete
                               ?>
                               
                               </a>
                              </td>
                           </tr>
                           
                           
                           <?php  }}?>
                        
                  </table>
                  <br />


                 
                  </div></div>
                   
                </div>
            </div>
        </div>