<?php
/**
 * library/PhwittrStatus.php
 *
 * @author  riaf<riaf@nequal.jp>
 * @package Phwittr
 * @version $Id$
 */

Rhaco::import('Phwittr');

class PhwittrStatus extends Phwittr
{
    function status_detail($statusId){
        $status = $this->dbUtil->get(new Status($statusId), new C(Q::fact()));
        if(Variable::istype('TableObjectBase', $status)){
            $user = $status->factUserId;
            if($status->factUserId->hasPermission($this->dbUtil, RequestLogin::getLoginSession())){
                $parser = new HtmlParser(Rhaco::constant('THEME'). '/status_detail.html');
                $parser->setVariable('object', $status);
                $parser->setVariable('user', $user);
                $parser->setVariable('following', $this->_getFollowing($user));
                if(RequestLogin::isLogin()){
                    $u = RequestLogin::getLoginSession();
                    if($u->getId() != $user->getId()) $parser->setVariable('isFollowing', $this->isFollowing($u->getId(), $user->getId()));
                }
                return $parser;
            } else {
                return $this->_protected($user);
            }
        }
        return $this->_notFound();
    }
    
    function status_update(){
        $user = $this->loginRequired();
        if($this->isPost()){
            $this->clearVariable('createdAt');
            $this->setVariable('userId', $user->getId());
            $status = $this->dbUtil->insert($this->toObject(new Status()));
        }
        switch($this->getVariable('type', 'html')){
            case 'json':
                if(Variable::istype('TableObjectBase', $status)){
                    // success
                    $replyUserName = $status->getReplyUserName();
                    $status->comment = PhwittrFormatter::c($status->comment);
                    $status->createdAt = TemplateFormatter::dateformat($status->createdAt);
                    $status->replyTo = ($status->replyUserId) ? sprintf(' in reply to <a href="%s">%s</a>', Rhaco::url($replyUserName), $replyUserName) : '';
                    $response = array(
                        'error' => 0,
                        'message' => 'status updated!',
                        'status' => $status,
                        'user' => $user,
                    );
                } else {
                    // fail
                    $error = array_shift(ExceptionTrigger::get('Status_comment'));
                    $response = array(
                        'error' => 1,
                        'message' => $error->getMessage(),
                    );
                }
                $response = V::toJson($response);
                header('Content-type: application/json; charset=UTF-8');
                header(sprintf("X-JSON: (%s)", $response));
                echo $response;
                break;
            default:
                if(Variable::istype('TableObjectBase', $status))
                    $this->setSession('notice', 'status updated!');
                Header::redirect(Rhaco::url('home'));
        }
        Rhaco::end();
    }
}
