setting sync and settings image uploading turned back on

This commit is contained in:
Ro 2021-04-16 16:15:47 -07:00
parent b68d4c190e
commit 179f007fab
9 changed files with 144 additions and 19 deletions

View file

@ -6,24 +6,70 @@ class ImagesAPI
{
}
public static function uploadImage($request)
public static function uploadImage($request, $type = null)
{
$image = $request->getUploadedFiles();
$file = $request->getUploadedFiles();
$uploadPath = "";
$path = date("Y") . "/" . date("m");
$response = [];
switch ($type) {
case "avatar":
$image = $file["avatar_upload"];
$uploadPath = "../public/assets/images/user/" . $path;
break;
case "background":
$image = $file["background_upload"];
$uploadPath = "../public/assets/images/user/" . $path;
break;
default:
$image = $file["post_image"];
$path = date("Y") . "/" . date("m");
$uploadPath = "../public/assets/images/blog/" . $path;
break;
}
$uploadPath = "../public/assets/images/blog/" . $path;
$result = FileUploader::uploadFile($uploadPath, $image);
FileUploader::uploadFile($uploadPath, $image["post_image"]);
switch ($type) {
case "avatar":
$response = [
"message" => "Avatar Added. You look great!",
"type" => "avatarUploaded",
"url" =>
"/assets/images/user/" . $path . "/" . $image->getClientFileName(),
];
$response = [
"message" => "Image Added. Very slick",
"type" => "postImageAdded",
"url" =>
"/assets/images/blog/" .
$path .
"/" .
$image["post_image"]->getClientFileName(),
];
//update member data
Member::updateData(
"avi",
"/assets/images/user/" . $path . "/" . $image->getClientFileName()
);
break;
case "background":
$response = [
"message" => "Background plugged in. That's nice!",
"type" => "siteBackgroundUploaded",
"url" =>
"/assets/images/user/" . $path . "/" . $image->getClientFileName(),
];
//update settings file
Settings::updateGlobalData(
"background",
"/assets/images/user/" . $path . "/" . $image->getClientFileName()
);
break;
default:
$response = [
"message" => "Image Added. Very slick",
"type" => "postImageAdded",
"url" =>
"/assets/images/blog/" . $path . "/" . $image->getClientFileName(),
];
break;
}
return $response;
}

View file

@ -8,10 +8,9 @@ class SettingsAPI
{
}
public static function handleSettingsTask($request, $args)
public static function handleSettingsTask($request, $args, $body = null)
{
$task = $args["fourth"];
echo $task;
switch ($task) {
case "publish":
$view = Twig::fromRequest($request);
@ -38,6 +37,20 @@ class SettingsAPI
"type" => "TASK_NONE",
];
break;
case "add-avatar":
$result = ImagesAPI::uploadImage($request, "avatar");
break;
case "add-feature-background":
$result = ImagesAPI::uploadImage($request, "background");
break;
case "sync":
Settings::sync($body);
$result = [
"message" => "Settings Synced. You're doing great!",
"type" => "settingsUpdated",
];
break;
default:
$result = [

View file

@ -59,7 +59,7 @@ class APIControl
case "settings":
$token = $request->getHeader("fipamo-access-token");
if (Session::verifyToken($token[0])) {
$result = SettingsAPI::handleSettingsTask($request, $args);
$result = SettingsAPI::handleSettingsTask($request, $args, $body);
} else {
$result = [
"message" => "API access denied, homie",

View file

@ -36,6 +36,7 @@ class DashControl
"currentTheme" => $settings["global"]["theme"],
"themes" => $themes,
"mailOption" => $settings["email"]["active"],
"status" => Session::active(),
];
} else {
header("Location: /dashboard");

33
brain/data/Member.inc.php Normal file
View file

@ -0,0 +1,33 @@
<?php
use function _\find;
class Member
{
public function __construct()
{
}
public static function updateData(string $key, string $data)
{
$folks = (new Settings())->getFolks();
$member = Session::get("member");
$found = find($folks, ["handle" => $member["handle"]]);
$found[$key] = $data;
//record time updated
$updated = new \Moment\Moment();
$found["updated"] = $updated->format("Y-m-d\TH:i:sP");
$newFolks = [];
array_push($newFolks, $found);
//save updated file
DocTools::writeSettings("../config/folks.json", $newFolks);
//update member data in session
$member = [
"handle" => $found["handle"],
"email" => $found["email"],
"role" => $found["role"],
"avatar" => $found["avi"],
];
Session::set("member", $member);
}
}

View file

@ -26,6 +26,27 @@ class Settings
}
}
public static function sync($data)
{
$settings = self::$settings;
$settings["global"]["base_url"] = $data["global"]["base_url"];
$settings["global"]["title"] = $data["global"]["title"];
$settings["global"]["descriptions"] = $data["global"]["descriptions"];
$settings["global"]["base_url"] = $data["global"]["base_url"];
$settings["global"]["private"] = $data["global"]["private"];
$settings["global"]["renderOnSave"] = $data["global"]["renderOnSave"];
$settings["global"]["theme"] = $data["global"]["theme"];
Member::updateData("handle", $data["member"]["handle"]);
Member::updateData("email", $data["member"]["email"]);
$settings["email"]["active"] = $data["email"]["active"];
$settings["email"]["smtp"] = $data["email"]["smtp"];
$settings["email"]["mailgun"] = $data["email"]["mailgun"];
DocTools::writeSettings("../config/settings.json", $settings);
}
public function getThemes()
{
return $this->themes;
@ -47,6 +68,13 @@ class Settings
return self::$settings;
}
public static function updateGlobalData($key, $data)
{
$settings = self::$settings;
$settings["global"][$key] = $data;
DocTools::writeSettings("../config/settings.json", $settings);
}
public static function getCurrentIndex()
{
$settings = self::$settings;

View file

@ -8,6 +8,7 @@ class FileUploader
string $directory,
UploadedFileInterface $file
) {
$response = [];
try {
if (!is_dir($directory)) {
//Directory does not exist, so lets create it.
@ -22,8 +23,10 @@ class FileUploader
$file->moveTo($directory . "/" . $file->getClientFileName());
} catch (Error $e) {
echo "failed to upload image: " . $e->getMessage();
throw new Error("Failed to upload image file");
echo $e;
//echo "failed to upload image: " . $e->getMessage();
//throw new Error("Failed to upload image file");
}
}
}

View file

@ -35,7 +35,7 @@
</div>
</div>
<div id="site-background">
<label>Header</label>
<label>Site Header</label>
<img id="background" src="{{background}}" alt="image for site background" for="background-upload"/>
<input id="background-upload" type="file" name="backgrond-upload" />
</div>

View file

@ -11,6 +11,7 @@ include "../brain/controller/RouteControl.inc.php";
include "../brain/data/Auth.inc.php";
include "../brain/utility/StringTools.inc.php";
include "../brain/data/Session.inc.php";
include "../brain/data/Member.inc.php";
include "../brain/utility/FileUploader.inc.php";
include "../brain/utility/DocTools.inc.php";