Skip to content

Commit

Permalink
pagincaao das postagens e crud funcionando
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancarlos committed May 11, 2012
1 parent cb21e77 commit d1de07b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 42 deletions.
9 changes: 7 additions & 2 deletions application/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@
* defaults for the URI.
*/

// FOR PAGINATION INT POSTAGENS
Route::set('postagens', '<controller>')
// FOR PAGINATION
Route::set('home', 'home')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Route::set('postagens', 'postagens')
->defaults(array(
'controller' => 'postagens',
'action' => 'index',
Expand Down
27 changes: 25 additions & 2 deletions application/classes/controller/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@

class Controller_Home extends Controller_ApplicationBlog {

public function action_index(){
public function action_index(){

$content = View::factory('site/home/page');
$posts = ORM::factory('post');

$count_all = $posts->count_all();

/* Pagination */
$pagination = Pagination::factory(array(
'current_page' => array('source' => 'query_string', 'key' => 'page'),
'total_items' => $count_all,
'items_per_page' => 4,
'view' => 'pagination/basic',
'auto_hide' => TRUE,
'first_page_in_url' => TRUE,
));

$pagination_links = $pagination->render();

$posts = $posts
->limit($pagination->items_per_page)
->offset($pagination->offset)
->order_by('id', 'DESC')
->find_all();

$content = View::factory('site/home/page')
->bind('posts', $posts)
->bind('pagination_links', $pagination_links);

$this->template->content = $content;
}
Expand Down
49 changes: 47 additions & 2 deletions application/classes/controller/postagens.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function action_index(){
$pagination = Pagination::factory(array(
'current_page' => array('source' => 'query_string', 'key' => 'page'),
'total_items' => $count_all,
'items_per_page' => 2,
'items_per_page' => 3,
'view' => 'pagination/basic',
'auto_hide' => TRUE,
'first_page_in_url' => TRUE,
Expand All @@ -25,6 +25,7 @@ public function action_index(){
$posts = $posts
->limit($pagination->items_per_page)
->offset($pagination->offset)
->order_by('id', 'DESC')
->find_all();

$content = View::factory('site/postagens/page')
Expand All @@ -42,7 +43,7 @@ public function action_novo(){
# apenas para inicializar
$post = array();

$content = View::factory('site/postagens/novo')
$content = View::factory('site/postagens/form')
->bind('action', $action)
->bind('post', $post);

Expand Down Expand Up @@ -73,6 +74,50 @@ public function action_novo(){

}

public function action_editar(){

# pega a action que está sendo utilizada
$action = $this->request->action();

# get the id to where condition
$id = (string) Arr::get($_GET, 'id');

$posts = ORM::factory('post');

$post = $posts
->where('id', '=', $id)
->find();

$content = View::factory('site/postagens/form')
->bind('action', $action)
->bind('post', $post);

$this->template->content = $content;

if ($_POST && isset($_POST['salvar'])) {

$time_now = new DateTime();

$post = ORM::factory('post', $id);

$post->titulo = (string) Arr::get($_POST, 'titulo');
$post->chamada = (string) Arr::get($_POST, 'chamada');
$post->texto = (string) Arr::get($_POST, 'texto');

$post->update();

$redirect = URL::site($this->_redirect);
$this->request->redirect($redirect);
}


if($_POST && isset($_POST['cancelar'])){

$redirect = URL::site($this->_redirect);
$this->request->redirect($redirect);
}
}

public function action_deletar(){

$id = (string) Arr::get($_GET, 'id');
Expand Down
61 changes: 36 additions & 25 deletions application/views/site/home/page.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@

<div id="listar_posts">

<dl class="post">
<dt>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</dt>
<dd class="data">22 de julho, por Zé - 12 comentários</dd>
<dd class="texto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec commodo, nulla varius imperdiet molestie, ligula dui lobortis lacus, ut elementum purus libero non odio. Suspendisse luctus libero sit amet sem dignissim vel mattis tellus bibendum. Pellentesque malesuada lacus sed neque rhoncus...</dd>
<dd class="mais"><a href="" title="Continuar lendo...">➔</a></dd>
</dl>

<dl class="post">
<dt>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</dt>
<dd class="data">22 de julho, por Zé - 12 comentários</dd>
<dd class="texto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec commodo, nulla varius imperdiet molestie, ligula dui lobortis lacus, ut elementum purus libero non odio. Suspendisse luctus libero sit amet sem dignissim vel mattis tellus bibendum. Pellentesque malesuada lacus sed neque rhoncus...</dd>
<dd class="mais"><a href="" title="Continuar lendo...">➔</a></dd>
</dl>

<dl class="post">
<dt>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</dt>
<dd class="data">22 de julho, por Zé - 12 comentários</dd>
<dd class="texto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec commodo, nulla varius imperdiet molestie, ligula dui lobortis lacus, ut elementum purus libero non odio. Suspendisse luctus libero sit amet sem dignissim vel mattis tellus bibendum. Pellentesque malesuada lacus sed neque rhoncus...</dd>
<dd class="mais"><a href="" title="Continuar lendo...">➔</a></dd>
</dl>


<div class="paginacao">« 1 2 3 4 »</div>

<!-- Condição para saber se tem postagens cadastradas -->
<?php if (!empty($posts)): ?>

<?php foreach ($posts as $post): ?>

<dl class="post">
<dt><?php echo $post->titulo; ?></dt>
<dd class="data"><?php $data_registro = new DateTime($post->data_registro);
$data_registro = $data_registro->format('d/m/Y');

echo $data_registro . ' por Zé - 6 comentários' ; ?></dd>
<dd class="texto"><?php echo $post->chamada ?></dd>
<dd class="mais"><a href="" title="Continuar lendo...">➔</a></dd>
</dl>

<?php endforeach ?>

<?php echo $pagination_links; ?>

<?php else: ?>
<!-- EXEMPLO -->
<dl class="post">
<dt>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</dt>
<dd class="data">22 de julho, por Zé - 12 comentários</dd>
<dd class="texto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec commodo, nulla varius imperdiet molestie, ligula dui lobortis lacus, ut elementum purus libero non odio. Suspendisse luctus libero sit amet sem dignissim vel mattis tellus bibendum. Pellentesque malesuada lacus sed neque rhoncus...</dd>
<dd class="mais"><a href="" title="Continuar lendo...">➔</a></dd>
</dl>

<dl class="post">
<dt>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</dt>
<dd class="data">22 de julho, por Zé - 12 comentários</dd>
<dd class="texto">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec commodo, nulla varius imperdiet molestie, ligula dui lobortis lacus, ut elementum purus libero non odio. Suspendisse luctus libero sit amet sem dignissim vel mattis tellus bibendum. Pellentesque malesuada lacus sed neque rhoncus...</dd>
<dd class="mais"><a href="" title="Continuar lendo...">➔</a></dd>
</dl>

<?php endif ?>

</div>

Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@

<p>=== <b> <?php echo $action; ?> </b> ===</p>

<?php foreach ($post as $post): ?>
<?php $post = $post; ?>
<?php endforeach ?>

<?php echo Form::open(); ?>

<div class="block">
<?php $titulo = isset($post['titulo']) ? $post['titulo'] : ''; ?>
<?php $titulo = isset($post->titulo) ? $post->titulo : ''; ?>
<?php echo Form::label('titulo', 'Titulo', array('title' => 'Titulo')); ?>:<br />
<?php echo Form::input('titulo', $titulo, array('id' => 'titulo', 'size' => '60')); ?>
</div>

<div class="block">
<?php $chamada = isset($post['chamada']) ? $post['chamada'] : ''; ?>
<?php $chamada = isset($post->chamada) ? $post->chamada : ''; ?>
<?php echo Form::label('chamada', 'Chamada', array('title' => 'Chamada')); ?>:<br />
<?php echo Form::textarea('chamada', $chamada, array('id' => 'chamada' , 'rows' => 5, 'cols' => '60' )); ?>
</div>

<div class="block">
<?php $texto = isset($post['texto']) ? $post['texto'] : ''; ?>
<?php $texto = isset($post->texto) ? $post->texto : ''; ?>
<?php echo Form::label('texto', 'Texto', array('title' => 'Texto')); ?>:<br />
<?php echo Form::textarea('texto', $texto, array('id' => 'texto' , 'rows' => 12, 'cols' => '60' )); ?>
</div>
Expand Down
2 changes: 1 addition & 1 deletion bd_blog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- POSTS
CREATE TABLE IF NOT EXISTS `posts`(
`id` INT(8) NOT NULL AUTO_INCREMENT,
`titulo` VARCHAR(30) NOT NULL,
`titulo` VARCHAR(200) NOT NULL,
`chamada` TEXT NOT NULL,
`texto` TEXT NOT NULL,
`data_registro` DATETIME,
Expand Down
14 changes: 8 additions & 6 deletions media/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ select:focus { outline: none; }


/*==MENU==*/
#menu nav { width:500px; height:30px; }
#menu nav ul li{ float: left; list-style: none; font-size: 11px; margin-right: 20px; }
#menu nav ul li a{ position: relative; color: #444; }
#menu nav ul li a:hover{ color: #999; }
#menu nav ul li a:active{ bottom: -2px;}
#menu nav { width:500px; height:30px; margin: 10px 0px; }
#menu nav ul li{ float: left; list-style: none; font-size: 11px; }
#menu nav ul li a{ position: relative; color: #444; padding: 8px 15px; background-color: #DDD; }
#menu nav ul li a:hover{ color: #666; background-color: #d6d6d6; }
#menu nav ul li a:active{ bottom: -3px; color: #888; background-color: #d4d4d4;}

/*==posts==*/

Expand All @@ -103,7 +103,9 @@ select:focus { outline: none; }
#listar_posts dl.post dd.mais a{ display: block; font-size: 22px; color: #999; padding: 0px 0px 2px 0px; }
#listar_posts dl.post dd.mais a:hover{ color: #333; }

#listar_posts .paginacao { font-size: 22px; text-align: center; margin: 20px; }
#listar_posts .pagination .go_to{ font-size: 28px; color: #333; }
#listar_posts .pagination { font-size: 22px; text-align: center; color: #aaa; word-spacing: 10px; margin: 20px; }
#listar_posts .pagination a{ color: #333; }


/*==SOBRE==*/
Expand Down
2 changes: 1 addition & 1 deletion modules/pagination
Submodule pagination updated from d1c34d to a0535f

0 comments on commit d1de07b

Please sign in to comment.