diff --git a/brain/api/v1/ImagesAPI.inc.php b/brain/api/v1/ImagesAPI.inc.php index aed3092..1bfed8e 100644 --- a/brain/api/v1/ImagesAPI.inc.php +++ b/brain/api/v1/ImagesAPI.inc.php @@ -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; } diff --git a/brain/api/v1/SettingsAPI.inc.php b/brain/api/v1/SettingsAPI.inc.php index 291a958..dbc6aab 100644 --- a/brain/api/v1/SettingsAPI.inc.php +++ b/brain/api/v1/SettingsAPI.inc.php @@ -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 = [ diff --git a/brain/controller/APIControl.inc.php b/brain/controller/APIControl.inc.php index 3875930..954e89d 100644 --- a/brain/controller/APIControl.inc.php +++ b/brain/controller/APIControl.inc.php @@ -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", diff --git a/brain/controller/DashControl.inc.php b/brain/controller/DashControl.inc.php index 22d7cdc..f6b0647 100644 --- a/brain/controller/DashControl.inc.php +++ b/brain/controller/DashControl.inc.php @@ -36,6 +36,7 @@ class DashControl "currentTheme" => $settings["global"]["theme"], "themes" => $themes, "mailOption" => $settings["email"]["active"], + "status" => Session::active(), ]; } else { header("Location: /dashboard"); diff --git a/brain/data/Member.inc.php b/brain/data/Member.inc.php new file mode 100644 index 0000000..fffa33c --- /dev/null +++ b/brain/data/Member.inc.php @@ -0,0 +1,33 @@ +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); + } +} diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index e5bac1c..950e312 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -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; diff --git a/brain/utility/FileUploader.inc.php b/brain/utility/FileUploader.inc.php index 5afbfc2..5609834 100644 --- a/brain/utility/FileUploader.inc.php +++ b/brain/utility/FileUploader.inc.php @@ -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"); } } } diff --git a/brain/views/dash/settings.twig b/brain/views/dash/settings.twig index 1742040..39b5b45 100644 --- a/brain/views/dash/settings.twig +++ b/brain/views/dash/settings.twig @@ -35,7 +35,7 @@