<?php
/*
ساخت منو برای یک گرو مدیریتی
*/
include ("include/config.php");
include ("include/protect.class.php");
include ("include/functions.php");
include ("include/jdf.php");
$prot = new protect();
if ($prot->showPage) {
    $db = new DB($globalConfig['dbase'], $globalConfig['dbhost'], $globalConfig['dbuser'],
        $globalConfig['dbpass']);
    mysql_query("SET NAMES utf8");
    if (isset($_GET['do'])) {
        $action = $_GET['do'];
    }
    switch ($action) {
            //-------------------------------------------------------------------------------------
        case 'pdelete':
            if (isset($_GET['confirm'])) {
                $db->execute("delete from typeusecat where catID = '" . (int)$_GET['catID'] . "'");
            }
            header("location: typeCatUse.php");
            break;
            //-------------------------------------------------------------------------------
        case 'pinsert':

            if (isset($_POST['Insert'])) {
               //echo $_POST['adgroup'];
                $res = $db->execute("insert into typeusecat set 
													parent ='".$db->safe_string($_POST['catID'])."',
													nameCat ='".$db->safe_string($_POST['nameCat'])."',
													description ='".$db->safe_string($_POST['description'])."',
													status =1");
            }
            header("location: typeCatUse.php");
            break;
            //-------------------------------------------------------------------------------
        case 'pedite':

            if (isset($_POST['ٍedite'])) {
              // echo $_POST['hiddenCatID'];
                $res = $db->execute("update typeusecat set 
													parent ='" . $db->safe_string($_POST['catID']) . "',
													nameCat ='" . $db->safe_string($_POST['nameCat']) . "',
													description ='" . $db->safe_string($_POST['description']) . "'
													where catID ='" . $db->safe_string($_POST['hiddenCatID']) . "' ");
            }
           // header("location: typeCatUse.php");
            break;			
			
    }
$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit = 30;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.

$numresults = $db->query("SELECT * FROM typeusecat where parent <> 0"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> مدیریت نوع استفاده </title>
<?php include("maintemplate/mainheader.php");?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="js/js_body.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" media="screen" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.1.js"></script>
<script type="text/javascript">   
	function creationFilde(catID){
		$(document).ready(function() {
				$.fancybox.showActivity();
				$.ajax({
					type		: "POST",
					cache	: false,
					url		: "send_ajax_chitasoft.php?act=creationFilde",
					data		: {'catID':catID},
					success: function(data) {
						$.fancybox(data);
					}
				});
				return false;		
		});
	}
	function insertFilde(catID){
		$(document).ready(function() {
				var filde = $("#filde").val();
				var status = $("#status").val();
				$.fancybox.showActivity();
				$.ajax({
					type		: "POST",
					cache	: false,
					url		: "send_ajax_chitasoft.php?act=insertFilde",
					data: "catID="+catID+"&filde="+filde+"&status="+status,
					success: function(data) {
						$.fancybox(data);
					}
				});
				return false;		
		});
	}

	function ListFilde(catID){
		$(document).ready(function() {
				var filde = $("#filde").val();
				$.fancybox.showActivity();
				$.ajax({
					type		: "POST",
					cache	: false,
					url		: "send_ajax_chitasoft.php?act=ListFilde",
					data: "catID="+catID,
					success: function(data) {
						$.fancybox(data);
					}
				});
				return false;		
		});
	}

	function dellFilde(fID){
		$(document).ready(function() {
				var pattern = $("#pattern").val();
				$.fancybox.showActivity();
				$.ajax({
					type		: "POST",
					cache	: false,
					url		: "send_ajax_chitasoft.php?act=ListFilde",
					data: "fID="+fID+"&dell=1",
					success: function(data) {
						$.fancybox(data);
					}
				});
				return false;		
		});
	}
    </script>			
		
</head>
<body>
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><br></div>
                <h2>مدیریت نوع استفاده</h2>
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                        <div class="postbox-container" style="width:100%;">
                            <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                             <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>مدیریت نوع استفاده  //( دقت شود بعد از تکمیل این قسمت موارد وارد شده حذف نشود. )</span></h3>
<?php
    if ($_GET['act'] == 'delete') {
        if (!isset($_GET['confirm'])) {
            echo 'آيا شما مطمئنيد؟ <br /> <a href="typeCatUse.php?do=pdelete&catID=' . (int)$_GET['catID'] .
                '&confirm"> بلــــه </a> | <a href="typeCatUse.php"> خيـــــر </a> ';
        }
    }

    switch ($action) {
            //-------------------------------------------------------------------------------------
        default:

            $sql2 = $db->query("select * from typeusecat where catID='" . $_GET['catID'] . "'");
            $res2 = $db->fetchNextArray($sql2);
            $num = $db->numRows($sql2);
			if($num){
				 $act = 'do=pedite';          
            }else{
                 $act = 'do=pinsert';   
			}
?>
             <form action="typeCatUse.php?<?= $act ?>" method="post">
  	               <table  class="table_insert" cellpadding="3" cellspacing="3" width="90%" align="center" >
                       <tr>
                          <td class="td_d">عنوان :</td>
                          <td class="td_d"><input type="text" name="nameCat" value="<?=$res2['nameCat']?>"  /></td> 
                       </tr>
                         <tr>
                            <td class="td_d"> گروه  :</td>
                            <td class="td_d">
                                <?php
                                	printCombobox("catID", "catID", "typeusecat", "catID", 'nameCat' ,$res2['parent'],' where parent = 0' );
                				?>
                            </td> 
                        </tr>  
                        
                       <tr>
                          <td class="td_d">توضیحات :</td><td class="td_d"><textarea type="text" name="description" ><?=$res2['description']?></textarea></td> 
                       </tr>                        
                                    
                         <tr>
                            <td colspan="2" align="center">
                            <?php
								if($num){
									echo '<input type="submit" name="ٍedite" value="ویرایش" />
									<input type="hidden" name="hiddenCatID" value="'.$res2['catID'].'" />';          
            					}else{
                                	echo '<input type="submit" name="Insert" value="ذخیره" />';   
								}
                			?>
                              </td>
                        </tr>
        </table>        
        </form>	
            <?php
            $sql = $db->query("select * from typeusecat where parent <> 0 ORDER BY parent ASC LIMIT $page, $limit ");
            echo '<table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0"  class="table1" >';
            echo '<tr>
                            <th class="td_header">عنوان</th>
							<th class="td_header">زیر گروه</th>
							<th class="td_header">توضیحات</th>
                  <th class="td_header" align="center"> امکانات </th>
                  </tr>';
            while ($res = $db->fetchNextArray($sql)) {
                echo '<tr>
                <td  align=center class="tablebattom1"> ' . $res['nameCat'] .' </td>
				<td  align=center class="tablebattom1"> ' . getCatUseName($res['parent']) .' </td>
				<td  align=center class="tablebattom1"> ' . $res['description'] .' </td>
			 		<td  align=center class="tablebattom1">
					<img border="0" width="25px" src="images/docs.png" alt="لیست سوال" title="لیست سوال" onclick="ListFilde('.$res['catID'].');">
					 <img border="0" width="25px" src="images/semn-plus-mare.png" alt="افزودن سوال" title="افزودن سوال" onclick="creationFilde('.$res['catID'].');">
                     <a href="typeCatUse.php?act=delete&catID=' . $res['catID'] .'"><img border="0" src="images/dp.png"></a> 
					 <a href="typeCatUse.php?catID=' . $res['catID'] .'"><img border="0" src="images/ep.png"></a> 
				</td></tr>';
            }
            echo '</table>';
            echo '<p align="center" style="margin-top:20px;">';

            if ($page != 0) { // Don't show back link if current page is first page.
                $back_page = $page - $limit;
                echo ("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\" class=\"pages\">قبلی</a>    \n");
            }

            for ($i = 1; $i <= $pages; $i++) // loop through each page and give link to it.
                {
                $ppage = $limit * ($i - 1);
                if ($ppage == $page) {
                    echo ("<b class=\"pages_on\">" . $i . "</b> \n");
                } // If current page don't give link, just text.
                else {
                    echo ("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\" class=\"pages\">" .
                        $i . "</a> \n");
                }
            }

            if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
                $next_page = $page + $limit;
                echo ("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\" class=\"pages\">بعدی</a>\n");
            }
            echo '</p>';
            break;
            //-------------------------------------------------------------------------------------
    }
?>
                              </div>
                                
                            </div>
                        </div>
                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php } ?>