Skip to content

Commit

Permalink
final app
Browse files Browse the repository at this point in the history
  • Loading branch information
duythien committed Jan 21, 2014
1 parent 8f1be69 commit 3889fa8
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 76 deletions.
8 changes: 8 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@

How to create blog use mongodb and php

Demo app [here](http://phpmongodb-duythien.rhcloud.com)

Backend [here](http://phpmongodb-duythien.rhcloud.com/admin)

username: duythien

password: duythien
115 changes: 64 additions & 51 deletions admin/index.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
require_once 'auth.php';
require_once '../app.php';
require_once '../vendor/markdown/Markdown.inc.php';

require '../app.php';
use \Michelf\MarkdownExtra,
\Michelf\Markdown;
require_once '../vendor/markdown/Markdown.inc.php';
use Michelf\MarkdownExtra,
Michelf\Markdown,
Blog\Functions;


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

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

if (isset($_GET['status'])&& $_GET['status']=='create') {
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
$article = array();
$article['title'] = $_POST['title'];
$article['content'] = Markdown::defaultTransform($_POST['content']);

$article['saved_at'] = new MongoDate();
switch ($status) {
case 'create':

if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
$article = array();
$article['title'] = $_POST['title'];
$article['content'] = Markdown::defaultTransform($_POST['content']);

if ( empty($article['title']) || empty($article['content']) ) {
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->insert($article);
$data['status'] = 'Row has successfully been inserted.';
$article['saved_at'] = new MongoDate();

if ( empty($article['title']) || empty($article['content']) ) {
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->insert($article);
$data['status'] = 'Row has successfully been inserted.';
}
}
}
view('admin/create', $data);
}elseif(isset($_GET['status'])&& $_GET['status']=='edit'){
$id = $_REQUEST['id'];
$data['status'] =null;
view('admin/create', $data);
break;
case 'edit':
$id = $_REQUEST['id'];
$data['status'] =null;

if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
$article = array();
$article['title'] = $_POST['title'];
$article['content'] = Markdown::defaultTransform($_POST['content']);
$article['saved_at'] = new MongoDate();

if ( empty($article['title']) || empty($article['content']) ) {
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->update(array('_id' => new MongoId($id)), $article);
$data['status'] = 'Row has successfully been update.';
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {

$article = array();
$article['title'] = $_POST['title'];
$article['content'] = Markdown::defaultTransform($_POST['content']);
$article['saved_at'] = new MongoDate();

if ( empty($article['title']) || empty($article['content']) ) {
$data['status'] = 'Please fill out both inputs.';
}else {
// then create a new row in the table
$conn->posts->update(array('_id' => new MongoId($id)), $article);
$data['status'] = 'Row has successfully been update.';
}
}
view('admin/edit',array(
'article' => Functions\getById($id,'posts',$conn),
'status' => $data['status']
));
break;
case 'delete':
$id = $_GET['id'];
$status = Functions\delete($id,'posts',$conn);
if ($status ==TRUE ) {
header("Location:index");
}
}
//var_dump(Blog\Functions\getById($id,'posts',$conn));

view('admin/edit',array(
'article' => Blog\Functions\getById($id,'posts',$conn),
'status' => $data['status']
));
}
else{
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
$data = Blog\Functions\get($currentPage,'posts',$conn);
break;
default:
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
$data = Functions\get($currentPage,'posts',$conn);


view('admin/dashboard',array(
'currentPage' => $data[0],
'totalPages' => $data[1],
'cursor' => $data[2],
view('admin/dashboard',array(
'currentPage' => $data[0],
'totalPages' => $data[1],
'cursor' => $data[2],

));
}
));
break;
}
}

11 changes: 0 additions & 11 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,3 @@
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.0.0.1','27017','blog')
);
/*define('URL', 'http://phpmongodb-duythien.rhcloud.com');
define('UserAuth', 'duythien');
define('PasswordAuth', 'duythien');
$config = array(
'username' => 'admin',
'password' => 'MHZKADzU6ylD',
'dbname' => 'phpmongodb',
//'cn' => sprintf('mongodb://%s:%d/%s', $hosts, $port,$database),
'connection_string'=> sprintf('mongodb://%s:%d/%s','127.5.62.2','27017','phpmongodb')
);*/
24 changes: 22 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ function connect($config){
* @return array
*/
function getById($id,$collection,$dbname){

// Convert strings of right length to MongoID
if (strlen($id) == 24){
$id = new \MongoId($id);
}
$table = $dbname->selectCollection($collection);
$cursor = $table->find(array('_id' => new \MongoId($id)));
$cursor = $table->find(array('_id' => $id));
$article = $cursor->getNext();

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

return $data;
}
/**
* delete article via id
* @return
*/
function delete($id,$collection,$dbname){
// Convert strings of right length to MongoID
if (strlen($id) == 24){
$id = new \MongoId($id);
}
$table = $dbname->selectCollection($collection);
$result = $table->remove(array('_id'=>$id));
if (!$id){
return false;
}
return $result;

}
function commentId($id,$collection,$dbname)
{
$table = $dbname->selectCollection($collection);
Expand Down
8 changes: 0 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
use Blog\Functions;

try {

// Fetch all the posts
/*$posts = Functions\get('posts', $conn);
view('index', array(
'cursor' => $posts,
'name' => 'Duy Thien'
));*/
$currentPage = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; //current page number
$data = Functions\get($currentPage,'posts',$conn);

Expand Down
2 changes: 1 addition & 1 deletion static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ form textarea, form input {
}
.blog-image img {
border-radius: 50%;
margin-left: 538px;
margin-left: 437px;
margin-top: 66px;
}
.blog-image{
Expand Down
4 changes: 1 addition & 3 deletions views/admin/create.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
<label for="content">Content</label>
<p><textarea name="content" id="content" cols="40" rows="8" class="span10"></textarea></p>

<div class="submit"><input type="submit" name="btn_submit" value="Save"/></div>


<div class="submit"><input type="submit" name="btn_submit" value="Save"/></div>
</form>


Expand Down
7 changes: 7 additions & 0 deletions views/admin/dashboard.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@
<div class="span12">
<ul class="pager">
<li>
<?php if($currentPage !== 1): ?>
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($currentPage - 1); ?>">&larr; Older</a>
<?php endif; ?>

</li>
<li lass="page-number"> <?php echo $currentPage; ?> </li>

<li>
<?php if($currentPage !== $totalPages): ?>
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($currentPage + 1); ?>">Newer &rarr;</a>
<?php endif;?>
</li>
</ul>
</div>
Expand Down
2 changes: 2 additions & 0 deletions views/index.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<?php endif; ?>

</li>
<li lass="page-number"> <?php echo $currentPage; ?> </li>

<li>
<?php if($currentPage !== $totalPages): ?>
<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($currentPage + 1); ?>">Newer &rarr;</a>
Expand Down

0 comments on commit 3889fa8

Please sign in to comment.