MyBB-PluginLibrary
MyBB-PluginLibrary copied to clipboard
task system
Consider adding task support.
It'd have to support creation / updating of tasks (on activate), as well as suspension (on deactivate), and removal (on uninstall). And it should probably do some sanity checks, such as is the task file actually present or not.
contribution by User Mipher @ MyBB.com:
Hello, I noticed that in the PluginLibrary issues on GitHub you wrote about task support. I made one that I am currently using for some custom plugins. If you are interested: I used it just in one plugin, but there should not be any particular problem. Feel free to do whatever you want with it. Thanks for the work you did for this plugin and others too
function tasks()
{
global $db, $cache;
$tasks = func_get_args();
$taskFilenamesStr = implode("','", array_column($tasks, 'file'));
$query = $db->simple_select('tasks', '*', "file IN ('{$taskFilenamesStr}')");
$existingTasks = [];
while ($task = $db->fetch_array($query))
{
$existingTasks[$task['file']] = $task;
}
require_once MYBB_ROOT . "inc/functions_task.php";
$defaultTaskData = [
'description' => "",
'minute' => 0,
'hour' => "*",
'day' => "*",
'month' => "*",
'weekday' => "*",
'enabled' => 1,
'logging' => 1
];
$tasksData = [];
foreach($tasks as $task)
{
/*$query = $db->simple_select('tasks', '*', "file = '{$task['file']}'");
$existingTask = $db->fetch_array($query);
$taskData = $existingTask ?: $defaultTaskData;*/
$taskData = $existingTasks[$task['file']] ?? $defaultTaskData;
$taskData = array_merge($taskData, $task);
$taskData['nextrun'] = fetch_next_run($taskData);
if (isset($existingTasks[$task['file']]))
{
$db->update_query('tasks', $taskData, "tid = {$taskData['tid']}");
}
else
{
$tasksData[] = $taskData;
}
}
if ($tasksData)
$db->insert_query_multiple('tasks', $tasksData);
$cache->update_tasks();
}
function tasks_delete()
{
global $db;
$fileNamesStr = implode("','", func_get_args());
$db->delete_query('tasks', "file IN ('{$fileNamesStr}')");
}
function tasks_deactivate()
{
global $db;
$fileNamesStr = implode("','", func_get_args());
$db->update_query('tasks', ['enabled' => 0], "file IN ('{$fileNamesStr}')");
}
Usage:
$PL->tasks(
[
'title' => "Task1",
'description' => "Optional description",
'file' => "taskfile1",
'hour' => "0,6,12,18"
],
[
'title' => "Task2",
'description' => "Optional description",
'file' => "taskfile2",
'weekday' => "6"
]
);