1 | <?php |
||
23 | class PartialSubmissionJob extends AbstractQueuedJob |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * The generated CSV files |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $files = []; |
||
31 | |||
32 | /** |
||
33 | * @var SiteConfig |
||
34 | */ |
||
35 | protected $config; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $addresses; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Prepare the data |
||
45 | */ |
||
46 | public function setup() |
||
47 | { |
||
48 | parent::setup(); |
||
49 | $this->config = SiteConfig::current_site_config(); |
||
50 | $this->validateEmails(); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Only add valid email addresses |
||
55 | */ |
||
56 | protected function validateEmails() |
||
57 | { |
||
58 | $email = $this->config->SendMailTo; |
||
59 | $result = Email::is_valid_address($email); |
||
60 | if ($result) { |
||
61 | $this->addresses[] = $email; |
||
62 | } |
||
63 | if (strpos($email, ',') !== false) { |
||
64 | $emails = explode(',', $email); |
||
65 | foreach ($emails as $address) { |
||
66 | $result = Email::is_valid_address(trim($address)); |
||
67 | if ($result) { |
||
68 | $this->addresses[] = trim($address); |
||
69 | } else { |
||
70 | $this->addMessage($address . _t(__CLASS__ . '.invalidMail', ' is not a valid email address')); |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getTitle() |
||
80 | { |
||
81 | return _t(__CLASS__ . '.Title', 'Export partial submissions to Email'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Do some processing yourself! |
||
86 | */ |
||
87 | public function process() |
||
88 | { |
||
89 | if (!$this->config->SendDailyEmail) { |
||
90 | $this->addMessage(_t(__CLASS__ . '.NotActive', 'Daily exports are not enabled')); |
||
91 | $this->isComplete = true; |
||
92 | |||
93 | return; |
||
94 | } |
||
95 | if (!count($this->addresses)) { |
||
96 | $this->addMessage(_t(__CLASS__ . '.EmailError', 'Can not process without valid email')); |
||
97 | $this->isComplete = true; |
||
98 | |||
99 | return; |
||
100 | } |
||
101 | |||
102 | $userDefinedForms = $this->getParents(); |
||
103 | |||
104 | /** @var UserDefinedForm $form */ |
||
105 | foreach ($userDefinedForms as $form) { |
||
106 | $fileName = _t(__CLASS__ . '.Export', 'Export of ') . |
||
107 | $form->Title . ' - ' . |
||
108 | DBDatetime::now()->Format(DBDatetime::ISO_DATETIME); |
||
109 | $file = '/tmp/' . $fileName . '.csv'; |
||
110 | $this->files[] = $file; |
||
111 | $this->buildCSV($file, $form); |
||
112 | } |
||
113 | |||
114 | $this->sendEmail(); |
||
115 | |||
116 | $this->isComplete = true; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return ArrayList |
||
121 | */ |
||
122 | protected function getParents() |
||
123 | { |
||
124 | /** @var DataList|PartialFormSubmission[] $exportForms */ |
||
125 | $allSubmissions = PartialFormSubmission::get()->filter(['IsSend' => false]); |
||
126 | /** @var ArrayList|UserDefinedForm[] $parents */ |
||
127 | $userDefinedForms = ArrayList::create(); |
||
128 | |||
129 | /** @var PartialFormSubmission $submission */ |
||
130 | /** @noinspection ForeachSourceInspection */ |
||
131 | foreach ($allSubmissions as $submission) { |
||
132 | // Due to having to support Elemental ElementForm, we need to manually get the parent |
||
133 | // It's a bit a pickle, but it works |
||
134 | $parentClass = $submission->ParentClass; |
||
135 | $parent = $parentClass::get()->byID($submission->UserDefinedFormID); |
||
136 | if ($parent && |
||
137 | $parent->ExportPartialSubmissions && |
||
138 | !$userDefinedForms->find('ID', $parent->ID) |
||
139 | ) { |
||
140 | $userDefinedForms->push($parent); |
||
141 | } |
||
142 | $submission->destroy(); |
||
143 | } |
||
144 | |||
145 | return $userDefinedForms; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string $file |
||
150 | * @param UserDefinedForm $form |
||
151 | * @throws \SilverStripe\ORM\ValidationException |
||
152 | */ |
||
153 | protected function buildCSV($file, $form) |
||
169 | |||
170 | /** |
||
171 | * @param UserDefinedForm $form |
||
172 | * @param DataList|PartialFormSubmission[] $submissions |
||
173 | * @param resource $resource |
||
174 | * @throws \SilverStripe\ORM\ValidationException |
||
175 | */ |
||
176 | protected function processSubmissions($form, $submissions, $resource) |
||
201 | |||
202 | /** |
||
203 | * Send out the email(s) |
||
204 | */ |
||
205 | protected function sendEmail() |
||
222 | |||
223 | /** |
||
224 | * @throws \Exception |
||
225 | */ |
||
226 | public function afterComplete() |
||
241 | |||
242 | /** |
||
243 | * Remove submissions that have been sent out |
||
244 | */ |
||
245 | protected function cleanupSubmissions() |
||
257 | |||
258 | /** |
||
259 | * Create a new queued job for tomorrow |
||
260 | * @throws \Exception |
||
261 | */ |
||
262 | protected function createNewJob() |
||
270 | |||
271 | /** |
||
272 | * @return array |
||
273 | */ |
||
274 | public function getMessages() |
||
278 | |||
279 | /** |
||
280 | * @return array |
||
281 | */ |
||
282 | public function getAddresses() |
||
286 | |||
287 | /** |
||
288 | * @param string $address |
||
289 | */ |
||
290 | public function addAddress($address) |
||
296 | |||
297 | /** |
||
298 | * @return SiteConfig |
||
299 | */ |
||
300 | public function getConfig() |
||
304 | } |
||
305 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.