<?php
session_start();	
?>
<?php
include_once 'ayarlar.php';
$mysql_baglanti= new yonetici(); 
$mysql_baglanti->mysql_baglan();
error_reporting(E_ALL ^ E_NOTICE);
ini_set('error_reporting', E_ALL ^ E_NOTICE); 
?>
<? 
if (isset($_SESSION['adminuser'])){
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?=$site_name?></title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="css/colorpicker.css" />
<link rel="stylesheet" href="css/datepicker.css" />
<link rel="stylesheet" href="css/uniform.css" />
<link rel="stylesheet" href="css/select2.css" />
<link rel="stylesheet" href="css/matrix-style.css" />
<link rel="stylesheet" href="css/matrix-media.css" />
<link rel="stylesheet" href="css/bootstrap-wysihtml5.css" />
<link href="font-awesome/css/font-awesome.css" rel="stylesheet" />
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,800' rel='stylesheet' type='text/css'>
</head>
<body>
<? include 'inc_header.php' ?>
<? include 'inc_leftmenu.php' ?>
<div id="content">
<div id="content-header">
  <div id="breadcrumb"> <a href="index.php" title="Anasayfaya Git" class="tip-bottom"><i class="icon-home"></i>ANASAYFA</a><a href="#" class="current">ÜRÜNLER</a> </div>
  <h1>ÜRÜNLER</h1>
</div>
<div class="container-fluid">
  <hr>
  <div class="row-fluid">
  <? function getparents($parenttitle,$recordid){
		$parent='';
		$parents=explode(",",$recordid);
		$size=sizeof($parents);
		for ($i=1;$i<$size;$i++){
			$result=mysql_fetch_assoc(mysql_query("Select * from product_categories where id='".$parents[$i]."'"));
			$parent= $parent.' >> '.$result['title'];
		}
		return $parent.' >> '.$parenttitle ;
	}?>
  <? if (isset($_GET['f'])){?>
	<? $dizin="images/products";?>
	<? if (isset($_POST['form_submit'])&&($_POST['form_submit']==1)){?>
        <? if ($_GET['f']=='add'){?>
            <? 
            $image='';
            if ($_FILES["image"]["error"]== 0)
            {
            $uzanti=array('image/jpeg','image/jpg','image/png','image/x-png','image/gif'); 
                if(in_array(strtolower($_FILES['image']['type']),$uzanti)){
                $image=turkcekarakter($_FILES['image']['name']);
                $random=rand(1,99999);
                $src="$dizin/".$random.$image;
                $image="../$dizin/".$random.$image;
                move_uploaded_file($_FILES['image']['tmp_name'],$image); // yükleme işlemi
                }
            }?>
	  		<? 
            $cover_image='';
            if ($_FILES["cover_image"]["error"]== 0)
            {
            $uzanti=array('image/jpeg','image/jpg','image/png','image/x-png','image/gif'); 
                if(in_array(strtolower($_FILES['cover_image']['type']),$uzanti)){
                $cover_image=turkcekarakter($_FILES['cover_image']['name']);
                $random=rand(1,99999);
                $cover_src="$dizin/".$random.$cover_image;
                $cover_image="../$dizin/".$random.$cover_image;
                move_uploaded_file($_FILES['cover_image']['tmp_name'],$cover_image); // yükleme işlemi
                }
            }?>
            <? 
			if ($_POST['parent']==0){
				$parent=0;
				$parents=0;
			}else{
			$pr=mysql_fetch_assoc(mysql_query("Select * from product_categories where id='".$_POST['parent']."'"));
				$parent=$_POST['parent'];
				$parents=$pr['parents'].','.$parent;
			}?>
          	
            <?
            mysql_query("Insert Into products (parent, parents, title, image, cover_image, price, buy_link) Values ('".$parent."', '".$parents."', '".addslashes($_POST['title'])."','".$src."','".$cover_src."',  '".addslashes($_POST['price'])."',  '".addslashes($_POST['buy_link'])."')");?>
           
            <div class="alert alert-success">
              <button class="close" data-dismiss="alert">×</button>
              <strong>Kayıt Başarılı!</strong> Yeni kayıt başarıyla eklenmiştir.
            </div>
            <script type="text/javascript" language="JavaScript">window.location.href="products.php"</script>
        <? }?>	
        <? if ($_GET['f']=='edit'){?>
            <? 
            $image='';
            if ($_FILES["image"]["error"]== 0)
            {
            $uzanti=array('image/jpeg','image/jpg','image/png','image/x-png','image/gif'); 
                if(in_array(strtolower($_FILES['image']['type']),$uzanti)){
                $image=turkcekarakter($_FILES['image']['name']);
                $random=rand(1,99999);
                $src="$dizin/".$random.$image;
                $image="../$dizin/".$random.$image;
                move_uploaded_file($_FILES['image']['tmp_name'],$image); // yükleme işlemi
                mysql_query("Update products set image='".$src."' where id='".$_POST['id']."'");
                }
            }?>
           	<? 
            $cover_image='';
            if ($_FILES["cover_image"]["error"]== 0)
            {
            $uzanti=array('image/jpeg','image/jpg','image/png','image/x-png','image/gif'); 
                if(in_array(strtolower($_FILES['cover_image']['type']),$uzanti)){
                $cover_image=turkcekarakter($_FILES['cover_image']['name']);
                $random=rand(1,99999);
                $src="$dizin/".$random.$cover_image;
                $cover_image="../$dizin/".$random.$cover_image;
                move_uploaded_file($_FILES['cover_image']['tmp_name'],$cover_image); // yükleme işlemi
                mysql_query("Update products set cover_image='".$src."' where id='".$_POST['id']."'");
                }
            }?>
            <? $lastparent=mysql_fetch_assoc(mysql_query("select * from products where id='".$_POST['id']."'"))?>
			      <? $lastpr=$lastparent['parents'].','.$_POST['id'];?>
            <? 
            if ($_POST['parent']==0){
                $parent=0;
                $parents=0;
            }else{
            $pr=mysql_fetch_assoc(mysql_query("Select * from product_categories where id='".$_POST['parent']."'"));
                $parent=$_POST['parent'];
                $parents=$pr['parents'].','.$parent;
            }?>
            
            <?
			     mysql_query("Update products set title='".addslashes($_POST['title'])."', parent='".$parent."', parents='".$parents."',  price='".addslashes($_POST['price'])."',  buy_link='".addslashes($_POST['buy_link'])."' where id='".$_POST['id']."'");?>
            <div class="alert alert-success">
              <button class="close" data-dismiss="alert">×</button>
              <strong>Düzenleme Başarılı!</strong> Yeni kayıt başarıyla güncellenmiştir.
            </div>
            <script type="text/javascript" language="JavaScript">window.location.href="products.php"</script>
        <? }?>	
        <? if ($_GET['f']=='delete'){?>
            <? mysql_query("Delete from products where id='".$_POST['id']."'");?>
            <? mysql_query("Delete from l_products where product='".$_POST['id']."'");?>
            <div class="alert alert-success">
              <button class="close" data-dismiss="alert">×</button>
              <strong>Silme Başarılı!</strong> Kayıt başarıyla silinmiştir.
            </div>
            <script type="text/javascript" language="JavaScript">window.location.href="products.php"</script>
        <? }?>	
    <? }?>
  	<? if(($_GET['f']=='edit')&&(!isset($_POST['form_submit']))){?>
    <? $data=mysql_fetch_assoc(mysql_query("Select * from products where id='".$_GET['id']."'"))?>
    <div class="span12">
      <div class="widget-box">
        <div class="widget-title"> <span class="icon"> <i class="icon-align-justify"></i> </span>
          <h5>Kayıt Düzenle</h5>
        </div>
        <div class="widget-content nopadding">
          <form action="products.php?f=edit" method="post" enctype="multipart/form-data" class="form-horizontal">
           	<div class="control-group">
              <label class="control-label">Ana Kategori :</label>
              <div class="controls">
                <select name="parent" id="parent" class="validate[required]" >
					<? $qc=mysql_query("Select * from product_categories")?>
                    <? while ($dc=mysql_fetch_assoc($qc)){?>
                    <? $control=mysql_query("Select * from product_categories where parent='".$dc['id']."'")?>
                    <? if(mysql_num_rows($control)==0){?>
                    <option <? if ($data['parent']==$dc['id']){?> selected<? }?> value="<?=$dc['id']?>"><?=stripslashes(getparents($dc['title'],$dc['parents']))?></option>
                    <? }?>
                    <? }?>
                </select>
              </div>
            </div>
			
            <div class="control-group">
              <label class="control-label">Başlık :</label>
              <div class="controls">
                <input type="text" class="span11" value="<?=stripslashes($data['title'])?>" name="title" />
              </div>
            </div>
			
            
            
            <div class="control-group">
              <label class="control-label">Fiyat :</label>
              <div class="controls">
                <input type="text" class="span11" value="<?=stripslashes($data['price'])?>" name="price" />
              </div>
            </div>
			<div class="control-group">
              <label class="control-label">Satın Alma Linki :</label>
              <div class="controls">
                <input type="text" class="span11" value="<?=stripslashes($data['buy_link'])?>" name="buy_link" />
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">Resim : <img src="../<?=$data['image']?>" alt="thumb" width="30" /></label>
              <div class="controls">
                <input type="file" name="image"/>
              </div>
            </div>
			<div class="control-group">
              <label class="control-label">Üst Resim : <img src="../<?=$data['cover_image']?>" alt="thumb" width="30" /></label>
              <div class="controls">
                <input type="file" name="cover_image"/>
              </div>
            </div>
         
            <div class="form-actions">
                <input type="hidden" name="id" value="<?=$data['id']?>" />
                <input type="hidden" name="form_submit" value="1" />
              	<button type="submit" class="btn btn-success">Kaydet</button>
            </div>
          </form>
        </div>
      </div>
    </div>
    <? }?>
	<? if(($_GET['f']=='delete')&&(!isset($_POST['form_submit']))){?>
    <? $data=mysql_fetch_assoc(mysql_query("Select * from products where id='".$_GET['id']."'"))?>
    <div class="span12">
      <div class="widget-box">
        <div class="widget-title"> <span class="icon"> <i class="icon-align-justify"></i> </span>
          <h5>Kayıt Sil</h5>
        </div>
        <div class="widget-content nopadding">
          <form action="products.php?f=delete" method="post" enctype="multipart/form-data" class="form-horizontal">
            <div class="control-group">
              <label class="control-label">Başlık :</label>
              <div class="controls">
                <input type="text" class="span11" value="<?=stripslashes($data['title'])?>" name="title" />
              </div>
            </div>
            <div class="form-actions">
                <input type="hidden" name="id" value="<?=$data['id']?>" />
                <input type="hidden" name="form_submit" value="1" />
              <button type="submit" class="btn btn-success">Kaydı Sil</button>
            </div>
          </form>
        </div>
      </div>
    </div>
    <? }?>
    <? }else{?>
    <div class="span12">
      <div class="widget-box">
        <div class="widget-title"> <span class="icon"> <i class="icon-align-justify"></i> </span>
          <h5>Kayıt Ekle</h5>
        </div>
        <div class="widget-content nopadding">
          <form action="products.php?f=add" method="post" enctype="multipart/form-data" class="form-horizontal">
           	<div class="control-group">
              <label class="control-label">Ana Kategori :</label>
              <div class="controls">
                <select name="parent" id="parent" class="validate[required] span5">
					<? $qc=mysql_query("Select * from product_categories")?>
                    <? while ($dc=mysql_fetch_assoc($qc)){?>
                    <? $control=mysql_query("Select * from product_categories where parent='".$dc['id']."'")?>
                    <? if(mysql_num_rows($control)==0){?>
                    <option value="<?=$dc['id']?>"><?=stripslashes(getparents($dc['title'],$dc['parents']))?></option>
                    <? }?>
                    <? }?>
                </select>
              </div>
            </div>
			
            <div class="control-group">
              <label class="control-label">Başlık :</label>
              <div class="controls">
                <input type="text" class="span11" value="" name="title" />
              </div>
            </div>
            
            
            <div class="control-group">
              <label class="control-label">Fiyat :</label>
              <div class="controls">
                <input type="text" class="span11" value="" name="price" />
              </div>
            </div>
			<div class="control-group">
              <label class="control-label">Satın Alma Linki :</label>
              <div class="controls">
                <input type="text" class="span11" value="" name="buy_link" />
              </div>
            </div>
            
            <div class="control-group">
              <label class="control-label">Resim : </label>
              <div class="controls">
                <input type="file" name="image"/>
              </div>
            </div>
       		<div class="control-group">
              <label class="control-label">Üst Resim :</label>
              <div class="controls">
                <input type="file" name="cover_image"/>
              </div>
            </div>
            <div class="form-actions">
                <input type="hidden" name="form_submit" value="1" />
              <button type="submit" class="btn btn-success">Kaydet</button>
            </div>
          </form>
        </div>
      </div>
      <div class="widget-box">
          <div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>
            <h5>Kayıtlar</h5>
            	<div style="margin:0 20px 0; padding:0 0 0; float:right;">
                	<div class="btn-group">
                        <button class="btn">Proje Grubu</button>
                        <button data-toggle="dropdown" class="btn dropdown-toggle"><span class="caret"></span></button>
                        <ul class="dropdown-menu">
                          	<li><a href="products.php">TÜMÜ</a></li>
                            <? $q=mysql_query("Select * from product_categories where parent='0' and parents='0' order by priority asc")?> 
                            <? while ($d=mysql_fetch_assoc($q)){?>
                            
                            <li><a href="products.php?parent=<?=$d['id']?>"><?=$d['title']?></a></li>
                            <? }?>
                        </ul>
                    </div>
                	<div class="btn-group">
                        <button class="btn btn-danger">İşlemler</button>
                        <button data-toggle="dropdown" class="btn btn-danger dropdown-toggle"><span class="caret"></span></button>
                        <ul class="dropdown-menu">
                        	<li><a href="javascript:void(0)" id="show_product_on_site">Sitede Göster</a></li>
                            <li><a href="javascript:void(0)" id="hide_product_on_site">Sitede Gösterme</a></li>
                          	<li><a href="javascript:void(0)" id="delete_records">Seçili Projeleri Sil</a></li>
                            <li><a href="javascript:void(0)" id="add_popular">Vitrine Ekle</a></li>
                            <li><a href="javascript:void(0)" id="remove_popular">Vitrinden Kaldır</a></li>
                        </ul>
                   </div>
                </div>
          </div>
          <div class="widget-content nopadding">
            <table class="table table-bordered data-table">
              <thead>
                	<tr>
                    <th>ID</th>
                    <th width="30%">Başlık</th>
                    <? $q=mysql_query("Select * from languages order by id asc")?>
				            <? while ($d=mysql_fetch_assoc($q)){?>
                    <th>İçerik(<?=stripslashes($d['title'])?>)</th>
                    <? }?>
                    <th class="sorting_disabled">Resim</th>
					<th class="sorting_disabled">Üst Resim</th>
                    <th class="sorting_disabled">Sekmeler</th>
                    <th>İşlem</th>
                    <th>/</th>                                      
                  </tr>
                </thead>
                <tbody>
                <? if (isset($_GET['parent'])){?>
                <? $q=mysql_fetch_assoc(mysql_query("Select * from product_categories where id='".$_GET['parent']."'"))?> 
                <? $query=mysql_query("Select * from products where parents like '".addslashes($q['parents']).",".addslashes($q['id'])."%'")?>
                <? }else{?>
                <? $query=mysql_query("Select * from products")?>
                <? }?>
                <? while ($data=mysql_fetch_assoc($query)){?>
                    <tr>
                        <td><?=stripslashes($data['id'])?></td>  
                        <td><?=stripslashes($data['title'])?></td>
                        <? $q=mysql_query("Select * from languages order by id asc")?>
						<? while ($d=mysql_fetch_assoc($q)){?>
                        <? $cont=mysql_query("Select * from l_products where product='".$data['id']."' and language='".$d['id']."'")?>
                        <td>
                            <? if (mysql_num_rows($cont)==0){?>
                            <a href="products_content.php?f=add&product=<?=$data['id']?>&language=<?=$d['id']?>" class="label label-important">Ekle</a>
                            <? }else{?>
                            <a href="products_content.php?f=edit&product=<?=$data['id']?>&language=<?=$d['id']?>" class="label label-warning">Düzenle</a>
                            <? }?>
                        </td>
                        <? }?>
                        <td><img src="../photo/90/90/100/<?=$data['image']?>" alt="thumb" width="90" /></td>
						<td><img src="../photo/90/90/100/<?=$data['cover_image']?>" alt="thumb" width="90" /></td> 
                        <td>
                        	<a href="product_tab.php?product=<?=stripslashes($data['id'])?>" class="tt btn btn-small" title="Kaydı Düzenle"><i class="isw-edit"></i>Ekstra Alan</a>
                        	<a href="product_gallery.php?product=<?=stripslashes($data['id'])?>" class="tt btn btn-small" title="Kaydı Düzenle"><i class="isw-edit"></i>Foto Galeri</a>
                        </td>
                        <td>
                            <a href="products.php?f=edit&id=<?=stripslashes($data['id'])?>" class="tt btn btn-small" title="Kaydı Düzenle"><i class="isw-edit"></i>Düzenle</a> 
                            <a href="products.php?f=delete&id=<?=stripslashes($data['id'])?>" class="tt btn btn-small" title="Kaydı Sil"><i class="isw-cancel"></i>Sil</a>
                        </td>
                        <td><input type="checkbox" rel="<?=stripslashes($data['id'])?>" p_code="<?=stripslashes($data['id'])?>" /></td>                                 
                    </tr>
                <? }?>
              </tbody>
            </table>
          </div>
        </div>
    </div>
    <? }?>
  </div>
</div></div>
<script src="js/jquery.min.js"></script> 
<script src="js/jquery.ui.custom.js"></script> 
<script src="js/bootstrap.min.js"></script> 
<script src="js/bootstrap-colorpicker.js"></script> 
<script src="js/bootstrap-datepicker.js"></script> 
<script src="js/jquery.toggle.buttons.html"></script> 
<script src="js/masked.js"></script> 
<script src="js/jquery.uniform.js"></script> 
<script src="js/select2.min.js"></script> 
<script src="js/matrix.js"></script> 
<script src="js/matrix.form_common.js"></script> 
<script src="js/wysihtml5-0.3.0.js"></script> 
<script src="js/jquery.peity.min.js"></script> 

<script>
$("#delete_records").click(function() {
	$("input[type='checkbox']").each(function() {
		if($(this).prop('checked')){
			var product= $( this ).attr("rel");
			$.ajax({
				type: "POST",
				url: "ajaxfunctions.php",
				data: { 
					f: "delete_product_list",
					product: product
				},
				success: function(){
					$("#product_"+product).remove();
				}
			})
		}
	});
}); 
</script>
<script>
$("#show_product_on_site").click(function() {
	$("input[type='checkbox']").each(function() {
		if($(this).prop('checked')){
			var product= $( this ).attr("rel");
			$.ajax({
				type: "POST",
				url: "ajaxfunctions.php",
				data: { 
					islem: "show_product_on_site",
					product: product
				},
				success: function(){
					$("#product_list_"+product).html('<span class="label label-success">Eklendi</span>');
				}
			})
		}
	});
}); 
</script>
<script>
$("#hide_product_on_site").click(function() {
	$("input[type='checkbox']").each(function() {
		if($(this).prop('checked')){
			var product= $( this ).attr("rel");
			$.ajax({
				type: "POST",
				url: "ajaxfunctions.php",
				data: { 
					islem: "hide_product_on_site",
					product: product
				},
				success: function(){
					$("#product_list_"+product).html('<span class="label label-inverse">Kaldırıldı</span>');
				}
			})
		}
	});
}); 
</script>





<script>
$("#remove_popular").click(function() {
	$("input[type='checkbox']").each(function() {
		if($(this).prop('checked')){
			var product= $( this ).attr("rel");
			$.ajax({
				type: "POST",
				url: "ajaxfunctions.php",
				data: { 
					islem: "remove_popular",
					product: product
				},
				success: function(){
					$("#popular_"+product).html('<span class="label label-inverse">Kaldırıldı</span>');
				}
			})
		}
	});
}); 
</script>
<script>
$("#add_popular").click(function(){
	$("input[type='checkbox']").each(function() {
		if($(this).prop('checked')){
			var product= $( this ).attr("rel");
			$.ajax({
				type: "POST",
				url: "ajaxfunctions.php",
				data: { 
					islem: "add_popular",
					product: product
				},
				success: function(){
					$("#popular_"+product).html('<span class="label label-success">Eklendi</span>');
				}
			})
		}
	});
});
</script>


</body>
</html>
<? } else { header('location:login.php');}?>