/**
  * @return Bengine_Admin_Controller_Globalmail
  */
 protected function send()
 {
     $subject = Str::validateXHTML($this->getParam("subject"));
     $message = richtext(nl2br($this->getParam("message")));
     if (Str::length($message) < 10) {
         Core::getTPL()->assign("messageError", Logger::getMessageField("MESSAGE_TOO_SHORT"));
         $error = true;
     }
     if (Str::length($subject) == 0) {
         Core::getTPL()->assign("subjectError", Logger::getMessageField("SUBJECT_TOO_SHORT"));
         $error = true;
     }
     if (!empty($error)) {
         return $this;
     }
     $sql = "INSERT INTO `" . PREFIX . "message` (`mode`, `time`, `sender`, `receiver`, `subject`, `message`, `read`) SELECT ?, ?, NULL, " . PREFIX . "user.userid, ?, ?, ? FROM " . PREFIX . "user";
     Core::getDB()->query($sql, array(1, TIME, $subject, $message, 0));
     return $this;
 }
 /**
  * Allows the user to write a global mail to all alliance member.
  *
  * @param string $reply
  * @return Bengine_Game_Controller_Alliance
  */
 protected function globalMailAction($reply)
 {
     $result = Core::getQuery()->select("user2ally u2a", array("a.founder", "ar.CAN_WRITE_GLOBAL_MAILS"), "LEFT JOIN " . PREFIX . "alliance a ON (a.aid = u2a.aid) LEFT JOIN " . PREFIX . "allyrank ar ON (ar.rankid = u2a.rank)", Core::getDB()->quoteInto("u2a.userid = ?", Core::getUser()->get("userid")));
     if ($row = $result->fetchRow()) {
         $result->closeCursor();
         if ($row["CAN_WRITE_GLOBAL_MAILS"] || $row["founder"] == Core::getUser()->get("userid")) {
             Core::getLanguage()->load("Message");
             if ($this->isPost()) {
                 $message = richtext($this->getParam("message"));
                 $length = Str::length(strip_tags($message));
                 $subject = Str::validateXHTML(trim($this->getParam("subject")));
                 $receiver = $this->getParam("receiver");
                 if ($length > 2 && $length <= Core::getOptions()->get("MAX_PM_LENGTH") && Str::length($subject) > 0 && Str::length($subject) < 101) {
                     Hook::event("SendGlobalMail", array($subject, &$message));
                     if ($receiver == "foo") {
                         $where = Core::getDB()->quoteInto("aid = ?", $this->aid);
                     } else {
                         $where = Core::getDB()->quoteInto("rank = ? AND aid = ?", array($receiver, $this->aid));
                     }
                     $_result = Core::getQuery()->select("user2ally", "userid", "", $where);
                     foreach ($_result->fetchAll() as $_row) {
                         Core::getQuery()->insert("message", array("mode" => 6, "time" => TIME, "sender" => Core::getUser()->get("userid"), "receiver" => $_row["userid"], "message" => $message, "subject" => $subject, "read" => $_row["userid"] == Core::getUser()->get("userid") ? 1 : 0));
                     }
                     $_result->closeCursor();
                     Logger::addMessage("SENT_SUCCESSFUL", "success");
                 } else {
                     if ($length < 3) {
                         Core::getTPL()->assign("messageError", Logger::getMessageField("MESSAGE_TOO_SHORT"));
                     }
                     if ($length > Core::getOptions()->get("MAX_PM_LENGTH")) {
                         Core::getTPL()->assign("messageError", Logger::getMessageField("MESSAGE_TOO_LONG"));
                     }
                     if (Str::length($subject) == 0) {
                         Core::getTPL()->assign("subjectError", Logger::getMessageField("SUBJECT_TOO_SHORT"));
                     }
                     if (Str::length($subject) > 100) {
                         Core::getTPL()->assign("subjectError", Logger::getMessageField("SUBJECT_TOO_LONG"));
                     }
                     Core::getTPL()->assign("subject", $this->getParam("subject"))->assign("message", $this->getParam("message"));
                 }
             } else {
                 if ($reply) {
                     $reply = preg_replace("#((RE|FW):\\s)+#is", "\\1", $reply);
                     Core::getTPL()->assign("subject", $reply);
                 }
             }
             $ranks = Core::getQuery()->select("allyrank", array("rankid", "name"), "", Core::getDB()->quoteInto("aid = ?", $this->aid));
             Core::getTPL()->assign("maxpmlength", fNumber(Core::getOptions()->get("MAX_PM_LENGTH")));
             Core::getTPL()->addLoop("ranks", $ranks);
         } else {
             Logger::dieMessage("MISSING_RIGHTS_FOR_GLOBAL_MAIL", "warning");
         }
     } else {
         Logger::dieMessage("MISSING_RIGHTS_FOR_GLOBAL_MAIL", "warning");
     }
     return $this;
 }