<?php
if (!defined("_VALID_ACCESS_") || _VALID_ACCESS_ !=1) {
	die("Foot Bokra");
}
switch($action) {

	case "addCatagories":
		if (isset($_POST[addCatagory]) && $_POST[addCatagory] == 1 && !empty($_POST[name])) {
			$name=addslashes($_POST[name]);
			$name=str_replace("\"", "\\\"", $name);
			$name=str_replace("\'", "\\\'", $name);

			if (!empty($_POST[mainCatagory]) && is_numeric($_POST[mainCatagory])) {
				$mainCatagory=intval($_POST[mainCatagory]);
				$level=2;
			}
			else {
				$mainCatagory=0;
				$level=1;
			}

			if ($level == 1) {
				$query="SELECT `id` FROM `catagories` WHERE `name` LIKE '$name' AND `level` =1";
				$mysqlQuery->mysqlQueryWOF($query);

				if ($mysqlQuery->resultNoRows==0) {
					$query="INSERT INTO `catagories` (`id`, `name`, `level`, `main_catagory`) 
                                    VALUES (NULL, '$name', ' $level', '$mainCatagory')";
					$mysqlQuery->mysqlQueryUpdate($query);
					$main .= "تمت اضافة القسم بنجاح".refresh("addCatagories");
				}
				else {
					$main .= "الاسم موجود من قبل من فضلك قم بإختيار اسم اخر للقسم";
				}
			}
			else {
				$query="INSERT INTO `catagories` (`id`, `name`, `level`, `main_catagory`) VALUES (NULL, '$name', '$level', '$mainCatagory')";
				$mysqlQuery->mysqlQueryUpdate($query);
				$main .= "تمت اضافة القسم بنجاح";
			}
		}
		else {
			$main .= "<form action='./admin.php?action=addCatagories' method='POST'>
						<input type='hidden' name='addCatagory' value='1'></input>
          <table width='50%' align='center' class='content' border='0' cellpadding='5'>
							<tr>
								<td width='50%'>اسم القسم</td>
								<td width='50%'><input type='text' maxlength='300' name='name'></input></td>
							</tr>
							<tr>
								<td width='100%' colspan='2'><input type='submit' value='إضافة القسم'></input></td>
							</tr>
						</table>
					</form>";
		}
		break;
		
		/***********************************************************************************************/
		/***********************************************************************************************/
		/***********************************************************************************************/
		
	case "editCatagories":
		if (isset($_GET[op]) && is_numeric($_GET[catId]) 
        && ($_GET[op] == "edit" || $_GET[op] == "delete" || $_GET[op] == "open")) {
			
			$catId = intval($_GET[catId]);

			if ($_GET[op] == "open") {
				$query="SELECT `id`, `name`  FROM `catagories` WHERE `level` = 2 AND `main_catagory` = '$catId'";
				$mysqlQuery->mysqlQueryWOF($query);
      
      if ($mysqlQuery->resultNoRows==0) {      
        	
        $main .=" لا يوجد اقسام فرعية بهذا القسم ";	}
        else{
				$counter = 1 ;
				$main .= "اقسام المقالات الفرعية";
        $main .= "<table width='100%' align='center' class='content' border='0' cellpadding='5'>";
				while (list($id,$name) = mysql_fetch_array($mysqlQuery->result)) {
					$main .= ("<tr>");
					$main .= ("<td class='content' width='10%'>$counter</td>");
					$main .= ("<td class='title' width='50%'>
				<a href='./admin.php?action=editCatagories&op=open&catId=$id'>$name</a></td>");
					$main .= ("<td class='content' width='20%'>
				<a href='./admin.php?action=editCatagories&op=edit&catId=$id'>تعديل</a></td>");
					$main .= ("<td class='content' width='20%'>
				<a href='./admin.php?action=editCatagories&op=delete&catId=$id'>حذف - سيتم حذف جميع المقالات فى هذا القسم</a></td>");
					$main .= ("</tr>");
					$counter++;
				}
				$main .= "</table>";
				}
			}

			elseif ($_GET[op] == "delete") {
				if( $Alevel == 1 OR $Alevel == 2 ) {
				
				$query="DELETE FROM `catagories` WHERE `id` = '$catId'";
				$mysqlQuery->mysqlQueryUpdate($query);
				$query="DELETE FROM `products` WHERE `catid` = '$catId'";
				$mysqlQuery->mysqlQueryUpdate($query);

				$query="SELECT `id` FROM `catagories` WHERE `main_catagory` = '$catId'";
				$mysqlQuery->mysqlQueryWOF($query);

				while (list($catId)=mysql_fetch_array($mysqlQuery->result)) {
					$query="DELETE FROM `catagories` WHERE `id` = '$catId'";
					$mysqlQuery->mysqlQueryUpdate($query);
					$query="DELETE FROM `products` WHERE `catid` = '$catId'";
					$mysqlQuery->mysqlQueryUpdate($query);
				}


				$query="SELECT `id`,`level`, `main_catagory` FROM `catagories` WHERE 1  ORDER BY `main_catagory` ASC";
				$mysqlQuery->mysqlQueryWOF($query);

				while (list($catId,$level,$mainCat) = mysql_fetch_array($mysqlQuery->result)) {
					$query="SELECT `id` FROM `catagories` WHERE `id` = '$mainCat'";
					$subMysqlQuery=new mysqlQueryClass();
					$subMysqlQuery->mysqlQuery($query);

					if ($subMysqlQuery->resultNoRows == 0 && $level != 1) {
						$query="DELETE FROM `catagories` WHERE `id` = '$catId'";
						$subMysqlQuery->mysqlQueryUpdate($query);
						$query="DELETE FROM `products` WHERE `catid` = '$catId'";
						$subMysqlQuery->mysqlQueryUpdate($query);
					}
				}

				$main .= "تم حذف القسم و حذف جميع الاقسام الفرعية التابعة له والمقالات التابعة لهم".refresh("editCatagories");
				
				}
				else 
				{
				$main .= noway("بحذف اقسام المقالات").refresh("editCatagories");
				}
			}

			else {
if($Alevel == 1 OR $Alevel == 2 OR $Alevel ==3) { 
				if ($_POST[editCatagory] == 1 && !empty($_POST[name])) {

					$name=addslashes($_POST[name]);
					$name=str_replace("\"", "\\\"", $name);
					$name=str_replace("\'", "\\\'", $name);
					
					if (!empty($_POST[mainCatagory]) && is_numeric($_POST[mainCatagory])) {
						$mainCatagory=intval($_POST[mainCatagory]);
						if ($catId == $mainCatagory) {
							$main .="لا يمكن عمل قسم فرعى قسم رئيسى لنفسه";
							break;
						}

						$level=2;
					}
					else {
						$mainCatagory=0;
						$level=1;
					}

					if ($level == 1) {
						$query="SELECT `id` FROM `catagories` WHERE `name` LIKE '$name' AND `level` =1";
						$mysqlQuery->mysqlQueryWOF($query);

						if ($mysqlQuery->resultNoRows==0) {
							$query="UPDATE `catagories` SET `name` = '$name', `level` = '$level' ,`main_catagory` = '$mainCatagory'  WHERE `id` = '$catId' LIMIT 1";

							$mysqlQuery->mysqlQueryUpdate($query);
							$main .= "تم تعديل القسم بنجاح".refresh("editCatagories");
						}
						else {
							$main .= "هذا الاسم موجود من قبل من فضلك قم بإختيار اسم اخر للقسم";
						}
					}
					else {
						$query="UPDATE `catagories` SET `name` = '$name', `level` = '$level' ,`main_catagory` = '$mainCatagory' WHERE `id` = '$catId' LIMIT 1";

						$mysqlQuery->mysqlQueryUpdate($query);
						$main .= "تم تعديل القسم بنجاح".refresh("editCatagories");
					}
				}
				else {
					$query="SELECT `name` FROM `catagories` WHERE `id` = '$catId' LIMIT 1";
					$mysqlQuery->mysqlQuery($query);

					list($name)=$mysqlQuery->result;

					$main .= "<form action='./admin.php?action=editCatagories&op=edit&catId=$catId' method='POST'>
						<input type='hidden' name='editCatagory' value='1'></input>
						<table width='50%' align='center' class='content' border='0' cellpadding='5'>
							<tr>
								<td width='50%'>اسم القسم</td>
								<td width='50%'><input type='text' maxlength='300' name='name' value='$name'></input></td>
							</tr>
							<tr>
								<td width='100%' colspan='2'><input type='submit' value='تعديل القسم'></input></td>
							</tr>
						</table>
					</form>";
				}
		}//level 
		else { $main .=noway("بتعديل اقسام المقالات").refresh("editCatagories") ;}
		 
		  }
			
			}
		
		else {
		
      $SmysqlQuery=new mysqlQueryClass();

			$query="SELECT `id`, `name` FROM `catagories` WHERE `level` = 1";
			$mysqlQuery->mysqlQueryWOF($query);
        
			$counter =1;
			$main .= "اقسام المقالات";
			$main .= "<table width='100%' align='center' class='content' border='0' cellpadding='5'>";
			while (list($id,$name) = mysql_fetch_array($mysqlQuery->result)) {

        /**** Sub Cats ****/
        
        $query="SELECT `id`, `name` FROM `catagories` WHERE `level` = 2 AND `main_catagory` = '$id'";
				$SmysqlQuery->mysqlQueryWOF($query);
      
      if ($SmysqlQuery->resultNoRows==0) {      
        	
        $SubCats =" لا يوجد اقسام فرعية بهذا القسم ";	}
        else{
				$Scounter = 1 ;
				$SubCats = "اقسام المقالات الفرعية";
        $SubCats .= "<table width='100%' align='center' class='content' border='0' cellpadding='5'>";
				while (list($catid,$cname) = mysql_fetch_array($SmysqlQuery->result)) {
					$SubCats .= ("<tr>");
					$SubCats .= ("<td class='content' width='10%'>$Scounter</td>");
					$SubCats .= ("<td class='title' width='50%'>
				<a href='./admin.php?action=editCatagories&op=open&catId=$catid'>$cname</a></td>");
					$SubCats .= ("</tr>");
					$Scounter++;
				}
				$SubCats .= "</table>";
				}
			
			/**** Sub Cats ****/
			
				$main .= ("<tr>");
				$main .= ("<td class='content' width='10%'>$counter</td>");
				$main .= ("<td class='title' width='50%'>
				<a href='./admin.php?action=editCatagories&op=open&catId=$id' >$name</a>
          
				</td>");
				$main .= ("<td class='content' width='20%'>
				<a href='./admin.php?action=editCatagories&op=edit&catId=$id'>تعديل</a></td>");
				$main .= ("<td class='content' width='20%'>
				<a href='./admin.php?action=editCatagories&op=delete&catId=$id'>حذف - سيتم حذف جميع المقالات فى هذا القسم</a></td>");
				$main .= ("</tr>");
				$counter++;
			}
			$main .= "</table>";
		}
		break;
}
?>