diff --git a/add-product.php b/add-product.php new file mode 100644 index 0000000..161c86a --- /dev/null +++ b/add-product.php @@ -0,0 +1,16 @@ + + + + + Product Add + + + + + diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..c01497b --- /dev/null +++ b/autoload.php @@ -0,0 +1,28 @@ +

Product List

- +
diff --git a/index.js b/index.js index fbca442..aec870e 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,8 @@ const loadItems = () => { const xhttp = new XMLHttpRequest(); xhttp.onload = function() { + console.log(this.responseText); + const products = JSON.parse(this.responseText); const boxes = products.map(product => @@ -15,14 +17,31 @@ const loadItems = () => { ${product.price} $
${product.attribute}

- - `) + ` + ) productsList.innerHTML = boxes.join('\n'); } - xhttp.open('GET', 'view/products.php', true); + xhttp.open('GET', 'src/View/Product', true); + xhttp.send(); +} +loadItems(); + +const deleteSelected = () => { + const checkboxes = document.querySelectorAll('input[class="delete-checkbox"]:checked'); + let values = []; + checkboxes.forEach(checkbox => values.push(checkbox.value)); + + const xhttp = new XMLHttpRequest(); + + xhttp.onload = function() { + loadItems(); + } + + xhttp.open('DELETE', 'src/View/Product', true); xhttp.send(); } -loadItems(); +const deleteButton = document.getElementById('delete-product-btn'); +deleteButton.addEventListener('click', deleteSelected); diff --git a/index.php b/index.php new file mode 100644 index 0000000..7ed317b --- /dev/null +++ b/index.php @@ -0,0 +1,4 @@ +select_db(self::DATABASE); - return $conn; - } -} - -trait Model { - public abstract static function fromRow($row) : self; - public abstract function insert($conn = NULL) : int; // should return id - private abstract static function getSelectAllQuery() : string; - - public static function selectAll($conn = NULL) : array { - if($conn === NULL) { - $conn = Database::connect(); - } - - $sql = self::getSelectAllQuery(); - - $rows = $conn->query($sql)->fetch_all(MYSQLI_ASSOC); - - $products = array(); - foreach($rows as $row) { - array_push($products, self::fromRow($row)); - } - return $products; - } -} diff --git a/model/product.php b/model/product.php deleted file mode 100644 index 798fe92..0000000 --- a/model/product.php +++ /dev/null @@ -1,263 +0,0 @@ -productId = $productId; - $this->variationId = $variationId; - $this->SKU = $SKU; - $this->name = $name; - $this->price = $price; - } - - public function getSKU() { - return $this->SKU; - } - - public function getName() { - return $this->name; - } - - public function getPrice() { - return $this->price; - } - - public function getProductId() { - return $this->productId; - } - - public function setVariationId($id) { - $this->variationId = $id; - } - - public function setProductId($id) { - $this->productId = $id; - } - - public abstract function getFormatedAttr(); - - public static function fromRow($row) : self { - if($row['size'] !== NULL) { - return DVD::fromRow($row); - } elseif($row['weight'] !== NULL) { - return Book::fromRow($row); - } elseif($row['height'] !== NULL) { - return Furniture::fromRow($row); - } else { - throw new Exception("Product without a type"); - } - } - - private static function getSelectAllQuery() : string { - return 'SELECT '.PRODUCT.'.id as product_id, COALESCE('.DVD.'.id, '.BOOK.'.id, '.FURNITURE.'.id) as variation_id, - name, sku, price, size, weight, width, height, length - FROM '.PRODUCT.' - LEFT JOIN '.DVD.' ON '.PRODUCT.'.id = '.DVD.'.product_id - LEFT JOIN '.BOOK.' ON '.PRODUCT.'.id = '.BOOK.'.product_id - LEFT JOIN '.FURNITURE.' ON '.PRODUCT.'.id = '.FURNITURE.'.product_id;'; - } - - public function insert($conn = NULL) : int { - if($conn === NULL) { - $conn = Database::connect(); - } - - $SKU = $this->getSKU(); - $name = $this->getName(); - $price = $this->getPrice(); - - $stmt = $conn->prepare("INSERT INTO ".PRODUCT." (sku, name, price) VALUES (?, ?, ?);"); - $stmt->bind_param('ssd', $SKU, $name, $price); - - if($stmt->execute() === TRUE) { - $this->setProductId($conn->insert_id); - return $conn->insert_id; - } else { - throw new Exception("Unable to insert object"); - } - } - - public function jsonSerialize() : mixed { - return [ - 'id' => $this->getProductId(), - 'sku' => $this->getSKU(), - 'name' => $this->getName(), - 'price' => $this->getPrice(), - 'attribute' => $this->getFormatedAttr() - ]; - } -} - -class DVD extends Product { - private $size; - - public function __construct($sku, $name, $price, $size, $productId = null, $variationId = null) { - parent::__construct($sku, $name, $price, $productId, $variationId); - $this->size = $size; - } - - public static function fromRow($row) : self { - return new DVD($row['sku'], $row['name'], $row['price'], $row['size'], $row['product_id'], $row['variation_id']); - } - - public function getSize() { - return $this->size; - } - - public function getFormatedAttr() { - $size = $this->getSize(); - return "Size: $size MB"; - } - - private static function getSelectAllQuery() : string { - return 'SELECT '.PRODUCT.'.*, '.DVD.'.id as variation_id, size - FROM '.PRODUCT.' LEFT JOIN '.DVD.' ON '.PRODUCT.'.id = '.DVD.'.product_id'; - } - - public function insert($conn = NULL) : int { - if($conn === NULL) { - $conn = Database::connect(); - } - - if($this->getProductId() === NULL) { - parent::insert($conn); - } - - $productId = $this->getProductId(); - $size = $this->getSize(); - - $stmt = $conn->prepare("INSERT INTO ".DVD." (product_id, size) VALUES (?, ?);"); - $stmt->bind_param('ii', $productId, $size); - - if($stmt->execute() === TRUE) { - $this->setVariationId($conn->insert_id); - return $conn->insert_id; - } else { - throw new Exception("Unable to insert object"); - } - } -} - -class Book extends Product { - private $weight; - - public function __construct($sku, $name, $price, $weight, $productId = null, $variationId = null) { - parent::__construct($sku, $name, $price, $productId, $variationId); - $this->weight = $weight; - } - - public static function fromRow($row) : self { - return new Book($row['sku'], $row['name'], $row['price'], $row['weight'], $row['product_id'], $row['variation_id']); - } - - public function getWeight() { - return $this->weight; - } - - public function getFormatedAttr() { - $weight = $this->getWeight(); - return "Weight: $weight KG"; - } - - private static function getSelectAllQuery() : string { - return 'SELECT '.PRODUCT.'.*, '.BOOK.'.id as variation_id, size - FROM '.PRODUCT.' LEFT JOIN '.BOOK.' ON '.PRODUCT.'.id = '.BOOK.'.product_id'; - } - - public function insert($conn = NULL) : int { - if($conn === NULL) { - $conn = Database::connect(); - } - - if($this->getProductId() === NULL) { - parent::insert($conn); - } - - $productId = $this->getProductId(); - $weight = $this->getWeight(); - - $stmt = $conn->prepare("INSERT INTO ".BOOK." (product_id, weight) VALUES (?, ?);"); - $stmt->bind_param('id', $productId, $weight); - - if($stmt->execute() === TRUE) { - $this->setVariationId($conn->insert_id); - return $conn->insert_id; - } else { - throw new Exception("Unable to insert object"); - } - } -} - -class Furniture extends Product { - private $height; - private $width; - private $length; - - public function __construct($SKU, $name, $price, $height, $width, $length, $productId = NULL, $variationId = NULL) { - parent::__construct($SKU, $name, $price, $productId, $variationId); - $this->height = $height; - $this->width = $width; - $this->length = $length; - } - - public static function fromRow($row) : self { - return new Furniture($row['sku'], $row['name'], $row['price'], $row['height'], - $row['width'], $row['length'], $row['product_id'], $row['variation_id']); - } - - public function getHeight() { - return $this->height; - } - - public function getWidth() { - return $this->width; - } - - public function getLength() { - return $this->length; - } - - public function getFormatedAttr() { - $height = $this->getHeight(); - $width = $this->getWidth(); - $length = $this->getLength(); - return 'Dimension: '.$height.'x'.$width.'x'.$length; - } - - private static function getSelectAllQuery() : string { - return 'SELECT '.PRODUCT.'.*, '.FURNITURE.'.id as variation_id, size - FROM '.PRODUCT.' LEFT JOIN '.FURNITURE.' ON '.PRODUCT.'.id = '.FURNITURE.'.product_id'; - } - - public function insert($conn = NULL) : int { - if($conn === NULL) { - $conn = Database::connect(); - } - - if($this->getProductId() === NULL) { - parent::insert($conn); - } - - $productId = $this->getProductId(); - $height = $this->getHeight(); - $width = $this->getWidth(); - $length = $this->getLength(); - - $stmt = $conn->prepare("INSERT INTO ".FURNITURE." (product_id, height, width, length) VALUES (?, ?, ?, ?);"); - $stmt->bind_param('iddd', $productId, $height, $width, $length); - - if($stmt->execute() === TRUE) { - $this->setVariationId($conn->insert_id); - return $conn->insert_id; - } else { - throw new Exception("Unable to insert object"); - } - } -} diff --git a/src/Model/Book.php b/src/Model/Book.php new file mode 100644 index 0000000..16ecef0 --- /dev/null +++ b/src/Model/Book.php @@ -0,0 +1,72 @@ +weight = $weight; + } + + public static function fromRow($row) : self + { + return new Book( + $row['sku'], + $row['name'], + $row['price'], + $row['weight'], + $row['product_id'], + $row['variation_id'] + ); + } + + public function getWeight() + { + return $this->weight; + } + + public function getFormatedAttr() + { + $weight = $this->getWeight(); + return "Weight: $weight KG"; + } + + private static function getSelectAllQuery() : string + { + return 'SELECT '.PRODUCT.'.*, '.BOOK.'.id as variation_id, size + FROM '.PRODUCT.' LEFT JOIN '.BOOK.' ON '.PRODUCT.'.id = '.BOOK.'.product_id'; + } + + public function insert($conn = null) : int + { + if ($conn === null) { + $conn = Database::connect(); + } + + if ($this->getProductId() === null) { + parent::insert($conn); + } + + $productId = $this->getProductId(); + $weight = $this->getWeight(); + + $stmt = $conn->prepare("INSERT INTO ".BOOK." (product_id, weight) VALUES (?, ?);"); + $stmt->bind_param('id', $productId, $weight); + + if ($stmt->execute() === true) { + $this->setVariationId($conn->insert_id); + return $conn->insert_id; + } else { + throw new Exception("Unable to insert object"); + } + } +} diff --git a/src/Model/DVD.php b/src/Model/DVD.php new file mode 100644 index 0000000..5859c42 --- /dev/null +++ b/src/Model/DVD.php @@ -0,0 +1,72 @@ +size = $size; + } + + public static function fromRow($row) : self + { + return new DVD( + $row['sku'], + $row['name'], + $row['price'], + $row['size'], + $row['product_id'], + $row['variation_id'] + ); + } + + public function getSize() + { + return $this->size; + } + + public function getFormatedAttr() + { + $size = $this->getSize(); + return "Size: $size MB"; + } + + private static function getSelectAllQuery() : string + { + return 'SELECT '.PRODUCT.'.*, '.DVD.'.id as variation_id, size + FROM '.PRODUCT.' LEFT JOIN '.DVD.' ON '.PRODUCT.'.id = '.DVD.'.product_id'; + } + + public function insert($conn = null) : int + { + if ($conn === null) { + $conn = Database::connect(); + } + + if ($this->getProductId() === null) { + parent::insert($conn); + } + + $productId = $this->getProductId(); + $size = $this->getSize(); + + $stmt = $conn->prepare("INSERT INTO ".DVD." (product_id, size) VALUES (?, ?);"); + $stmt->bind_param('ii', $productId, $size); + + if ($stmt->execute() === true) { + $this->setVariationId($conn->insert_id); + return $conn->insert_id; + } else { + throw new Exception("Unable to insert object"); + } + } +} diff --git a/src/Model/Database.php b/src/Model/Database.php new file mode 100644 index 0000000..e6cf64f --- /dev/null +++ b/src/Model/Database.php @@ -0,0 +1,22 @@ +select_db(self::DATABASE); + return $conn; + } +} diff --git a/src/Model/Furniture.php b/src/Model/Furniture.php new file mode 100644 index 0000000..10b73ba --- /dev/null +++ b/src/Model/Furniture.php @@ -0,0 +1,98 @@ +height = $height; + $this->width = $width; + $this->length = $length; + } + + public static function fromRow($row) : self + { + return new Furniture( + $row['sku'], + $row['name'], + $row['price'], + $row['height'], + $row['width'], + $row['length'], + $row['product_id'], + $row['variation_id'] + ); + } + + public function getHeight() + { + return $this->height; + } + + public function getWidth() + { + return $this->width; + } + + public function getLength() + { + return $this->length; + } + + public function getFormatedAttr() + { + $height = $this->getHeight(); + $width = $this->getWidth(); + $length = $this->getLength(); + return 'Dimension: '.$height.'x'.$width.'x'.$length; + } + + private static function getSelectAllQuery() : string + { + return 'SELECT '.PRODUCT.'.*, '.FURNITURE.'.id as variation_id, size + FROM '.PRODUCT.' + LEFT JOIN '.FURNITURE.' ON '.PRODUCT.'.id = '.FURNITURE.'.product_id'; + } + + public function insert($conn = null) : int + { + if ($conn === null) { + $conn = Database::connect(); + } + + if ($this->getProductId() === null) { + parent::insert($conn); + } + + $productId = $this->getProductId(); + $height = $this->getHeight(); + $width = $this->getWidth(); + $length = $this->getLength(); + + $stmt = $conn->prepare( + "INSERT INTO ".FURNITURE." (product_id, height, width, length) + VALUES (?, ?, ?, ?);" + ); + $stmt->bind_param('iddd', $productId, $height, $width, $length); + + if ($stmt->execute() === true) { + $this->setVariationId($conn->insert_id); + return $conn->insert_id; + } else { + throw new Exception("Unable to insert object"); + } + } +} diff --git a/src/Model/Model.php b/src/Model/Model.php new file mode 100644 index 0000000..3002fce --- /dev/null +++ b/src/Model/Model.php @@ -0,0 +1,26 @@ +query($sql)->fetch_all(MYSQLI_ASSOC); + + $products = array(); + foreach($rows as $row) { + array_push($products, self::fromRow($row)); + } + return $products; + } +} diff --git a/src/Model/Product.php b/src/Model/Product.php new file mode 100644 index 0000000..22d4fa9 --- /dev/null +++ b/src/Model/Product.php @@ -0,0 +1,117 @@ +productId = $productId; + $this->variationId = $variationId; + $this->SKU = $SKU; + $this->name = $name; + $this->price = $price; + } + + public function getSKU() + { + return $this->SKU; + } + + public function getName() + { + return $this->name; + } + + public function getPrice() + { + return $this->price; + } + + public function getProductId() + { + return $this->productId; + } + + public function setVariationId($id) + { + $this->variationId = $id; + } + + public function setProductId($id) + { + $this->productId = $id; + } + + abstract public function getFormatedAttr(); + + public static function fromRow($row) : self + { + if ($row['size'] !== null) { + return DVD::fromRow($row); + } elseif ($row['weight'] !== null) { + return Book::fromRow($row); + } elseif ($row['height'] !== null) { + return Furniture::fromRow($row); + } else { + throw new Exception("Product without a type"); + } + } + + private static function getSelectAllQuery() : string + { + return 'SELECT '.PRODUCT.'.id as product_id, + COALESCE('.DVD.'.id, '.BOOK.'.id, '.FURNITURE.'.id) as variation_id, + name, sku, price, size, weight, width, height, length + FROM '.PRODUCT.' + LEFT JOIN '.DVD.' ON '.PRODUCT.'.id = '.DVD.'.product_id + LEFT JOIN '.BOOK.' ON '.PRODUCT.'.id = '.BOOK.'.product_id + LEFT JOIN '.FURNITURE.' ON '.PRODUCT.'.id = '.FURNITURE.'.product_id;'; + } + + public function insert($conn = null) : int + { + if ($conn === null) { + $conn = Database::connect(); + } + + $SKU = $this->getSKU(); + $name = $this->getName(); + $price = $this->getPrice(); + + $stmt = $conn->prepare( + 'INSERT INTO '.PRODUCT.' (sku, name, price) + VALUES (?, ?, ?);' + ); + $stmt->bind_param('ssd', $SKU, $name, $price); + + if ($stmt->execute() === true) { + $this->setProductId($conn->insert_id); + return $conn->insert_id; + } else { + throw new Exception("Unable to insert object"); + } + } + + public function jsonSerialize() : mixed + { + return [ + 'id' => $this->getProductId(), + 'sku' => $this->getSKU(), + 'name' => $this->getName(), + 'price' => $this->getPrice(), + 'attribute' => $this->getFormatedAttr() + ]; + } +} diff --git a/src/View/Product.php b/src/View/Product.php new file mode 100644 index 0000000..de0c3f2 --- /dev/null +++ b/src/View/Product.php @@ -0,0 +1,4 @@ +