<commit_before><?php

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $config = Zend_Registry::get('config');
        $bookName = $this->_getParam('book', $config->defaultBook);
        $pageName = $this->_getParam('page');
        
        $book = new HumanHelp_Model_Book($bookName);
        
        if ($pageName) {
            $page = $book->getPage($pageName);
        } else {
            $page = $book->getDefaultPage();
            $pageName = $page->getName();
        }
        
        $commentForm = new HumanHelp_Form_Comment(array(
            'action' => '#post-comment'
        ));
        
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($commentForm->isValid($formData)) {
                
                $comment = new HumanHelp_Model_Comment(array(
                    'author_name'  => $commentForm->getValue('name'),
                    'author_email' => $commentForm->getValue('email'),
                    'created_at'   => $_SERVER['REQUEST_TIME'],
                    'book'         => $bookName,
                    'page'         => $pageName,
                    'comment'      => $commentForm->getValue('comment'),
                    'token'        => HumanHelp_Model_Comment::generateToken(),
                ));
                
                if (! $config->moderateComments) {
                    $comment->setFlags(HumanHelp_Model_Comment::FLAG_APPROVED);
                }
                
                try {
                    $comment->save();
                    $this->_sendNewCommentEmail($comment, $page);
                    
                    // Redirect back to page
                    $this->_redirect("/$bookName/$pageName?done=ok#");
                } catch (Exception $ex) {
                    throw $ex;
                }
            }
        }
        
        $this->view->book = $book; 
        $this->view->page = $page;
        $this->view->commentForm = $commentForm;
        $this->view->commentsAreModerated = $config->moderateComments;  
    }
    
    public function _sendNewCommentEmail(HumanHelp_Model_Comment $comment, HumanHelp_Model_Page $page)
    {
        $config = Zend_Registry::get('config');
        
        $emailTemplate = new Zend_View();
        $emailTemplate->setScriptPath(APPLICATION_PATH . '/views/emails');
        $emailTemplate->addHelperPath(APPLICATION_PATH . '/views/helpers', 'HumanHelp_View_Helper_');
        $emailTemplate->setEncoding('UTF-8');
        
        $emailTemplate->comment = $comment;
        $emailTemplate->page = $page;
        $emailTemplate->baseUrl = 'http://' . $_SERVER['HTTP_HOST'] . $this->view->baseUrl;
        
        $bodyHtml = $emailTemplate->render('newComment.phtml');
        $bodyText = $emailTemplate->render('newComment.ptxt');
         
        $mail = new Zend_Mail();
        $mail->setType(Zend_Mime::MULTIPART_ALTERNATIVE)
             ->setBodyHtml($bodyHtml, 'UTF-8')
             ->setBodyText($bodyText, 'UTF-8')
             ->setSubject("New comment on '{$page->getTitle()}' in '{$page->getBook()->getTitle()}'")<commit_msg>minor: adding some TODO comment<commit_after><?php

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $config = Zend_Registry::get('config');
        $bookName = $this->_getParam('book', $config->defaultBook);
        $pageName = $this->_getParam('page');
        
        $book = new HumanHelp_Model_Book($bookName);
        
        if ($pageName) {
            $page = $book->getPage($pageName);
        } else {
            $page = $book->getDefaultPage();
            $pageName = $page->getName();
        }
        
        $commentForm = new HumanHelp_Form_Comment(array(
            'action' => '#post-comment'
        ));
        
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($commentForm->isValid($formData)) {
                
                $comment = new HumanHelp_Model_Comment(array(
                    'author_name'  => $commentForm->getValue('name'),
                    'author_email' => $commentForm->getValue('email'),
                    'created_at'   => $_SERVER['REQUEST_TIME'],
                    'book'         => $bookName,
                    'page'         => $pageName,
                    'comment'      => $commentForm->getValue('comment'),
                    'token'        => HumanHelp_Model_Comment::generateToken(),
                ));
                
                if (! $config->moderateComments) {
                    $comment->setFlags(HumanHelp_Model_Comment::FLAG_APPROVED);
                }
                
                try {
                    $comment->save();
                    $this->_sendNewCommentEmail($comment, $page);
                    
                    // Redirect back to page
                    $this->_redirect("/$bookName/$pageName?done=ok#");
                } catch (Exception $ex) {
                    throw $ex;
                }
            }
        }
        
        $this->view->book = $book; 
        $this->view->page = $page;
        $this->view->commentForm = $commentForm;
        $this->view->commentsAreModerated = $config->moderateComments;  
    }
    
    /**
     * Send email notification to moderators when a new comment is posted
     * 
     * @todo move logic to model / library class
     * 
     * @param HumanHelp_Model_Comment $comment
     * @param HumanHelp_Model_Page $page
     */
    public function _sendNewCommentEmail(HumanHelp_Model_Comment $comment, HumanHelp_Model_Page $page)
    {
        $config = Zend_Registry::get('config');
        
        $emailTemplate = new Zend_View();
        $emailTemplate->setScriptPath(APPLICATION_PATH . '/views/emails');
        $emailTemplate->addHelperPath(APPLICATION_PATH . '/views/helpers', 'HumanHelp_View_Helper_');
        $emailTemplate->setEncoding('UTF-8');
        
        $emailTemplate->comment = $comment;
        $emailTemplate->page = $page;
        $emailTemplate->baseUrl = 'http://' . $_SERVER['HTTP_HOST'] . $this->view->baseUrl;
        
        $bodyHtml = $emailTemplate->render('newComment.phtml');
        $bodyText = $emailTemplate->render('newComment.ptxt');
         
        $mail = new Zend_Mail();
        $mail->setType(Zend_Mime::MULTIPART_ALTERNATIVE)
             ->setBodyHtml($bodyHtml, 'UTF-8')
             ->setBodyText($bodyText, 'UTF-8')
             ->setSubject("New comment on '{$page->getTitle()}' in '{$page->getBook()->getTitle()}'")
{'fraction_non_alphanumeric': 0.14107384569108136, 'fraction_numerical': 0.0009023913370431644, 'mean_word_length': 1.750206782464847, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6709021', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1755, 'n_words': 273}