1

Is it possible to somehow notify the user from a completing task? I am walking trough a set of .csv files and on completion i would like to notify the user.

// last step
$stepIndex = $step + 1;
if ( $stepIndex == $this->getTotalSteps() )
{
    craft()->userSession->getFlash('notice', 'Reroutes  imported.');
    // craft()->userSession->setNotice(Craft::t('Reroutes   imported.'));
    // craft()->userSession->getFlashes();
}

return true;

I tried with the UserSessionService but i guess that won't work because the task is running separately from the session. If so reloading the page would be a good alternative but how can I let the front-end know of the task being completed.

1 Answer 1

3

The simple way to do this would be to log a message to your Plugin's custom log using

ExamplePlugin::log( 'TaskName has completed' );

This would require the user to visit the log and refresh to see if the task had completed - not ideal!

However, if you wanted something that updated as the task was being run you could possibly redirect them to a Plugin template with a text area that displays a log for the task where you could show completion of each step of the task and then finally a "Completed Task" message.

// Create task
$task = craft()->tasks->createTask( 'TaskName', 'desc' ), array(
  'products' => $products
));

// Set session notice for start
craft()->userSession->setNotice(Craft::t('Import process started.'));

// Redirect to Plugin template page that displays a log
$this->redirect( 'templateName?task=' . $task->id );

Then inside the task you could log messages to be displayed in that template

if($step === $this->getTotalSteps()-1) {
  Log::message( 'Task finished.' );
}
1
  • Thanks Pearce for this insight, maybe in combination what this guy is pointing to craftcms.stackexchange.com/questions/10158/… it is possible to show this in the current view. For now I will stick with the log options for a first version release.
    – Jacob
    Commented May 26, 2016 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.