<?php include 'sets.php';?>
<?php
$superuser="0";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Webadmin <?php echo $domain ?></title>
<?php include 'codes.php';?>
</head>
<body>

<?php include 'navbar.php';?>

<?php 
$catid = $mysqli->real_escape_string(stripslashes($_REQUEST['catid']));
$date = date ("j.m.Y");

$result = $mysqli->query("SELECT name, type FROM categories WHERE insert_id='$catid' AND lang_id=$lang ORDER BY priority desc LIMIT 1");
$row = $result->fetch_array();
$category=$row['name'];
$typec=$row['type'];

$resultsc = $mysqli->query("SELECT id FROM sections WHERE lang_id=$lang");
$srows = mysqli_num_rows($resultsc);

$result = $mysqli->query("SELECT insert_id FROM items WHERE lang_id=$lang ORDER BY insert_id desc LIMIT 1");
$row = $result->fetch_array(); 
$isid=$row['insert_id'];
$insertid=$isid + 1;
?>
    
<div class="container">
<div class="frame">
<div class="frametitles fadd"><?php echo $adminadditem1 ?>
<div class="floatright"><a href="list-items.php?catid=<?php echo $catid ?>"><img src="img/list.png" alt="<?php echo $category ?>" title="<?php echo $category ?>"></a></div>
</div>
<div class="framein">

<form name="forma" method="post" enctype="multipart/form-data">
<input type="hidden" name="max_file_size" value="5000000">

<table>
    <tr>
      <td class="tabletd1">&nbsp;</td>
      <td class="tabletd2"><span class="icon fa-folder-open"></span></td>
      <td>
<select name="category_id">
<?php
$result1 = $mysqli->query("SELECT insert_id, name, type, level, parent FROM categories WHERE level=1 AND type<3 AND lang_id=$lang ORDER BY priority");
while($row = $result1->fetch_array()) {
$catid1=$row['insert_id'];
$category1=$row['name'];
$typec1=$row['type'];
$level=$row['level'];
$parent=$row['parent'];
?>
<option value="<?php echo $catid1 ?>"<?php if ($catid1==$catid) { echo " selected"; }?><?php if ($typec1==2) { echo ' class="color1"'; }?>>
&raquo; <?php echo $category1 ?>
</option>
  
<?php 
$result2 = $mysqli->query("SELECT insert_id, name, type FROM categories WHERE parent='$catid1' AND lang_id=$lang ORDER BY priority");
while($row = $result2->fetch_array()) {
$catid2=$row['insert_id'];
$category2=$row['name'];
$typec2=$row['type'];
?>
<option value="<?php echo $catid2 ?>"<?php if ($catid2==$catid) { echo " selected"; }?><?php if ($typec2==2) { echo ' class="color1"'; }?>> 
----&raquo; <?php echo $category2 ?>
</option>
<?php } ?>
<?php } ?>
</select>

<?php if ($srows>0 && $catid>1) { ?>        
&nbsp; &nbsp;
<select name="section_id">
<option value="0">&raquo; <?php echo $adminregion ?></option>  
<option value="0">----</option> 
<?php
$result1s = $mysqli->query("SELECT insert_id, name, level, parent FROM sections WHERE level=1 AND lang_id=$lang ORDER BY priority");
while($row = $result1s->fetch_array()) {
$secid1=$row['insert_id'];
$section1=$row['name'];
$level=$row['level'];
$parent=$row['parent'];
?>
  <option value="<?php echo $secid1 ?>" class="color2"> 
&raquo; <?php echo $section1 ?>
  </option>
  
<?php 
$result2s = $mysqli->query("SELECT insert_id, name FROM sections WHERE parent=$secid1 AND lang_id=$lang ORDER BY priority");
while($row = $result2s->fetch_array()) {
$secid2=$row['insert_id'];
$section2=$row['name'];
?>
  <option value="<?php echo $secid2 ?>"> 
----&raquo; <?php echo $section2 ?>
  </option>

<?php } ?>
<?php } ?>
</select>
<?php } ?>
      </td>
    </tr>
    </table>
    
