source: trunk/Modules/EmailQueue/EmailQueue.php

Last change on this file was 912, checked in by chronos, 3 years ago
  • Modified: Updated Common package.
File size: 4.7 KB
Line 
1<?php
2
3class PageEmailQueueProcess extends Page
4{
5 function __construct(System $System)
6 {
7 parent::__construct($System);
8 $this->Title = 'Fronta pošty';
9 $this->Description = 'Odeslání pošty z fronty';
10 $this->ParentClass = 'PagePortal';
11 }
12
13 function Show(): string
14 {
15 $Output = ModuleEmailQueue::Cast($this->System->GetModule('EmailQueue'))->Process();
16 $Output = $this->SystemMessage('Zpracování fronty emailů', 'Nové emaily byly odeslány').$Output;
17 return $Output;
18 }
19}
20
21class EmailQueue extends Model
22{
23 static function GetModelDesc(): ModelDesc
24 {
25 $Desc = new ModelDesc(self::GetClassName());
26 $Desc->AddDateTime('Time');
27 $Desc->AddString('To');
28 $Desc->AddString('Subject');
29 $Desc->AddText('Content');
30 $Desc->AddString('Headers');
31 $Desc->AddBoolean('Archive');
32 $Desc->AddString('From');
33 $Desc->AddReference('AttachmentFile', File::GetClassName());
34 return $Desc;
35 }
36}
37
38class ModuleEmailQueue extends Module
39{
40 function __construct(System $System)
41 {
42 parent::__construct($System);
43 $this->Name = 'EmailQueue';
44 $this->Version = '1.0';
45 $this->Creator = 'Chronos';
46 $this->License = 'GNU/GPLv3';
47 $this->Description = 'Queue for delayed email sending and history';
48 $this->Dependencies = array(ModuleLog::GetName());
49 $this->Models = array(EmailQueue::GetClassName());
50 }
51
52 function DoStart(): void
53 {
54 $this->System->RegisterPage(['fronta-posty'], 'PageEmailQueueProcess');
55 $this->System->FormManager->RegisterClass('Email', array(
56 'Title' => 'Nový email',
57 'Table' => 'EmailQueue',
58 'SubmitText' => 'Odeslat',
59 'Items' => array(
60 'Address' => array('Type' => 'String', 'Caption' => 'Adresa', 'Default' => ''),
61 'Subject' => array('Type' => 'String', 'Caption' => 'Předmět', 'Default' => ''),
62 'Content' => array('Type' => 'Text', 'Caption' => 'Obsah', 'Default' => ''),
63 ),
64 ));
65 $this->System->FormManager->RegisterClass('EmailQueue', array(
66 'Title' => 'Fronta e-mailů',
67 'Table' => 'EmailQueue',
68 'DefaultSortColumn' => 'Time',
69 'DefaultSortOrder' => 1,
70 'Items' => array(
71 'Time' => array('Type' => 'DateTime', 'Caption' => 'Vytvořeno'),
72 'To' => array('Type' => 'String', 'Caption' => 'Příjemce', 'Default' => ''),
73 'Subject' => array('Type' => 'String', 'Caption' => 'Předmět', 'Default' => ''),
74 'Content' => array('Type' => 'Text', 'Caption' => 'Obsah', 'Default' => ''),
75 'Headers' => array('Type' => 'String', 'Caption' => 'Hlavička', 'Default' => ''),
76 'Archive' => array('Type' => 'Boolean', 'Caption' => 'Odesláno', 'Default' => '0'),
77 'From' => array('Type' => 'String', 'Caption' => 'Odesílatel', 'Default' => ''),
78 'AttachmentFile' => array('Type' => 'TFile', 'Caption' => 'Příloha', 'Default' => '', 'Null' => true),
79 ),
80 'Actions' => array(
81 array('Caption' => 'Odeslat nové', 'URL' => '/fronta-posty/'),
82 ),
83 ));
84 }
85
86 function AddItem(string $To, string $Subject, string $Content, string $From, string $AttachmentFileId = ''): void
87 {
88 $Values = array('To' => $To,
89 'Subject' => $Subject, 'Content' => $Content, 'Time' => 'NOW()', 'Headers' => '',
90 'From' => $From);
91 if ($AttachmentFileId != '') $Values['AttachmentFile'] = $AttachmentFileId;
92 $this->Database->insert('EmailQueue', $Values);
93 }
94
95 function Process(): string
96 {
97 $Output = '';
98 $DbResult = $this->Database->select('EmailQueue', '*', 'Archive=0');
99 while ($DbRow = $DbResult->fetch_assoc())
100 {
101 $Mail = new Mail();
102 $Mail->AddToCombined($DbRow['To']);
103 $Mail->Subject = $DbRow['Subject'];
104 $Mail->From = $DbRow['From'];
105 $Mail->AddBody(strip_tags($DbRow['Content']), 'text/plain');
106 $Mail->AddBody($DbRow['Content'], 'text/html');
107 if ($DbRow['AttachmentFile'] != '')
108 {
109 $DbResult2 = $this->Database->select('File', '*', 'Id='.$DbRow['AttachmentFile']);
110 while ($File = $DbResult2->fetch_assoc())
111 $Mail->AttachFile($this->Config['Web']['FileRootFolder'].$File['DrivePath'], $File['MimeType']);
112 }
113 $Mail->Send();
114 $this->Database->update('EmailQueue', 'Id='.$DbRow['Id'], array('Archive' => 1));
115 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('System', 'SendEmail', $DbRow['Id']);
116 $Output .= 'To: '.$DbRow['To'].' Subject: '.$DbRow['Subject'].'<br />';
117 }
118 return $Output;
119 }
120
121 static function Cast(Module $Module): ModuleEmailQueue
122 {
123 if ($Module instanceof ModuleEmailQueue)
124 {
125 return $Module;
126 }
127 throw new Exception('Expected ModuleEmailQueue type but '.gettype($Module));
128 }
129}
Note: See TracBrowser for help on using the repository browser.