<?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 m_user_menu where id = '" . (int)$_GET['id'] . "'");
            }
            header("location: m_make_menu_users.php");
            break;
            //-------------------------------------------------------------------------------
        case 'pinsert':

            if (isset($_POST['Insert'])) {
               echo $_POST['adgroup'];
                $res = $db->execute("insert into m_user_menu set 
													link ='" . $db->safe_string($_POST['mlink']) . "',
													title ='" . $db->safe_string($_POST['mtitle']) . "',
													titleen ='" . $db->safe_string($_POST['mtitleen']) . "',
													parent ='" . $db->safe_string($_POST['pmenu']) . "',
													position = '" . $db->safe_string($_POST['position']) . "',
													available = '" . $db->safe_string($_POST['available']) . "',
													usergroup ='" . $db->safe_string($_POST['ugroup']) . "'");
            }
            header("location: m_make_menu_users.php");
            break;
       
            //-------------------------------------------------------------------------------
        case 'pedite':

            if (isset($_POST['edite'])) {
               echo $_POST['adgroup'];
                $res = $db->execute("update  m_user_menu set 
													link ='" . $db->safe_string($_POST['mlink']) . "',
													title ='" . $db->safe_string($_POST['mtitle']) . "',
													titleen ='" . $db->safe_string($_POST['mtitleen']) . "',
													parent ='" . $db->safe_string($_POST['pmenu']) . "',
													position = '" . $db->safe_string($_POST['position']) . "',
													available = '" . $db->safe_string($_POST['available']) . "',
													usergroup ='" . $db->safe_string($_POST['ugroup']) . "'
												where id='".$_POST['id']."'");
            }
            header("location: m_make_menu_users.php");
            break;
 
    }

$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit = 60;} // Default results per-page.
if (!($page)){$page = 0;} // Default page value.
$numresults = $db->query("SELECT * FROM m_user_menu ORDER BY usergroup ,parent ASC"); // 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.


?>
<!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>

<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script>
function user_file(id){ 
			$.ajax({
   type: "POST",
   url: "send_ajax_chitasoft.php?act=user_file",
   data: "id="+id,
   success: function(msg){
document.getElementById("result").innerHTML=msg;
   }
 });
}	
</script>			
	<?php include("maintemplate/mainheader.php");?>	
</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 ">  
<?php
    if ($_GET['act'] == 'delete') {
        if (!isset($_GET['confirm'])) {
            echo 'آيا شما مطمئنيد؟ <br /> <a href="m_make_menu_users.php?do=pdelete&id=' . (int)$_GET['GID'] .
                '&confirm"> بلــــه </a> | <a href="m_make_menu_users.php"> خيـــــر </a> ';
        }
    }

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

            $sql2 = $db->query("select * from m_user_menu where id='" . $_GET['id'] . "' LIMIT $page, $limit");
            $res2 = $db->fetchNextArray($sql2);
            $num = $db->numRows($sql2);
            if ($num) {     
            	$act = 'do=pedite';
            } else {
                ////////////////////////اضافه کردن منو برای یک گروه خواص////////////////////////////////////////////
                $act = 'do=pinsert';
			}
?>
             <form action="m_make_menu_users.php?<?= $act ?>" method="post">
  <table  class="table_insert" cellpadding="3" cellspacing="3" width="100%" align="center" >
        <tr>
            <td class="td_d">عنوان منو:</td><td class="td_d"><input type="text" name="mtitle" value="<?=$res2['title']?>"  /></td> 
        </tr>
        <tr>
            <td class="td_d">menu title:</td><td class="td_d"><input type="text" name="mtitleen" value="<?=$res2['titleen']?>"  /></td> 
        </tr>
        <tr>
           <td class="td_d">LINK :</td><td class="td_d"><input type="text" name="mlink" value="<?=$res2['link']?>"  /></td>            
         </tr>
         <tr>
            <td class="td_d">گروه مدیریت :</td>
            <td class="td_d">
<?php
                printCombobox("ugroup", "ugroup", "u_groups", "groupsID", "groupName",$res2['usergroup']);
?>
            </td> 
        </tr>     
        <tr>
            <td class="td_d">منو والد :</td>
            <td class="td_d">
            <?php getusermenu("pmenu","pmenu","m_user_menu","id","title","منو اصلی",$res2['parent'])?>
            </td> 
        </tr>  
        <?php
			if($res2['available']==2){
				$selected_available2='selected="selected"';
			}else{
				$selected_available1='selected="selected"';
			}
		?>
        <tr>
            <td class="td_d">مجوز :</td>
            <td class="td_d">
           <select name="available" id="available">
            	<option value="1" <?=$selected_available1?> >همه وضعیتها</option>
                <option value="2" <?=$selected_available2?>>مشاهده فقط بعد از مجوز</option>
            </select>
            </td> 
        </tr>         
         <?php
			if($res2['position']==1){
				$selected_position1='selected="selected"';
			}elseif($res2['position']==2){
				$selected_position2='selected="selected"';
			}elseif($res2['position']==3){
				$selected_position3='selected="selected"';
			}
		?>
        <tr>
            <td class="td_d">موقعیت  :</td>
            <td class="td_d">
			<select name="position" id="position">
            	<option value="1" <?=$selected_position1?>>control Panel</option>
                <option value="2" <?=$selected_position2?>>header</option>
                <option value="3" <?=$selected_position3?>>footer</option>
            </select>
            </td> 
        </tr>         
               
         <tr>
            <td colspan="2" align="center">
<?php
if($num){
      echo '<input type="submit" name="edite" value="ویرایش" /><input type="hidden" name="id" value="'.$_GET['id'].'" />';  
}else{
	 echo '<input type="submit" name="Insert" value="ذخیره" />';  
}
?>
              </td>
        </tr>
        </table>        
        </form>	
            <?php
            $sql = $db->query("select * from m_user_menu  ORDER BY usergroup ,parent ASC LIMIT $page, $limit");
            echo '<table width="100%"  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">موقعیت</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"> ' . user_group_name($res['usergroup']) .' </td>
				<td  align=center class="tablebattom1"> ' . get_parent_menu_user($res['parent']) . ' </td>
				<td  align=center class="tablebattom1"> ' . get_available_menu_user($res['available']) . ' </td>
                <td  align=center class="tablebattom1"> ' . get_Position_menu_user($res['position']) . ' </td>
                <td  align=center class="tablebattom1"> ' . $res['title'] .' </td>
                <td  align=center class="tablebattom1"> ' . $res['link'] . ' </td>
			 	<td  align=center class="tablebattom1">
					 <a href="m_make_menu_users.php?id='.$res['id'].'"><img border="0" src="images/ep.png"></a>
                     <a href="m_make_menu_users.php?act=delete&GID=' . $res['id'] .'"><img border="0" src="images/dp.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 } ?>