HOME


Mini Shell 1.0
DIR: /home/islapiiu/sites/pramudi/class/
Upload File :
Current File : /home/islapiiu/sites/pramudi/class/ProductCategory.php
<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of ProductCategory
 *
 * @author Nipuni
 */
class ProductCategory {
    public $id;
    public $name;
    public $image_name;
    public $queue;
    public function __construct($id) {
        if ($id) {
            $query = "SELECT `id`,`name`,`image_name`,`queue` FROM `product_category` WHERE `id`=" . $id;
            $db = new Database();
            $result = mysql_fetch_array($db->readQuery($query));
            $this->id = $result['id'];
            $this->name = $result['name'];
            $this->image_name = $result['image_name'];
            $this->queue = $result['queue'];
            return $this;
        }
    }
    public function create() {
        $query = "INSERT INTO `product_category` (`name`,`image_name`,`queue`) VALUES  ('"
                . $this->name . "','"
                . $this->image_name . "', '"
                . $this->queue . "')";
        $db = new Database();
        $result = $db->readQuery($query);
        if ($result) {
            $last_id = mysql_insert_id();
            return $this->__construct($last_id);
        } else {
            return FALSE;
        }
    }
    public function all() {
        $query = "SELECT * FROM `product_category`";
        $db = new Database();
        $result = $db->readQuery($query);
        $array_res = array();
        while ($row = mysql_fetch_array($result)) {
            array_push($array_res, $row);
        }
        return $array_res;
    }
     public function getProductsByProductCategory($product_category) {
        $query = "SELECT * FROM `product` WHERE `product_category` = $product_category ORDER BY queue ASC";
        $db = new Database();
        $result = $db->readQuery($query);
        $array_res = array();
        while ($row = mysql_fetch_array($result)) {
            array_push($array_res, $row);
        }
        return $array_res;
    }
    public function update() {
        $query = "UPDATE  `product_category` SET "
                . "`name` ='" . $this->name . "', "
                . "`image_name` ='" . $this->image_name . "', "
                . "`queue` ='" . $this->queue . "' "
                . "WHERE `id` = '" . $this->id . "'";
        $db = new Database();
        $result = $db->readQuery($query);
        if ($result) {
            return $this->__construct($this->id);
        } else {
            return FALSE;
        }
    }
    public function delete() {
       
       
        $this->deletePhotos();
//        unlink(Helper::getSitePath() . "upload/product_category/" . $this->image_name);
        $query = 'DELETE FROM `product_category` WHERE id="' . $this->id . '"';
        $db = new Database();
        return $db->readQuery($query);
    }
    public function deletePhotos() {
        
        $PRODUCT = new Product(NULL);
        $allPhotos = $PRODUCT->getProductById($this->id);
        foreach ($allPhotos as $photo) {
            $IMG = $PRODUCT->image_name = $photo["image_name"];
            unlink(Helper::getSitePath() . "upload/product_category/product/" . $IMG);
          
            $PRODUCT->id = $photo["id"];
            $PRODUCT->delete();
        }
    }
}