<?php
$result30 = $mysqli->query("SELECT * FROM lang WHERE active=1 ORDER BY priority");
while($row = $result30->fetch_array()) {
$langid=$row['id'];
$language=$row['language'];
$dir=$row['dir'];
?>

<?php 
if  (isset($_POST['submit'])) {
$category_id = $mysqli->real_escape_string(stripslashes($_REQUEST['category_id']));
if ($_REQUEST['section_id']) { 
$section_id = $mysqli->real_escape_string(stripslashes($_REQUEST['section_id']));
}else{
$section_id=0;
}

$name = $mysqli->real_escape_string(stripslashes($_REQUEST["name$langid"]));
$name = strip_tags($name);
$name = trim($name);

$page=preg_replace('/[^\p{L}\p{N}\s]/u', ' ', $name);
$page=transliterate($page);
setlocale(LC_ALL, 'en_GB'); 
$page = iconv("UTF-8", "ASCII//TRANSLIT", $page); 
$page=preg_replace("/[^a-zA-Z0-9- ]/", ' ', $page);
$page = trim($page);
$page=preg_replace('/\s+/', '-', $page);
$page=strtolower(preg_replace('/\-+/', '-', $page));

$name = preg_replace('/\s+/', ' ',$name);
$description = $mysqli->real_escape_string(stripslashes($_REQUEST["description$langid"]));
$description = strip_tags($description);
$description = str_replace('\r\n', ' ',$description);
$description = preg_replace('/\s+/', ' ',$description);
$details = $mysqli->real_escape_string(stripslashes($_REQUEST["details$langid"]));
$details = str_replace('\r\n', ' ',$details);
$text = $mysqli->real_escape_string(stripslashes($_REQUEST["text$langid"]));
$text = str_replace('\r\n', ' ',$text);
$extra = $mysqli->real_escape_string(stripslashes($_REQUEST["extra$langid"]));
$unit = $mysqli->real_escape_string(stripslashes($_REQUEST["unit$langid"]));

$code = $mysqli->real_escape_string(stripslashes($_REQUEST['code']));
$url = $mysqli->real_escape_string(stripslashes($_REQUEST['url']));
$url=preg_replace('/\s+/', '', $url);
$email = $mysqli->real_escape_string(stripslashes($_REQUEST['email']));
$email=preg_replace('/\s+/', '', $email);
$email = strtolower($email);
$script = $mysqli->real_escape_string(stripslashes($_REQUEST['script']));
$price = $mysqli->real_escape_string(stripslashes($_REQUEST['price']));	
$offerprice = $mysqli->real_escape_string(stripslashes($_REQUEST['offerprice']));
$weight = $mysqli->real_escape_string(stripslashes($_REQUEST['weight']));
$new = $mysqli->real_escape_string(stripslashes($_REQUEST['new']));	
$available = $mysqli->real_escape_string(stripslashes($_REQUEST['available']));	
$promote = $mysqli->real_escape_string(stripslashes($_REQUEST['promote']));	
$showpage = $mysqli->real_escape_string(stripslashes($_REQUEST['showpage']));	
$publish = $mysqli->real_escape_string(stripslashes($_REQUEST['publish']));

if( $filen=='' ){
$file= "";
}

$result = $mysqli->query("SELECT page, type FROM categories WHERE insert_id='$category_id' AND lang_id=$langid");
$row = $result->fetch_array(); 
$cpage=$row['page'];
$typec=$row['type'];

if ($typec==0){
$typei = 1;
} else {
$typei = $typec;
}

$result = $mysqli->query("SELECT page_id FROM items WHERE page_id>0 AND insert_id!=$insertid ORDER BY page_id desc LIMIT 1");
$row = $result->fetch_array(); 
$pgid=$row['page_id'];
$pageid=$pgid + 1;

if ($typec==2 OR ($details AND $text)){
$pageid = $pageid;
} else {
$pageid = 0;
}
if ($pageid>0 AND !$page){
$page = $cpage;
}

if ($category_id==1){
$promote = 0;
}

$result = $mysqli->query("SELECT priority FROM items WHERE category_id=$category_id AND section_id=$section_id AND insert_id!=$insertid AND lang_id=$lang ORDER BY priority desc LIMIT 1");
$row = $result->fetch_array(); 
$pid=$row['priority'];
$priorityid=$pid + 1;

$mysqli->query("INSERT INTO items (insert_id, lang_id, category_id, section_id, page_id, page, name, description, details, text, extra, code, url, email, script, price, offerprice, weight, unit, file, type, new, available, promote, showpage, publish, priority, date) VALUES ('$insertid', '$langid', '$category_id', '$section_id', '$pageid', '$page', '$name', '$description', '$details', '$text', '$extra', '$code', '$url', '$email', '$script', '$price', '$offerprice', '$weight', '$unit',  '$file', '$typei', '$new', '$available', '$promote', '$showpage', '$publish', '$priorityid', '$date')");
 } ?> 

<hr class="overline bordercolor1">
  <table>
    <tr>
      <td class="tabletd1"><img src="img/<?php echo $dir ?>.png" width="16" height="16" title="<?php echo $language ?>" alt="<?php echo $language ?>"></td> 
      <td class="tabletd2">
	  <?php echo $adminnamed ?> <?php if ($typec==2) { echo '*'; } ?>
       </td>
      <td> 
        <input type="text" name="name<?php echo $langid ?>" size="100"<?php if ($typec==2) { echo ' required'; } ?>>
        </td>
    </tr>
    <tr>
      <td><img src="img/<?php echo $dir ?>.png" width="16" height="16" title="<?php echo $language ?>" alt="<?php echo $language ?>"></td>
      <td><?php echo $admintext ?></td>
      <td>
      <div class="diveditor1"><textarea name="details<?php echo $langid ?>" cols="75" rows="10" class="mceEditor"></textarea></div>
      <div class="diveditor2"><textarea name="text<?php echo $langid ?>" cols="100" rows="10" class="mceEditor"></textarea></div>
      </td>
    </tr>
    <tr>
      <td><?php if ($adminunit OR $adminextra) { ?><img src="img/<?php echo $dir ?>.png" width="16" height="16" title="<?php echo $language ?>" alt="<?php echo $language ?>"><?php } ?></td>
      <td><?php echo $adminunit ?> </td>
      <td>
      <?php if ($adminunit) { ?>
      <input name="unit<?php echo $langid ?>" type="text" size="15" class="inputauto">
      &nbsp; &nbsp; &nbsp;
      <?php } ?>
      <?php if ($adminextra) { ?>  
      <?php echo $adminextra ?> <input type="text" name="extra<?php echo $langid ?>" size="40" class="inputauto">
      <?php } ?>
      </td>
    </tr>
    </table>

<div class="divtags">  
<span class="icon icstyle1 fa-tags color2" title="meta tags"></span>
<hr>
  <table class="tcolor">
    <tr>
      <td class="tabletd1"><img src="img/<?php echo $dir ?>.png" width="16" height="16" title="<?php echo $language ?>" alt="<?php echo $language ?>"></td>
      <td class="tabletd2"><?php echo $admindesc ?></td>
      <td><textarea name="description<?php echo $langid ?>" cols="75" rows="3"></textarea></td>
    </tr>
    </table> 
</div>
  
<?php } ?>

