latest update
[namibia] / module / Newsletter / src / Newsletter / Service / Newsletter.php
1 <?php
2 namespace Newsletter\Service;
3
4
5
6 /**
7  * Manage Newsletter data.
8  * @author andre.fourie
9  */
10 class Newsletter extends \Newsletter\DataBin\Newsletter
11 {
12
13
14         /**
15          * ExecuteAfter: Create, Update.
16          * New newsletter created, notifications be required, probably.
17          * @param array $meta
18          * @param object|null $jobRecord
19          * @param object|null $record
20          * @param \Workspace\Utility\ServiceInputParams $contract
21          */
22         public function sendNewsletter($meta, $jobRecord, $record, \Workspace\Utility\ServiceInputParams $contract)
23         {
24                 #-> Options.
25                 \Utility\Debug::errorLog('options', $contract->options);
26                 $jobRecord = !is_null($jobRecord)
27                         ? $jobRecord
28                         : $record;
29                 $profileId = 0;
30                 if ('Sending' == $jobRecord->jobState)
31                 {
32                         return;
33                 }
34                 if ($contract->options->SaveDraft)
35                 {
36                         $jobRecord->jobState = 'Draft';
37                         $this->em->flush($jobRecord);
38                 }
39                 elseif ($contract->options->TestDraft)
40                 {
41                         $jobRecord->jobState = 'Draft';
42                         $this->em->flush($jobRecord);
43                         $profileId = \Utility\Registry::getAuthParam('id');
44                         exec("php /var/www/namibia/public/index.php newsletter send "
45                                         . $jobRecord->id
46                                         . " 1 $profileId > /dev/null &");
47                 }
48                 elseif ($contract->options->Send)
49                 {
50                         $jobRecord->jobState = 'Sending';
51                         $this->em->flush($jobRecord);
52                         exec("php /var/www/namibia/public/index.php newsletter send "
53                                         . $jobRecord->id
54                                         . " 0 $profileId > /dev/null &");
55                 }
56         }
57
58
59         /**
60          * Cron: newsletter send
61          * Send newsletter.
62          * @param integer $jobId
63          * @param integer $test
64          * @param integer $profileId
65          */
66         public function send($jobId, $test, $profileId)
67         {
68                 #-> Options.
69                 $jobRecord = $this->em
70                         ->getRepository('\Newsletter\Entity\Newsletter')
71                         ->find($jobId);
72                 if (is_null($jobRecord))
73                 {
74                         return;
75                 }
76                 if (1 == $test)
77                 {
78                         $oNotify = new \Utility\Comms\Notification();
79                         $sentTo = $oNotify->sendNewsletter($jobRecord->id, true, $profileId);
80                         $jobRecord->jobState = 'Draft';
81                         $jobRecord->sentTo = is_numeric($sentTo)
82                                 ? $sentTo
83                                 : 0;
84                 }
85                 elseif (0 == $test)
86                 {
87                         $oNotify = new \Utility\Comms\Notification();
88                         $sentTo = $oNotify->sendNewsletter($jobRecord->id, false);
89                         $jobRecord->jobState = 'Sent';
90                         $jobRecord->sentTo = is_numeric($sentTo)
91                                 ? $sentTo
92                                 : 0;
93                 }
94                 $this->em->flush($jobRecord);
95         }
96
97 }