Skip to content

Commit 3889fa8

Browse files
committed
final app
1 parent 8f1be69 commit 3889fa8

File tree

9 files changed

+105
-76
lines changed

9 files changed

+105
-76
lines changed

README

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11

22
How to create blog use mongodb and php
3+
4+
Demo app [here](http://phpmongodb-duythien.rhcloud.com)
5+
6+
Backend [here](http://phpmongodb-duythien.rhcloud.com/admin)
7+
8+
username: duythien
9+
10+
password: duythien

admin/index.php

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
require_once 'auth.php';
3+
require_once '../app.php';
4+
require_once '../vendor/markdown/Markdown.inc.php';
35

4-
require '../app.php';
5-
use \Michelf\MarkdownExtra,
6-
\Michelf\Markdown;
7-
require_once '../vendor/markdown/Markdown.inc.php';
6+
use Michelf\MarkdownExtra,
7+
Michelf\Markdown,
8+
Blog\Functions;
89

910

1011
$url_action = (empty($_REQUEST['action'])) ? 'logIn' : $_REQUEST['action'];
@@ -19,59 +20,71 @@
1920

2021
if (is_array($_SESSION) &&$_SESSION['username'] ==UserAuth) {
2122
$data = array();
23+
$status = (empty($_GET['status'])) ? 'dashboard':$_GET['status'];
2224

23-
if (isset($_GET['status'])&& $_GET['status']=='create') {
24-
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
25-
$article = array();
26-
$article['title'] = $_POST['title'];
27-
$article['content'] = Markdown::defaultTransform($_POST['content']);
28-
29-
$article['saved_at'] = new MongoDate();
25+
switch ($status) {
26+
case 'create':
27+
28+
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
29+
$article = array();
30+
$article['title'] = $_POST['title'];
31+
$article['content'] = Markdown::defaultTransform($_POST['content']);
3032

31-
if ( empty($article['title']) || empty($article['content']) ) {
32-
$data['status'] = 'Please fill out both inputs.';
33-
}else {
34-
// then create a new row in the table
35-
$conn->posts->insert($article);
36-
$data['status'] = 'Row has successfully been inserted.';
33+
$article['saved_at'] = new MongoDate();
34+
35+
if ( empty($article['title']) || empty($article['content']) ) {
36+
$data['status'] = 'Please fill out both inputs.';
37+
}else {
38+
// then create a new row in the table
39+
$conn->posts->insert($article);
40+
$data['status'] = 'Row has successfully been inserted.';
41+
}
3742
}
38-
}
39-
view('admin/create', $data);
40-
}elseif(isset($_GET['status'])&& $_GET['status']=='edit'){
41-
$id = $_REQUEST['id'];
42-
$data['status'] =null;
43+
view('admin/create', $data);
44+
break;
45+
case 'edit':
46+
$id = $_REQUEST['id'];
47+
$data['status'] =null;
4348

44-
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
45-
$article = array();
46-
$article['title'] = $_POST['title'];
47-
$article['content'] = Markdown::defaultTransform($_POST['content']);
48-
$article['saved_at'] = new MongoDate();
49-
50-
if ( empty($article['title']) || empty($article['content']) ) {
51-
$data['status'] = 'Please fill out both inputs.';
52-
}else {
53-
// then create a new row in the table
54-
$conn->posts->update(array('_id' => new MongoId($id)), $article);
55-
$data['status'] = 'Row has successfully been update.';
49+
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
50+
51+
$article = array();
52+
$article['title'] = $_POST['title'];
53+
$article['content'] = Markdown::defaultTransform($_POST['content']);
54+
$article['saved_at'] = new MongoDate();
55+
56+
if ( empty($article['title']) || empty($article['content']) ) {
57+
$data['status'] = 'Please fill out both inputs.';
58+
}else {
59+
// then create a new row in the table
60+
$conn->posts->update(array('_id' => new MongoId($id)), $article);
61+
$data['status'] = 'Row has successfully been update.';
62+
}
63+
}
64+
view('admin/edit',array(
65+
'article' => Functions\getById($id,'posts',$conn),
66+
'status' => $data['status']
67+
));
68+
break;
69+
case 'delete':
70+
$id = $_GET['id'];
71+
$status = Functions\delete($id,'posts',$conn);
72+
if ($status ==TRUE ) {
73+
header("Location:index");
5674
}
57-
}
58-
//var_dump(Blog\Functions\getById($id,'posts',$conn));
59-
60-
view('admin/edit',array(
61-
'article' => Blog\Functions\getById($id,'posts',$conn),
62-
'status' => $data['status']
63-
));
64-
}
65-
else{
66-
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
67-
$data = Blog\Functions\get($currentPage,'posts',$conn);
75+
break;
76+
default:
77+
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
78+
$data = Functions\get($currentPage,'posts',$conn);
6879

6980

70-
view('admin/dashboard',array(
71-
'currentPage' => $data[0],
72-
'totalPages' => $data[1],
73-
'cursor' => $data[2],
81+
view('admin/dashboard',array(
82+
'currentPage' => $data[0],
83+
'totalPages' => $data[1],
84+
'cursor' => $data[2],
7485

75-
));
76-
}
86+
));
87+
break;
88+
}
7789
}
90+

