<?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_pages';
include('header.php');

$action = $_GET['do'];
if ( ! in_array($action, array('edit', 'new', 'delete')) )
{
	$action = 'new';	//	default action
}

?>
<div id="sub_menu">
<li><a href="page_manager.php?do=new">Add new</a></li>
<li><a href="pages.php">Manage pages</a></li>
</div>

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

<?php

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

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


$total_pages = 0;
$limit = 20;
$from = $current_page * $limit - ($limit);

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

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

//	Batch delete
if ('' != $_POST['submit'] && $_POST['submit'] == "Delete checked")
{
	if(count($_POST['checkboxes']) > 0)
	{
		$page_ids = array();
		foreach ($_POST['checkboxes'] as $k => $id)
		{
			$page_ids[] = (int) $id;
		}
		
		$result = mass_delete_pages($page_ids);
		
		if ($result['type'] == 'error')
		{
			echo '<div class="info_msg_errimg">'. $result['msg'] .'</div>';
		}
		else
		{
			echo '<div class="info_msg_ok">'. $result['msg'] .'</div>';
		}
	}
	else
	{
		echo '<div class="info_msg_ok">No items were selected!</div>';
	}
}

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

		case 'public':
		
			$total_pages = count_entries('pm_pages', 'status', '1');
		
		break;
	}
	$pages = list_pages('', '', $from , $limit, $filter, $filter_value);
}

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

if( ! isset($_POST['submit']))
{
	$pagination = a_generate_smart_pagination($current_page, $total_pages, $limit, 5, $filename, '');
} 
	

?>
<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="pages.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 pages: <?php echo $total_pages; ?>
	 </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 by: </strong></small>&nbsp;
	 <form name="other_filter" action="pages.php">
	 <select name="URL" onChange="window.parent.location=this.form.URL.options[this.form.URL.selectedIndex].value">
	 	<option value="">by status ...</option>
	 	<option value="pages.php?page=1&filter=mostviewed">Most viewed</option>
	 	<option value="pages.php?page=1&filter=public">Published</option>
	 	<option value="pages.php?page=1&filter=private">Private</option>
	 </select>
	 </form>
	 <a href="pages.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="pages_checkboxes" id="pages_checkboxes" action="pages.php?page=<?php echo $current_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="6"></th>
	<th width="50%">Title</th>
	<th width="5%">Views</th>
	<th width="10%">Author</th>	
    <th width="90">Added</th>
    <th width="120">Action</th>
   </tr>
  </thead>
  <tbody>
	<?php 
	
	/*
	 *  List existing pages
	 */ 
	if ( ! array_key_exists('type', $pages) && $total_pages > 0)
	{
		$alt = 1;
		
		foreach ($pages as $k => $page)
		{
			$col = ($alt % 2) ? 'table_row1' : 'table_row2';
			$alt++;
			
			?>
			 
			<tr class="<?php echo $col;?>" id="page-<?php echo $page['id'];?>">
			 <td align="center" width="3%">
			 	<input name="checkboxes[]" type="checkbox" value="<?php echo $page['id']; ?>" />
			 </td>
			 <td align="center">
			    <?php 
					if ($page['status'] == 0)
					{
						echo ' <img src="images/locked.png" onMouseover="showhint(\'This page is private. Only the Administrator can see this page.\', this, event, \'220px\')" border="0"> ';
					}
				?>
			 </td>
			 <td>
			   <a href="<?php echo _URL.'/page.php?p='. $page['id']; ?>" target="_blank"><?php echo $page['title']; ?></a>
     		 </td>
			 <td align="center"><?php echo pm_number_format($page['views']); ?></td>
			 <td align="center">
			  <?php 
			  	$author = fetch_user_advanced($page['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', $page['date']); ?></small></td>
			 <td align="center">
			  <a href="page_manager.php?do=edit&id=<?php echo $page['id'];?>" class="b_edit">Edit</a>
			  | <a href="#" onclick="onpage_delete_page('<?php echo $page['id']; ?>', '#display_result', '#page-<?php echo $page['id'];?>')" class="b_delete">Delete</a>
			 </td>
		    </tr>
			
			<?php
		}
	}
	else	//	Error?
	{
		if (strlen($pages['msg']) > 0)
		{
			echo '<div class="info_msg_err">'. $pages['msg'] .'</div>';
		}
		
		if ($total_pages == 0)
		{
			?>
			<tr>
			 <td colspan="8" align="center">
			 There aren't any pages matching your criteria.
			 </td>
			</tr>
			<?php
		}
	}
	?>
  </tbody>
 </table>
 <?php
 if ($total_pages > 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> <!-- #content end -->
<?php
include('footer.php');
?>