<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

$showm = 'mod_article';
include('header.php');

?>
<div id="sub_menu">
<li><a href="article_manager.php?do=new">Post a new article</a></li>
<li><a href="articles.php">Manage articles</a></li>
<li><a href="article_categories.php">Manage categories</a></li>
<?php 
if (is_admin() || (is_moderator() && mod_can('manage_comments')))
{
	?>
	<li><a href="comments.php?page=1&filter=articles">Manage comments</a></li>
	<?php
}
?>
</div>

<div id="content">
<h2>Article manager</h2>

 <?php
 if ( ! $config['mod_article'])
 {
 	?>
 	 <div class="info_msg">
	  The Article Module is currently disabled. Please enable it from '<a href="settings.php">Settings</a>'.
	 </div>
	</div>
	<?php
	include('footer.php');
	exit();
 }
 ?>

<?php

$action	= (int) $_GET['action'];
$page	= (int) $_GET['page'];

if($page == 0)
	$page = 1;


$total_articles = 0;
$limit = 20;		//	articles per page
$from = $page * $limit - ($limit);

$filter = '';
$filters = array('public', 'private', 'mostviewed', 'category');
$filter_value = '';

if(in_array(strtolower($_GET['filter']), $filters) !== false)
{
	$filter = strtolower($_GET['filter']);
	$filter_value = $_GET['fv'];
	
	if ($filter == 'category' && $filter_value == '')
	{
		$filter = '';
	}
}


//	Batch delete
if ('' != $_POST['submit'] && $_POST['submit'] == "Delete checked")
{
	$total_checkboxes = count($_POST['checkboxes']);
	if($total_checkboxes > 0 && (is_admin() || (is_moderator() && mod_can('manage_articles'))))
	{
		$article_ids = array();
		foreach ($_POST['checkboxes'] as $k => $id)
		{
			$article_ids[] = (int) $id;
		}
		
		$result = mass_delete_articles($article_ids);
		
		if ($result['type'] == 'error')
		{
			echo '<div class="info_msg_err">'. $result['msg'] .'</div>';
		}
		else
		{
			echo '<div class="info_msg_ok">'. $result['msg'] .'</div>';
		}
	}
	else if ($total_checkboxes > 0 && (is_editor() || (is_moderator() && mod_cannot('manage_articles'))))
	{
		echo '<div class="info_msg_err">You are not allowed delete articles.</div>';
	}
	else
	{
		echo '<div class="info_msg">No items were selected!</div>';
	}
}

if ('' != $_POST['submit'] && $_POST['submit'] == 'Search')
{
	$articles = list_articles($_POST['keywords'], $_POST['search_type'], $from , $limit); 
	$total_articles = count($articles);
}
else
{
	switch ($filter)
	{
		default:
		case 'mostviewed':
		
			$total_articles = $config['total_articles'];
		
		break;
		
		case 'private':
		
			$total_articles = count_entries('art_articles', 'status', '0');
		
		break;

		case 'public':
		
			$total_articles = count_entries('art_articles', 'status', '1');
		
		break;

		case 'category':
		
			$filter_value = (int) $filter_value;
			if ($filter_value > 0)
			{
				$sql = "SELECT COUNT(*) as total_found 
						FROM art_articles  
						WHERE category LIKE '". $filter_value ."' 
						   OR category LIKE '". $filter_value .",%' 
						   OR category LIKE '%,". $filter_value ."' 
						   OR category LIKE '%,". $filter_value .",%'";
				$result = mysql_query($sql);
				$row = mysql_fetch_assoc($result);
				mysql_free_result($result);
				
				$total_articles = $row['total_found'];
				unset($row, $result, $sql);
			}
			else
			{
				$total_articles = 0;
			}
		
		break;
	}
	$articles = list_articles('', '', $from , $limit, $filter, $filter_value); 
}

// generate smart pagination
$filename = 'articles.php';
$pagination = '';

if(!isset($_POST['submit'])) 
	$pagination = a_generate_smart_pagination($page, $total_articles, $limit, 5, $filename, '&filter='. $filter .'&fv='. $filter_value);


if ($_GET['action'] == "deleted") 
{
	echo '<div class="info_msg_err">Comments were deleted.</div>';
}

?>
<div id="display_result" style="display:none;"></div>

<table width="100%" border="0" cellspacing="1" cellpadding="3">
 <tr>
    <td valign="top">
	<form name="search" action="articles.php" method="post" onsubmit="return validateSearch('true');">
	<input type="text" name="keywords" value="<?php echo $_POST['keywords']; ?>" />
	<select name="search_type">
	  <option value="title" selected="selected">Title</option>
	  <option value="content">Content</option>
	</select>
	<input name="submit" type="submit" value="Search" class="inputbutton" />
	<br />
	<small>Please make the search as descriptive as possible.</small>
	</form>	
	</td>
    <td width="220" valign="top">
	 <div class="totals">
	  Total articles: <?php echo $total_articles; ?>
	 </div>
  </td>
 </tr>
