Skip to content

Commit a527356

Browse files
committed
commit
0 parents  commit a527356

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+26858
-0
lines changed

.htaccess

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine on
3+
RewriteRule ^$ public/ [L]
4+
RewriteRule (.*) public/$1 [L]
5+
</IfModule>

app/.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Options -Indexes

app/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
// Load Config
3+
require_once 'config/config.php';
4+
require_once 'helpers/url_helpers.php';
5+
require_once 'helpers/session_helper.php';
6+
7+
// Autoload Core Libraries
8+
spl_autoload_register(function($className){
9+
require_once 'libraries/' . $className . '.php';
10+
});
11+

app/config/config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
// DB Params
3+
define('DB_HOST', 'localhost');
4+
define('DB_USER', 'root');
5+
define('DB_PASS', '');
6+
define('DB_NAME', 'shareposts');
7+
8+
// App Root
9+
define('APPROOT', dirname(dirname(__FILE__)));
10+
// URL Root
11+
define('URLROOT', 'http://localhost:8080/shareposts');
12+
// Site Name
13+
define('SITENAME', '');
14+
// App Version
15+
define('APPVERSION', '');
16+
?>

app/controllers/Pages.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
class Pages extends Controller {
3+
public function __construct(){
4+
5+
}
6+
7+
public function index(){
8+
if(isLoggedIn()){
9+
redirect('posts');
10+
}
11+
$data = [
12+
'title' => 'Simple Blog',
13+
'description' => 'Blog with MVC framework',
14+
'info' => 'You can contact me with the following details',
15+
'name' => 'Nirav Gajera',
16+
'location' => 'Ahmedabad, Gujarat',
17+
'contact' => '+91 7383171393',
18+
'mail' => '[email protected]'
19+
];
20+
21+
$this->view('pages/index', $data);
22+
}
23+
24+
public function about(){
25+
$data = [
26+
'title' => 'About Us',
27+
'description' => 'App to share posts with other users'
28+
];
29+
30+
$this->view('pages/about', $data);
31+
}
32+
33+
public function contact(){
34+
$data = [
35+
'title' => 'Contact Us',
36+
'description' => 'You can contact us through this details',
37+
'info' => 'You can contact me with the following details for work on your project',
38+
'name' => 'Nirav Gajera',
39+
'location' => 'Ahmedabad, Gujarat',
40+
'contact' => '+91 7383171393',
41+
'mail' => '[email protected]'
42+
];
43+
44+
$this->view('pages/contact', $data);
45+
}
46+
}
47+
?>

