You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
7.1 KiB
PHP

<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
require_once __DIR__ . "/../required.php";
class Item {
private $itemid = null;
private $itemdata = [];
private $itemimages = [];
function __construct($itemid) {
global $binstack;
$this->itemdata = $binstack->get('items', ['itemid', 'catid', 'name', 'text1', 'qty', 'want', 'cost', 'price'], ['itemid' => $itemid]);
$this->itemid = $itemid;
$this->itemimages = $binstack->select('images', ['imagename', 'primary', 'imageid'], ['itemid' => $itemid, 'ORDER' => ['primary' => 'DESC']]);
}
function getId() {
return $this->itemid;
}
function getPrice($customer = null) {
global $database;
$binprice = $this->itemdata['price'];
if (!is_null($customer)) {
if ($database->has('customer_pricing', ['AND' => ['itemid' => $this->itemid, 'customerid' => $customer]])) {
return $database->get('customer_pricing', 'price', ['AND' => ['itemid' => $this->itemid, 'customerid' => $customer]]);
}
}
return $binprice;
}
function getName() {
return $this->itemdata['name'];
}
function getDescription() {
return $this->itemdata['text1'];
}
function getQty() {
return $this->itemdata['qty'];
}
function getWant() {
return $this->itemdata['want'];
}
function getCategoryName() {
global $binstack;
return $binstack->get('items', ['[>]categories' => 'catid'], 'catname', ['itemid' => $this->itemid]);
}
function getCategoryId() {
return $this->itemdata['catid'];
}
function getImageCount() {
return count($this->itemimages);
}
function getPrimaryImage(): ItemImage {
if (count($this->itemimages) > 0) {
foreach ($this->itemimages as $i) {
if ($i['primary'] == true) {
return new ItemImage($i['imagename'], $i['imageid'], $i['primary'] == true, $this->itemid);
}
}
$i = $this->itemimages[0];
return new ItemImage($i['imagename'], $i['imageid'], $i['primary'] == true, $this->itemid);
}
return null;
}
function getImages(): array {
$images = [];
foreach ($this->itemimages as $i) {
$images[] = new ItemImage($i['imagename'], $i['imageid'], $i['primary'] == true, $this->itemid);
}
return $images;
}
}
class ItemImage {
private $url = "";
private $primary = false;
private $imageid = 0;
private $itemid = 0;
function __construct(string $url, int $imageid, bool $primary, int $itemid) {
$this->url = $url;
$this->imageid = $imageid;
$this->primary = $primary;
$this->itemid = $itemid;
}
function getName(): string {
return $this->url;
}
function getAbsoluteUrl(): string {
global $SETTINGS;
return $SETTINGS['binstack_image.php'] . "?i=" . $this->url;
}
function isPrimary(): bool {
return $this->primary == true;
}
function getID(): int {
return $this->imageid;
}
function getItemID(): int {
return $this->itemid;
}
}
class RenderItem {
static function card(Item $item) {
$id = $item->getId();
$name = $item->getName();
$catid = $item->getCategoryId();
$catname = $item->getCategoryName();
$price = $item->getPrice();
$image = "";
if ($item->getImageCount() > 0) {
$image = '<img class="card-img-top" src="' . $item->getPrimaryImage()->getAbsoluteUrl() . '" alt="' . $name . '" />';
}
/* $html = <<<END
<div class="col-12 col-sm-6 col-md-4 col-lg-4 col-xl-3">
<div class="card mb-4">
$image
<div class="card-body d-flex flex-column justify-content-end align-items-center">
<a href="./?page=item&id=$id" class="font-weight-bold">$name</a><br />
<span>$$price</span>
</div>
</div>
</div>
END; */
$html = <<<END
<div class="card">
$image
<div class="card-body d-flex flex-column justify-content-end align-items-center">
<a href="./?page=item&id=$id" class="font-weight-bold">$name</a><br />
<span>$$price</span>
</div>
</div>
END;
return $html;
}
static function line(Item $item) {
$id = $item->getId();
$name = $item->getName();
$catid = $item->getCategoryId();
$catname = $item->getCategoryName();
$price = $item->getPrice();
$image = "";
if ($item->getImageCount() > 0) {
$image = '<img class="d-inline-block mr-2 item-line-img" src="' . $item->getPrimaryImage()->getAbsoluteUrl() . '" alt="' . $name . '" />';
}
$html = <<<END
<div class="list-group-item d-flex flex-wrap">
$image
<div>
<h4><a href="./?page=item&id=$id">$name</a></h4>
<span>$$price</span>
</div>
<div class="ml-auto">
<form action="./action.php" method="POST">
<div class="input-group">
<input type="number" name="qty" class="form-control mb-2" placeholder="Quantity" value="1" />
<div class="input-group-addon">
<button type="submit" class="btn btn-primary"><i class="fas fa-cart-plus"></i> Add</button>
</div>
</div>
<input type="hidden" name="item" value="$id" />
<input type="hidden" name="action" value="addtocart" />
</form>
</div>
</div>
END;
return $html;
}
static function cart(Item $item, int $qty) {
$id = $item->getId();
$name = $item->getName();
$catid = $item->getCategoryId();
$catname = $item->getCategoryName();
$price = $item->getPrice();
$linetotal = number_format($price * $qty, 2);
$image = "";
if ($item->getImageCount() > 0) {
$image = '<img class="d-inline-block mr-2 item-line-img" src="' . $item->getPrimaryImage()->getAbsoluteUrl() . '" alt="' . $name . '" />';
}
$html = <<<END
<div class="list-group-item d-flex flex-wrap">
$image
<div>
<h4><a href="./?page=item&id=$id">$name</a></h4>
<span>$$price</span>
</div>
<div class="ml-auto">
<form action="./action.php" method="POST">
<div class="input-group">
<input type="number" name="qty" class="form-control mb-2" placeholder="Quantity" value="$qty" />
<div class="input-group-addon">
<button type="submit" class="btn btn-primary"><i class="fas fa-sync"></i> Update</button>
</div>
</div>
<input type="hidden" name="item" value="$id" />
<input type="hidden" name="action" value="updatecart" />
</form>
<div class="text-right"><b>$$linetotal</b></div>
</div>
</div>
END;
return $html;
}
}