config.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,3 @@
1010
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
1111
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.0.0.1','27017','blog')
1212
);
13-
/*define('URL', 'http://phpmongodb-duythien.rhcloud.com');
14-
define('UserAuth', 'duythien');
15-
define('PasswordAuth', 'duythien');
16-
17-
$config = array(
18-
'username' => 'admin',
19-
'password' => 'MHZKADzU6ylD',
20-
'dbname' => 'phpmongodb',
21-
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
22-
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.5.62.2','27017','phpmongodb')
23-
);*/

functions.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ function connect($config){
1919
* @return array
2020
*/
2121
function getById($id,$collection,$dbname){
22-
22+
// Convert strings of right length to MongoID
23+
if (strlen($id) == 24){
24+
$id = new \MongoId($id);
25+
}
2326
$table = $dbname->selectCollection($collection);
24-
$cursor = $table->find(array('_id' => new \MongoId($id)));
27+
$cursor = $table->find(array('_id' => $id));
2528
$article = $cursor->getNext();
2629

2730
if (!$article ){
@@ -56,6 +59,23 @@ function get($page,$collection,$dbname){
5659

5760
return $data;
5861
}
62+
/**
63+
* delete article via id
64+
* @return
65+
*/
66+
function delete($id,$collection,$dbname){
67+
// Convert strings of right length to MongoID
68+
if (strlen($id) == 24){
69+
$id = new \MongoId($id);
70+
}
71+
$table = $dbname->selectCollection($collection);
72+
$result = $table->remove(array('_id'=>$id));
73+
if (!$id){
74+
return false;
75+
}
76+
return $result;
77+
78+
}
5979
function commentId($id,$collection,$dbname)
6080
{
6181
$table = $dbname->selectCollection($collection);

index.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66
use Blog\Functions;
77

88
try {
9-
10-
// Fetch all the posts
11-
/*$posts = Functions\get('posts', $conn);
12-
13-
view('index', array(
14-
'cursor' => $posts,
15-
'name' => 'Duy Thien'
16-
));*/
179
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
1810
$data = Functions\get($currentPage,'posts',$conn);
1911

static/css/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ form textarea, form input {
195195
}
196196
.blog-image img {
197197
border-radius: 50%;
198-
margin-left: 538px;
198+
margin-left: 437px;
199199
margin-top: 66px;
200200
}
201201
.blog-image{

views/admin/create.view.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
<label for="content">Content</label>
2323
<p><textarea name="content" id="content" cols="40" rows="8" class="span10"></textarea></p>
2424

25-
<div class="submit"><input type="submit" name="btn_submit" value="Save"/></div>
26-
27-
25+
<div class="submit"><input type="submit" name="btn_submit" value="Save"/></div>
2826
</form>
2927

3028

views/admin/dashboard.view.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@
3737
<div class="span12">
3838
<ul class="pager">
3939
<li>
40+
<?php if($currentPage !== 1): ?>
4041
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($currentPage - 1); ?>">&larr; Older</a>
42+
<?php endif; ?>
43+
4144
</li>
45+
<li lass="page-number"> <?php echo $currentPage; ?> </li>
46+
4247
<li>
48+
<?php if($currentPage !== $totalPages): ?>
4349
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($currentPage + 1); ?>">Newer &rarr;</a>
50+
<?php endif;?>
4451
</li>
4552
</ul>
4653
</div>

views/index.view.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
<?php endif; ?>
3030

3131
</li>
32+
<li lass="page-number"> <?php echo $currentPage; ?> </li>
33+
3234
<li>
3335
<?php if($currentPage !== $totalPages): ?>
3436
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($currentPage + 1); ?>">Newer &rarr;</a>

0 commit comments

Comments
 (0)