Skip to content

Commit

Permalink
Fix posts state (Reference #10 and #11).
Browse files Browse the repository at this point in the history
  • Loading branch information
osaris committed Feb 13, 2013
1 parent f7f2c30 commit 0a64e98
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 33 deletions.
34 changes: 34 additions & 0 deletions Application/Controller/Admin/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-> import('Model.Post')
-> import('Model.Comment');

from('Hoa')
-> import('Stringbuffer.Read');

}

namespace Application\Controller\Admin {
Expand Down Expand Up @@ -48,6 +51,37 @@ public function IndexAction ( ) {
return;
}

public function ShowAction ( $id ) {

$post = $this->LoadPost($this, $id, \Application\Model\Post::STATE_ALL);

$this->data->title = $post->title;
$this->data->post = $post;

$buffer = new \Hoa\Stringbuffer\Read();
$buffer->initializeWith(
'<?xml version="1.0" encoding="utf-8"?>' . "\n\n" .
'<fragment xmlns="http://hoa-project.net/xyl/xylophone">' . "\n".
' <snippet id="main">' . "\n" .
$post->content .
' </snippet>' . "\n" .
'</fragment>'
);
$this->view->addFragment(
$buffer->getStreamName(),
'content'
);

// TODO use post id from post in view
$this->data->post_id = $post->id;
$this->data->comments = $post->comments;

$this->view->addOverlay('hoa://Application/View/Admin/Posts/Show.xyl');
$this->view->render();

return;
}

public function NewAction ( ) {

$this->adminGuard();
Expand Down
70 changes: 38 additions & 32 deletions Application/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ protected function construct ( ) {
return;
}

static public function findByIdAndState ( $id, $state = STATE_PUBLISHED ) {
static public function findByIdAndState ( $id, $state = Post::STATE_PUBLISHED ) {

$post = new Post();
$data = $post->getMappingLayer()
->prepare(
$query = $post->getMappingLayer()
->prepare(
'SELECT id, posted, title, content, state ' .
'FROM post ' .
'WHERE id = :id ' .
'AND (state1 = :state OR :state2 = -1)'
)
->bindParam(':id', $id, PDO::PARAM_INT)
->bindParam(':state1', $state, PDO::PARAM_INT)
->bindParam(':state2', $state, PDO::PARAM_INT)
->fetchAll();
'WHERE (id = :id) ' .
'AND (state = :state1 OR :state2 = -1)'
);

$query->bindParameter(':id', $id, \PDO::PARAM_INT);
$query->bindParameter(':state1', $state, \PDO::PARAM_INT);
$query->bindParameter(':state2', $state, \PDO::PARAM_INT);

$data = $query->execute()
->fetchAll();

if(!empty($data)) {
$post->map($data[0]);
Expand Down Expand Up @@ -152,19 +155,21 @@ public function getList ( $current_page, $post_per_page, $state = Post::STATE_PU

$first_entry = ($current_page - 1) * $post_per_page;

$list = $this->getMappingLayer()
->prepare(
'SELECT id, title, posted, state ' .
'FROM post ' .
'WHERE (state = :state1 OR :state2 = -1)' .
'ORDER BY posted DESC ' .
'LIMIT :first_entry, :post_per_page'
)
->bindParam(':state1', $state, PDO::PARAM_INT)
->bindParam(':state2', $state, PDO::PARAM_INT)
->bindParam(':first_entry', $first_entry, PDO::PARAM_INT)
->bindParam(':post_per_page', $post_per_page, PDO::PARAM_INT)
->fetchAll();
$query = $this->getMappingLayer()
->prepare(
'SELECT id, title, posted, state ' .
'FROM post ' .
'WHERE (state = :state1 OR :state2 = -1)' .
'ORDER BY posted DESC ' .
'LIMIT :first_entry, :post_per_page'
);
$query->bindParameter(':state1', $state, \PDO::PARAM_INT);
$query->bindParameter(':state2', $state, \PDO::PARAM_INT);
$query->bindParameter(':first_entry', $first_entry, \PDO::PARAM_INT);
$query->bindParameter(':post_per_page', $post_per_page, \PDO::PARAM_INT);

$list = $query->execute()
->fetchAll();

foreach($list as &$post) {

Expand All @@ -190,16 +195,17 @@ public static function getNormalizedTitle( $title ) {

public function count ( $state = Post::STATE_PUBLISHED ) {

return $this->getMappingLayer()
->prepare(
$query = $this->getMappingLayer()
->prepare(
'SELECT COUNT(*) ' .
'FROM post ' .
'WHERE (state = :state1 OR :state2 = -1)'
)
->bindParam(':state1', $state, PDO::PARAM_INT)
->bindParam(':state2', $state, PDO::PARAM_INT)
->execute()
->fetchColumn();
'FROM post ' .
'WHERE (state = :state1 OR :state2 = -1)'
);
$query->bindParameter(':state1', $state, \PDO::PARAM_INT);
$query->bindParameter(':state2', $state, \PDO::PARAM_INT);

return $query->execute()
->fetchColumn();
}
}

Expand Down
1 change: 1 addition & 0 deletions Application/Public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
->post('login', '/admin/log/in', 'admin\log', 'in')
->get('logout', '/admin/log/out', 'admin\log', 'out')
->get('admin_posts', '/admin/posts', 'admin\posts', 'index')
->get('admin_post', '/admin/posts/(?<id>\d+)\-(?<normalized_title>.+)\.html', 'admin\posts', 'show')
->get('new_post', '/admin/posts/new', 'admin\posts', 'new')
->post('create_post','/admin/posts/create', 'admin\posts', 'create')
->get('edit_post', '/admin/posts/(?<id>\d+)/edit', 'admin\posts', 'edit')
Expand Down
2 changes: 1 addition & 1 deletion Application/View/Admin/Posts/Index.xyl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<time><value formatter="date"
formatter-format="d/m/Y"
formatter-timestamp="(?posted)" /></time> —
<a href="@post:id=(?id)&amp;normalized_title=(?normalized_title)" bind="?title" />
<a href="@admin_post:id=(?id)&amp;normalized_title=(?normalized_title)" bind="?title" />
</li>
</ul>
<paginator max="(?number)" select="(?current)" />
Expand Down
19 changes: 19 additions & 0 deletions Application/View/Admin/Posts/Show.xyl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
added dynamically
<?xyl-fragment href="…" as="content"?>
-->

<overlay xmlns="http://hoa-project.net/xyl/xylophone">
<div id="main">
<p><a href="@admin_posts">☜ Back to list</a></p>

<yield bind="?post">
<h2 bind="?title" />
<time><value formatter="date"
formatter-format="d/m/Y"
formatter-timestamp="(?posted)" /></time>
<yield select="?f:content#main" />
</yield>
</div>
</overlay>

0 comments on commit 0a64e98

Please sign in to comment.