app/controllers/Posts.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
class Posts extends Controller {
3+
4+
public function __construct(){
5+
if(!isLoggedIn()){
6+
redirect('users/login');
7+
}
8+
//new model instance
9+
$this->postModel = $this->model('Post');
10+
$this->userModel = $this->model('User');
11+
}
12+
13+
public function index(){
14+
15+
$posts = $this->postModel->getPosts();
16+
$data = [
17+
'posts' => $posts
18+
];
19+
20+
$this->view('posts/index', $data);
21+
}
22+
public function getUser()
23+
{
24+
if(isset($_POST['search_user'])) {
25+
$checkUser = $this->model->getUser($_POST['name']);
26+
}
27+
}
28+
//add new post
29+
public function add(){
30+
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
31+
if($_SERVER['REQUEST_METHOD'] == 'POST'){
32+
//$img_path = URLROOT;
33+
$uploads_dir = '/public/img';
34+
35+
$tmp_name = $_FILES["img"]["name"];
36+
$name = basename($_FILES["img"]["name"]);
37+
38+
move_uploaded_file($tmp_name, "$uploads_dir/$name");
39+
40+
$data = [
41+
'title' => trim($_POST['title']),
42+
//'img' => (isset($_POST['img']) ? move_uploaded_file($_POST['img']) : ''),
43+
'img' => trim($tmp_name),
44+
'body' => trim($_POST['body']),
45+
'user_id' => $_SESSION['user_id'],
46+
'title_err' => '',
47+
'body_err' => '',
48+
'img_err' => '',
49+
];
50+
51+
if(empty($data['title'])){
52+
$data['title_err'] = 'Please enter post title';
53+
}
54+
55+
// echo '<pre>';
56+
// print_r($data);
57+
// echo '</pre>';exit;
58+
59+
if(empty($tmp_name)){
60+
$data['img_err'] = 'Please upload image';
61+
}
62+
if(empty($data['body'])){
63+
$data['body_err'] = 'Please enter the post content';
64+
}
65+
66+
//validate error free
67+
if(empty($data['title_err']) && empty($data['body_err'])){
68+
if($this->postModel->addPost($data)){
69+
flash('post_message', 'Your post have been added');
70+
redirect('posts');
71+
}else{
72+
die('something went wrong');
73+
}
74+
75+
//load view with error
76+
}else{
77+
$this->view('posts/add', $data);
78+
}
79+
}
80+
else{
81+
$data = [
82+
'title' => (isset($_POST['title']) ? trim($_POST['title']) : ''),
83+
// 'img' => (isset($_POST['img']) ? move_uploaded_file($_POST['img']) : ''),
84+
'body' => (isset($_POST['body'])? trim($_POST['body']) : '')
85+
];
86+
$this->view('posts/add', $data);
87+
}
88+
}
89+
90+
//show single post
91+
public function show($id){
92+
93+
$post = $this->postModel->getPostById($id);
94+
// print_r($post); exit;
95+
$user = $this->userModel->getUserById($post->user_id);
96+
97+
$data = [
98+
'post' => $post,
99+
'user' => $user
100+
];
101+
102+
$this->view('posts/show', $data);
103+
}
104+
105+
//edit post
106+
public function edit($id){
107+
108+
if($_SERVER['REQUEST_METHOD'] == 'POST'){
109+
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
110+
111+
$uploads_dir = '/public/img';
112+
113+
$tmp_name = $_FILES["img"]["name"];
114+
$name = basename($_FILES["img"]["name"]);
115+
116+
move_uploaded_file($tmp_name, "$uploads_dir/$name");
117+
118+
$oldimg = $this->postModel->getPostById($id);
119+
120+
121+
122+
$data = [
123+
'id' => $id,
124+
'title' => trim($_POST['title']),
125+
'body' => trim($_POST['body']),
126+
'img' => (!empty($tmp_name)? trim($tmp_name): $oldimg->img),
127+
'user_id' => $_SESSION['user_id'],
128+
'title_err' => '',
129+
'body_err' => '',
130+
];
131+
//validate the title
132+
if(empty($data['title'])){
133+
$data['title_err'] = 'Please enter post titke';
134+
}
135+
136+
//validate the image
137+
if(empty($tmp_name)){
138+
$data['img_err'] = 'Please upload image';
139+
140+
}
141+
//validate the body
142+
if(empty($data['body'])){
143+
$data['body_err'] = 'Please enter the post content';
144+
}
145+
146+
//validate error free
147+
if(empty($data['title_err']) && empty($_data['img_err']) && empty($data['body_err'])){
148+
if($this->postModel->updatePost($data)){
149+
flash('post_message', 'Your post have been updated');
150+
redirect('posts');
151+
}else{
152+
die('something went wrong');
153+
}
154+
155+
//load with the error
156+
}else{
157+
$this->view('posts/edit', $data);
158+
}
159+
}else{
160+
//check for the owner and call method from post model
161+
$post = $this->postModel->getPostById($id);
162+
if($post->user_id != $_SESSION['user_id']){
163+
redirect('posts');
164+
}
165+
$data = [
166+
'id' => $id,
167+
'title' => $post->title,
168+
'img' => $post->img,
169+
'body' => $post->body
170+
];
171+
172+
$this->view('posts/edit', $data);
173+
}
174+
}
175+
176+
//delete post
177+
public function delete($id){
178+
if($_SERVER['REQUEST_METHOD'] == 'POST'){
179+
//check for owner
180+
$post = $this->postModel->getPostById($id);
181+
if($post->user_id != $_SESSION['user_id']){
182+
redirect('posts');
183+
}
184+
185+
//call delete method from post mode
186+
if($this->postModel->deletePost($id)){
187+
flash('post_message', 'Post Removed');
188+
redirect('posts');
189+
}else{
190+
die('Something went wrong...!');
191+
}
192+
} else {
193+
redirect('posts');
194+
}
195+
}
196+
}
197+
?>

0 commit comments

Comments
 (0)