Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task system #6

Open
frostschutz opened this issue Dec 10, 2012 · 1 comment
Open

task system #6

frostschutz opened this issue Dec 10, 2012 · 1 comment
Labels

Comments

@frostschutz
Copy link
Owner

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.

@frostschutz
Copy link
Owner Author

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"
    ]
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant