Anonymous avatar Anonymous committed 9f5f167

Gravação dos comentários funcional.

Comments (0)

Files changed (4)

application/controllers/posts.php

         // Verificando se tem POST (Comentário)
         if ($this->input->post()) {
             // Validar o post e gravar
-            $this->_validar_gravar_comentario();
+            $this->_validar_gravar_comentario($data['post']);
         }
 
         $this->load->view('base', $data);
      * Valida e grava um comentário em um post
      * 
      * @access private
+     * @param $post Um objeto do post
      * @return void
      */
-    private function _validar_gravar_comentario()
+    private function _validar_gravar_comentario($post)
     {
         $this->load->library('form_validation');
+        $this->load->model('comentarios_model');
 
         $this->form_validation->set_error_delimiters('<p class="erro_form">', '</p>');
 
 
         if ($this->form_validation->run()) {
             // Validação passou
+            $comentario = array(
+                'post_id'       => $post->id,
+                'autor'         => $this->input->post('form_comentario_nome'),
+                'email_autor'   => $this->input->post('form_comentario_email_autor'),
+                'texto'         => $this->input->post('form_comentario_texto'),
+                'publicacao'    => date('Y-m-d H:i:s')
+            );
+
+            $this->comentarios_model->gravar($comentario);
+
+            $this->session->set_flashdata(
+                'mensagem',
+                'Comentário inserido com sucesso!'
+            );
+
+            redirect('posts/post/'.$post->url);
         }
     }
 }

application/models/comentarios_model.php

+<?php
+
+class Comentarios_model extends CI_Model
+{
+    public function gravar($comentario)
+    {
+        $this->db->insert('comentarios', $comentario);
+    }
+}

application/views/base.php

         <h1>Blog CI</h1>
         <hr />
 
+        <?php if ($this->session->flashdata('mensagem')) : ?>
+            <div id="mensagem">
+                <p><?php echo $this->session->flashdata('mensagem'); ?></p>
+            </div>
+        <?php endif; ?>
+
         <!-- Carregamento das views -->
         <?php $this->load->view($view); ?>
 

assets/css/base.css

+#mensagem {
+    background-color: #CCC;
+    padding: 10px;
+}
+
 form input[type=text],
 form textarea {
     display: block;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.