PaperBag/www/api/v1/products.php

154 lines
4.3 KiB
PHP

<?php
function numberLowest( ...$nums ){
$lowest = null;
foreach ($nums as $num){
if($num < $lowest || $lowest == null){
$lowest = $num;
}
}
return $lowest;
}
function reorderArray($array): array {
$arrays = array();
foreach ($array as $k1 => $v1){
if($k1 > 2){ break; }
$newArr = array();
$newArr[] = $v1;
foreach ($array as $v2){
if(!in_array($v2, $newArr)){
$newArr[] = $v2;
}
}
$arrays[] = $newArr;
if(count($array) > 2){
$newArr = array();
$newArr[] = $v1;
foreach (array_reverse($array) as $v2){
if(!in_array($v2, $newArr)){
$newArr[] = $v2;
}
}
$arrays[] = $newArr;
}
}
return $arrays;
}
class Product {
static function addProduct(string $name, string $productGroup){
}
static function search(string $search){
global $db;
$results = array();
$searchWords = explode(' ', $search);
$searchOrg = $search;
$search = str_replace(' ', '%', strtolower($search));
$productSql = "SELECT * FROM product WHERE name LIKE '%$search%' ORDER BY IF(name LIKE '$search%', 0, 1)";
$productSqlRes = $db->query($productSql);
while($row = $productSqlRes->fetch_assoc()){
$resultArr = array();
$resultArr['name'] = $row['name'];
$results[] = $resultArr;
if($productSqlRes->num_rows == 1){
$productId = $row['product_id'];
}
}
$whereClauseOr = array();
$whereClauseOr[] = "p.name LIKE '%$search%'";
foreach (reorderArray($searchWords) as $arr){
$whereClauseOr[] = "pv.name LIKE '%".implode('%', $arr)."%'";
}
$variantSql = "
SELECT
pv.*
FROM
product_variant pv
INNER JOIN product p on pv.product_id = p.product_id
WHERE
".implode(" OR ", $whereClauseOr)."
ORDER BY
IF(pv.name LIKE '$searchOrg', 0, 1), #exact search result first
IF(pv.name LIKE '$search%', 0, 1)
;";
// echo $variantSql;
// return array();
$variantSqlRes = $db->query($variantSql);
while ($row = $variantSqlRes->fetch_assoc()){
$product = new Product($row['product_id'], $row['name'], $row['variant_id']);
$results[] = $product->toArray(true);
}
return $results;
}
private int $product_id;
private int $variant_id;
private string $product_name;
public ?float $price = null;
public ?DateTime $price_updated = null;
public ?int $price_age = null;
public function __construct(int $product_id, string $product_name, int $variant_id = 0){
$this->product_id = $product_id;
$this->product_name = $product_name;
$this->variant_id = $variant_id;
}
public function getPrice(): int|false {
global $db;
if(!empty($this->price)){ return $this->price; }
// TO DO: Support for store-based-result
$getPriceSql = $db->query("SELECT product_id, date, price, product_variant FROM product_price WHERE product_id = {$this->product_id} AND product_variant = {$this->variant_id} ORDER BY date DESC LIMIT 1");
if($getPriceSql->num_rows == 1){
$row = $getPriceSql->fetch_assoc();
$this->price = $row['price'];
$this->price_updated = DateTime::createFromFormat('Y-m-d', $row['date']);
$this->price_age = $this->price_updated->diff(new DateTime())->days;
return $this->price;
}
return false;
}
public function toArray($fetchMissingData = false): array {
if($fetchMissingData){
$this->getPrice();
}
$productArray = array();
$productArray['name'] = $this->product_name;
if($this->price){
$productArray['price']['price'] = $this->price;
$productArray['price']['updated'] = $this->price_updated->format('Y-m-d');
$productArray['price']['age'] = $this->price_age;
$productArray['price']['store'] = "store_id";
}
return $productArray;
}
}
class ProductGroup {
private string $name;
}