Beispiel #1
0
 /**
  * Saves a comment and sends an email
  *
  * @param array $data
  * @return boolean
  **/
 public function save($data = array())
 {
     $data['comment'] = nl2br($data['comment']);
     $this->form->setData($data);
     if ($this->form->isValid()) {
         $comment = $this->form->getData();
         $comment->setDateAdded(new DateTime());
         $comment->setPost($this->post);
         $comment->setStatus(1);
         $comment->setIpAddress($_SERVER['REMOTE_ADDR']);
         //For display purposes
         if ($comment->getParentId()) {
             foreach ($this->post->getComments() as $oldComment) {
                 if ($oldComment->getId() == $comment->getParentId()) {
                     if ($oldComment->getParentId()) {
                         $comment->setParentId($oldComment->getParentId());
                     }
                     break;
                 }
             }
         }
         $this->em->persist($comment);
         $this->em->flush();
         $this->post->addComment($comment);
         $this->form->setData(array('name' => null, 'website' => null, 'email' => null, 'comment' => null, 'parent' => array('id' => 0)));
         $message = strip_tags($comment->getComment()) . "\r\n\r\n";
         $message .= $comment->getEmail() . "\r\n\r\n";
         $message .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
         $mail = new Message();
         $mail->setBody($message);
         $mail->setFrom('*****@*****.**');
         $mail->addTo('*****@*****.**', 'Rob Keplin');
         $mail->setSubject('New comment on robkeplin.com');
         $transport = new Sendmail();
         try {
             $transport->send($mail);
         } catch (RuntimeException $e) {
             $this->addMessage('A notification email was not sent due to sendmail failing. Doh!', self::MSG_ERROR);
         }
         $this->addMessage('Thanks for commenting.', self::MSG_NOTICE);
         return true;
     }
     $this->addMessage('Hey, Wait!  Something went wrong down there.  Please try commenting again, it did not go through.', self::MSG_ERROR);
     return false;
 }