Commits

Anonymous committed dede082

Validação e gravação de novos posts.

Já é possível validar e gravar novos posts pelo admin.
O Admin continua sem senha..

Comments (0)

Files changed (6)

application/config/form_validation.php

             'label' => 'código de segurança',
             'rules' => 'trim|required|callback__callback_validar_captcha'
         )
+    ),
+    'admin/post' => array(
+        array(
+            'field' => 'form_post_titulo',
+            'label' => 'título',
+            'rules' => 'trim|required|max_length[60]'
+        ),
+        array(
+            'field' => 'form_post_autor',
+            'label' => 'autor',
+            'rules' => 'trim|required|max_length[20]'
+        ),
+        array(
+            'field' => 'form_post_texto',
+            'label' => 'texto',
+            'rules' => 'trim|required|min_length[10]'
+        ),
+        array(
+            'field' => 'form_post_tags',
+            'label' => 'tags',
+            'rules' => 'trim|max_length[100]'
+        )
     )
 );

application/config/routes.php

 $route['default_controller'] = "posts";
 $route['404_override'] = '';
 
+$route['post/(:any)'] = 'posts/post/$1';
+
 /* End of file routes.php */
 /* Location: ./application/config/routes.php */

application/controllers/admin/posts.php

 
         $data = array();
 
+        // Verificando se tem post
+        if ($this->input->post()) {
+            $this->_validar_gravar_post();
+        }
+
         $data['view'] = 'admin/posts/novo';
 
         $this->load->view('admin/base', $data);       
     }
+
+    /**
+     * _validar_gravar_post 
+     *
+     * Valida o form para adição de posts e grava o post no banco
+     * 
+     * @access public
+     * @return void
+     */
+    public function _validar_gravar_post()
+    {
+        $this->load->library('form_validation');
+
+        $this->form_validation->set_error_delimiters('<p class="erro_form">', '</p>');
+
+        if ($this->form_validation->run('admin/post')) {
+            // Deu certo! Vamos gravar no banco
+            $post = array(
+                'titulo'    => $this->input->post('form_post_titulo'),
+                'url'       => url_title($this->input->post('form_post_titulo'), '-', TRUE),
+                'texto'     => $this->input->post('form_post_texto'),
+                'autor'     => $this->input->post('form_post_autor'),
+                'tags'      => $this->input->post('form_post_tags'),
+                'publicacao'=> date('Y-m-d H:i:s')
+            );
+
+            $this->posts_model->gravar($post);
+
+            $this->session->set_flashdata('mensagem', 'Post inserido com sucesso!');
+
+            redirect('admin/posts');
+        }
+    }
 }

application/models/posts_model.php

 
 class Posts_model extends CI_Model
 {
+    public function gravar($post) {
+        $this->db->insert('posts', $post);
+    }
+
     public function pegar_todos()
     {
         $this->db->order_by('publicacao DESC');

application/views/posts/index.php

 <?php foreach ($posts as $p) : ?>
     <h2>
-        <a href="<?php echo site_url('posts/post/'.$p->url); ?>">
+        <a href="<?php echo site_url('post/'.$p->url); ?>">
             <?php echo $p->titulo; ?>
         </a>
     </h2>
     </p>
     <p><strong>Autor: <?php echo $p->autor; ?></strong></p>
     <div>
-        <?php echo $p->texto; ?>
+        <?php echo nl2br($p->texto); ?>
     </div>
     <p>Tags: <?php echo $p->tags; ?></p>
 <?php endforeach; ?>

application/views/posts/post.php

 </p>
 <p><strong>Autor: <?php echo $post->autor; ?></strong></p>
 <div>
-    <?php echo $post->texto; ?>
+    <?php echo nl2br($post->texto); ?>
 </div>
 <p>Tags: <?php echo $post->tags; ?></p>