PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/ApplicationsModule/MailModule/presenters/ComposePresenter.php

https://code.google.com/
PHP | 315 lines | 234 code | 48 blank | 33 comment | 27 complexity | 1de24b67e6aed39c31960f77943bdae6 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * CelebrioMail Default
  4. *
  5. * @copyright Copyright (c) 2011 Celebrio Software
  6. * @package CelebrioMail
  7. */
  8. namespace ApplicationsModule\MailModule;
  9. use Nette\Environment;
  10. use Nette\Forms\Form;
  11. use Nette\Application\AppForm;
  12. use Nette\Caching\Cache;
  13. use ApplicationsModule\BasePresenter;
  14. use ApplicationsModule\PeopleModule\PeopleManagement;
  15. use \Albireo\Forms\AlbireoForm;
  16. /**
  17. * @author honza, pavel
  18. */
  19. class ComposePresenter extends BasePresenter {
  20. /** @var MailManagement */
  21. private $mailManager;
  22. /**
  23. * @return MailManagement
  24. */
  25. private function getMailManager() {
  26. return $this->mailManager;
  27. }
  28. /** @var DraftManager*/
  29. private $draftManager;
  30. /**
  31. *
  32. * @return Draft
  33. */
  34. private function getDraft() {
  35. return $this->draft;
  36. }
  37. /** @var Draft*/
  38. private $draft;
  39. private $mail_reply;
  40. /** @persistent */
  41. public $id_server;
  42. public function startup() {
  43. parent::startup();
  44. $this->mailManager = new MailManagement($this->getUser());
  45. //$this->activeMailServers = $this->getMailManager()->getMailServers(Environment::getUser()->getIdentity()->id);
  46. $this->template->app_name = _("Mail");
  47. }
  48. public function renderCompose($id_server = null, $id_mail = null, $forward = false) {
  49. $this->template->title = _("Compose mail");
  50. $draftManager = new DraftManager($this->getUser());
  51. $draft = $draftManager->get_draft_for_user();
  52. if($draft === null) {
  53. $draft = new Draft();
  54. }
  55. if($draft === null) {
  56. $draft = new Draft();
  57. }
  58. if($forward === true) {
  59. $mail = $this->getMailManager()->readMail($id_mail, $id_server);
  60. $draft->setSubject($mail["mailSubject"]);
  61. $draft->setText(html_entity_decode(strip_tags($mail["mailContents"])));
  62. }
  63. $this->draft = $draft;
  64. $this->template->composeForm = $this["composeForm"];
  65. $this->getTemplate()->left_action = _("Back to menu");
  66. $this->getTemplate()->action_link = $this->link("Default:default");
  67. if ($draft->getRecipients() && !$forward) {
  68. $peopleManager = new PeopleManagement();
  69. $this->template->people = $peopleManager->getFriends(
  70. $this->getUser()->getIdentity()->id,
  71. $draft->getRecipients());
  72. } else {
  73. $this->template->people = array();
  74. }
  75. }
  76. public function renderReply($id_server, $mail_id) {
  77. if($id_server === null) {
  78. throw new \InvalidArgumentException();
  79. }
  80. $this->template->left_action = _("Back to mail");
  81. $action_args = array("number" => $mail_id, "folder" => "INBOX", "id_server" => $id_server);
  82. $this->template->action_link = $this->link("Default:mail", $action_args);
  83. $this->id_server = $id_server;
  84. $manager = $this->getMailManager();
  85. $mail = $manager->readMail($mail_id, $id_server, "INBOX", false);
  86. $this->mail_reply = $mail;
  87. $this->template->image_from = $mail["from_image"];
  88. $this->template->reply_form = $this->getComponent("replyForm");
  89. $this->template->original_mail = html_entity_decode(strip_tags($mail["mailContents"]));
  90. }
  91. public function actionRecipients() {
  92. $peopleManager = new PeopleManagement();
  93. $draftManager = $this->getDraftManager();
  94. $this->getTemplate()->people = $peopleManager->getFriends();
  95. //TODO connect with backend
  96. $this->getTemplate()->recipients = $draftManager->get_recipients();
  97. $this->getTemplate()->title = _("Add recipients");
  98. $this->getTemplate()->left_action = _("Back to composing");
  99. $this->getTemplate()->action_link = $this->link("Compose:compose");
  100. }
  101. public function actionSelected() {
  102. $params = $this->getRequest()->getPost();
  103. $draftManager = $this->getDraftManager();
  104. if(!empty($params)) {
  105. $selected = $params["selected"]; // e.g. array(1,2,4,10)
  106. $draftManager->save_recipients($selected);
  107. } else {
  108. $draftManager->save_recipients(array());
  109. }
  110. $this->redirect("compose");
  111. }
  112. public function handleDraft() {
  113. $params = $this->getRequest()->getPost();
  114. if(isset($params["draft"])) {
  115. if($params["draft"]["id_draft"] === "") {
  116. $params["draft"]["id_draft"] = null;
  117. }
  118. if($params["draft"]["recipients"] == "") {
  119. $params["draft"]["recipients"] = array();
  120. }
  121. $this->saveDraft($params["draft"]);
  122. }
  123. }
  124. protected function createComponentReplyForm() {
  125. $mail = $this->mail_reply;
  126. $form = new AlbireoForm();
  127. $form->addHidden("to", $mail["mailReply"])->setHtmlId("reply_address");
  128. $form->addHidden("subject", $mail["mailSubject"]);
  129. $form->addTextArea("contents", null, 20, 4)
  130. ->addRule(Form::FILLED, _('Please write your message.'))
  131. ->setHtmlId("resizable_textarea");
  132. $form->addSubmit('submited', _('Send'))->htmlId = "send";
  133. $form->onSubmit[] = callback($this, 'replyFormSubmitted');
  134. $form->renderer->wrappers["control"][".submit"] = "system_app_button";
  135. return $form;
  136. }
  137. public function replyFormSubmitted($form) {
  138. $values = $form->values;
  139. $subject = $values["subject"];
  140. $already_replied = preg_match('/^Re:*/', $subject);
  141. if(!$already_replied) {
  142. $values["subject"] = "Re: " . $subject;
  143. }
  144. $result = $this->sendMail($values);
  145. if($result === TRUE) {
  146. $form->setValues(array(), true);
  147. }
  148. $this->redirect("Default:default");
  149. }
  150. protected function createComponentComposeForm() {
  151. if($this->draft === null) {
  152. $draftManager = $this->getDraftManager();
  153. $draft = $draftManager->get_draft_for_user();
  154. } else {
  155. $draft = $this->draft;
  156. }
  157. $form = new AlbireoForm();
  158. $form->addHidden("id_draft", $draft->getId())->setHtmlId("mail_id_draft");
  159. $form->addTextArea("contents", null, 20, 4)
  160. ->addRule(Form::FILLED, _('Please write your message.'))
  161. ->setDefaultValue($draft->getText())
  162. ->setHtmlId("resizable_textarea");
  163. $form->addHidden("to", "")->setHtmlId("mail_recipients_hidden");
  164. $form->addSubmit('submited', _('Send'))->htmlId = "send";
  165. $form->onSubmit[] = callback($this, 'composeSubmitted');
  166. $form->renderer->wrappers["control"][".submit"] = "system_app_button";
  167. return $form;
  168. }
  169. /**
  170. * process submitted settings
  171. * @param Form
  172. */
  173. public function composeSubmitted($form) {
  174. $values = $form->values;
  175. $this->saveDraftFromCopose($values);
  176. $values["to"] = $this->compareAndGetFriendMails($values["to"]);
  177. $values["contents"] = $this->appendCelebrioGreetings($values["contents"]);
  178. $result = $this->sendMail($values);
  179. if($result === TRUE) {
  180. $this->getDraftManager()->delete_draft_for_user();
  181. $form->setValues(array(), true);
  182. }
  183. }
  184. private function saveDraftFromCopose($values) {
  185. $values["recipients"] = $this->removeNonNumbers($values["to"]);
  186. //IMPORTANT: this blocks subject. When implement subject to mail this should be replaced
  187. $values["subject"] = "null";
  188. $values["text"] = $values["contents"];
  189. unset($values["to"]);
  190. unset($values["contents"]);
  191. $this->saveDraft($values);
  192. }
  193. private function saveDraft($draft) {
  194. $draftManager = $this->getDraftManager();
  195. if($draft != null) {
  196. $draftManager->save_draft($draft);
  197. }
  198. }
  199. /**
  200. * sends an e-mail
  201. * @param array $values
  202. * @return Mixed returns boolean or an array of undelivered e-mails
  203. */
  204. private function sendMail($values) {
  205. // TODO: add support for other mail accounts
  206. $manager = new MailManagement($this->getUser());
  207. $servers = $manager->getActiveMailServers();
  208. $mailer = new MailSender(reset($servers));
  209. if (!$mailer->connect()) {
  210. $this->template->composeStatus = _("Celebrio cannot connect to SMTP server.");
  211. return FALSE;
  212. }
  213. $result = $mailer->sendMail($values);
  214. if (is_array($result)) {
  215. $this->template->composeStatus = _("E-mail could not be delivered to one or more recipients:");
  216. foreach ($result as $mail) {
  217. $this->template->composeStatus += " $mail,";
  218. }
  219. $this->template->composeStatus = substr($this->template->composeStatus, -1);
  220. return FALSE;
  221. } elseif ($result == TRUE) {
  222. $this->template->composeStatus = _("E-mail sent.");
  223. return TRUE;
  224. } else {
  225. $this->template->composeStatus = _("An error occurred and your e-mail could not be delivered.");
  226. return FALSE;
  227. }
  228. }
  229. private function compareAndGetFriendMails($values) {
  230. $id_recipients = $this->removeNonNumbers($values);
  231. $peopleManager = new PeopleManagement();
  232. $friends = $peopleManager->getFriends(
  233. $this->getUser()->getIdentity()->id,
  234. $id_recipients);
  235. return implode(",", $this->getMailAdresses($friends));
  236. }
  237. private function getMailAdresses($friends) {
  238. $emails = array();
  239. foreach($friends as $entry) {
  240. $mailArray = $entry->getEmails();
  241. if(isset($mailArray[0])) {
  242. $emails[] = $mailArray[0];
  243. }
  244. }
  245. return $emails;
  246. }
  247. private function removeNonNumbers($values) {
  248. $id_recipients = \explode(",", $values);
  249. $result = array();
  250. foreach($id_recipients as $value) {
  251. if(is_numeric($value)) {
  252. $result[] = (int) $value;
  253. }
  254. }
  255. return $result;
  256. }
  257. private function getDraftManager() {
  258. if ($this->draftManager === NULL) {
  259. $this->draftManager = new DraftManager($this->getUser());
  260. }
  261. return $this->draftManager;
  262. }
  263. private function appendCelebrioGreetings($content) {
  264. $content .= "\n\n" . _("Sent from my Celebrio");
  265. return $content;
  266. }
  267. }