Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ProcessMaker/Models/ProcessRequestToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class ProcessRequestToken extends ProcessMakerModel implements TokenInterface
'token_properties' => 'array',
'is_priority' => 'boolean',
'is_actionbyemail' => 'boolean',
'is_emailsent' => 'boolean',
];

/**
Expand Down
65 changes: 63 additions & 2 deletions ProcessMaker/Repositories/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter
$dataManager = new DataManager();
$tokenData = $dataManager->getData($token);
$feel = new FeelExpressionEvaluator();
$evaluatedUsers = $selfServiceUsers ? $feel->render($selfServiceUsers, $tokenData) ?? null: [];
$evaluatedUsers = $selfServiceUsers ? $feel->render($selfServiceUsers, $tokenData) ?? null : [];
$evaluatedGroups = $selfServiceGroups ? $feel->render($selfServiceGroups, $tokenData) ?? null : [];

// If we have single values we put it inside an array
Expand Down Expand Up @@ -172,7 +172,10 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter
CaseUpdate::dispatchSync($request, $token);

if (!is_null($user)) {
// Review if the task has enable the action by email
$this->validateAndSendActionByEmail($activity, $token, $user->email);
// Review if the user has enable the email notification
$this->validateEmailUserNotification($token, $user);
}
$this->instanceRepository->persistInstanceUpdated($token->getInstance());
}
Expand All @@ -183,7 +186,6 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter
* @param ActivityInterface $activity
* @param TokenInterface $token
* @param string $to
* @param array $data
*
* @return void
*/
Expand Down Expand Up @@ -225,6 +227,65 @@ private function validateAndSendActionByEmail(ActivityInterface $activity, Token
}
}

/**
* Validates the user's email notification settings and sends an email if enabled.
*
* @param TokenInterface $token The token containing task information.
* @param User $user The user to whom the email notification will be sent.
* @return mixed|null Returns the result of the email sending operation or null if not sent.
*/
private function validateEmailUserNotification(TokenInterface $token, User $user)
{
try {
Log::Info('User isEmailTaskEnable: ' . $user->email_task_notification);
// Return if email task notification is not enabled or email is empty
if (!$user->email_task_notification || empty($user->email)) {
return null;
}
// Prepare data for the email
$data = $this->prepareEmailData($token, $user);

// Send Email
return (new TaskActionByEmail())->sendAbeEmail($data['configEmail'], $user->email, $data['emailData']);
} catch (\Exception $e) {
// Catch and log the error
Log::error('Failed to validate and send email task notification', [
'error' => $e->getMessage(),
]);
}
}

/**
* Prepares the email data and configuration for sending an email notification.
*
* @param TokenInterface $token The token containing task information.
* @param User $user The user for whom the email data is being prepared.
* @return array An associative array containing 'emailData' and 'configEmail'.
*/
private function prepareEmailData(TokenInterface $token, User $user)
{
$taskName = $token->element_name ?? '';
// Prepare the email data
$emailData = [
Comment thread
fagubla marked this conversation as resolved.
Resolved
'firstname' => $user->firstname ?? '',
'assigned_by' => '', // You may want to populate this if needed
'element_name' => $taskName,
'case_title' => '', // Populate this if needed
'due_date' => $token->due_at ?? '',
];
// Prepare the email configuration
$configEmail = [
'emailServer' => 0, // Use the default email server
'subject' => "{$user->firstname} assigned you in {$taskName}",
'screenEmailRef' => 0, // Define here the screen to use
];

return [
'emailData' => $emailData,
'configEmail' => $configEmail,
];
}

/**
* Get due Variable
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('process_request_tokens', function (Blueprint $table) {
$table->boolean('is_emailsent')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('process_request_tokens', function (Blueprint $table) {
$table->dropColumn('is_emailsent');
});
}
};