</table>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
 <tr>
  <td valign="top">
  <div id="sort_by" align="right">
   <small><strong>Filter: </strong></small>&nbsp;
     <form name="category_filter" action="articles.php" method="get">
	  <input type="hidden" name="filter" value="category" />
	  <select name="fv" onchange=submit()>
	   <option value="">by category...</option>
	   <?php
	   $categories = art_get_categories();
	   
	   foreach ($categories as $id => $cat)
	   {
	   		$option = '<option value="'. $id .'" ';
	  		if ($filter_value == $id && $filter == 'category')
			{
				$option .= ' selected="selected" ';
			}
			$option .= '>'. $cat['name'] .'</option>';
			echo $option;
	   }
	   ?>
	  </select>
	 </form>
	 <form name="other_filter" action="articles.php">
	 <select name="URL" onChange="window.parent.location=this.form.URL.options[this.form.URL.selectedIndex].value">
	 	<option value="videos.php">by status ...</option>
	 	<option value="articles.php?page=1&filter=mostviewed">Most viewed</option>
	 	<option value="articles.php?page=1&filter=public">Published</option>
	 	<option value="articles.php?page=1&filter=private">Private</option>
	 </select>
	 </form>
	 <a href="articles.php" title="Remove filter"><img src="images/ico_filter_remove.gif" width="21" height="15" align="absmiddle" alt="Remove filter" border="0" /></a>
	 </div>
  </td>
 </tr>
</table>
<form name="articles_checkboxes" id="articles_checkboxes" action="articles.php?page=<?php echo $page;?>" method="post">
 <table width="100%" cellspacing="1" class="tablesorter">
 <thead>
   <tr>
	<th align="center" width="3%"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/></th>
	<th width="10"></th>
	<th width="40%">Title</th>
	<th width="5%">Views</th>
    <th width="16%">Categories</th>
	<th width="10%">Author</th>	
    <th width="90">Added</th>
	<th width="150">Comments</th>
    <th width="110">Action</th>
   </tr>
  </thead>
  <tbody>
	<?php 
	
	/*
	 *  List articles
	 */ 
	if ( ! array_key_exists('type', $articles) && $total_articles > 0)
	{
		$alt = 1;
		
		foreach ($articles as $k => $article)
		{
			$col = ($alt % 2) ? 'table_row1' : 'table_row2';
			$alt++;
			
			$total_comments = count_entries('pm_comments', 'uniq_id', 'article-'.$article['id']);
			?>
			 
			<tr class="<?php echo $col;?>" id="article-<?php echo $article['id'];?>">
			 <td align="center" width="3%">
			 	<input name="checkboxes[]" type="checkbox" value="<?php echo $article['id']; ?>" />
			 </td>
			 <td align="center" width="10">
			    <?php 
					if ($article['status'] == 0)
					{
						echo ' <img src="images/locked.png" onMouseover="showhint(\'This article is private. Only the Administrator can see this article.\', this, event, \'220px\')" border="0"> ';
					}
				?>
			 </td>
			 <td>
			   <a href="<?php echo _URL.'/article_read.php?a='. $article['id']; if ($article['status'] == 0) echo '&mode=preview'; ?>" target="_blank"><?php echo $article['title']; ?></a>
			  </td>
			 <td align="center"><?php echo pm_number_format($article['views']); ?></td>
			 <td>
			  <?php 
			 	$str = '';
				foreach ($article['category_as_arr'] as $id => $name)
				{
					if ($id != '' && $name != '')
					{
						$str .= '<a href="articles.php?filter=category&fv='. $id .'" title="List articles from '. $name .' only">'. $name .'</a>, ';
					}
				}
			 	echo substr($str, 0, -2);
			  ?>
			 </td>
			 <td align="center">
			  <?php 
			  	$author = fetch_user_advanced($article['author']);
				
				echo '<a href="edit_user_profile.php?uid='. $author['id'] .'" title="Edit">'. $author['username'] .'</a>';
			  ?>
			 </td>
			 <td align="center"><small><?php echo date('M d Y', $article['date']); ?></small></td>
			 <td align="center"> 
			  <a href="comments.php?vid=<?php echo 'article-'.$article['id'];?>" title="View comments" class="b_view">View</a> 
			  <?php 
			  if (is_admin() || (is_moderator() && mod_can('manage_comments')))
			  {
			  	?>
			  	| <a href="#" title="Delete all comments" onClick='del_video_comments("article-<?php echo $article['id'];?>", "<?php echo $page;?>")' class="b_delete">Delete All (<?php echo $total_comments; ?>)</a>
				<?php
			  }
			  ?>
			 </td>
			 <td align="center">
			  <a href="article_manager.php?do=edit&id=<?php echo $article['id'];?>" class="b_edit">Edit</a>
			  | <a href="#" onclick="onpage_delete_article('<?php echo $article['id']; ?>', '#display_result', '#article-<?php echo $article['id'];?>')" class="b_delete">Delete</a>
			  </td>
		    </tr>
			
			<?php
		}
	}
	else	//	Error?
	{
		if (strlen($articles['msg']) > 0)
		{
			echo '<div class="info_msg_err">'. $articles['msg'] .'</div>';
		}
		
		if ($total_articles == 0)
		{
			?>
			<tr>
			 <td colspan="8" align="center">
			 There aren't any articles matching your criteria.
			 </td>
			</tr>
			<?php
		}
	}
	?>
  </tbody>
 </table>
 <?php
 if ($total_articles > 0)
 {
 ?>
 <table>
    <tr style="background-color: #fff;">
	<td align="left">
	 <input type="submit" name="submit" value="Delete checked" class="deletebutton" onClick="return confirm_delete_all();" /> 
	</td>
   </tr>
 </table>
 <?php
 }
 ?>
</form>

<div align="center"><?php echo $pagination; ?></div>

</div>	<!-- end <div id="content"> -->
<?php
include('footer.php');
?>