Location: PHPKode > projects > Havalite > havalite/hava_cat.php
<?php 
include("hava_head.php"); 
$myCount = 0;

// check if icon column exists
column_exists("cat", "icon", "ALTER TABLE cat ADD icon VARCHAR(255)");
///////////////

if(isset($_GET['catId'])) $catId = $_GET['catId'];
if(isset($_GET['subId'])) $subCat = $_GET['subId'];
if(isset($_GET['del'])) $delCat = $_GET['del'];

$catTitle = $hava_lang['newCat'];

if(isset($_POST['catName'])){
	if(isset($_POST['catId'])) $catId = $_POST['catId'];
	if(isset($_POST['catName'])) $catName = $_POST['catName']; 		else $catName = '';
	if(isset($_POST['catDesc'])) $catDesc = $_POST['catDesc'];		else $catDesc = '';
	if(isset($_POST['catIcon'])) $catIcon = $_POST['catIcon'];		else $catIcon = '';
	//if(isset($_POST['catSort'])) $catSort = $_POST['catSort'];		else $catSort = '';
	//if(isset($_POST['subCat'])) $subCat	 = $_POST['subCat'];		else $catSub = NULL;
	if(isset($_POST['copyCat'])) $copyCat = $_POST['copyCat'];		else $copyCat = NULL;
	
	if(empty($catId)){ // Add new Cat -------------
		$catId = saveSqlite("INSERT INTO cat (name, desc, icon) VALUES (?, ?, ?)", array($catName, $catDesc, $catIcon), 1);
	}
	elseif(!empty($copyCat)){ // copy all posts to another cat ----------------------------
		saveSqlite("UPDATE posts SET cat = ? WHERE cat=?", array($copyCat, $catName), 1); 
	}
	else{ // Change name or description or icon ------------------------------
		$oldCatName = hava_single_query("SELECT * FROM cat WHERE id =?", $catId, "name");
		if($oldCatName != $catName){ // chamge cat name by posts too ------------------------
			saveSqlite("UPDATE posts SET cat=? WHERE cat=?", array($catName, $oldCatName), 1); 
		}
		saveSqlite("UPDATE cat SET name=?, desc=?, icon=? WHERE id=?", array($catName, $catDesc, $catIcon, $catId)); 
	}
}
elseif(isset($delCat) and $userLvl <2){

	saveSqlite("DELETE FROM cat WHERE id = ?", array($delCat));
}


if(isset($catId)){ 
	$catArray = hava_single_query("SELECT * FROM cat WHERE id = ?", $catId);
}

if(isset($subCat)) $subCat =  getCatOptions('id', $subCat);
$copyCat = getCatOptions('name');

function getCatOptions($value, $sel=''){
	$res = '';
	$result = hava_all_queries("SELECT * FROM cat ORDER BY sort");
	foreach($result as $ro){
		$selected = '';
		if($sel and $sel == $ro['id']){ $selected = ' selected="selected"'; }
		$res .= '<option value="'.$ro[$value].'" '.$selected.'>'.$ro['name'].'</option>';
	}
	return $res;
}
?>
<script language="javascript">

