<?php
/**
 * 管理者ログイン
 */
require_once 'Zend/Controller/Action.php';


require_once 'Platy/Member.php';

class Manage_LoginController extends Zend_Controller_Action
{
    public function indexAction()
    {
        global $db, $Me, $session;
        
        $email = '';
        $password = '';
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $email = h($_POST['email']);
            $password = h($_POST['password']);
            $hashed_password = sha1($password);
            $sql = "SELECT id, email FROM Platy_Member"
                 . " WHERE email=" . $db->quote($email) . " AND password=" . $db->quote($hashed_password);
            $row = $db->fetchRow($sql);
            if (is_array($row)) {
                // 最終ログイン日時を更新する
                $last_login_date = date('Y-m-d H:i:s');
                $sql = "UPDATE Platy_Member SET last_login='{$last_login_date}' WHERE id={$row['id']}";
                $db->query($sql);
                $Me = new Platy_Member($row['id']);
                $session->Me = $Me;
                $this->_redirect('/Manage/');
            }
        }
        
        Zend_Loader::loadClass('Zend_View');
        $view = new Zend_View();
        $view->setScriptPath(APP_PATH . 'Manage/views');
        $view->email = $email;
        $view->password = $password;
        echo $view->render('login.php');
    }

    public function noRouteAction()
    {
        //$this->_redirect('/');
        echo "noRouteAction";
    }
}
?>