<?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>Categories 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 = $_GET['do'];
if ( ! in_array($action, array('update', 'new', 'delete', 'move')) )
{
	$action = 'new';	//	default action
}

if ('' != $_POST['update'] || '' != $_POST['submit']) 
{
	if ($action == 'new')
	{
		$result = art_insert_category($_POST);
	}
	else if ($action == 'update')
	{
		$cid = (int) $_POST['cid'];
		$result = art_update_category($cid, $_POST);
	}
	
	if ($result['type'] == 'error')
	{
		echo '<div class="info_msg_err">'. $result['msg'] .'</div>';
	}
	else
	{
		$_POST = array();
		echo '<div class="info_msg_ok">'. $result['msg'] .'</div>';
	}
}

if ($_GET['move'] != '' && $_GET['id'] != '')
{
	$id = (int) $_GET['id'];
	
	if ($id > 0)
	{
		$cat = art_get_categories();
		
		$limit = 0;
		$is_parent = false;
		$is_child = false;
			
		if ($cat[$id]['parent_id'] == 0)
		{
			foreach ($cat as $c_id => $c_arr)
			{
				if ($c_arr['parent_id'] == 0)
				{
					$is_parent = true;
					$limit++;
				}
			}
		}
		else
		{
			foreach ($cat as $c_id => $c_arr)
			{
				if ($c_arr['parent_id'] == $cat[$id]['parent_id'])
				{
					$is_child = true;
					$limit++;
				}
			}
		}
		
		$current_position = $cat[$id]['position'];
		$prev_cat_id = $next_cat_id = 0;
		
		// find neighbours 
		foreach ($cat as $c_id => $c_arr)
		{
			if ($c_arr['position'] == ($current_position - 1) && $c_arr['parent_id'] == $cat[$id]['parent_id'])
			{
				$prev_cat_id = $c_id;
			}
			
			if ($c_arr['position'] == ($current_position + 1) && $c_arr['parent_id'] == $cat[$id]['parent_id'])
			{
				$next_cat_id = $c_id;
			}
		}
		
		switch ($_GET['move'])
		{
			case 'up':
				
				if ($current_position > 1 && $current_position <= $limit && $prev_cat_id)
				{
					$sql_1 = "UPDATE art_categories
							   SET position = '". ($cat[$prev_cat_id]['position'] + 1) ."' 
							 WHERE id = '". $prev_cat_id ."'";
					$sql_2 = "UPDATE art_categories
							   SET position = '". ($cat[$id]['position'] - 1) ."' 
							 WHERE id = '". $id ."'";
					
					$cat[$prev_cat_id]['position']++;
					$cat[$id]['position']--;
				}
				
			break;
	
			case 'down':
				
				if ($current_position >= 1 && $current_position < $limit && $next_cat_id)
				{
					$sql_1 = "UPDATE art_categories
							   SET position = '". ($cat[$id]['position'] + 1) ."' 
							 WHERE id = '". $id ."'";
					
					$sql_2 = "UPDATE art_categories
							   SET position = '". ($cat[$next_cat_id]['position'] - 1) ."' 
							 WHERE id = '". $next_cat_id ."'";
					
					$cat[$id]['position']++;
					$cat[$next_cat_id]['position']--;
				}
				
			break;
		}

		if ($sql_1 != '' && $sql_2 != '')
		{
			if ( ! ($result = mysql_query($sql_1)))
			{
				$info_msg  = '<div class="info_msg_err">There was a problem while updating your database!<br />';
				$info_msg .= '<strong>MySQL Reported:</strong>: '.mysql_error().'</div>';
			}
			else
			{
				if ( ! ($result = mysql_query($sql_2)))
				{
					$info_msg  = '<div class="info_msg_err">There was a problem while updating your database!<br />';
					$info_msg .= '<strong>MySQL Reported:</strong>: '.mysql_error().'</div>';
				}
			}
			
			echo $info_msg;
			unset($_article_categories);
		}
	}
	
	if ($info_msg == '')
	{
		echo '<meta http-equiv="refresh" content="0;URL=article_categories.php" />';
		exit();
	}
	
}

$categories = art_get_categories();
$total_categories = count($categories);

$categories_dropdown_options = array('db_table' => 'art_categories',
									 'first_option_text' => '- Root -', 
									 'selected' => $_POST['parent_id'], 
									 'attr_name' => 'parent_id'
									 );

?>
<div id="display_result" style="display:none;"></div>
<form name="article_new_category" action="article_categories.php?do=new" method="post">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
 <tr>
  <td valign="top">
   <input name="name" type="text" onclick='value=""' value="<?php echo ($_POST['name'] != '') ? $_POST['name'] : 'Category Name';?>" size="22" />
   <input name="tag" type="text" onclick='value=""' value="<?php echo ($_POST['tag'] != '') ? $_POST['tag'] : 'tag';?>" size="10" /> 
   Create in:
   <?php echo categories_dropdown($categories_dropdown_options); ?>
   <input name="submit" type="submit" value="Add category" class="inputbutton" />
   <br />
   <small>* tags are required and are used in URLs. Tags can only contain numbers, letters, dashes '-' and underscores '_'.</small>
  </td>
  <td width="220" valign="top">
   <div class="totals">
    Total categories: <?php echo $total_categories; ?>
   </div>
  </td>
 </tr>
</table>
</form>

<table width="100%" class="tablesorter" cellspacing="1">
 <thead>
  <tr>
  	<th width="5%">ID</th>
	<th width="25%">Category Name</th>
    <th width="30%">Tag</th>
    <th width="25%">Parent Category</th>
	<th width="5%">Articles</th>
	<th width="5%">Position</th>
    <th width="10%" align="center">Action</th>
  </tr>
 </thead>
 <tbody>
	
	<?php 
	$args = array('page' => 'article_categories.php',
				  'form_action' => 'article_categories.php?do=update',
				);		
	echo a_category_table_body($categories, $args);	
	?>
 </tbody>
</table>

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