<?php

include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
include_once( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" );
include_once( PLOG_CLASS_PATH."class/data/validator/httpurlvalidator.class.php" );
include_once( PLOG_CLASS_PATH."class/data/validator/usernamevalidator.class.php" );
include_once( PLOG_CLASS_PATH."plugins/editcomments/class/view/admineditcommentview.class.php" );
include_once( PLOG_CLASS_PATH."plugins/editcomments/class/view/admineditcommentsview.class.php" );
include_once( PLOG_CLASS_PATH."class/view/admin/adminpostslistview.class.php" );

class AdminUpdateCommentAction extends AdminAction{
    var $_commentId;
    var $_articleId;
	var $_op;
    
    function AdminUpdateCommentAction($actionInfo, $request){
        $this->AdminAction($actionInfo, $request);

        $this->_commentId = $this->_request->getValue("commentId");
        $this->_articleId = $this->_request->getValue("articleId");
        $this->_op = $actionInfo->getActionParamValue();
        if($this->_request->getValue("cancel")){
            $this->_op = "cancel";
        }
        
        if($this->_op == "updateComment"){
            $view = new AdminEditCommentView($this->_blogInfo, $commentId);

            $this->registerFieldValidator("commentId", new IntegerValidator());
            $this->registerFieldValidator("commentText", new StringValidator());
            $this->registerFieldValidator("authorName", new StringValidator());
            $this->registerFieldValidator("commentStatus", new IntegerValidator());
                // no validation??  
            $this->registerField("commentTopic");
            $this->registerField("authorEmail");
            $this->registerField("authorUrl");
            $this->registerField("commentDateTime");
            $this->registerField("commentIp");
            
            $view->setErrorMessage( $this->_locale->tr("invalid_data"));
            $this->setValidationErrorView( $view );
        }
    }

    function _updateComment($id, $topic, $text, $user_name, $user_email,
                            $user_url, $client_ip, $status, $date){
        if(!ArticleCommentStatus::isValidStatus($status))
            return false;

        $prefix = Db::getPrefix();
        $filter = new TextFilter();
        $db =& Db::getDb();
        
        $query = "UPDATE ".$prefix."articles_comments ";
        $query .= "set topic='".Db::qstr($topic)."', ";
        $query .= "text='".Db::qstr($text)."', ";
        $query .= "user_name='".Db::qstr($user_name)."', ";
        $query .= "user_email='".Db::qstr($user_email)."', ";
        $query .= "user_url='".Db::qstr($user_url)."', ";
        $query .= "client_ip='".$client_ip."', ";
        $query .= "status=".$status.", ";
        $query .= "date='".$date->getTimestamp()."', ";
        $query .= "normalized_text='".Db::qstr($filter->normalizeText($text))."', ";
        $query .= "normalized_topic='".Db::qstr($filter->normalizeText($topic))."' ";
        $query .= "WHERE id=".$id;
//        print $query;
        return($db->Execute( $query ));

        return false;
    }

        // copied from AdminPostManagementCommon class
    function _fetchCommentDateInformation(){
        $commentDateTime = $this->_request->getValue("commentDateTime");
        $dateTimeParts = explode(" ", $commentDateTime);
        $dateParts = explode("/", $dateTimeParts[0] );
        $timeParts = explode(":",$dateTimeParts[1] );
        
        $postTimestamp = new Timestamp();
        $postTimestamp->setMinutes($timeParts[1]);
        $postTimestamp->setHour($timeParts[0]);
        $postTimestamp->setDay($dateParts[0]);
        $postTimestamp->setMonth($dateParts[1]);
        $postTimestamp->setYear($dateParts[2]);

        return $postTimestamp;
    }

    
    function perform(){
        if($this->_op == "updateComment"){
            $topic = $this->_request->getValue("commentTopic");
            $text = $this->_request->getValue("commentText");
            $name = $this->_request->getValue("authorName");
            $email = $this->_request->getValue("authorEmail");
            $url = $this->_request->getValue("authorUrl");
            $dateTime = $this->_fetchCommentDateInformation();
            $status = $this->_request->getValue("commentStatus");
            $ip = $this->_request->getValue("commentIp");
            
            if($this->_updateComment($this->_commentId, $topic, $text, $name, $email,
                                     $url, $ip, $status, $dateTime)){
                if($this->_articleId){
                    $articles = new Articles();
                    $article = $articles->getBlogArticle($this->_articleId,
                                                         $this->_blogInfo->getId());
                    $this->_view = new AdminEditCommentsView($this->_blogInfo,
                                                             array("article" => $article));
                }
                else{
                    $this->_view = new AdminPostsListView( $this->_blogInfo );
                }
                $this->_view->setSuccessMessage( $this->_locale->tr("edit_comment_ok") );
                CacheControl::resetBlogCache( $this->_blogInfo->getId());
            }
            else{
                $this->_view = new AdminEditCommentView($this->_blogInfo, $id);
                $this->_view->setErrorMessage( $this->_locale->tr("edit_comment_failed") );
            }
        }
        else if($this->_articleId){
			$articles = new Articles();
			$article = $articles->getBlogArticle( $this->_articleId, $this->_blogInfo->getId());

            $this->_view = new AdminEditCommentsView($this->_blogInfo, array("article" => $article));
            $this->_view->setSuccessMessage( $this->_locale->tr("edit_comment_cancelled") );
        }
        else{
            $this->_view = new AdminPostsListView($this->_blogInfo);
        }
        
        
        $this->setCommonData();
    }
}
?>