<hr>
<table>
  <tr>
    <td class="tabletd1">&nbsp;</td>
    <td class="tabletd2"><span class="icon fa-link" title="Link url"></span></td>
    <td><input type="text" name="url" size="100"></td>
  </tr>
   <?php if ($type==4 && $typec==2) { ?>
  <tr>
    <td>&nbsp;</td>
    <td><span class="icon fa-envelope" title="Email"></span></td>
    <td><input type="text" name="email" size="40"></td>
  </tr>
 <?php } ?> 
  <tr>
    <td>&nbsp;</td>
    <td><span class="icon fa-code" title="<?php echo $adminscript ?>"></span></td>
    <td><textarea name="script" cols="75" rows="6"></textarea></td>
  </tr>
  
 <?php if ($typec==2 && $type!=3 && $type!=4 && $type!=6) { ?>
 <tr>
    <td>&nbsp;</td>
    <td><span class="icon fa-barcode" title="<?php echo $admincode ?>"></span></td>
    <td><input type="text" name="code" size="60" class="inputauto"></td>
  </tr>
    <tr class="price divdprice">
    <td>&nbsp;</td>
    <td><span class="icon fa-eur"></span>
    <div class="floatright"><?php echo $adminprice ?></div>
    </td>
    <td>
     <input type="text" name="price" maxlength="11" size="15" class="inputauto">
      &nbsp; &nbsp; 
     <?php echo $adminpriceo ?> <input type="text" name="offerprice" maxlength="11" size="15" class="inputauto">
     </td>
  </tr>
  <?php if ($adminweight) { ?>
    <tr class="divweight">
      <td>&nbsp;</td>
      <td><span title="<?php echo $adminweight ?> Kg">Kg</span></td>
      <td>
  <input type="text" name="weight" size="15" maxlength="11" class="inputauto"> 
      </td>
    </tr>
 <?php } ?>
 <?php } ?>    
   
    <tr>
      <td>&nbsp;</td> 
      <td><span class="icon fa-image" title="<?php echo $adminimg ?>"></span></td>
      <td>
        <input name="upload" type="file" size="50" title="upload photo .jpg">   
        </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><span class="icon fa-file-text" title="<?php echo $adminfile ?>"></span></td>
      <td>
   <input name="uploadfile" type="file" size="50" title="upload file - max 5 MB">   
      </td>
    </tr>

