1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
namespace XoopsModules\Wgevents; |
7
|
|
|
|
8
|
|
|
/* |
9
|
|
|
You may not change or alter any portion of this comment or credits |
10
|
|
|
of supporting developers from this source code or any supporting source code |
11
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
12
|
|
|
|
13
|
|
|
This program is distributed in the hope that it will be useful, |
14
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* wgEvents module for xoops |
20
|
|
|
* |
21
|
|
|
* @copyright 2021 XOOPS Project (https://xoops.org) |
22
|
|
|
* @license GPL 2.0 or later |
23
|
|
|
* @package wgevents |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
* @min_xoops 2.5.11 Beta1 |
26
|
|
|
* @author Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
use XoopsModules\Wgevents; |
30
|
|
|
use XoopsModules\Wgevents\{ |
31
|
|
|
Constants, |
|
|
|
|
32
|
|
|
MailHandler |
|
|
|
|
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class Object Handler Task |
37
|
|
|
*/ |
38
|
|
|
class TaskHandler extends \XoopsPersistableObjectHandler |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param \XoopsDatabase $db |
44
|
|
|
*/ |
45
|
|
|
public function __construct(\XoopsDatabase $db) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($db, 'wgevents_task', Task::class, 'id', 'id'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param bool $isNew |
52
|
|
|
* |
53
|
|
|
* @return object |
54
|
|
|
*/ |
55
|
|
|
public function create($isNew = true) |
56
|
|
|
{ |
57
|
|
|
return parent::create($isNew); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* retrieve a field |
62
|
|
|
* |
63
|
|
|
* @param int $id field id |
64
|
|
|
* @param null fields |
|
|
|
|
65
|
|
|
* @return \XoopsObject|null reference to the {@link Get} object |
66
|
|
|
*/ |
67
|
|
|
public function get($id = null, $fields = null) |
68
|
|
|
{ |
69
|
|
|
return parent::get($id, $fields); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* get inserted id |
74
|
|
|
* |
75
|
|
|
* @param null |
76
|
|
|
* @return int reference to the {@link Get} object |
77
|
|
|
*/ |
78
|
|
|
public function getInsertId() |
79
|
|
|
{ |
80
|
|
|
return $this->db->getInsertId(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Count Task in the database |
85
|
|
|
* @param int $start |
86
|
|
|
* @param int $limit |
87
|
|
|
* @param string $sort |
88
|
|
|
* @param string $order |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getCountTasks($start = 0, $limit = 0, $sort = 'id', $order = 'ASC') |
92
|
|
|
{ |
93
|
|
|
$crCountTasks = new \CriteriaCompo(); |
94
|
|
|
$crCountTasks = $this->getTasksCriteria($crCountTasks, $start, $limit, $sort, $order); |
95
|
|
|
return $this->getCount($crCountTasks); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get All Task in the database |
100
|
|
|
* @param int $start |
101
|
|
|
* @param int $limit |
102
|
|
|
* @param string $sort |
103
|
|
|
* @param string $order |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getAllTasks($start = 0, $limit = 0, $sort = 'id', $order = 'ASC') |
107
|
|
|
{ |
108
|
|
|
$crAllTasks = new \CriteriaCompo(); |
109
|
|
|
$crAllTasks = $this->getTasksCriteria($crAllTasks, $start, $limit, $sort, $order); |
110
|
|
|
return $this->getAll($crAllTasks); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get Criteria Task |
115
|
|
|
* @param $crTasks |
116
|
|
|
* @param int $start |
117
|
|
|
* @param int $limit |
118
|
|
|
* @param string $sort |
119
|
|
|
* @param string $order |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
private function getTasksCriteria($crTasks, $start, $limit, $sort, $order) |
123
|
|
|
{ |
124
|
|
|
$crTasks->setStart($start); |
125
|
|
|
$crTasks->setLimit($limit); |
126
|
|
|
$crTasks->setSort($sort); |
127
|
|
|
$crTasks->setOrder($order); |
128
|
|
|
return $crTasks; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get count open tasks for cron.php |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
public function getCountTasksOpen() |
136
|
|
|
{ |
137
|
|
|
$crTask = new \CriteriaCompo(); |
138
|
|
|
$crTask->add(new \Criteria('status', Constants::STATUS_DONE, '<')); |
139
|
|
|
|
140
|
|
|
return $this->getCount($crTask); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create a task |
145
|
|
|
* @param $type |
146
|
|
|
* @param $recipient |
147
|
|
|
* @param $params |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function createTask($type, $recipient, $params) |
151
|
|
|
{ |
152
|
|
|
$uidCurrent = \is_object($GLOBALS['xoopsUser']) ? (int)$GLOBALS['xoopsUser']->uid() : 0; |
153
|
|
|
|
154
|
|
|
$taskObj = $this->create(); |
155
|
|
|
// Set Vars |
156
|
|
|
$taskObj->setVar('type', $type); |
157
|
|
|
$taskObj->setVar('params', $params); |
158
|
|
|
$taskObj->setVar('recipient', $recipient); |
159
|
|
|
$taskObj->setVar('datecreated', time()); |
160
|
|
|
$taskObj->setVar('submitter', $uidCurrent); |
161
|
|
|
$taskObj->setVar('status', Constants::STATUS_PENDING); |
162
|
|
|
|
163
|
|
|
// Insert Data |
164
|
|
|
return (bool)$this->insert($taskObj); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* process all task if limit is not exceeded |
169
|
|
|
* @param $log_level |
170
|
|
|
* @param $isCron |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function processTasks($log_level = 0, $isCron = false) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$helper = \XoopsModules\Wgevents\Helper::getInstance(); |
176
|
|
|
|
177
|
|
|
// get limit_hour from primary account |
178
|
|
|
$accountHandler = $helper->getHandler('Account'); |
179
|
|
|
$limitHour = $accountHandler->getLimitHour(); |
180
|
|
|
|
181
|
|
|
$resProcess = ''; |
182
|
|
|
|
183
|
|
|
$crTaskPending = new \CriteriaCompo(); |
184
|
|
|
$crTaskPending->add(new \Criteria('status', Constants::STATUS_PENDING)); |
185
|
|
|
$tasksCountPending = $this->getCount($crTaskPending); |
186
|
|
|
|
187
|
|
|
// if all works properly there shouldn't be a task type 'processing' left |
188
|
|
|
$crTaskProcessing = new \CriteriaCompo(); |
189
|
|
|
$crTaskProcessing->add(new \Criteria('status', Constants::STATUS_PROCESSING)); |
190
|
|
|
$tasksCountProcessing = $this->getCount($crTaskProcessing); |
191
|
|
|
|
192
|
|
|
$crTaskDone = new \CriteriaCompo(); |
193
|
|
|
$crTaskDone->add(new \Criteria('status', Constants::STATUS_DONE)); |
194
|
|
|
$crTaskDone->add(new \Criteria('datedone', time() - 3600, '>')); |
195
|
|
|
$tasksCountDone = $this->getCount($crTaskDone); |
196
|
|
|
|
197
|
|
|
$counterDone = 0; |
198
|
|
|
if ($log_level > 0) { |
199
|
|
|
$resProcess .= '<br>Start processTasks'; |
200
|
|
|
$resProcess .= '<br>time - 3600: ' . \formatTimestamp(time() - 3600, 'm'); |
201
|
|
|
if ($tasksCountProcessing > 0) { |
202
|
|
|
$resProcess .= '<br><span style="color:#ff0000;font-weight:700">Count processing at start: ' . $tasksCountProcessing . '</span>'; |
203
|
|
|
} |
204
|
|
|
$resProcess .= '<br>Count pending at start: ' . $tasksCountPending; |
205
|
|
|
$resProcess .= '<br>Count done at start: ' . $tasksCountDone; |
206
|
|
|
} |
207
|
|
|
if (($tasksCountPending > 0) && ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
208
|
|
|
if ($limitHour > 0) { |
209
|
|
|
$crTaskPending->setLimit($limitHour); |
210
|
|
|
} |
211
|
|
|
$tasksAll = $this->getAll($crTaskPending); |
212
|
|
|
foreach (\array_keys($tasksAll) as $i) { |
213
|
|
|
// check whether task is still pending |
214
|
|
|
// ignore it if meanwhile another one started to process the task |
215
|
|
|
if ($log_level > 1) { |
216
|
|
|
$resProcess .= '<br>Task key: ' . $i; |
217
|
|
|
} |
218
|
|
|
if ((Constants::STATUS_PENDING == (int)$tasksAll[$i]->getVar('status')) |
219
|
|
|
&& ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
220
|
|
|
$taskProcessObj = $this->get($i); |
221
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_PROCESSING); |
222
|
|
|
if ($this->insert($taskProcessObj)) { |
223
|
|
|
$mailsHandler = new MailHandler(); |
224
|
|
|
$mailParams = json_decode($tasksAll[$i]->getVar('params', 'n'), true); |
225
|
|
|
$mailParams['recipients'] = $tasksAll[$i]->getVar('recipient'); |
226
|
|
|
$mailParams['taskId'] = $i; |
227
|
|
|
$mailsHandler->setParams($mailParams); |
228
|
|
|
$mailsHandler->setType($tasksAll[$i]->getVar('type')); |
229
|
|
|
// send mails |
230
|
|
|
$result = $mailsHandler->execute(); |
231
|
|
|
unset($mailsHandler); |
232
|
|
|
//update task list corresponding the result |
233
|
|
|
if ($result) { |
234
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_DONE); |
235
|
|
|
$taskProcessObj->setVar('datedone', time()); |
236
|
|
|
$counterDone++; |
237
|
|
|
if ($log_level > 1) { |
238
|
|
|
$resProcess .= ' - done'; |
239
|
|
|
} |
240
|
|
|
} else { |
241
|
|
|
$taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
242
|
|
|
if ($log_level > 1) { |
243
|
|
|
$resProcess .= ' - failed'; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
$this->insert($taskProcessObj); |
247
|
|
|
} else { |
248
|
|
|
$resProcess .= ' - skipped'; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
// check once more number of done |
252
|
|
|
$tasksCountDone = $this->getCount($crTaskDone); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
// check once more number of open tasks |
256
|
|
|
$crTaskOpen = new \CriteriaCompo(); |
257
|
|
|
$crTaskOpen->add(new \Criteria('status', Constants::STATUS_DONE, '<')); |
258
|
|
|
$tasksCountOpen = $this->getCount($crTaskOpen); |
259
|
|
|
|
260
|
|
|
if ($log_level > 0) { |
261
|
|
|
$resProcess .= '<br>End processTasks'; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return ['pending' => $tasksCountPending, 'done' => $counterDone, 'resprocess' => $resProcess, 'still_open' => $tasksCountOpen]; |
|
|
|
|
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: