<?php
/**
 * Bbs 電子掲示板 モジュール
 * 
 */
require_once 'Zend/Controller/Action.php';

class BBSController extends Zend_Controller_Action
{
    public $admin_email;
    
    public function indexAction()
    {
        $this->listAction();
    }
    
    public function noRouteAction()
    {
        //$this->_redirect('/');
        echo "noRouteAction";
    }
    
    public function listAction()
    {
        global $db;
        
        ob_start();
        
        echo '<h2>BBS</h2>' . "\n";
        
        echo '<form method="post" action="' . BASE_URL . 'Bbs/add/' . '">' . "\n";
        echo '<table>';
        echo '<tr><td>お名前:</td>' . "\n";
        echo '<td><input type="text" name="name" size="60" />' . "</td></tr>\n";
        echo '<tr><td>Eメールアドレス:' . "</td>\n";
        echo '<td><input type="text" name="email" size="60" />' . "</td></tr>\n";
        echo '<tr><td>タイトル:</td>' . "\n";
        echo '<td><input type="text" name="title" size="60" />' . "</td></tr>\n";
        echo '<tr><td>メッセージ:</td>' . "\n";
        echo '<td><textarea name="description" cols="40" rows="4"></textarea>' . "</td></tr>\n";
        echo '<tr><td>削除用パスワード:</td>' . "\n";
        echo '<td><input type="text" name="delkey" size="10" />' . "</td></tr>\n";

        echo '</table>';
        echo '<input type="submit" value="投稿する" />' . "\n";
        echo '</form>' . "\n";
        
        $sql = "SELECT title, description, name,created FROM Platy_Bbs ORDER BY created DESC";
        $rows = $db->fetchAll($sql);
        foreach ($rows as $row) {
            echo '<div class="content">' . "\n";
            echo '<div class="title">' . "\n";
            echo $row['title'];
            echo '</div>';
            echo $row['description'];
            echo '<div class="footer">' . "\n";
            echo 'by&nbsp;' . $row['name'] . '&nbsp;' . $row['created'];
            echo '</div>';
            echo "</div>\n";
        }
        
        $contents = ob_get_contents();
        ob_end_clean();
        
        Zend_Loader::loadClass('Zend_View');
        $view = new Zend_View();
        $view->setScriptPath(APP_PATH . 'views');
        $view->contents = $contents;
        echo $view->render('Bbs.php');
    }
    
    /**
     * メッセージの投稿
     */
    public function addAction()
    {
        global $db;
        
        $name = h(e($_POST['name']));
        $email = h(e($_POST['email']));
        $title = h(e($_POST['title']));
        $description = h(e($_POST['description']));
        $delkey = h(e($_POST['delkey']));
        $created = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO Platy_Bbs(title, description, name, email, delkey, created) VALUES("
             . "'{$title}','{$description}','{$name}','{$email}','{$delkey}','{$created}');";
        $db->query($sql);
        
        $this->_redirect('Bbs/');
    }
}
?>