1: <?php
2: namespace Budovy;
3: use Nette\Utils\Validators;
4: use Nette\Mail\Message;
5:
6: class EmailPresenter extends BasePresenter {
7:
8: public $scheduledTaskRepository;
9:
10:
11: public $periodicTaskRepository;
12:
13:
14: public $userRepository;
15:
16:
17: public $contactBindingRepository;
18:
19:
20: public $mailer;
21:
22: function actionEmail($id) {
23: $emails = $this->userRepository->getEnabledEmails();
24: $scheduledTasks = $this->scheduledTaskRepository->getTasksNotice()->where('`sendemail`=1');
25: $periodicTasks = $this->periodicTaskRepository->getTasksNotice()->where('`sendemail`=1');
26:
27: 28: 29:
30: foreach ($periodicTasks as $task) {
31: if (!$this->periodicTaskRepository->checkPeriod($task->period, $task->date)) {
32: continue;
33: }
34:
35: $email = $task->email;
36:
37: if ($email == null) {
38: $contact = $this->contactBindingRepository->getContact($task->id_unit, $task->id_operation);
39:
40: if ($contact !== false) {
41: $email = $contact->email;
42: }
43: }
44:
45: if (Validators::isEmail($email) || count($emails) > 0) {
46: $template = $this->createTemplate();
47: $template->setFile(__DIR__ . '/../templates/Email/email.latte');
48:
49: $template->date = $task->date;
50: $template->unit = $task->unit;
51: $template->operation = $task->operation;
52:
53: $this->fillCompanyInfo($template);
54:
55: $mail = new Message;
56: $mail->setFrom($this->context->parameters['company']['email'], $this->context->parameters['company']['name'])
57: ->setHtmlBody($template);
58:
59: if (Validators::isEmail($email)) {
60: $mail->addTo($email);
61: }
62:
63: foreach ($emails as $email) {
64: $mail->addTo($email->email);
65: }
66:
67: $this->mailer->send($mail);
68: }
69: }
70:
71: 72: 73:
74: foreach ($scheduledTasks as $task) {
75: $email = $task->email;
76:
77: if ($email == null) {
78: $contact = $this->contactBindingRepository->getContact($task->id_unit, $task->id_operation);
79:
80: if ($contact !== false) {
81: $email = $contact->email;
82: }
83: }
84:
85: if (Validators::isEmail($email) || count($emails) > 0) {
86: $template = $this->createTemplate();
87: $template->setFile(__DIR__ . '/../templates/Email/email.latte');
88:
89: $template->date = $task->start;
90: $template->unit = $task->unit;
91: $template->operation = $task->operation;
92:
93: $this->fillCompanyInfo($template);
94:
95: $mail = new Message;
96: $mail->setFrom($this->context->parameters['company']['email'], $this->context->parameters['company']['name'])
97: ->setHtmlBody($template);
98:
99: if (Validators::isEmail($email)) {
100: $mail->addTo($email);
101: }
102:
103: foreach ($emails as $email) {
104: $mail->addTo($email->email);
105: }
106:
107: $this->mailer->send($mail);
108: }
109: }
110:
111: $this->terminate();
112: }
113:
114: private function fillCompanyInfo($template) {
115: $template->email = $this->context->parameters['company']['email'];
116: $template->phone = $this->context->parameters['company']['phone'];
117: $template->company = $this->context->parameters['company']['name'];
118: $template->street = $this->context->parameters['company']['street'];
119: $template->city = $this->context->parameters['company']['city'];
120: $template->zip = $this->context->parameters['company']['zip'];
121: }
122:
123: 124: 125: 126:
127: protected function checkLogin() {
128: }
129: }