Skip to content

Commit

Permalink
crud usando ORM, configurando modulo de paginacao, configurado o banc…
Browse files Browse the repository at this point in the history
…o de dados para a ORM
  • Loading branch information
fabiancarlos committed May 10, 2012
1 parent f761300 commit 00b4ba3
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 41 deletions.
4 changes: 4 additions & 0 deletions application/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,18 @@
'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'unittest' => MODPATH.'unittest', // Unit testing
'userguide' => MODPATH.'userguide', // User guide and API documentation
'pagination' => MODPATH.'pagination', // Pagination for Kohana 3.2
));

/**
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/


Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));

61 changes: 60 additions & 1 deletion application/classes/controller/postagens.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,44 @@

class Controller_Postagens extends Controller_ApplicationBlog {

protected $_redirect = "../postagens";

public function action_index(){

$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' => 2,
'view' => 'pagination/basic',
'auto_hide' => TRUE,
'first_page_in_url' => TRUE,
));

$pagination_links = $pagination->render();

$content = View::factory('site/postagens/page');
$posts = $posts
->limit($pagination->items_per_page)
->offset($pagination->offset)
->find_all();

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

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

public function action_novo(){

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

# apenas para inicializar
$post = array();

$content = View::factory('site/postagens/novo')
Expand All @@ -23,6 +48,40 @@ public function action_novo(){

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

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

$time_now = new DateTime();

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

$post->titulo = (string) Arr::get($_POST, 'titulo');
$post->chamada = (string) Arr::get($_POST, 'chamada');
$post->texto = (string) Arr::get($_POST, 'texto');
$post->data_registro = $time_now->format('Y-m-d H:i:s');

$post->save();


$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');

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

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

}
14 changes: 14 additions & 0 deletions application/classes/model/post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php defined('SYSPATH') or die('No direct script access');

class Model_Post extends ORM{

protected $_table_name = 'posts';
protected $_primary_key = 'id';



}




19 changes: 19 additions & 0 deletions application/config/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php defined('SYSPATH') or die('No direct access allowed.');

return array
(
'default' => array
(
'type' => 'mysql',
'connection' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => 'adminadmin',
'persistent' => FALSE,
'database' => 'bd_blog',
),
'table_prefix' => '',
'charset' => 'utf8',
'profiling' => TRUE,
),
);
54 changes: 19 additions & 35 deletions application/views/site/postagens/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,30 @@
<p>=============================== Listar ================================</p>

<table cellspacing="0" cellpadding="0">
<thead><th>nº</th> <th>titulo</th> <th>cadastro</th> <th>ações</th></thead>
<thead><th>nº</th> <th class="titulo">titulo</th> <th>cadastro</th> <th>ações</th></thead>
<tbody>
<tr>
<td>78</td>
<td>Lorem ipsum dolor sit amet...</td>
<td>23/07/2012</td>
<td>
<a href="<?php echo URL::site('postagens/novo'); ?>" title="novo">novo -</a>
<a href="<?php echo URL::site('postagens/editar'); ?>" title="editar">editar -</a>
<a href="<?php echo URL::site('postagens/excluir'); ?>" title="deletar">excluir</a>
</td>
</tr>

<tr>
<td>78</td>
<td>Lorem ipsum dolor sit amet...</td>
<td>23/07/2012</td>
<td>
<a href="<?php echo URL::site('postagens/novo'); ?>" title="novo">novo -</a>
<a href="<?php echo URL::site('postagens/editar'); ?>" title="editar">editar -</a>
<a href="<?php echo URL::site('postagens/excluir'); ?>" title="deletar">excluir</a>
</td>
</tr>

<tr>
<td>78</td>
<td>Lorem ipsum dolor sit amet...</td>
<td>23/07/2012</td>
<td>
<a href="<?php echo URL::site('postagens/novo'); ?>" title="novo">novo -</a>
<a href="<?php echo URL::site('postagens/editar'); ?>" title="editar">editar -</a>
<a href="<?php echo URL::site('postagens/excluir'); ?>" title="deletar">excluir</a>
</td>
</tr>

<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post->id; ?></td>
<td class="titulo"><?php echo $post->titulo; ?></td>
<td><?php $data_registro = new DateTime($post->data_registro);
$data_registro = $data_registro->format('d/m/Y');

echo $data_registro; ?></td>
<td>
<a href="<?php echo URL::site('postagens/novo'); ?>" class="novo" title="novo">novo -</a>
<a href="<?php echo URL::site('postagens/editar/?id=') . $post->id ; ?>" class="editar" title="editar">editar -</a>
<a href="<?php echo URL::site('postagens/deletar/?id=') . $post->id ; ?>" class="deletar" title="deletar">deletar</a>
</td>
</tr>

<?php endforeach ?>
</tbody>
</table>


<div class="paginacao">« 1 2 3 4 »</div>
<?php echo $pagination_links; ?>


</div>

6 changes: 4 additions & 2 deletions bd_blog.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
-- nome para banco de dados: bd_blog

-- configurar o arquivo em 'appication/config/database.php' , adcionar usuário e senha do banco

-- POSTS
CREATE TABLE IF NOT EXISTS `posts`(
`id` INT(8) NOT NULL AUTO_INCREMENT,
`nome` VARCHAR(30) NOT NULL,
`titulo` VARCHAR(30) NOT NULL,
`chamada` TEXT NOT NULL,
`texto` TEXT NOT NULL,
`user_admin` VARCHAR(50),
`data_registro` DATETIME,
PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand Down
17 changes: 14 additions & 3 deletions media/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,21 @@ select:focus { outline: none; }
#postagens p{ margin: 10px 0px; }

#postagens table{ width:500px; }
#postagens table tr{ height:20px; }
#postagens table tr td{ text-align: center; }
#postagens table th{ border-bottom: 1px solid #ccc; }
#postagens table th.titulo{ width:220px; padding: 0px 4px; text-align: left; }

#postagens .paginacao { font-size: 22px; text-align: center; margin: 20px; }
#postagens table tr{ height:30px; }
#postagens table tr td{ text-align: center; border-bottom: 1px solid #ccc; }
#postagens table tr td.titulo{ width:220px; padding: 0px 4px; text-align: left; }

#postagens table tr td a{ color: #333; }
#postagens table tr td a.novo{ color: green; }
#postagens table tr td a.editar{ color: blue; }
#postagens table tr td a.deletar{ color: red; }

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

#postagens input[type="text"]{ font-size: 15px; padding: 4px 2px; }
#postagens textarea{ font-size: 15px; padding: 4px 2px; }
Expand Down
1 change: 1 addition & 0 deletions modules/pagination
Submodule pagination added at d1c34d

0 comments on commit 00b4ba3

Please sign in to comment.