// check post title and category -----------------------------
function formSubmission(){
	var catName = $('#catName').val();
	catName = jQuery.trim(catName);
	if(catName.match(/[#&"']/)) {
		alert('<?=$hava_lang['alphaNummeric']; ?>');
		$("#catName").focus();
		return false;
	}
	else if(catName == ""){
		return false;
	}
}

function clearSerialize(str){
	str = str.replace(/(id\[|\]|root)/g, "");
	str = str.replace(/&/g, ",");
	return str;
}

$(document).ready(function(){
	$('ol.sortable').nestedSortable({
		disableNesting: 'no-nest',
		forcePlaceholderSize: true,
		handle: 'div',
		helper:	'clone',
		items: 'li',
		maxLevels: 3,
		opacity: .6,
		placeholder: 'placeholder',
		revert: 250,
		tabSize: 25,
		tolerance: 'pointer',
		toleranceElement: '> div'
	});
		
	$("ol.sortable").nestedSortable({
       	update: function(event, ui) {
			var order = $(this).nestedSortable("serialize");
			var result = clearSerialize(order);
			$('#imgLoad').attr({ src:"sys/img/ajaxLoad.gif" });
			$.post("hava_ajax_response.php", { catSerial:result });
			setTimeout(function(){ $('#imgLoad').attr({ src:"sys/img/trans.gif" }); }, 3000);
		}
	});

	$('#serialize').click(function(){
		serialized = $('ol.sortable').nestedSortable('serialize');
		//$('#serializeOutput').text(serialized+'\n\n');
		result = clearSerialize(serialized);
		window.location.href = '?catSerial=' + result;
	})

});

</script>

<div id="admin_index">
<div id="upper_title">
<table width="100%" border="0" cellspacing="3" cellpadding="0">
  <tr>
    <td width="1"><img src="sys/img/cat.png" width="32" height="32" border="0" /></td>
    <td><?php echo $hava_lang['categories']; ?>  <span id="allPostsNav"><a href="hava_cat.php"><img src="sys/img/newCat.png" border="0" /> <?=$catTitle; ?></a></span></td>
  </tr>
</table>
</div>




<table id="catTable" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="350" valign="top" id="catSplitter">
<div><span id="catTreeTitle"> <?=$hava_lang['categories']; ?>  <?=$hava_lang['sort']; ?> <img src="sys/img/trans.gif" border="0" id="imgLoad" /></span>

<?php

function catMenuArray(){
	$firstRes = hava_all_queries("SELECT * FROM cat ORDER BY sort");
	$menu_array = '';
	foreach($firstRes as $ro){
		$postAmount = hava_num_rows("SELECT * FROM posts WHERE cat = ?", array($ro['name']));
		$menu_array[$ro['id']] = array('id' => $ro['id'], 'name' => $ro['name'], 'sub' => $ro['sub'], 'desc' =>$ro['desc'], 'posts' =>$postAmount);
	}
	return $menu_array;
}

$catMenuRes = '';
$menu_array = catMenuArray();

function generate_menu($parent){
	$has_childs = false;
	global $menu_array, $catMenuRes, $hava_lang;
	if(!empty($menu_array)){
		foreach($menu_array as $key => $value){
			if ($value['sub'] == $parent) {       
				if ($has_childs === false){
					$has_childs = true;
					$catMenuRes .= '<ol class="sortable">
					';
				}
				$catMenuRes .= '<li id="id_'.$value['id'].'"><div><a title="'.$value['desc'].'" href="?catId='.$value['id'].'&subId='.$value['sub'].'">'.$value['name'].'</a> <a id="itemsCount" href="hava_all_posts.php?cat='.$value['name'].'">-&gt; '.$value['posts'].' '.$hava_lang['items'].'</a></div>';
				generate_menu($key);
				$catMenuRes .= '</li>
				';
			}
		}
		if ($has_childs === true) $catMenuRes .= '</ol>';
		return $catMenuRes;
	}
	else return "No category defined!";
}


echo generate_menu(0);

if(isset($catArray['sort'])){ $thisCatSort = $catArray['sort']; }
else {
	$thisCatSort = hava_single_query("SELECT * FROM cat ORDER BY sort DESC", 'all', 'sort');
	$thisCatSort++;
}
?> 
</div>
<style>
ol.sortable, ol.sortable ol { margin: 0 0 0 25px; padding: 0; list-style-type: none; }
ol.sortable { margin: 0; }
.sortable li { margin: 7px 0 0 0; padding: 0; }
.sortable li div  { 
	border: 1px solid #ccc; padding: 3px 3px 3px 28px; margin: 0; cursor: move; 
	background-image:url(sys/img/dots.png); background-repeat:no-repeat; background-position:1px 1px;
	-webkit-border-radius: 4px;
	-moz-border-radius: 4px;
	border-radius: 4px; 
}

</style></td>
<td valign="top">
<form id="catForm" name="catForm" method="post" action="hava_cat.php" onsubmit="return formSubmission()">
<div style="border:1px solid #ccc; padding:7px;">
 <table id="catForm" border="0" cellspacing="0" cellpadding="7" >
    <tr><td><?php if(isset($catArray)) echo '<img src="'.$catArray['icon'].'" />'; ?></td><td> </td></tr>
	<tr>
      <td width="100" valign="top"><?=$hava_lang['name']; ?>:</td>
      <td valign="top"><input name="catName" type="text" id="catName" value="<?php if(isset($catArray)) echo $catArray['name']; ?>" /> <span id="expl"><?php if(isset($catId)) echo itemCount($catId).' '.$hava_lang['items']; ?></span> <input name="catId" type="hidden" id="catId" value="<?php if(isset($catArray)) echo $catArray['id']; ?>" /></td>
    </tr>
    <tr>
      <td valign="top"><?=$hava_lang['description']; ?>:
        <input name="catSort" type="hidden" id="catSort" value="<?=$thisCatSort; ?>" /></td>
      <td valign="top"><input name="catDesc" type="text" id="catDesc" size="70" value="<?php if(isset($catArray)) echo $catArray['desc']; ?>" /></td>
    </tr>
	<tr>
	  <td valign="top">Icon:</td>
      <td valign="top"><input name="catIcon" type="text" id="catIcon" size="70" value="<?php if(isset($catArray)) echo $catArray['icon']; ?>" /></td>
    </tr>
<?php
function itemCount($id){
	$res = 0;
	if(isset($id)){
		$catName = hava_single_query("SELECT * FROM cat WHERE id = ?", $id, "name");
		if($catName) $res = hava_num_rows("SELECT * FROM posts WHERE cat = ?", array($catName));
	}
	return $res;
}

	
if(isset($catId) and itemCount($catId) >0){
?>
    <tr>
      <td valign="top"><?=$hava_lang['copyTo']; ?>:</td>
      <td valign="top"><select name="copyCat" id="copyCat">
        <option></option>
        <?=$copyCat; ?>
      </select> <span id="expl"><?=$hava_lang['copyTo1']; ?></span>      </td>
    </tr>
<?php
}
?>
    <tr>
      <td valign="top">&nbsp;</td>
      <td valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td align="right" valign="top"><?php 

if(isset($catId) and itemCount($catId) <1){ 
?>
		<button id="deleteSubmit" onclick="zebraConfirm('<?=$hava_lang['deleted']; ?><br><br><span style=\'color:red;\'><?=$hava_lang['deleted1']; ?></span><br>', '<?=$catArray['name']; ?>', '?del=<?=$catArray['id']; ?>', 'warning', true); return false;"><?=$hava_lang['delete']; ?></button>
<?php } ?>
		<input id="saveSubmit" type="submit" name="Submit" value="        <?=$hava_lang['save']; ?>         " /></td>
    </tr>
  </table>  
 </div>   
</form></td>
</tr>
</table>
</div>







<?php include('hava_foot.php'); ?>
Return current item: Havalite