/app/ApplicationsModule/MailModule/presenters/ComposePresenter.php
PHP | 315 lines | 234 code | 48 blank | 33 comment | 27 complexity | 1de24b67e6aed39c31960f77943bdae6 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
- <?php
- /**
- * CelebrioMail Default
- *
- * @copyright Copyright (c) 2011 Celebrio Software
- * @package CelebrioMail
- */
- namespace ApplicationsModule\MailModule;
- use Nette\Environment;
- use Nette\Forms\Form;
- use Nette\Application\AppForm;
- use Nette\Caching\Cache;
- use ApplicationsModule\BasePresenter;
- use ApplicationsModule\PeopleModule\PeopleManagement;
- use \Albireo\Forms\AlbireoForm;
- /**
- * @author honza, pavel
- */
- class ComposePresenter extends BasePresenter {
-
- /** @var MailManagement */
- private $mailManager;
- /**
- * @return MailManagement
- */
- private function getMailManager() {
- return $this->mailManager;
- }
-
- /** @var DraftManager*/
- private $draftManager;
-
- /**
- *
- * @return Draft
- */
- private function getDraft() {
- return $this->draft;
- }
-
- /** @var Draft*/
- private $draft;
-
- private $mail_reply;
-
- /** @persistent */
- public $id_server;
-
-
- public function startup() {
- parent::startup();
- $this->mailManager = new MailManagement($this->getUser());
- //$this->activeMailServers = $this->getMailManager()->getMailServers(Environment::getUser()->getIdentity()->id);
- $this->template->app_name = _("Mail");
- }
-
-
- public function renderCompose($id_server = null, $id_mail = null, $forward = false) {
- $this->template->title = _("Compose mail");
-
- $draftManager = new DraftManager($this->getUser());
- $draft = $draftManager->get_draft_for_user();
-
- if($draft === null) {
- $draft = new Draft();
- }
-
- if($draft === null) {
- $draft = new Draft();
- }
- if($forward === true) {
- $mail = $this->getMailManager()->readMail($id_mail, $id_server);
- $draft->setSubject($mail["mailSubject"]);
- $draft->setText(html_entity_decode(strip_tags($mail["mailContents"])));
- }
-
- $this->draft = $draft;
-
- $this->template->composeForm = $this["composeForm"];
- $this->getTemplate()->left_action = _("Back to menu");
- $this->getTemplate()->action_link = $this->link("Default:default");
- if ($draft->getRecipients() && !$forward) {
- $peopleManager = new PeopleManagement();
- $this->template->people = $peopleManager->getFriends(
- $this->getUser()->getIdentity()->id,
- $draft->getRecipients());
- } else {
- $this->template->people = array();
- }
- }
-
- public function renderReply($id_server, $mail_id) {
- if($id_server === null) {
- throw new \InvalidArgumentException();
- }
- $this->template->left_action = _("Back to mail");
- $action_args = array("number" => $mail_id, "folder" => "INBOX", "id_server" => $id_server);
- $this->template->action_link = $this->link("Default:mail", $action_args);
- $this->id_server = $id_server;
- $manager = $this->getMailManager();
- $mail = $manager->readMail($mail_id, $id_server, "INBOX", false);
- $this->mail_reply = $mail;
- $this->template->image_from = $mail["from_image"];
- $this->template->reply_form = $this->getComponent("replyForm");
- $this->template->original_mail = html_entity_decode(strip_tags($mail["mailContents"]));
- }
- public function actionRecipients() {
- $peopleManager = new PeopleManagement();
- $draftManager = $this->getDraftManager();
- $this->getTemplate()->people = $peopleManager->getFriends();
- //TODO connect with backend
- $this->getTemplate()->recipients = $draftManager->get_recipients();
- $this->getTemplate()->title = _("Add recipients");
- $this->getTemplate()->left_action = _("Back to composing");
- $this->getTemplate()->action_link = $this->link("Compose:compose");
- }
-
- public function actionSelected() {
- $params = $this->getRequest()->getPost();
- $draftManager = $this->getDraftManager();
- if(!empty($params)) {
- $selected = $params["selected"]; // e.g. array(1,2,4,10)
- $draftManager->save_recipients($selected);
- } else {
- $draftManager->save_recipients(array());
- }
- $this->redirect("compose");
- }
- public function handleDraft() {
- $params = $this->getRequest()->getPost();
- if(isset($params["draft"])) {
- if($params["draft"]["id_draft"] === "") {
- $params["draft"]["id_draft"] = null;
- }
- if($params["draft"]["recipients"] == "") {
- $params["draft"]["recipients"] = array();
- }
- $this->saveDraft($params["draft"]);
- }
- }
- protected function createComponentReplyForm() {
- $mail = $this->mail_reply;
- $form = new AlbireoForm();
- $form->addHidden("to", $mail["mailReply"])->setHtmlId("reply_address");
- $form->addHidden("subject", $mail["mailSubject"]);
- $form->addTextArea("contents", null, 20, 4)
- ->addRule(Form::FILLED, _('Please write your message.'))
- ->setHtmlId("resizable_textarea");
- $form->addSubmit('submited', _('Send'))->htmlId = "send";
- $form->onSubmit[] = callback($this, 'replyFormSubmitted');
- $form->renderer->wrappers["control"][".submit"] = "system_app_button";
- return $form;
- }
-
- public function replyFormSubmitted($form) {
- $values = $form->values;
- $subject = $values["subject"];
- $already_replied = preg_match('/^Re:*/', $subject);
- if(!$already_replied) {
- $values["subject"] = "Re: " . $subject;
- }
- $result = $this->sendMail($values);
- if($result === TRUE) {
- $form->setValues(array(), true);
- }
- $this->redirect("Default:default");
- }
- protected function createComponentComposeForm() {
- if($this->draft === null) {
- $draftManager = $this->getDraftManager();
- $draft = $draftManager->get_draft_for_user();
- } else {
- $draft = $this->draft;
- }
- $form = new AlbireoForm();
- $form->addHidden("id_draft", $draft->getId())->setHtmlId("mail_id_draft");
- $form->addTextArea("contents", null, 20, 4)
- ->addRule(Form::FILLED, _('Please write your message.'))
- ->setDefaultValue($draft->getText())
- ->setHtmlId("resizable_textarea");
-
- $form->addHidden("to", "")->setHtmlId("mail_recipients_hidden");
- $form->addSubmit('submited', _('Send'))->htmlId = "send";
- $form->onSubmit[] = callback($this, 'composeSubmitted');
- $form->renderer->wrappers["control"][".submit"] = "system_app_button";
- return $form;
- }
-
- /**
- * process submitted settings
- * @param Form
- */
- public function composeSubmitted($form) {
- $values = $form->values;
- $this->saveDraftFromCopose($values);
- $values["to"] = $this->compareAndGetFriendMails($values["to"]);
- $values["contents"] = $this->appendCelebrioGreetings($values["contents"]);
- $result = $this->sendMail($values);
- if($result === TRUE) {
- $this->getDraftManager()->delete_draft_for_user();
- $form->setValues(array(), true);
- }
- }
-
-
- private function saveDraftFromCopose($values) {
- $values["recipients"] = $this->removeNonNumbers($values["to"]);
- //IMPORTANT: this blocks subject. When implement subject to mail this should be replaced
- $values["subject"] = "null";
- $values["text"] = $values["contents"];
- unset($values["to"]);
- unset($values["contents"]);
- $this->saveDraft($values);
- }
-
- private function saveDraft($draft) {
- $draftManager = $this->getDraftManager();
- if($draft != null) {
- $draftManager->save_draft($draft);
- }
- }
-
- /**
- * sends an e-mail
- * @param array $values
- * @return Mixed returns boolean or an array of undelivered e-mails
- */
- private function sendMail($values) {
- // TODO: add support for other mail accounts
- $manager = new MailManagement($this->getUser());
- $servers = $manager->getActiveMailServers();
- $mailer = new MailSender(reset($servers));
- if (!$mailer->connect()) {
- $this->template->composeStatus = _("Celebrio cannot connect to SMTP server.");
- return FALSE;
- }
- $result = $mailer->sendMail($values);
- if (is_array($result)) {
- $this->template->composeStatus = _("E-mail could not be delivered to one or more recipients:");
- foreach ($result as $mail) {
- $this->template->composeStatus += " $mail,";
- }
- $this->template->composeStatus = substr($this->template->composeStatus, -1);
- return FALSE;
- } elseif ($result == TRUE) {
- $this->template->composeStatus = _("E-mail sent.");
- return TRUE;
- } else {
- $this->template->composeStatus = _("An error occurred and your e-mail could not be delivered.");
- return FALSE;
- }
- }
-
- private function compareAndGetFriendMails($values) {
- $id_recipients = $this->removeNonNumbers($values);
- $peopleManager = new PeopleManagement();
- $friends = $peopleManager->getFriends(
- $this->getUser()->getIdentity()->id,
- $id_recipients);
- return implode(",", $this->getMailAdresses($friends));
- }
-
- private function getMailAdresses($friends) {
- $emails = array();
- foreach($friends as $entry) {
- $mailArray = $entry->getEmails();
- if(isset($mailArray[0])) {
- $emails[] = $mailArray[0];
- }
- }
-
- return $emails;
- }
-
- private function removeNonNumbers($values) {
- $id_recipients = \explode(",", $values);
- $result = array();
- foreach($id_recipients as $value) {
- if(is_numeric($value)) {
- $result[] = (int) $value;
- }
- }
- return $result;
- }
- private function getDraftManager() {
- if ($this->draftManager === NULL) {
- $this->draftManager = new DraftManager($this->getUser());
- }
-
- return $this->draftManager;
- }
-
- private function appendCelebrioGreetings($content) {
- $content .= "\n\n" . _("Sent from my Celebrio");
- return $content;
- }
- }