<?php if ($catid>1 AND ($typec==2 OR $promoteitem==1)) { ?>    
<tr class="divextra">
    <td>&nbsp;</td>
    <td><span class="icon fa-cog" title="<?php echo $adminfeatures ?>"></span></td>
    <td>
    <table class="widthauto aligncenter text1">
        <tr>
    <?php if ($typec==2) { ?>
          <td>
          <?php echo $adminnew ?> <br>
  <input type="hidden" name="new" value="0"> <input type="checkbox" name="new" value="1">         
          </td>
          <td>
          <?php echo $adminavailable ?> <br>
 <input type="hidden" name="available" value="0"> <input type="checkbox" name="available" value="1"<?php if ($type!=4) { echo " checked"; }?>>
          </td>
          <td>
          <?php echo $adminshow ?> <br>
 <input type="hidden" name="showpage" value="0"> <input type="checkbox" name="showpage" value="1" checked> 
          </td>
     <?php } ?> 
         
          <td>
<input type="hidden" name="promote" value="0"> 
          
<?php if ($promoteitem==1) { ?>
<?php echo $adminpromote ?> <br>
<span class="icon icstyle fa-home"></span>
<input type="checkbox" name="promote" value="1">
&nbsp; &nbsp;
<?php } ?>
          </td>
          </tr>
        </table>
        </td>
  </tr>
<?php } ?>

    <tr>
      <td>&nbsp;</td> 
      <td><span class="icon fa-eye" title="<?php echo $adminpublish ?>"></span></td>
      <td><input type="hidden" name="publish" value="no"> 
          <input type="checkbox" name="publish" value="yes" checked></td>
    </tr>
    </table>
    
<!--UPLOAD --> 
<?php
if  ( isset($_POST['submit']) AND ($_FILES['upload']['tmp_name'] != "" ) ) {

$file_tmp =$_FILES['upload']['tmp_name'];
	   
		$uploadid=$insertid;
		$uploaddir = $filepath.'items/';
		$uploadname = $uploadid.'.jpg'; 
	
		copy($file_tmp,$uploaddir.$uploadname);
		$mysqli->query("UPDATE items SET image='1' WHERE insert_id=$uploadid");
	
resizephoto($uploaddir.$uploadname,$uploaddir.$uploadname,$thumb_w,$thumb_h);

}
?>

<?php
if  ( isset($_POST['submit']) AND ($_FILES['uploadfile']['tmp_name'] != "" ) ) {

$file_tmpf =$_FILES['uploadfile']['tmp_name'];
		
$filename = $_FILES["uploadfile"]["name"];
$tmp = explode('.', $filename);
$filetype = end($tmp);

if( $filetype == "exe" ){
$file= "";
} else {
$file= $filetype;
}

		$uploadid=$insertid;
		$uploaddirfile = $filepath.'items/files/';
		$uploadnamefile = $uploadid.".".$filetype; 
	
		copy($file_tmpf,$uploaddirfile.$uploadnamefile);
		$mysqli->query("UPDATE items SET file='$file' WHERE insert_id=$uploadid");

}
?>    

   <hr>
  <div class="divallbuttons">
<div class="divlist"><a href="list-items.php?catid=<?php echo $catid ?>" title="<?php echo $adminlist ?>" class="icstyle1 color1"><span class="icon fa-tasks"></span></a></div>
<div class="divbuttons">
  <button type="submit" name="submit" class="button"><span class="icon fa-check"></span> <?php echo $adminsubmit ?></button>
  <button type="reset" name="reset" class="button buttondelete"><span class="icon fa-refresh"></span> <?php echo $adminreset ?></button> 
</div>
   </div>
   
</form>  

</div>
</div>
</div>

<?php include 'menudown.php';?>

</body>
</html>