diff --git a/database-seed.sql b/database-seed.sql index ef56c0e125795088e9bb9f114e6259818d273ddf..3c0bb195b9bb294aac1b010560422257378940cd 100644 --- a/database-seed.sql +++ b/database-seed.sql @@ -49,7 +49,7 @@ CREATE TABLE "posts" ( ); -- CURRENT IMPLEMENTATION ONLY ACCEPTS "refer_type" values of {'Repost', 'Reply'} -CREATE TABLE "images" ( +CREATE TABLE "post_resources" ( "post_id" integer, "post_owner_id" integer, "path" varchar @@ -83,11 +83,7 @@ COMMENT ON COLUMN "posts"."body" IS 'Content of the post'; COMMENT ON COLUMN "posts"."refer_type" IS 'Refer type, e.g. Repost(Retweet), Reply'; -COMMENT ON COLUMN "images"."path" IS 'Path to the image in filesystem'; - -COMMENT ON COLUMN "videos"."path" IS 'Path to the video in filesystem'; - -COMMENT ON COLUMN "audios"."path" IS 'Path to the audio in filesystem'; +COMMENT ON COLUMN "post_resources"."path" IS 'Path to the resource in filesystem'; COMMENT ON COLUMN "likes"."user_id" IS 'The user which likes the post'; @@ -103,11 +99,7 @@ ALTER TABLE "posts" ADD FOREIGN KEY ("owner_id") REFERENCES "users" ("id"); ALTER TABLE "posts" ADD FOREIGN KEY ("refer_post", "refer_post_owner") REFERENCES "posts" ("post_id", "owner_id"); -ALTER TABLE "images" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); - -ALTER TABLE "videos" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); - -ALTER TABLE "audios" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); +ALTER TABLE "post_resources" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); ALTER TABLE "likes" ADD FOREIGN KEY ("post_id", "post_owner_id") REFERENCES "posts" ("post_id", "owner_id"); diff --git a/src/app/baseclasses/BaseManager.php b/src/app/baseclasses/BaseManager.php index 8cacdd3c2c34009d25b0bd726b1325142208ac88..45f7624c6f3dc229ca9288607801c1fedf290c6e 100644 --- a/src/app/baseclasses/BaseManager.php +++ b/src/app/baseclasses/BaseManager.php @@ -142,7 +142,7 @@ abstract class BaseManager return $stmt->fetch(); } - public function insert($model, $attributes, $idName = null) // attributes[$attribute] = $type + public function insert($model, $attributes, $retIDName = null) // attributes[$attribute] = $type { $sql = "INSERT INTO $this->tableName ("; @@ -164,7 +164,7 @@ abstract class BaseManager } $stmt->execute(); - return $this->pdo->lastInsertId($idName); + return $this->pdo->lastInsertId($retIDName); } public function update($model, $attributes) // attributes[$attribute] = $type diff --git a/src/app/controllers/Page/ComposePage.php b/src/app/controllers/Page/ComposePage.php index 87e96ba075f9c5d5a979c1f7191e3e0a4da5381a..689fe635c93f7c75944b08377e968b0d75e660e5 100644 --- a/src/app/controllers/Page/ComposePage.php +++ b/src/app/controllers/Page/ComposePage.php @@ -1,20 +1,31 @@ <?php require_once SRC_ROOT_PATH . "/app/baseclasses/BaseController.php"; +require_once SRC_ROOT_PATH . "/app/controllers/Post/PostController.php"; + class ComposePage extends BaseController{ protected static $instance; - public function __construct(){ - parent::__construct(null); + public function __construct($srv){ + parent::__construct($srv); } public static function getInstance(){ if(!isset(self::$instance)){ - self::$instance = new static(); + self::$instance = new static( + PostController::getInstance() + ); } return self::$instance; } + public function post($urlParams) + { + $srv->post(); + require PAGE_PATH . "/submission.php"; + exit(); + } + public function get($urlParams) { require PAGE_PATH . "/post.php"; diff --git a/src/app/controllers/Post/PostController.php b/src/app/controllers/Post/PostController.php index 90a8d0e2fe3fae5c33cb5bc684d21b0343fb0b5d..1fa10674cfdc8b241160f71bb93263b153d36d77 100644 --- a/src/app/controllers/Post/PostController.php +++ b/src/app/controllers/Post/PostController.php @@ -3,7 +3,11 @@ require_once SRC_ROOT_PATH . "/app/baseclasses/BaseController.php"; require_once SRC_ROOT_PATH . "app/core/FileAccess.php"; + +require_once SRC_ROOT_PATH . "/app/models/PostModel.php"; +require_once SRC_ROOT_PATH . "/app/models/PostResourceModel.php"; require_once SRC_ROOT_PATH . "/app/modelmanagers/PostManager.php"; +require_once SRC_ROOT_PATH . "/app/modelmanagers/PostResourceManager.php"; class PostController extends BaseController { @@ -24,10 +28,70 @@ class PostController extends BaseController return self::$instance; } - protected function post() + protected function createPost( + $owner_id, + $body, + $refer_type = null, + $refer_post = null, + $refer_post_owner = null + ) + { + preg_match_all("/(#\w+)/u", $body, $matches); + if ($matches) { + $tagsArray = array_count_values($matches[0]); + $tags = array_keys($tagsArray); + } + + $postArr = [ + 'owner_id' => $owner_id, + 'body' => $body + ]; + + if(!is_null($refer_type)) { + $postArr['refer_type'] = $refer_type; + $postArr['refer_post'] = $refer_post; + $postArr['refer_post_owner'] = $refer_owner; + } + + if(isset($tags)) { + $postArr['tags'] = $tags; + } + + $postObj = new PostModel(); + $postObj = $postObj->constructFromArray($postArr); + + $post_id = $srv->insert($postObj, array_keys($postArr), 'post_id'); + + return $post_id; + } + + protected function insertResources($post_id, $owner_id, $resources) + { + + } + + public function compose() { - $fileAccess = new FileAccess(FileAccess::POSTS_PATH); - $newFileName = $fileAccess->saveFileAuto($_FILES['file']['tmp_name']); + $resources = []; + + if(isset($_FILES['file']['tmp_name'])) { + $fileAccess = new FileAccess(FileAccess::POSTS_PATH); + $newFileName = $fileAccess->saveFileAuto($_FILES['file']['tmp_name']); + + $resources[] = $newFileName; + } + // insert post tweetpost here + $user_id = $_SESSION['user_id']; + $post_id = $this->createPost( + owner_id: $user_id, + body: $_POST['body'], + ); + + $this->insertResources( + $post_id, + $user_id, + $resources + ); } } \ No newline at end of file diff --git a/src/app/modelmanagers/PostResourceManager.php b/src/app/modelmanagers/PostResourceManager.php new file mode 100644 index 0000000000000000000000000000000000000000..b1256994852a119e78946908e42a095fa849c5aa --- /dev/null +++ b/src/app/modelmanagers/PostResourceManager.php @@ -0,0 +1,22 @@ +<?php + +require_once SRC_ROOT_PATH . "/baseclasses/BaseManager.php"; + +class PostResourceManager extends BaseManager +{ + protected static $instance; + protected $tableName = 'post_resources'; + + protected function __construct() + { + parent::__construct(); + } + + public static function getInstance() + { + if (!isset(self::$instance)) { + self::$instance = new static(); + } + return self::$instance; + } +} \ No newline at end of file diff --git a/src/app/models/PostModel.php b/src/app/models/PostModel.php index 039479d7f26e5a2c530a59512e763feae21bb85d..18811f501725388d30c1e5208e02d20940fec698 100644 --- a/src/app/models/PostModel.php +++ b/src/app/models/PostModel.php @@ -15,7 +15,7 @@ class PostModel extends BaseModel public $tags = []; - private function __construct() + public function __construct() { $this->_primary_key = ['post_id', 'owner_id']; } diff --git a/src/app/models/PostResourceModel.php b/src/app/models/PostResourceModel.php new file mode 100644 index 0000000000000000000000000000000000000000..35f18852683a633c8f1beee13cd924372139e3ab --- /dev/null +++ b/src/app/models/PostResourceModel.php @@ -0,0 +1,15 @@ +<?php + +require_once SRC_ROOT_PATH . '/app/baseclasses/BaseModel.php'; + +class PostResourceModel extends BaseModel +{ + public $post_id; + public $post_owner_id; + public $body; + + public function __construct() + { + $this->_primary_key = ['post_id', 'post_owner_id']; + } +} diff --git a/src/app/view/post.php b/src/app/view/post.php index e6dc18508ae08ed8c41460ced0af9d6980a3ea70..ad33858b006d946bc56c64e75e77cf08354f2cc4 100644 --- a/src/app/view/post.php +++ b/src/app/view/post.php @@ -11,9 +11,9 @@ <body> <section id="overlay"> - <img src="/public/assets/github_post_logo.jpg" alt="Github Logo" id="logo"> + <img src="/public/assets/Logo.png" alt="Kicau Logo" id="logo"> <hr> - <form action="submission.php" method="POST"> + <form action="create" method="POST"> <h1>Create a post!</h1> <section class="text-input"> <label for="textarea-input">Type in the box below:</label> @@ -22,24 +22,14 @@ </section> <br> - <section class="image"> - <label for="image-input">Select an image file:</label> - <input type="file" id="image-input" name="image-input"> + <section class="file"> + <label for="file-input">Select a file:</label> + <input type="file" id="file-input" name="image-input"> </section> <hr> - <section class="video"> - <label for="video-input">Select a video file:</label> - <input type="file" id="video-input" name="video-input"> - </section> - <hr> - <section class="audio"> - <label for="audio-input">Select an audio file:</label> - <input type="file" id="audio-input" name="audio-input"> - </section> - <br> <section class="submission"> - <input type="submit" value="Post"> + <input type="submit" id="btn-post" value="Post"> </section> </form> </section> diff --git a/src/app/view/submission.php b/src/app/view/submission.php index a5447c4abeb4491ee0b7536db27bd54124f4242f..e576f7ec2067c3f12005434e153f7958b631fe3c 100644 --- a/src/app/view/submission.php +++ b/src/app/view/submission.php @@ -3,14 +3,14 @@ <head> <meta charset="utf-8"> - <link rel="stylesheet" type="text/css" href="public/css/post.css"> + <link rel="stylesheet" type="text/css" href="/public/css/post.css"> <link href="https://fonts.googleapis.com/css?family=Rubik" rel="stylesheet"> <title>Post Review</title> </head> <body> <section id="overlay"> - <img src="public/assets/github_post_logo.jpg" alt="Github Logo" id="logo"> + <img src="/public/assets/github_post_logo.jpg" alt="Github Logo" id="logo"> <hr> <h1>You have made a new post!</h1> </section> diff --git a/src/index.php b/src/index.php index 9a20d29978c05399be94307ff3cbb8b725b3ed68..47bb4bcc053b35014df5152dd292e95fff91e15c 100644 --- a/src/index.php +++ b/src/index.php @@ -33,8 +33,9 @@ $router->addHandler("/api/setadmin", SetAdminController::getInstance(), []); $router->addHandler("/login", LoginPage::getInstance(), []); $router->addHandler("/compose/kicau", ComposePage::getInstance(), []); -$router->addHandler("/settings/*", SettingsPage::getInstance(), []); -$router->addHandler("/admin", AdminPage::getInstance(), []); $router->addHandler("/*", UserPage::getInstance(), []); +$router->addHandler("/settings/*", SettingsPage::getInstance(), []); +$router->addHandler("/admin/*", AdminPage::getInstance(), []); +$router->addHandler("/compose/kicau/*", ComposePage::getInstance(), []); $router->run($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); \ No newline at end of file diff --git a/src/public/assets/.gitkeep b/src/public/assets/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/public/assets/Logo.png b/src/public/assets/Logo.png new file mode 100644 index 0000000000000000000000000000000000000000..9d16352501f18f0fba67bf127eea4b97d3c42bed Binary files /dev/null and b/src/public/assets/Logo.png differ diff --git a/src/public/assets/github_post_logo.jpg b/src/public/assets/github_post_logo.jpg deleted file mode 100644 index b0fae0e500ca4fd3ff0949eec0be059673e14c9d..0000000000000000000000000000000000000000 Binary files a/src/public/assets/github_post_logo.jpg and /dev/null differ