Hello,
I want to create a trigger that send automatic emails when a project/tasks is created, modificated or deleted.
I’ve followed this tutorial Triggers system - Dolibarr ERP CRM Wiki and with the help of chatgpt I wrote down this code:
<?php
/* Copyright (C) ---Put here your own copyright and developer email---
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file core/triggers/interface_99_modMyModule_MyModuleTriggers.class.php
* \ingroup mymodule
* \brief Example trigger.
*
* Put detailed description here.
*
* \remarks You can create other triggers by copying this one.
* - File name should be either:
* - interface_99_modMyModule_MyTrigger.class.php
* - interface_99_all_MyTrigger.class.php
* - The file must stay in core/triggers
* - The class name must be InterfaceMytrigger
*/
require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
/**
* Class of triggers for MyModule module
*/
class InterfaceCustomTaskNotifications extends DolibarrTriggers
{
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
$this->name = preg_replace('/^Interface/i', '', get_class($this));
$this->family = "demo";
$this->description = "MyModule triggers.";
// 'development', 'experimental', 'dolibarr' or version
$this->version = 'development';
$this->picto = 'mymodule@mymodule';
}
/**
* Trigger name
*
* @return string Name of trigger file
*/
public function getName()
{
return $this->name;
}
/**
* Trigger description
*
* @return string Description of trigger file
*/
public function getDesc()
{
return $this->description;
}
/**
* Function called when a Dolibarrr business event is done.
* All functions "runTrigger" are triggered if file
* is inside directory core/triggers
*
* @param string $action Event action code
* @param CommonObject $object Object
* @param User $user Object user
* @param Translate $langs Object langs
* @param Conf $conf Object conf
* @return int Return integer <0 if KO, 0 if no triggered ran, >0 if OK
*/
public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
{
if (!isModEnabled('mymodule')) {
return 0; // If module is not enabled, we do nothing
}
// Put here code you want to execute when a Dolibarr business events occurs.
// Data and type of action are stored into $object and $action
// You can isolate code for each action in a separate method: this method should be named like the trigger in camelCase.
// For example : COMPANY_CREATE => public function companyCreate($action, $object, User $user, Translate $langs, Conf $conf)
/*$methodName = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($action)))));
$callback = array($this, $methodName);
if (is_callable($callback)) {
dol_syslog(
"Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id
);
return call_user_func($callback, $action, $object, $user, $langs, $conf);
}*/
dol_syslog("OK1", LOG_ERR);
// Or you can execute some code here
switch ($action) {
// Projects
//case 'PROJECT_CREATE':
//case 'PROJECT_MODIFY':
//case 'PROJECT_DELETE':
// Project tasks
case 'TASK_CREATE': send_email_notification($object,"Task created","Created"); dol_syslog("OK2", LOG_ERR);break;
case 'TASK_MODIFY':send_email_notification($object,"Task modified","Modified"); dol_syslog("OK3", LOG_ERR);break;
case 'TASK_DELETE':send_email_notification($object,"Task deleted","Deleted"); break;
default:
dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
break;
}
return 0;
}
private function send_email_notification($object, $subjectPrefix, $actionType)
{
dol_syslog("OK4", LOG_ERR);
global $conf;
// Ensure that the task object has the necessary details
if (!isset($object->user_assigned) || !isset($object->label)) {
return;
}
dol_syslog("OK5", LOG_ERR);
$assignedUser = $object->user_assigned; // User to whom the task is assigned
$taskTitle = $object->label; // Task title
$projectTitle = isset($object->project->title) ? $object->project->title : 'No Project'; // Project title
$email = $assignedUser->email; // Assigned user's email address
// Email subject and message
$subject = "$subjectPrefix: " . $taskTitle;
$message = "Hello " . $assignedUser->firstname . ",\n\n" .
"The task titled '" . $taskTitle . "' has been $actionType.\n" .
"Project: " . $projectTitle . "\n\n" .
"Please check the Dolibarr system for more details.";
dol_syslog("OK6", LOG_ERR);
// Send email using Dolibarr's internal mail function
include_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
$mail = new DolibarrMailer();
$mail->send($email, $subject, $message);
dol_syslog("OK7", LOG_ERR);
if ($mail->error) {
dol_syslog("Failed to send email notification to " . $email, LOG_ERR);
}
}
private function send_comment_notification($object, $user)
{
global $conf;
// Ensure the comment object has necessary details
if (!isset($object->content) || !isset($object->task)) {
return;
}
$task = $object->task;
$taskTitle = $task->label; // Task title
$assignedUser = $task->user_assigned; // User to whom the task is assigned
$email = $assignedUser->email; // Assigned user's email address
// Email subject and message
$subject = "New Comment Added to Task: " . $taskTitle;
$message = "Hello " . $assignedUser->firstname . ",\n\n" .
"A new comment has been added to the task titled '" . $taskTitle . "'.\n\n" .
"Comment: " . $object->content . "\n\n" .
"Please check the Dolibarr system for more details.";
// Send email using Dolibarr's internal mail function
include_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
$mail = new DolibarrMailer();
$mail->send($email, $subject, $message);
if ($mail->error) {
dol_syslog("Failed to send email notification for comment to " . $email, LOG_ERR);
}
}
}
but nothing is happening. What am I doing wrong?