<?php

//require once is necessary when working with multiple mods or snippets..
require_once(SITE_PATH.'/admin/classes/db.master.functions.class.php'); 

class blogFunctions extends dbMasterFunctions {

public function showButtons($page_id,$section_id){
echo '<ul class="nav nav-tabs" role="tablist" id="mainTabs">';
echo '<li role="presentation" class="active"><a href="#categories" aria-controls="cats" role="tab" data-toggle="tab">Categories</a></li>';
echo '<li role="presentation"><a href="#posts" aria-controls="cats" role="tab" data-toggle="tab">Posts</a></li>';
echo '<li role="presentation"><a href="#settings" aria-controls="options" role="tab" data-toggle="tab">Settings</a></li>';
echo '</ul>';
}

//ADMIN  CATEGORIES..
public function showblogCatsAdmin($page_id,$section_id,$mod_id,$mod_name,$mod_path){

print '
<h3>Blog Categories <a class="btn btn-danger btn-sm pull-right" href="../modules/'.$mod_name.'/admin/create_category.php?page_id='.$page_id.'&section_id='.$section_id.'&mod_id='.$mod_id.'">Create Category</a></h3>
<hr/>';
$show_rows = array('cat_id','category_name');
//function variables $variables = null,$table,$where = null,$and = null,$order_by = null,$direction = null,$limit = null,$show_rows=array(),$view_file = null,$edit_file = null,$delete_file = null,$find_by_id_alt = null,$find_by_id = null
$this->listRecordsAdmin($page_id,$section_id,$mod_id,null,$table='mod_blog_categories',"page_id = '$page_id' AND section_id = '$section_id'",null,$order_by='category_name',$direction='ASC',null,$show_rows,$mod_path.'/admin/edit_category.php','cid','cat_id'); 

}

//ADMIN  POSTS..
public function showblogPostsAdmin($page_id,$section_id,$mod_id,$mod_name,$mod_path){

print '
<h3>Blog Posts <a class="btn btn-danger btn-sm pull-right" href="../modules/'.$mod_name.'/admin/create_post.php?page_id='.$page_id.'&section_id='.$section_id.'&mod_id='.$mod_id.'">Create Post</a></h3>
<hr/>';
$show_rows = array('post_id','post_title');
//function variables $variables = null,$table,$where = null,$and = null,$order_by = null,$direction = null,$limit = null,$show_rows=array(),$view_file = null,$edit_file = null,$delete_file = null,$find_by_id_alt = null,$find_by_id = null
$this->listRecordsAdmin($page_id,$section_id,$mod_id,null,$table='mod_blog_posts',"page_id = '$page_id' AND section_id = '$section_id'",null,$order_by='post_title',$direction='ASC',null,$show_rows,$mod_path.'/admin/edit_post.php','pid','post_id'); 

}


public function showPosts($page_id){
	$this->getPosts($page_id);
}
public function showBlogId($page_id,$section_id){
	$this->getBlogId($page_id,$section_id);
}

public function showCatInfo($page_id,$section_id,$cat_id){
	$this->getCatInfo($page_id,$section_id,$cat_id); 
}


private function getBlogId($page_id,$section_id){
require(SITE_PATH.'/includes/db_connect.php');
$query = "SELECT * FROM mod_blog WHERE page_id = '$page_id' AND section_id = '$section_id'";
$result = $db->query($query);
if(!$result){
	echo 'No page id found...';
	exit;
}	

$row = mysqli_fetch_array($result); 

global $blog_id;

$blog_id = $row['blog_id']; 

return $blog_id;

$db->close(); 
}

private function getCats($page_id,$section_id,$blog_id,$version){
require(SITE_PATH.'/includes/db_connect.php');


$query = "SELECT * FROM mod_blog_categories WHERE page_id = '$page_id' and section_id = '$section_id' AND blog_id = '$blog_id' ORDER BY category_name";
$result = $db->query($query);

//MAIN ADMIN LIST
if($version == 'admin_main'){
echo '
<h3>Blog Categories</h3>
<div class="well">
';

echo '
<table class="table">
<thead>
<th>Category Name</th>
<th>Manage</th>
</thead>
<tbody>';
while($row = mysqli_fetch_array($result)){
$cat_id = $row['cat_id']; 
$blog_id = $row['blog_id'];
$category_name = $row['category_name']; 
echo '<tr>
<td>'.$category_name.'</td>';
$category_name = strtolower(str_replace(' ','_',$category_name));
echo '
<td><a href="../modules/blog/admin/edit_category.php?page_id='.$page_id.'&section_id='.$section_id.'&blog_id='.$blog_id.'&cat_id='.$cat_id.'">Edit</a> | <a class="deleteCategory" href="#" id="'.$cat_id.'">Delete</a></td>
</tr>';
}
echo '
</tbody>
</table>
</div>';
}

//POST LIST
if($version == 'post'){
while($row = mysqli_fetch_array($result)){
$cat_id = $row['cat_id']; 
$category_name = $row['category_name']; 
echo '<option value="'.$cat_id.'">'.$category_name.'</option>'.PHP_EOL;
}
}
$db->close(); 

}

public function showCatsFront($page_id,$section_id){
require(SITE_PATH.'/includes/db_connect.php');
$query = "SELECT * FROM mod_blog_categories WHERE page_id = '$page_id' and section_id = '$section_id' ORDER BY category_name";
$result = $db->query($query);
$num_rows = mysqli_num_rows($result);

if($num_rows > 0){ 

print '  <aside class="widget widget_categories">
                                <h3 class="widget-title h5">Categories</h3>
                                <ul>';
		 
while($row = mysqli_fetch_array($result)){
echo '<li class="cat-item"><a href="?category='.$row['category_name'].'&c='.$row['cat_id'].'">'.$row['category_name'].'</a></li>'.PHP_EOL;
}

print ' 
</ul>
</aside><!-- /widget_categories -->';
}

$db->close(); 

}

private function getPosts($page_id){
require(SITE_PATH.'/includes/db_connect.php');

echo '
<h3>Blog Posts</h3>
<div class="well">
';
//the main query...
$query = "
SELECT * FROM mod_blog 
LEFT JOIN mod_blog_posts ON mod_blog.blog_id = mod_blog_posts.blog_id 
LEFT JOIN mod_blog_categories ON mod_blog_categories.cat_id = mod_blog_posts.cat_id
WHERE mod_blog.page_id = '$page_id'";
$result = $db->query($query);
$num_rows = mysqli_num_rows($result); 
if($num_rows < 1){
	echo 'No posts found...';
}
else {
echo '
<table class="table">
<thead>
<th>Post Title</th>
<th>Category</th>
<th>Created By</th>
<th>Date Created</th>
<th>Manage</th>
</thead>
<tbody>
';
while($row = mysqli_fetch_array($result)){
	
$post_title = $row['post_title']; 
$post_id = $row['post_id'];
$category_name = $row['category_name']; 
$created_by = $row['created_by'];
$date_created = $row['date_created'];
$tags = $row['tags'];

echo '<tr>
<td>'.$post_title.'</td>
<td>'.$category_name.'</td>
<td>'.$created_by.'</td>
<td>'.$date_created.'</td>';
$category_name = strtolower(str_replace(' ','_',$category_name));
$post_title = strtolower(str_replace(' ','_',$post_title));
echo '
<td><a href="../modules/blog/admin/edit_post.php?post_id='.$post_id.'">Edit</a> | <a class="deletePost" href="#" id="'.$post_id.'">Delete</a></td>
</tr>';
}
echo '
</tbody>
</table>
</div>';
}
$db->close(); 
}

protected function getCatInfo($page_id,$section_id,$cat_id){

require_once(SITE_PATH.'/includes/db_connect.php');

$query = "
SELECT * FROM mod_blog_categories WHERE page_id = '$page_id' AND section_id = '$section_id' AND cat_id = '$cat_id'";
$result = $db->query($query);
if(!$result){
	exit(mysqli_error($db));
}	

$row = mysqli_fetch_array($result); 

global $category_name;
$category_name = $row['category_name'];

return $category_name;

$db->close(); 

}

public function showBlogPosts($page_id,$section_id,$mod_id,$page){

//pager
if($page == ''){
$page = 1;
}
$per_page = 10; //not user defined; set manually in this case. 
$start_from = ($page-1) * $per_page;
	
require(SITE_PATH.'/includes/db_connect.php');
$query = "SELECT * FROM mod_blog_posts LEFT JOIN mod_blog_categories ON mod_blog_posts.cat_id = mod_blog_categories.cat_id WHERE mod_blog_posts.page_id = '$page_id' AND mod_blog_posts.section_id = '$section_id' ORDER BY date_created LIMIT $start_from,$per_page";
$result = $db->query($query);
$num_rows = mysqli_num_rows($result);

if($num_rows > 0){

$posts_found = 'yes';
	
while($row = mysqli_fetch_array($result)){
$cat_id = $row['cat_id'];
$category_name = $row['category_name'];
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$created_by = $row['created_by'];
$post = $row['post'];
$main_blog_image = $row['main_blog_image'];
$s = $row['date_created'];
$dt = new DateTime($s);
$day = $dt->format('d');
$month_year = $dt->format('M, Y');
$full_date = $dt->format('M, d Y');
$time = $dt->format('H:i:s');

$post=$post;
if (preg_match('/^.{1,260}\b/s', $post, $match))
{
    $post=$match[0].'>...';
}

echo '
<div class="col-md-12">
<article class="post format-standard">
<div class="post-thumbnail">';

if($main_blog_image != ''){
print '<img class="img-responsive" alt="'.$post_title.'" src="'.SITE_URL.'/modules/blog/uploads/'.$post_id.'/main_image.jpg">';
}

print '
<a href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'" class="overlay"></a>
</div><!--/ post-thumbnail -->

<header class="entry-header">
          <h2 class="entry-title h4">          <a href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'">'.$post_title.'</a></h2>
<span class="byline">Posted by <a href="#">(ADMIN)</a> - </span>
                                    <span class="posted-on"><a href="#">'.$day.' '.$month_year.'</a> - </span>
                                    <span>Posted in <a href="#">'.$category_name.'</a></span>
          </header>
<div class="entry-content">
          '.$post.'
</div>
          <a class="more-link" href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'">Read more</a> </div>
        <!--end item--> 
        
</article>
';
}

}

else {
	$posts_found = 'no';
	
	echo '<div class="blog-holder-12">';
	echo 'No posts found.';
	echo '
		</div>
		<div class="clearfix"></div>
        <div class="col-divider-margin-4"></div>
		';
}
// Count the total records
$rec_query = $query;
$recs_result = $db->query($rec_query); 
$total_records = mysqli_num_rows($recs_result);

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

if($total_records > 0){
	print '

        <br/>
        <div class=" divider-line solid light margin opacity-7"></div>
        <div class="col-md-12">
 <nav class="pager">       
         <ul class="pagination">
          <li><a href="'; 
		  if($page > 1 && $page != 1){
			  print '?page='.$page-1;
		  }
		  else {
			  print '?page='.$page;
		  }
		  print'" class="page-numbers left"><i class="fa fa-angle-left"></i></a></li>';
		  
for ($i=1; $i<=$total_pages; $i++) {
echo '<li><a href="?page='.$i.'" class="page-numbers'; if($i == $page){echo ' current';} echo '" id="'.$i.'">'.$i.'</a></li>';
};

     print '<li><a href="'; 
	 if($page > 1 && $total_pages != $page){
		 print '?page='.($page+1);
	 }
	 else {
		 print '?page='.$page;
	 }
		 print'" class="page-numbers right"><i class="fa fa-angle-right"></i></a></li>
        </ul>
</nav>
 </div>      

';
}

$db->close(); 

}

public function showPost($post_id){

require(SITE_PATH.'/includes/db_connect.php');
$query = "SELECT * FROM mod_blog_posts LEFT JOIN mod_blog_categories ON mod_blog_posts.cat_id = mod_blog_categories.cat_id WHERE mod_blog_posts.post_id = '$post_id'";
$result = $db->query($query);
$row = mysqli_fetch_array($result);
$cat_id = $row['cat_id'];
$category_name = $row['category_name'];
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$created_by = $row['created_by'];
$post = $row['post'];
$main_blog_image = $row['main_blog_image'];
$s = $row['date_created'];
$dt = new DateTime($s);
$day = $dt->format('d');
$month_year = $dt->format('M, Y');
$full_date = $dt->format('M, d Y');
$time = $dt->format('H:i:s');

$tags = $row['tags'];
$tags = explode(', ', $tags);



echo '
      <div class="col-md-12 bmargin">';
if($main_blog_image != ''){
print '
<div class="blog-holder-12">
            <div class="image-holder">
              <div class="overlay bg-opacity-1">
                <div class="post-date-box three">'.$day.'<span>'.$month_year.'</span> </div>
              </div>
              <img class="img-responsive" alt="'.$post_title.'" src="'.SITE_URL.'/modules/blog/uploads/'.$post_id.'/main_image.jpg"> </div>
          </div>
          <div class="clearfix"></div>
          <br/>
		  ';
}
		  print '
          <a href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'">
          <h4 class="less-mar1">'.$post_title.'</h4>
          </a>
          <br/>
          '.$post.'
          <br/>
		  <p>&nbsp;</p>
		  </div>
        <!--end item--> 
        
        <div class="clearfix"></div>
        <div class="col-divider-margin-4"></div>
		
		<br/>
<div style="background: #e9098c; color: #ffffff; width: 275px; height: auto!important; padding: 10px 20px 5px 20px; min-height: 40px; line-height:40px;">
<b>SHARE</b>
            <ul class="social-list white" style="margin-top: -40px; margin-left: 70px;">
              <li><a href="https://twitter.com/home?status='.str_replace(' ','%20',$post_title).'%20%20http%3A//'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'"><i class="fa fa-twitter"></i></a></li>
              <li><a href="https://www.facebook.com/sharer/sharer.php?u=http%3A//'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'"><i class="fa fa-facebook"></i></a></li>

<!--
              <li><a href="https://plus.google.com/share?url=http%3A//'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'"><i class="fa fa-google-plus"></i></a></li>
-->

              <li><a href="https://www.linkedin.com/shareArticle?mini=true&url=http%3A//'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'&title='.str_replace(' ','%20',$post_title).'&summary=&source="><i class="fa fa-linkedin"></i></a></li>
            </ul>
</div>
            <div class="clearfix"></div>
            <br/>
';

$db->close();
/*
//OUT FOR NOW!
print '
<nav class="post-navigation" role="navigation">
<div class="row">
<div class="col-sm-6">
<div class="prev-post">
<span>Previous article </span>
<a href="#" rel="prev">Etiam accumsan</a>
</div>
</div>
<div class="col-sm-6">
<div class="next-post">
<span>Next article </span>
<a href="#" rel="next">Vivamus vel sodales nisi</a>
</div>
</div>
</div>
</nav>
';
*/
}


public function showCatPosts($cat_id,$page){
	
//pager
if($page == ''){
$page = 1;
}
$per_page = 10; //not user defined; set manually in this case. 
$start_from = ($page-1) * $per_page;

require(SITE_PATH.'/includes/db_connect.php'); 
$query = "SELECT * FROM mod_blog_posts LEFT JOIN mod_blog_categories ON mod_blog_posts.cat_id = mod_blog_categories.cat_id WHERE mod_blog_posts.cat_id = '$cat_id'";
$result = $db->query($query);
$num_rows = mysqli_num_rows($result);

if($num_rows > 0){

$posts_found = 'yes';
	
while($row = mysqli_fetch_array($result)){
$cat_id = $row['cat_id'];
$category_name = $row['category_name'];
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$created_by = $row['created_by'];
$post = $row['post'];
$main_blog_image = $row['main_blog_image'];
$s = $row['date_created'];
$dt = new DateTime($s);
$day = $dt->format('d');
$month_year = $dt->format('M, Y');
$full_date = $dt->format('M, d Y');
$time = $dt->format('H:i:s');

$post=$post;
if (preg_match('/^.{1,260}\b/s', $post, $match))
{
    $post = $match[0].'>...';
}

echo '
<div class="col-md-12">
<article class="post format-standard">
<div class="post-thumbnail">';

if($main_blog_image != ''){
print '<img class="img-responsive" alt="'.$post_title.'" src="'.SITE_URL.'/modules/blog/uploads/'.$post_id.'/main_image.jpg">';
}

print '
<a href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'" class="overlay"></a>
</div><!--/ post-thumbnail -->

<header class="entry-header">
          <h2 class="entry-title h4">          <a href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'">'.$post_title.'</a></h2>
<span class="byline">Posted by <a href="#">(ADMIN)</a> - </span>
                                    <span class="posted-on"><a href="#">'.$day.' '.$month_year.'</a> - </span>
                                    <span>Posted in <a href="#">'.$category_name.'</a></span>
          </header>
<div class="entry-content">
          '.$post.'
</div>
          <a class="more-link" href="?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'" title="'.$row['post_title'].'">Read more</a> </div>
        <!--end item--> 
        
</article>
';
}

}

else {
	$posts_found = 'no';
	
	echo '<div class="blog-holder-12">';
	echo 'No posts found.';
	echo '
		</div>
		<div class="clearfix"></div>
        <div class="col-divider-margin-4"></div>
		';
}
// Count the total records
$rec_query = $query;
$recs_result = $db->query($rec_query); 
$total_records = mysqli_num_rows($recs_result);

//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);

if($total_records > 0){
	print '

        <br/>
        <div class=" divider-line solid light margin opacity-7"></div>
        <div class="col-md-12">
 <nav class="pager">       
         <ul class="pagination">
          <li><a href="'; 
		  if($page > 1 && $page != 1){
			  print '?page='.$page-1;
		  }
		  else {
			  print '?page='.$page;
		  }
		  print'" class="page-numbers left"><i class="fa fa-angle-left"></i></a></li>';
		  
for ($i=1; $i<=$total_pages; $i++) {
echo '<li><a href="?page='.$i.'" class="page-numbers'; if($i == $page){echo ' current';} echo '" id="'.$i.'">'.$i.'</a></li>';
};

     print '<li><a href="'; 
	 if($page > 1 && $total_pages != $page){
		 print '?page='.($page+1);
	 }
	 else {
		 print '?page='.$page;
	 }
		 print'" class="page-numbers right"><i class="fa fa-angle-right"></i></a></li>
        </ul>
</nav>
 </div> 

';
}

$db->close(); 
}




public function showBlogPostsWidget($news_page,$page_id,$limit){
	
require(SITE_PATH.'/includes/db_connect.php');
$query = "SELECT * FROM mod_blog_posts LEFT JOIN mod_blog_categories ON mod_blog_posts.cat_id = mod_blog_categories.cat_id WHERE mod_blog_posts.page_id = '$page_id' LIMIT $limit";
$result = $db->query($query);
$num_rows = mysqli_num_rows($result);
print '
<div class="posts" data-paginate="page_num" data-layout="default">
<div class="posts-wrap posts-layout-default">
';
if($num_rows > 0){
while($row = mysqli_fetch_array($result)){
$cat_id = $row['cat_id'];
$category_name = $row['category_name'];
$post_id = $row['post_id'];
$post_title = $row['post_title'];
$date_created = $row['date_created'];
$created_by = $row['created_by'];
$post = $row['post'];
$post_id = $row['post_id'];
$main_blog_image = $row['main_blog_image'];

$post=$post;
if (preg_match('/^.{1,50}\b/s', $post, $match))
{
    $post=$match[0].'...';
}

$s = $row['date_created'];
$dt = new DateTime($s);
$day = $dt->format('d');
$month_year = $date = $dt->format('M Y');
$time = $dt->format('H:i:s');

if($main_blog_image != ''){
echo '
<div class="image-left">
<div class="fo-postimg-inner overflow-hidden"><img height="80" width="80" src="'.SITE_URL.'/modules/blog/uploads/'.$post_id.'/main_image.jpg" alt=""/></div>
</div>';
}
echo '
<div class="';
if($main_blog_image != ''){
print 'text-box-right';
}
else {
	print 'text-box';
}
echo '">
<h5 class="text-white less-mar3"><a href="'.SITE_URL.'/pages/'.$news_page.'?category='.$category_name.'&c='.$cat_id.'&post_title='.$post_title.'&p='.$post_id.'">'.$post_title.'</a></h5>
'.$post.'<br/>
<div class="footer-post-info"><span>'.$month_year.'</span> </div>
</div>
<div class="divider-line solid dark margin opacity-5"></div>
';
}
}

else {
	echo 'No posts found.';
}

print '
</div>
</div>
';

$db->close(); 

}

}//end class
?>