diff --git a/ProcessMaker/Models/ProcessRequestToken.php b/ProcessMaker/Models/ProcessRequestToken.php index 9e266fe1cd..34dbdca57b 100644 --- a/ProcessMaker/Models/ProcessRequestToken.php +++ b/ProcessMaker/Models/ProcessRequestToken.php @@ -154,6 +154,7 @@ class ProcessRequestToken extends ProcessMakerModel implements TokenInterface 'token_properties' => 'array', 'is_priority' => 'boolean', 'is_actionbyemail' => 'boolean', + 'is_emailsent' => 'boolean', ]; /** diff --git a/ProcessMaker/Repositories/TokenRepository.php b/ProcessMaker/Repositories/TokenRepository.php index 74a8e79e0b..2dab177fed 100644 --- a/ProcessMaker/Repositories/TokenRepository.php +++ b/ProcessMaker/Repositories/TokenRepository.php @@ -12,9 +12,11 @@ use ProcessMaker\Models\FeelExpressionEvaluator; use ProcessMaker\Models\ProcessAbeRequestToken; use ProcessMaker\Models\ProcessCollaboration; +use ProcessMaker\Models\ProcessRequest; use ProcessMaker\Models\ProcessRequest as Instance; use ProcessMaker\Models\ProcessRequestToken; use ProcessMaker\Models\ProcessRequestToken as Token; +use ProcessMaker\Models\Screen; use ProcessMaker\Models\User; use ProcessMaker\Nayra\Bpmn\Collection; use ProcessMaker\Nayra\Bpmn\Models\EndEvent; @@ -134,7 +136,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 @@ -172,7 +174,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()); } @@ -183,7 +188,6 @@ public function persistActivityActivated(ActivityInterface $activity, TokenInter * @param ActivityInterface $activity * @param TokenInterface $token * @param string $to - * @param array $data * * @return void */ @@ -225,6 +229,70 @@ 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) + { + // Get the case + $caseTitle = ProcessRequest::where('id', $token->process_request_id)->value('case_title'); + // Prepare the email data + $taskName = $token->element_name ?? ''; + $emailData = [ + 'firstname' => $user->firstname ?? '', + 'assigned_by' => Auth::user()->fullname ?? '', + 'element_name' => $taskName, + 'case_title' => $caseTitle, // Populate this if needed + 'due_date' => $token->due_at ?? '', + 'imgHeader' => config('app.url') . '/img/processmaker-login.svg', + ]; + // Get the screen + $screen = Screen::where('title', 'Default Email Task Notification')->first(); + // Prepare the email configuration + $configEmail = [ + 'emailServer' => 0, // Use the default email server + 'subject' => "{$user->firstname} assigned you in {$taskName}", + 'screenEmailRef' => $screen->id ?? 0, // Define here the screen to use + ]; + + return [ + 'emailData' => $emailData, + 'configEmail' => $configEmail, + ]; + } + /** * Get due Variable * diff --git a/database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php b/database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php new file mode 100644 index 0000000000..52d47aed34 --- /dev/null +++ b/database/migrations/2025_01_10_183851_add_is_emailsent_column_for_process_request_tokens_table.php @@ -0,0 +1,27 @@ +boolean('is_emailsent')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('process_request_tokens', function (Blueprint $table) { + $table->dropColumn('is_emailsent'); + }); + } +};