<?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");


 function checkAccess($file_name, $group_id){
        $db = con();
        $sql = $db->query("SELECT * FROM u_admin_file_to_groups WHERE groupID ='" . $group_id .
            "' AND  fileName  ='" . $file_name . "'");
        $num = mysql_num_rows($sql);
        if ($num > 0) {
            return 'checked="checked"';
        }
        return '';
    }
    function checkAccess2($file_name, $group_id){
        $db = con();
        $sql = $db->query("SELECT * FROM  u_file_to_groups WHERE groupID ='" . $group_id .
            "' AND  fileName  ='" . $file_name . "'");
        $num = mysql_num_rows($sql);
        if ($num > 0) {
            return 'checked="checked"';
        }
        return '';
    }

 if(isset($_GET['act'])) $action = $_GET['act'];
  switch($action){
	case 'get_send_contact':
			$id=$_POST['field_1'];
			  $sql=$db->query("select * from contact where id='".$id."'");
			  $row=mysql_fetch_array($sql);
			  echo '<div dir="rtl" align="right">';
			  echo '» نام : '.$row['name'].'<br>';
			  
			  echo '» تلفن تماس: '.$row['phone'].'<br>';
			  echo '» ایمیل : '.$row['mail'].'<br>';
			  echo '» پیام : '.$row['description'].'<br>';
			  echo '» تاریخ : '.tep_hejri2($row['date']).'</div>';
			$sql2="update contact set
			state='2'
			where id='".$id."' ";
			$query2=$db->query($sql2);		  
		 // echo $query2;
	break;
//--------------------------------------------------------
case 'admin_file':
$adminID=$_POST['id'];
 $sql = $db->query("select * from u_admin_files");
                $i = 1;
                echo '<table border="0" width="600">';
                while ($res = $db->fetchNextArray($sql)) {
                    echo '<td width="170" height="40" align="right"><input type="checkbox" ' . checkAccess($res['fileName'], $adminID) . ' name="file' . $i . '" value="' .
                        $res['fileName'] . '" />' . $res['description'] . '</td>';
                    if (($i % 4) == 0)
                        echo '</tr><tr>';
                    $i++;
                }
                echo '</table>';
                echo '<input type="hidden" name="num" value="' . ($i - 1) . '" />';
                
break;
//--------------------------------------------------------------------------------
case 'user_file':
$usergroupID=$_POST['id'];
 $sql = $db->query("select * from  u_user_files");
                $i = 1;
                echo '<table border="0" width="600">';
                while ($res = $db->fetchNextArray($sql)) {
                    echo '<td width="170" height="40" align="right"><input type="checkbox" ' . checkAccess2($res['fileName'], $usergroupID) . ' name="file' . $i . '" value="' .
                        $res['fileName'] . '" />' . $res['description'] . '</td>';
                    if (($i % 4) == 0)
                        echo '</tr><tr>';
                    $i++;
                }
                echo '</table>';
                echo '<input type="hidden" name="num" value="' . ($i - 1) . '" />';    
break;
//---------------------------------------------
case 'creationFilde':
	$catID = $_POST['catID'];
?>
	<table border="0" cellpadding="3" cellspacing="3" width="400px" class="reg-plans2" dir="rtl">
		<tr>
			<td> نام فیلد:</td>
            <td><input name="filde" type="text" id="filde"><input type="hidden" id="catID" name="catID" value="<?=$catID?>" />
            </td>
		</tr>
		<tr>
			<td> نوع فیلد :</td>
            <td>
            <select name="status" id="status" >
            	<option value="1">اجباری</option>
                <option value="2">غیر اجباری</option>
            </select>
            </td>
		</tr>        
		<tr>
			<td colspan="2"><input type="submit" value="ذخیره" onclick="insertFilde(<?=$catID?>);"/></td>
		</tr>
	</table>
  <?php
break; 
//---------------------------------------------
case 'ListFilde':
	$fID = $_POST['fID'];
	$catID = $_POST['catID'];
	if($_POST['dell']==1){
		$dell=$db->query("delete from typeUseCat_filde where fID='".$_POST['fID']."'"); 
		echo 'فیلد با موفقیت حذف شد';
	}
$query=$db->query("select * from typeUseCat_filde where catID ='".$catID."'  ");
echo '<table border="0" cellpadding="3" cellspacing="3" width="500px" class="reg-plans2" dir="rtl">
		<tr>
			<td> نام فیلد:</td>
			<td>حذف</td>
		</tr>';
	while($res=$db->fetchNextArray($query)){
	echo '
		<tr>
			<td>'.$res['filde'].'</td>
			<td><img onclick="dellFilde('.$res['fID'].');" border="0" src="images/dell.gif" alt="حذف"></td>
		</tr>';
	}
echo '</table>';
break;
//-------------------------------------------------------------------------------------------------------
case 'insertFilde':
	$catID = $_POST['catID'];
	$title = $_POST['title'];
	$db->execute("insert into typeUseCat_filde set
											filde= '".$db->safe_string($_POST['filde'])."',
											status ='".$db->safe_string($_POST['status'])."', 
											catID='".$catID."' ");
	echo 'فیلد با موفقیت ذخیره شد';

break;

//---------------------------------------------
case 'creationPage':
	$p_id = $_POST['p_id'];
	
?>
	<table border="0" cellpadding="3" cellspacing="3" width="400px" class="reg-plans2" dir="rtl">
		<tr>
			<td> نام گروه:</td><td><input name="title" type="text" id="title"><input type="hidden" id="p_id" name="p_id" value="<?=$p_id?>" /></td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="ذخیره" onclick="insertPage(<?=$p_id?>);"/></td>
		</tr>
	</table>
  <?php
break; 
//-------------------------------------------------------------------------------------------------------
case 'insertPage':
	$p_id = $_POST['p_id'];
	$title = $_POST['title'];
	$group = get_user_group2();
	$db->execute("insert into page_index_group set
											title= '".$db->safe_string($_POST['title'])."',
											groupID='".$group."' ");
	echo 'صفحه با موفقیت ذخیره شد';

break;
//---------------------------------------------
case 'ListPage':
	$p_id = $_POST['p_id'];
	if($_POST['dell']==1){
		$dell=$db->query("delete from page_index_group where p_id='".$_POST['p_id']."'"); 
		echo 'صفحه با موفقیت حذف شد';
	}
$query=$db->query("select * from page_index_group where groupID ='".get_user_group2()."'  ");
echo '<table border="0" cellpadding="3" cellspacing="3" width="500px" class="reg-plans2" dir="rtl">
		<tr>
			<td> نام گروه:</td>
			<td>حذف</td>
		</tr>';
	while($res=$db->fetchNextArray($query)){
	echo '
		<tr>
			<td>'.$res['title'].'</td>
			<td><img onclick="dellPage('.$res['p_id'].');" border="0" src="images/dell.gif" alt="حذف"></td>
		</tr>';
	}
echo '</table>';
break;
//--------------------------------------------------------

case 'getTypeUseSubcat':
	if($_POST['kind']==1){
			$sql=$db->query("select * from typeusecat where parent = '".$_POST['typeUseCat']."'");
			while($res=$db->fetchNextArray($sql)){
					echo '<option value="'.$res['catID'].'">'.$res['nameCat'].'</option>';
			}
		}elseif($_POST['kind']==2){
			$sql_user=$db->query("select * from u_users where ID='".$_POST['userID']."'");
			$res_user=$db->fetchNextArray($sql_user);
			$typeUseSubcat=$res_user['typeUseSubcat'];
			$typeUseFilde=$res_user['typeUseFilde'];
			$ex_typeUseFilde=explode("|",$typeUseFilde);
					$filde='';
					$sql=$db->query("select * from typeUseCat_filde where catID='".$_POST['typeUseSubcat']."'");
					$num=$db->numRows($sql);
					if($num){
					$i=1;
					echo '<table><tr><td colspan="2">لطفا فیلدهای زیر را تکمیل نمایید : </td></td></tr>';
					while($res=$db->fetchNextArray($sql)){
						if($res['status']==1){
							$star=' class="validate[required,length[0,50]] inp_text" ';
							$star2=' <font color="red">*</font> ';
						}else {$star='';$star2='';}
						$j=$i-1;
						if($typeUseSubcat==$res['catID']){
							$value_typeUseFilde=$ex_typeUseFilde[$j];
						}
						echo '
							<tr>
								<td>'.$res['filde'].' : '.$star2.'</td>
								<td><input type="text" '.$star.' name="filde_'.$i.'" id="filde_'.$i.'" value="'.$value_typeUseFilde.'"></td>
							</tr>';
					$i++;
					}
					echo '<input type="hidden" id="countUse" name="countUse" value="'.$i.'" /></table>';
					
					}
		}
break;
case 'getCities':
		$sql=$db->query("select * from city where ProvinceID = '".$_POST['province']."'");
		while($res=$db->fetchNextArray($sql)){
				echo '<option value="'.$res['CityID'].'">'.$res['CityName'].'</option>';
		}
break;
case 'brands':
		$sql=$db->query("select * from brands where kind = '".$_POST['kind']."'");
		echo '<option value="">مهم نیست</option>';
		while($res=$db->fetchNextArray($sql)){
				echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
		}
break;
case 'models':
		$sql=$db->query("select * from models where brands = '".$_POST['brands']."'");
		echo '<option value="">مهم نیست</option>';
		while($res=$db->fetchNextArray($sql)){
				echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
		}
break;
case 'year':
		$sql=$db->query("select * from brands where id = '".$_POST['brands']."'");
		$res=$db->fetchNextArray($sql);
		echo '<option value="">مهم نیست</option>';
		if($res['years']==1){
				for($i=1930;$i<2050;$i++){
				echo '<option value="'.$i.'">'.$i.'</option>';
				}
		}else{
			
		for($i=1340;$i<1420;$i++){
				echo '<option value="'.$i.'">'.$i.'</option>';
				}
			
			}
break;
case 'getcat':
		$sql=$db->query("select * from cats where part_id = '".$_POST['part_id']."'");
		while($res=$db->fetchNextArray($sql)){
				echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
		}
break;
case 'getTypeUseSubcat':
	if($_POST['kind']==1){
			$sql=$db->query("select * from typeusecat where parent = '".$_POST['typeUseCat']."'");
			while($res=$db->fetchNextArray($sql)){
					echo '<option value="'.$res['catID'].'">'.$res['nameCat'].'</option>';
			}
		}elseif($_POST['kind']==2){
			$sql_user=$db->query("select * from u_users where ID='".$_POST['userID']."'");
			$res_user=$db->fetchNextArray($sql_user);
			$typeUseSubcat=$res_user['typeUseSubcat'];
			$typeUseFilde=$res_user['typeUseFilde'];
			$ex_typeUseFilde=explode("|",$typeUseFilde);
					$filde='';
					$sql=$db->query("select * from typeUseCat_filde where catID='".$_POST['typeUseSubcat']."'");
					$num=$db->numRows($sql);
					if($num){
					$i=1;
					echo '<table><tr><td colspan="2">لطفا فیلدهای زیر را تکمیل نمایید : </td></td></tr>';
					while($res=$db->fetchNextArray($sql)){
						if($res['status']==1){
							$star=' class="validate[required,length[0,50]] inp_text" ';
							$star2=' <font color="red">*</font> ';
						}else {$star='';$star2='';}
						$j=$i-1;
						if($typeUseSubcat==$res['catID']){
							$value_typeUseFilde=$ex_typeUseFilde[$j];
						}
						echo '
							<tr>
								<td>'.$res['filde'].' : '.$star2.'</td>
								<td><input type="text" '.$star.' name="filde_'.$i.'" id="filde_'.$i.'" value="'.$value_typeUseFilde.'"></td>
							</tr>';
					$i++;
					}
					echo '<input type="hidden" id="countUse" name="countUse" value="'.$i.'" /></table>';
					
					}
		}
break;
case 'checkurl':
		if($_POST['slug']==''){
			$name = $_POST['name'];
			}else{
				$name = $_POST['slug'];
				}
				$slug = str_replace(" ", "-",$name);?>
                <input name="slug"  id="slug" size="16" value="<?php echo $slug; ?>" dir="ltr" class="regular-text" type="text">
                <?php 
				$sql=$db->query("select * from parts where slug = '".$slug."' and id <> '".$_POST['postid']."'");
				$res=mysql_num_rows($sql);
				if($res>0){
					?>
					<img src="images/0.png" />
					<?php
					}else{?>
					<img src="images/00.gif" />
					<?php }
				 ?>
                
                <?php
break;
case 'addplace':


$lat = $_GET['lat'];
$lng = $_GET['lng'];


$query = "update ads set
						lat = '".$db->safe_string($lat)."',
						lang = '".$db->safe_string($lng)."'
						where id='".$_GET['id']."'";
														

$result = mysql_query($query);

if (!$result) {
  die('Invalid query: ' . mysql_error());
}



break;
case 'tagname':
		if($_POST['tagname']==''){
			}else{
				$sql=$db->query("select * from tags where name = '".$_POST['tagname']."'");
				$res=mysql_num_rows($sql);
				if($res>0){
					$tag=$db->fetchNextArray($sql);
					$db->execute("insert into tags_relations set
											tag_id= '".$tag['id']."',
											post_id ='".$_POST['postid']."' ");
					}else{
						$db->execute("insert into tags set
											name= '".$_POST['tagname']."',
											slug ='".str_replace(" ", "-", $_POST['tagname'])."',
											date=NOW() ");
						$lastid =$db->lastInsertedId();
						$db->execute("insert into tags_relations set
											tag_id= '".$lastid."',
											post_id ='".$_POST['postid']."' ");
						
						 }
				}
break;
case 'currenttag':
		?>
        <div class="befortag">
                <ul class="tags">
                <?php 
				$sql=$db->query("select * from tags_relations where post_id = '".$_POST['postid']."'");
				while($tag=$db->fetchNextArray($sql)){
					$etagname=$db->query("select * from tags where id = '".$tag['tag_id']."'");
					$tagname=$db->fetchNextArray($etagname);
				?>
                    <li><a href="javascript:void(0);" onclick="deltag(<?php echo $_POST['postid']; ?>,<?php echo $tag['tag_id']; ?>);"><?php echo $tagname['name']?></a></li>
                <?php } ?>
                </ul>
             </div>
        <?php
break;
case 'deltag':
	$dell=$db->query("delete from tags_relations where post_id='".$_POST['postid']."' and tag_id='".$_POST['tagid']."'"); 	
break;
case 'addim':
	$db->execute("insert into tags_relations set
											tag_id= '".$_POST['tagid']."',
											post_id ='".$_POST['postid']."' "); 	
break;
   } 
}
?>