Add product edit page

This commit is contained in:
grandeljay 2022-02-27 11:37:34 +01:00
parent ce83385596
commit ac056cd511
9 changed files with 251 additions and 68 deletions

View file

@ -24,11 +24,11 @@ switch ($_SERVER['REQUEST_METHOD']) {
WHERE `id` = ' . $_GET['wish_id'] . ';') WHERE `id` = ' . $_GET['wish_id'] . ';')
->fetch(); ->fetch();
$wish = new wish($columns); $wish = new wish($columns, true);
$response = array( $response = array(
'info' => $wish, 'info' => $wish,
'html' => $wish->getCard($_GET['wishlist_user'], true) 'html' => $wish->getCard($_GET['wishlist_user'])
); );
} elseif (isset($_GET['wish_url'])) { } elseif (isset($_GET['wish_url'])) {
$cache = new EmbedCache($_GET['wish_url']); $cache = new EmbedCache($_GET['wish_url']);
@ -42,10 +42,18 @@ switch ($_SERVER['REQUEST_METHOD']) {
break; break;
case 'POST': case 'POST':
if (isset($_POST['wishlist_id'], $_POST['wish_url'])) { if (isset($_POST['wishlist_id'])) {
/** /**
* Insert New Wish * Insert New Wish
*/ */
if (
empty($_POST['wish_title'])
&& empty($_POST['wish_description'])
&& empty($_POST['wish_url'])
) {
break;
}
$database->query('INSERT INTO `wishes` $database->query('INSERT INTO `wishes`
( (
`wishlist`, `wishlist`,
@ -54,9 +62,9 @@ switch ($_SERVER['REQUEST_METHOD']) {
`url` `url`
) VALUES (' ) VALUES ('
. $_POST['wishlist_id'] . ', . $_POST['wishlist_id'] . ',
"' . $_POST['wish_title'] . '", "' . trim($_POST['wish_title']) . '",
"' . $_POST['wish_description'] . '", "' . trim($_POST['wish_description']) . '",
"' . $_POST['wish_url'] . '" "' . trim($_POST['wish_url']) . '"
) )
;'); ;');

View file

@ -45,17 +45,20 @@ switch ($_SERVER['REQUEST_METHOD']) {
$user = isset($_GET['userid']) ? new User($_GET['userid']) : new User(); $user = isset($_GET['userid']) ? new User($_GET['userid']) : new User();
$wishlists = $user->getWishlists(); $wishlists = $user->getWishlists();
$wishlists = array_map(function ($dataWishlist) { $wishlists = array_map(
function ($dataWishlist) {
$data = $dataWishlist; $data = $dataWishlist;
// $newFormat['name'] = $wishlist['name']; // $newFormat['name'] = $wishlist['name'];
$data['value'] = $dataWishlist['id']; $data['value'] = $dataWishlist['id'];
$data['text'] = $dataWishlist['name']; $data['text'] = $dataWishlist['name'];
$wishlist = new Wishlist(intval($dataWishlist['id'])); $wishlist = new Wishlist($dataWishlist['id']);
$data['cards'] = $wishlist->getCards(); $data['cards'] = $wishlist->getCards();
return $data; return $data;
}, $wishlists); },
$wishlists
);
$response['results'] = $wishlists; $response['results'] = $wishlists;
$response['success'] = true; $response['success'] = true;

View file

@ -1,5 +1,6 @@
:root { :root {
--lineHeight: 1.4285; --lineHeight: 1.4285;
--wishPreviewHeight: 25vh;
} }
img { img {
@ -33,16 +34,16 @@ img {
/** /**
* Card * Card
*/ */
.ui.card {
overflow: hidden;
}
.ui.fluid.card.stretch { .ui.fluid.card.stretch {
height: 100%; height: 100%;
} }
/** Overlay */ /** Overlay */
@media (hover: hover) { @media (hover: hover) {
.ui.card:hover {
z-index: 102;
}
.ui.card .overlay { .ui.card .overlay {
transition: 0.4s ease opacity; transition: 0.4s ease opacity;
@ -64,8 +65,9 @@ img {
/** Image */ /** Image */
.ui.card > .image > img.preview{ .ui.card > .image > img.preview{
height: 25vh; height: var(--wishPreviewHeight);
object-fit: cover; object-fit: cover;
object-position: 50%;
background-color: #fff; background-color: #fff;
} }
@ -197,6 +199,19 @@ img {
flex: 1 1 auto; flex: 1 1 auto;
} }
.ui.card .ui.menu {
flex: 1 1 auto;
margin: 0;
box-shadow: none;
}
.ui.card .ui.menu .ui.button {
margin: 0;
text-align: center;
}
/** /**
* Label * Label
*/ */

6
src/assets/css/wish.css Normal file
View file

@ -0,0 +1,6 @@
.ui.fluid.image {
height: var(--wishPreviewHeight);
object-fit: cover;
object-position: 50%;
}

View file

@ -24,6 +24,8 @@ $(function () {
element.dropdown('set selected', wishlists[0].value); element.dropdown('set selected', wishlists[0].value);
} }
} }
$('.ui.dropdown').dropdown();
} }
}); });
} }
@ -146,6 +148,8 @@ $(function () {
.finally(function() { .finally(function() {
card.attr('data-cache', 'true'); card.attr('data-cache', 'true');
card.removeClass('loading'); card.removeClass('loading');
$('.ui.dropdown').dropdown();
}); });
refresh.removeClass('working'); refresh.removeClass('working');
@ -234,9 +238,9 @@ $(function () {
/** /**
* Delete Wish * Delete Wish
*/ */
$(document).on('click', '.ui.button.delete', function () { $(document).on('click', '.menu.options .item.delete', function () {
var button = $(this); var buttonDelete = $(this);
var card = button.closest('.ui.card'); var card = buttonDelete.closest('.ui.card');
var column = card.closest('.column'); var column = card.closest('.column');
var modalDefault = $('.ui.modal.default'); var modalDefault = $('.ui.modal.default');
@ -260,7 +264,7 @@ $(function () {
/** /**
* Delete wish * Delete wish
*/ */
button.api({ buttonDelete.api({
action: 'delete wish', action: 'delete wish',
method: 'DELETE', method: 'DELETE',
data: { data: {

View file

@ -35,7 +35,7 @@ class EmbedCache
{ {
} }
public function get(bool $generateCache = false): mixed public function get(bool $generateCache = false): \stdClass
{ {
$info = null; $info = null;
$maxAge = 2592000; // 30 days $maxAge = 2592000; // 30 days

View file

@ -10,6 +10,8 @@ namespace wishthis;
class Wish class Wish
{ {
private EmbedCache $cache;
public int $id; public int $id;
public int $wishlist; public int $wishlist;
public ?string $title; public ?string $title;
@ -17,13 +19,15 @@ class Wish
public ?string $url; public ?string $url;
public ?string $status; public ?string $status;
public function __construct(int|array $wish) public \stdClass $info;
public function __construct(int|array $wish, bool $generateCache = false)
{ {
global $database; global $database;
$columns = array(); $columns = array();
if (is_int($wish)) { if (is_numeric($wish)) {
$wish = $database $wish = $database
->query('SELECT * ->query('SELECT *
FROM `wishes` FROM `wishes`
@ -40,9 +44,26 @@ class Wish
$this->$key = $value; $this->$key = $value;
} }
} }
$this->info = new \stdClass();
if ($this->url) {
$this->cache = new EmbedCache($this->url);
$this->info = $this->cache->get($generateCache);
} }
public function getCard(int $ofUser, bool $generateCache = false): string if (empty($this->info->image)) {
$this->info->image = '/src/assets/img/no-image.svg';
}
foreach ($columns as $key => $value) {
if (empty($value) && isset($this->info->$key)) {
$this->$key = $this->info->$key;
}
}
}
public function getCard(int $ofUser): string
{ {
ob_start(); ob_start();
@ -52,40 +73,29 @@ class Wish
$userIsCurrent = isset($_SESSION['user']['id']) && intval($_SESSION['user']['id']) === $ofUser; $userIsCurrent = isset($_SESSION['user']['id']) && intval($_SESSION['user']['id']) === $ofUser;
if ($this->url) { if ($this->url) {
$cache = new EmbedCache($this->url); $exists = $this->cache->exists() || !$this->url ? 'true' : 'false';
$info = $cache->get($generateCache);
$exists = $cache->exists() || !$this->url ? 'true' : 'false';
} else { } else {
$exists = 'true'; $exists = 'true';
} }
$title = $this->title ?? $info->title ?? null;
$description = $this->description ?? $info->description ?? null;
$url = $this->url ?? $info->url ?? null;
$image = $info->image ? $info->image : '/src/assets/img/no-image.svg';
$favicon = $info->favicon ?? null;
$providerName = $info->providerName ?? null;
$keywords = $info->keywords ?? null;
?> ?>
<div class="ui fluid card stretch" data-id="<?= $this->id ?>" data-cache="<?= $exists ?>"> <div class="ui fluid card stretch" data-id="<?= $this->id ?>" data-cache="<?= $exists ?>">
<div class="overlay"></div> <div class="overlay"></div>
<div class="image"> <div class="image">
<?php if ($image) { ?> <?php if (isset($this->info->image)) { ?>
<img class="preview" src="<?= $image ?>" loading="lazy" /> <img class="preview" src="<?= $this->info->image ?>" loading="lazy" />
<?php } ?> <?php } ?>
<?php if ($favicon) { ?> <?php if (isset($this->info->favicon)) { ?>
<img class="favicon" src="<?= $favicon ?>" loading="lazy" /> <img class="favicon" src="<?= $this->info->favicon ?>" loading="lazy" />
<?php } ?> <?php } ?>
<?php if ($providerName) { ?> <?php if (isset($this->info->providerName)) { ?>
<span class="provider"><?= $providerName ?></span> <span class="provider"><?= $this->info->providerName ?></span>
<?php } ?> <?php } ?>
<?php if ($userIsCurrent && $url) { ?> <?php if ($userIsCurrent && isset($this->url)) { ?>
<button class="ui icon button refresh"> <button class="ui icon button refresh">
<i class="refresh icon"></i> <i class="refresh icon"></i>
</button> </button>
@ -93,25 +103,25 @@ class Wish
</div> </div>
<div class="content"> <div class="content">
<?php if ($title) { ?> <?php if ($this->title) { ?>
<div class="header"> <div class="header">
<?php if ($url) { ?> <?php if ($this->url) { ?>
<a href="<?= $url ?>" target="_blank"><?= $title ?></a> <a href="<?= $this->url ?>" target="_blank"><?= $this->title ?></a>
<?php } else { ?> <?php } else { ?>
<?= $title ?> <?= $this->title ?>
<?php } ?> <?php } ?>
</div> </div>
<?php } ?> <?php } ?>
<?php if ($keywords) { ?> <?php if (isset($this->info->keywords)) { ?>
<div class="meta"> <div class="meta">
<?= implode(', ', $keywords) ?> <?= implode(', ', $this->info->keywords) ?>
</div> </div>
<?php } ?> <?php } ?>
<?php if ($description) { ?> <?php if ($this->description) { ?>
<div class="description"> <div class="description">
<?= $description ?> <?= $this->description ?>
</div> </div>
<div class="description-fade"></div> <div class="description-fade"></div>
<?php } ?> <?php } ?>
@ -119,25 +129,40 @@ class Wish
<div class="extra content buttons"> <div class="extra content buttons">
<?php if (!$userIsCurrent) { ?> <?php if (!$userIsCurrent) { ?>
<a class="ui small primary labeled icon button commit"> <a class="ui primary labeled icon button commit">
<i class="shopping cart icon"></i> <i class="shopping cart icon"></i>
Commit Commit
</a> </a>
<?php } ?> <?php } ?>
<?php if ($url) { ?> <?php if ($this->url) { ?>
<a class="ui small labeled icon button" href="<?= $url ?>" target="_blank"> <a class="ui labeled icon button" href="<?= $this->url ?>" target="_blank">
<i class="external icon"></i> <i class="external icon"></i>
Visit Visit
</a> </a>
<?php } ?> <?php } ?>
<?php if ($userIsCurrent) { ?> <?php if ($userIsCurrent) { ?>
<a class="ui small labeled red icon button delete"> <div class="ui menu">
<div class="ui fluid floating dropdown labeled icon button">
<i class="cog icon"></i>
Options
<div class="bottom menu options">
<a class="item" href="/?page=wish&id=<?= $this->id ?>">
<i class="pen icon"></i>
Edit
</a>
<a class="item delete">
<i class="trash icon"></i> <i class="trash icon"></i>
Delete Delete
</a> </a>
</div>
</div>
</div>
<?php } ?> <?php } ?>
</div> </div>
</div> </div>
<?php <?php

View file

@ -24,10 +24,9 @@ class Wishlist
$column; $column;
if (is_int($id_or_hash)) { if (is_numeric($id_or_hash)) {
$column = 'id'; $column = 'id';
} } elseif (is_string($id_or_hash)) {
if (is_string($id_or_hash)) {
$column = 'hash'; $column = 'hash';
$id_or_hash = '"' . $id_or_hash . '"'; $id_or_hash = '"' . $id_or_hash . '"';
} }
@ -61,7 +60,7 @@ class Wishlist
->fetchAll(); ->fetchAll();
foreach ($this->wishes as &$wish) { foreach ($this->wishes as &$wish) {
$wish = new Wish($wish); $wish = new Wish($wish, false);
} }
} }
@ -89,7 +88,7 @@ class Wishlist
<div class="ui three column doubling stackable grid"> <div class="ui three column doubling stackable grid">
<?php foreach ($wishes as $wish) { ?> <?php foreach ($wishes as $wish) { ?>
<div class="column"> <div class="column">
<?= $wish->getCard($this->data['user'], false) ?> <?= $wish->getCard($this->data['user']) ?>
</div> </div>
<?php } ?> <?php } ?>
</div> </div>

123
src/pages/wish.php Normal file
View file

@ -0,0 +1,123 @@
<?php
/**
* Template for viewing a wish.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\{Page, Wish};
$messages = array();
if ('POST' === $_SERVER['REQUEST_METHOD'] && count($_POST) >= 0) {
$database
->query('UPDATE `wishes`
SET `title` = "' . trim($_POST['wish_title']) . '",
`description` = "' . trim($_POST['wish_description']) . '",
`url` = "' . trim($_POST['wish_url']) . '"
WHERE `id` = ' . trim($_POST['wish_id']) . ';');
$messages[] = Page::success('Wish successfully updated.', 'Success');
}
$wish = new Wish($_GET['id'], false);
/*
if (!$wish->exists()) {
http_response_code(404);
?>
<h1>Not found</h1>
<p>The requested Wish was not found.</p>
<?php
die();
}
*/
$page = new page(__FILE__, $wish->title);
$page->header();
$page->navigation();
$referer = '/?page=wishlists&wishlist=' . $wish->wishlist;
?>
<main>
<div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1>
<?= $page->messages($messages) ?>
<div class="ui segment">
<form class="ui form" method="POST">
<input type="hidden" name="wish_id" value="<?= $_GET['id'] ?>">
<div class="ui two column grid">
<div class="stackable row">
<div class="column">
<?php if ($wish->info->image) { ?>
<img class="ui fluid rounded image" src="<?= $wish->info->image ?>" />
<?php } ?>
</div>
<div class="column">
<div class="ui header">Options</div>
<a class="ui labeled icon button" href="<?= $wish->url ?>" target="_blank">
<i class="external icon"></i>
Visit
</a>
</div>
</div>
<div class="one column row">
<div class="column">
<div class="field">
<label>Title</label>
<div class="ui input">
<input type="text"
name="wish_title"
placeholder="<?= $wish->title ?>"
value="<?= $wish->title ?>"
/>
</div>
</div>
<div class="field">
<label>Description</label>
<textarea name="wish_description"
placeholder="<?= $wish->description ?>"
><?= $wish->description ?></textarea>
</div>
<div class="field">
<label>URL</label>
<div class="ui input">
<input type="url"
name="wish_url"
placeholder="<?= $wish->url ?>"
value="<?= $wish->url ?>"
/>
</div>
</div>
<input class="ui primary button" type="submit" value="Save" />
<input class="ui button" type="reset" value="Reset" />
<a class="ui secondary button" href="<?= $referer ?>">Cancel</a>
</div>
</div>
</div>
</form>
</div>
</div>
</main>
<?php
$page->footer();
?>