save
This commit is contained in:
parent
e435d32588
commit
662394d7c3
|
@ -2,22 +2,24 @@
|
|||
namespace app\controllers;
|
||||
|
||||
use app\models\addresses;
|
||||
use app\models\emails;
|
||||
use app\models\magic_links;
|
||||
use app\models\users;
|
||||
use app\models\user_settings;
|
||||
|
||||
class account
|
||||
{
|
||||
public static function index($defaults): void
|
||||
{
|
||||
$user = users::getById($_SESSION['user_id']);
|
||||
$addresses = addresses::getByUserId($_SESSION['user_id']);
|
||||
$user = users::getById($_SESSION['user_id']);
|
||||
$addresses = addresses::getByUserId($_SESSION['user_id']);
|
||||
$user_settings = user_settings::getByUserId($_SESSION['user_id']);
|
||||
|
||||
echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
|
||||
'child_template' => 'account/index.twig',
|
||||
'page_title' => 'Manage Account - ' . $_ENV['APP_NAME'],
|
||||
'user' => $user,
|
||||
'addresses' => $addresses,
|
||||
'user_settings' => $user_settings,
|
||||
'breadcrumbs' => [
|
||||
[
|
||||
'url' => null,
|
||||
|
@ -273,6 +275,7 @@ class account
|
|||
echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
|
||||
'child_template' => 'account/shipping.twig',
|
||||
'page_title' => $_ENV['APP_NAME'] . ' Shipping',
|
||||
'user' => $user,
|
||||
'addresses' => $addresses,
|
||||
'breadcrumbs' => [
|
||||
[
|
||||
|
@ -358,7 +361,6 @@ class account
|
|||
$verified,
|
||||
$dark_theme
|
||||
);
|
||||
emails::updateUserIdByEmail($email, $user_id);
|
||||
$_SESSION['user_id'] = $user_id;
|
||||
if (! $verified) {
|
||||
header("Location: /magic-link?email=$email&signup=1");
|
||||
|
|
56
src/models/user_settings.php
Normal file
56
src/models/user_settings.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
namespace app\models;
|
||||
|
||||
use app\app;
|
||||
|
||||
class user_settings
|
||||
{
|
||||
public static function init()
|
||||
{
|
||||
app::$db->exec("CREATE TABLE IF NOT EXISTS user_settings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
opt_in_promotional BOOLEAN NOT NULL,
|
||||
opt_in_subscription BOOLEAN DEFAULT TRUE,
|
||||
opt_in_order BOOLEAN DEFAULT TRUE,
|
||||
dark_theme BOOLEAN NOT NULL,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);");
|
||||
}
|
||||
|
||||
public static function add($user_id, $opt_in_promotional, $dark_theme)
|
||||
{
|
||||
$query = "INSERT INTO user_settings (user_id, opt_in_promotional, opt_in_subscription, opt_in_order, dark_theme)
|
||||
VALUES (:user_id, :opt_in_promotional, :opt_in_subscription, :opt_in_orders, :dark_theme)";
|
||||
$stmt = app::$db->prepare($query);
|
||||
$stmt->bindParam(':user_id', $user_id);
|
||||
$stmt->bindParam(':opt_in_promotional', $opt_in_promotional);
|
||||
$stmt->bindParam(':dark_theme', $dark_theme);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public static function update($user_id, $columnValues)
|
||||
{
|
||||
$setClause = [];
|
||||
foreach ($columnValues as $column => $value) {
|
||||
$setClause[] = "$column = :$column";
|
||||
}
|
||||
$setClause = implode(', ', $setClause);
|
||||
$query = "UPDATE user_settings SET $setClause WHERE user_id = :user_id";
|
||||
$stmt = app::$db->prepare($query);
|
||||
foreach ($columnValues as $column => &$value) {
|
||||
$stmt->bindParam(":$column", $value);
|
||||
}
|
||||
$stmt->bindParam(':user_id', $user_id);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public static function getByUserId($user_id)
|
||||
{
|
||||
$query = "SELECT * FROM user_settings WHERE user_id = :user_id";
|
||||
$stmt = app::$db->prepare($query);
|
||||
$stmt->bindParam(':user_id', $user_id);
|
||||
$stmt->execute();
|
||||
return $stmt->fetch(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,8 @@ namespace app\models;
|
|||
|
||||
use app\app;
|
||||
use app\models\addresses;
|
||||
use app\models\emails;
|
||||
use app\models\user_settings;
|
||||
use swentel\nostr\Key\Key;
|
||||
|
||||
class users
|
||||
|
@ -14,13 +16,9 @@ class users
|
|||
email TEXT UNIQUE,
|
||||
shipping_address_id INTEGER,
|
||||
billing_address_id INTEGER,
|
||||
opt_in_promotional BOOLEAN NOT NULL,
|
||||
opt_in_subscription BOOLEAN DEFAULT TRUE,
|
||||
opt_in_orders BOOLEAN DEFAULT TRUE,
|
||||
lifetime_spend INTEGER DEFAULT 0,
|
||||
lifetime_orders INTEGER DEFAULT 0,
|
||||
verified BOOLEAN NOT NULL,
|
||||
dark_theme BOOLEAN NOT NULL,
|
||||
nsec TEXT,
|
||||
npub TEXT NOT NULL,
|
||||
attached_lightning_address TEXT,
|
||||
|
@ -89,18 +87,14 @@ class users
|
|||
email,
|
||||
shipping_address_id,
|
||||
billing_address_id,
|
||||
opt_in_promotional,
|
||||
verified,
|
||||
dark_theme,
|
||||
nsec,
|
||||
npub
|
||||
) VALUES (
|
||||
:email,
|
||||
:shipping_address_id,
|
||||
:billing_address_id,
|
||||
:opt_in_promotional,
|
||||
:verified,
|
||||
:dark_theme,
|
||||
:nsec,
|
||||
:npub
|
||||
)";
|
||||
|
@ -108,17 +102,17 @@ class users
|
|||
$stmt->bindParam(':email', $email);
|
||||
$stmt->bindParam(':shipping_address_id', $ship_id);
|
||||
$stmt->bindParam(':billing_address_id', $bill_id);
|
||||
$stmt->bindParam(':opt_in_promotional', $opt_in_promotional);
|
||||
$stmt->bindParam(':verified', $verified);
|
||||
$stmt->bindParam(':dark_theme', $dark_theme);
|
||||
$stmt->bindParam(':nsec', $nsec);
|
||||
$stmt->bindParam(':npub', $npub);
|
||||
$stmt->execute();
|
||||
$user_id = app::$db->lastInsertId();
|
||||
user_settings::add($user_id, $opt_in_promotional, $dark_theme);
|
||||
addresses::updateUserIdById($ship_id, $user_id);
|
||||
if ($ship_id != $bill_id) {
|
||||
addresses::updateUserIdById($bill_id, $user_id);
|
||||
}
|
||||
emails::updateUserIdByEmail($email, $user_id);
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ use app\models\quote_items;
|
|||
use app\models\subscriptions;
|
||||
use app\models\transactions;
|
||||
use app\models\users;
|
||||
use app\models\user_settings;
|
||||
|
||||
// db models go brrr...
|
||||
addresses::init();
|
||||
|
@ -42,4 +43,5 @@ quote_items::init();
|
|||
quotes::init();
|
||||
subscriptions::init();
|
||||
transactions::init();
|
||||
user_settings::init();
|
||||
users::init();
|
||||
|
|
|
@ -138,7 +138,7 @@
|
|||
{% include 'lib/inputs/toggle.twig' with {
|
||||
label: 'Recieve coupons & more',
|
||||
name: 'opt_in_promotional',
|
||||
on: user.opt_in_promotional
|
||||
on: user_settings.opt_in_promotional
|
||||
} %}
|
||||
{% include 'lib/button.twig' with {
|
||||
label: 'Save',
|
||||
|
|
|
@ -28,6 +28,6 @@
|
|||
{% include 'lib/inputs/toggle.twig' with {
|
||||
name: 'dark_theme',
|
||||
label: 'Use dark theme',
|
||||
on: user.dark_theme
|
||||
on: user_settings.dark_theme
|
||||
} %}
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue