Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once $_SERVER['DOCUMENT_ROOT'].'/tutorial/core/init.php';
- if(!is_logged_in()){
- login_error_redirect();
- }
- include 'includes/head.php';
- include 'includes/navigation.php';
- if (isset($_GET['delete'])) {
- $delID = (int)$_GET['delete'];
- $delID = sanitize($delID);
- $delSql = "UPDATE products SET deleted = 1 WHERE id = '$delID'";
- $db->query($delSql);
- header('Location: products.php');
- }
- $dbpath = '';
- if (isset($_GET['add']) || isset($_GET['edit'])) {
- $parentQ = $db->query("SELECT * FROM categories WHERE parent = 0 ORDER BY category");
- $title = ((isset($_POST['title']) && $_POST['title'] != '')?sanitize($_POST['title']):'');
- $parent = ((isset($_POST['parent']) && !empty($_POST['parent']))?sanitize($_POST['parent']):'');
- $category = ((isset($_POST['child']) && !empty($_POST['child']))?sanitize($_POST['child']):'');
- $price = ((isset($_POST['price']) && $_POST['price'] != '')?sanitize($_POST['price']):'');
- $price = preg_replace("/[^0-9]/", "", $price);
- $description = ((isset($_POST['description']))?sanitize($_POST['description']):'');
- $units = ((isset($_POST['units']) && $_POST['units'] != '')?sanitize($_POST['units']):'');
- $units = rtrim($units,',');
- $quantity = ((isset($_POST['quantity']) && $_POST['quantity'] != '')?sanitize($_POST['quantity']):'');
- $saved_image = '';
- if (isset($_GET['edit'])) {
- $edit_id = (int)$_GET['edit'];
- $edit_id = sanitize($edit_id);
- $productresults = $db->query("SELECT * FROM products WHERE id = '$edit_id'");
- $products = mysqli_fetch_assoc($productresults);
- if (isset($_GET['delete_image'])) {
- $imgi = (int)$_GET['imgi'] - 1;
- $images = explode(',',$products['image']);
- $image_url = $_SERVER['DOCUMENT_ROOT'].$images[$imgi];
- unlink($image_url);
- unset($images[$imgi]);
- $imageString = implode(',',$images);
- $db->query("UPDATE products SET image = '{$imageString}' WHERE id = '{$edit_id}'");
- header('Location: products.php?edit='.$edit_id);
- }
- $category = ((isset($_POST['child']) && $_POST['child'] != '')?sanitize($_POST['child']):$products['categories']);
- $title = ((isset($_POST['title']) && $_POST['title'] != '')?sanitize($_POST['title']):$products['title']);
- $parentResult = $db->query("SELECT * FROM categories WHERE id = '$category'");
- $pQ = mysqli_fetch_assoc($parentResult);
- $parent = ((isset($_POST['parent']) && $_POST['parent'] != '')?sanitize($_POST['parent']):$pQ['parent']);
- $price = ((isset($_POST['price']) && $_POST['price'] != '')?sanitize($_POST['price']):money($products['price']));
- $price = preg_replace("/[^0-9]/", "", $price);
- $description = ((isset($_POST['description']))?sanitize($_POST['description']):$products['description']);
- $units = ((isset($_POST['units']) && $_POST['units'] != '')?sanitize($_POST['units']):$products['units']);
- $units = rtrim($units,',');
- $quantity = ((isset($_POST['quantity']) && $_POST['quantity'] != '')?sanitize($_POST['quantity']):$products['quantity']);
- $saved_image = (($products['image'] != '')?$products['image']:'');
- $dbpath = $saved_image;
- }
- if (!empty($units)) {
- $unitstring = sanitize($units);
- $unitstring = rtrim($unitstring,',');
- $unitsArray = explode(',',$unitstring);
- $sArray = array();
- $qArray = array();
- $tArray = array();
- foreach ($unitsArray as $ss) {
- $s = explode(':',$ss);
- $sArray[] = $s[0];
- $qArray[] = $s[1];
- $tArray[] = $s[2];
- }
- }else{
- $unitsArray = array();
- }
- if ($_POST) {
- $errors = array();
- $required = array('title', 'parent', 'child', 'units', 'quantity');
- $allowed = array('png','jpg','jpeg','gif');
- $photoName = array();
- $uploadPath = array();
- $tmpLoc = array();
- foreach ($required as $field) {
- if ($_POST[$field] == '') {
- $errors[] = 'Form tidak boleh ada yang kosong!';
- break;
- }
- }
- var_dump($_FILES['photo']);
- $photoCount = count($_FILES['photo']['name']);
- if ($photoCount > 0) {
- for ($i=0; $i < $photoCount; $i++) { echo $i;
- $name = $_FILES['photo']['name'][$i];
- $nameArray = explode('.',$name);
- $fileName = $nameArray[0];
- $fileExt = $nameArray[1];
- $mime = explode('/',$_FILES['photo']['type'][$i]);
- $mimeType = $mime[0];
- $mimeExt = $mime[1];
- $tmpLoc[] = $_FILES['photo']['tmp_name'][$i];
- $fileUnit = $_FILES['photo']['size'][$i];
- $uploadName = md5(microtime().$i).'.'.$fileExt;
- $uploadPath[] = BASEURL.'images/products'.$uploadName;
- if ($i != 0) {
- $dbpath .= ',';
- }
- $dbpath .= '/tutorial/images/products'.$uploadName;
- if ($mimeType != 'image') {
- $errors[] = 'File harus berupa gambar';
- }
- if (!in_array($fileExt, $allowed)) {
- $errors[] = 'Gambar harus berekstensi png, jpg, jpeg atau gif';
- }
- if ($fileUnit > 15000000) {
- $errors[] = 'Ukuran file tidak boleh lebih dari 15MB.';
- }
- if ($fileExt != $mimeExt && ($mimeExt == 'jpeg' && $fileExt != 'jpg')) {
- $errors[] = 'Errors 404 ! Ulangi upload dengan benar';
- }
- }
- }
- if (!empty($errors)) {
- echo display_errors($errors);
- }else{
- if($photoCount > 0){
- //Upload file dan tambah ke dalam database
- for ($i=0; $i < $photoCount; $i++) {
- move_uploaded_file($tmpLoc[$i], $uploadPath[$i]);
- }
- }
- $insertSql = "INSERT INTO products (title, price, categories, units, image, description)
- VALUES ('$title', '$price', '$categories', '$units', '$quantity','$dbpath', '$description')";
- if (isset($_GET['edit'])) {
- $insertSql = "UPDATE products SET title = '$title', price = '$price',
- categories = '$categories', units = '$units', $quantity = 'quantity', image = '$dbpath', description = '$description'
- WHERE id = '$edit_id'";
- }
- $db->query($insertSql);
- header('Location: products.php');
- }
- }
- ?>
- <div class="container">
- <h2 class="text-center"><?=((isset($_GET['edit']))?'Edit ':'');?>Products</h2><hr>
- <form action="products.php?<?=((isset($_GET['edit']))?'edit='.$edit_id:'add=1');?>" method="post" enctype="multipart/form-data">
- <div class="form-group col-md-3">
- <label for="title">Title*:</label>
- <input type="text" name="title" class="form-control" id="title" value="<?=$title;?>">
- </div>
- <div class="form-group col-md-3">
- <label for="parent">Parent Category*:</label>
- <select class="form-control" id="parent" name="parent">
- <option value=""<?=(($parent == '')?' selected':'');?>></option>
- <?php foreach ($parentQ as $p) : ?>
- <option value="<?=$p['id'];?>"<?=(($parent == $p['id'])?' selected':'');?>><?=$p['category'];?></option>
- <?php endforeach; ?>
- </select>
- </div>
- <div class="form-group col-md-3">
- <label for="child">Child Category*:</label>
- <select class="form-control" id="child" name="child">
- </select>
- </div>
- <div class="form-group col-md-3">
- <label for="price">Price*:</label>
- <input type="text" id="price" name="price" class="form-control" value="<?=((isset($_GET['edit']))?money($price):'');?>">
- </div>
- <div class="form-group col-md-3">
- <label for="quantity">Quantity*:</label>
- <input type="text" id="quantity" name="quantity" class="form-control" value="<?=((isset($_GET['edit']))?($quantity):'');?>">
- </div>
- <div class="form-group col-md-3">
- <label for="quantity">Units*:</label>
- <select class="form-control" id="units" name="units" value="<?=((isset($_GET['edit']))?($quantity):'');?>">
- <option>Kg</option>
- <option>Pcs</option>
- <option>Cyl</option>
- <option>Pkt</option>
- </select>
- </div>
- <div class="form-group col-md-6">
- <?php if($saved_image != '') : ?>
- <?php
- $imgi = 1;
- $images = explode(',',$saved_image);
- ?>
- <?php foreach($images as $image): ?>
- <div class="saved-image col-md-3">
- <img src="<?=$image;?>" alt="saved image"><br>
- <a href="products.php?delete_image=1&edit=<?=$edit_id;?>&imgi=<?=$imgi;?>" class="btn btn-xs btn-danger">Delete Image</a>
- </div>
- <?php
- $imgi++;
- endforeach; ?>
- <?php else: ?>
- <label for="photo">Products Photo*:</label>
- <input type="file" name="photo[]" id="photo" class="form-control" multiple>
- <?php endif; ?>
- </div>
- <div class="form-group col-md-6">
- <label for="description">Description*:</label>
- <textarea name="description" class="form-control" id="description" rows="6"><?=$description;?></textarea>
- </div>
- <div class="form-group pull-right" style="margin-right: 10px;">
- <a href="products.php" class="btn btn-default">Cancel</a>
- <input type="submit" class="btn btn-primary" value="<?=((isset($_GET['edit']))?'Edit':'');?>Add Product">
- </div><div class="clearfix"></div>
- </form>
- </div>
- <!-- Modal -->
- <?php
- }else{
- $sql = "SELECT * FROM products WHERE deleted = 0";
- $presult = $db->query($sql);
- if (isset($_GET['featured'])) {
- $id = (int)$_GET['id'];
- $featured = (int)$_GET['featured'];
- $fsql = "UPDATE products SET featured = '$featured' WHERE id = '$id'";
- $db->query($fsql);
- header('Location: products.php');
- }
- ?>
- <div class="container">
- <h2 class="text-center">Products</h2>
- <a href="products.php?add=1" class="btn btn-primary pull-right" style="margin-top: -35px;">Add Product</a><div class="clearfix"></div>
- <hr>
- <table class="table table-bordered table-condensed table-striped">
- <thead>
- <th>Action</th><th>Produk</th><th>Harga</th><th>Kategori</th><th>Fitur</th><th>Terjual</th>
- </thead>
- <tbody>
- <?php foreach ($presult as $produk) :
- $childID = $produk['categories'];
- $catSql = "SELECT * FROM categories WHERE id = '$childID'";
- $result = $db->query($catSql);
- $child = mysqli_fetch_assoc($result);
- $parentID = $child['parent'];
- $pSql = "SELECT * FROM categories WHERE id = '$parentID'";
- $presult = $db->query($pSql);
- $parent = mysqli_fetch_assoc($presult);
- $category = $parent['category'].'->'.$child['category'];
- ?>
- <tr>
- <td>
- <a href="products.php?edit=<?=$produk['id'];?>" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span></a>
- <a href="products.php?delete=<?=$produk['id'];?>" class="btn btn-xs btn-default" onclick="return confirm('Yakin akan menghapus data ini ?');">
- <span class="glyphicon glyphicon-remove"></span>
- </a>
- </td>
- <td><?=$produk['title'];?></td>
- <td><?=money($produk['price']);?></td>
- <td><?=$category;?></td>
- <td>
- <a href="products.php?featured=<?=(($produk['featured'] == 0)?'1':'0');?>&id=<?=$produk['id'];?>" class="btn btn-xs btn-default"
- title="<?=(($produk['featured'] == 0)?'Klik Untuk Menampilkan Produk':'Klik Untuk Tidak Menampilkan Produk Ini');?>">
- <span class="glyphicon glyphicon-<?=(($produk['featured'] == 1)?'minus':'plus');?>"></span>
- </a>
- <?=(($produk['featured'] == 1)?'Ditampilkan':'Tidak Ditampilkan');?>
- </td>
- <td>0</td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- <?php } include 'includes/footer.php'; ?>
- <script type="text/javascript">
- $('document').ready(function(){
- get_child_options('<?=$category;?>');
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement