From a9b468bbcdafa1f3c4344abf0ffc124f01c696d5 Mon Sep 17 00:00:00 2001 From: Jon Bendtsen Date: Wed, 15 Oct 2025 03:19:45 +0200 Subject: [PATCH] New: Adding linked project to mass mailing card (#35723) * New: Adding linked project to mass mailing card * modify project on mailing card * mailing card load fk_project pre commit fix * only fetch projects with id>0 * fix PHPstan errors regarding mesg * projects related to mass mailings does not have a soc or socid * remove last trace of soc and socid * checking if mesg is set before using it * just loading all projects, because maybe you have a mailing list with workers for a thirdparty? think company party * showing the project during edit * given that action is already something else, it will never not be classify * making PHPstan happy * Adding project column to list of mass mailings. Search, order and click able * pasted the wrong place * no need to check for filtermail * x now also clears project search --------- Co-authored-by: Jon Bendtsen Co-authored-by: Laurent Destailleur --- htdocs/comm/mailing/card.php | 93 ++++++++++++++++++++- htdocs/comm/mailing/class/mailing.class.php | 6 ++ htdocs/comm/mailing/list.php | 54 +++++++++++- 3 files changed, 150 insertions(+), 3 deletions(-) diff --git a/htdocs/comm/mailing/card.php b/htdocs/comm/mailing/card.php index 084482579db..ce5642d8652 100644 --- a/htdocs/comm/mailing/card.php +++ b/htdocs/comm/mailing/card.php @@ -4,6 +4,7 @@ * Copyright (C) 2005-2016 Regis Houssin * Copyright (C) 2021 Waël Almoman * Copyright (C) 2024-2025 MDW + * Copyright (C) 2025 Jon Bendtsen * Copyright (C) 2024-2025 Frédéric France * * This program is free software; you can redistribute it and/or modify @@ -40,6 +41,10 @@ require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; +if (isModEnabled('project')) { + require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php'; + require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php'; +} /** * @var Conf $conf @@ -58,6 +63,7 @@ $action = GETPOST('action', 'aZ09'); $confirm = GETPOST('confirm', 'alpha'); $cancel = GETPOST('cancel', 'aZ09'); $urlfrom = GETPOST('urlfrom'); +$projectid = GETPOSTINT('projectid'); $backtopageforcancel = GETPOST('backtopageforcancel'); // Initialize a technical objects @@ -71,6 +77,17 @@ $extrafields->fetch_name_optionals_label($object->table_element); // Load object include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be 'include', not 'include_once'. +// Load object->fk_project +if (isset($object->fk_project) && $object->fk_project > 0 ) { + dol_syslog('isset($object->fk_project)='.$object->fk_project, LOG_DEBUG); + $ret = $object->fetchProject(); + if ($ret <= 0) { + setEventMessages($object->error, $object->errors, 'errors'); + $action = ''; + } +} + + // Array of possible substitutions (See also file mailing-send.php that should manage same substitutions) $object->substitutionarray = FormMail::getAvailableSubstitKey('emailing'); @@ -569,6 +586,7 @@ if (empty($reshook)) { $object->body = (string) GETPOST("bodyemail", 'restricthtml'); $object->bgcolor = preg_replace('/^#/', '', (string) GETPOST("bgcolor")); $object->bgimage = (string) GETPOST("bgimage"); + $object->fk_project = GETPOSTINT('projectid'); if (!$object->title) { $mesgs[] = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTitle")); @@ -593,6 +611,23 @@ if (empty($reshook)) { $action = "create"; } + if ($action == 'classin' && $permissiontocreate) { + $mesgs = array(); + $setResult = $object->setProject(GETPOSTINT('projectid')); + dol_syslog('Mailing card, action classin, setProject', LOG_DEBUG); + if ($setResult) { + $result = $object->update($user); + if ($result >= 0) { + header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id); + exit; + } + $mesg = $object->error; + } + + setEventMessages($mesg, $mesgs, 'errors'); + $action = ""; + } + // Action update description of emailing if (($action == 'settitle' || $action == 'setemail_from' || $action == 'setemail_replyto' || $action == 'setreplyto' || $action == 'setemail_errorsto' || $action == 'setevenunsubscribe') && $permissiontovalidatesend) { $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, getDolGlobalInt('MAILING_USE_NEW_PATH_FOR_FILES') ? 0 : 2, 0, 1, $object, 'mailing'); @@ -613,7 +648,7 @@ if (empty($reshook)) { $object->evenunsubscribe = (GETPOST('evenunsubscribe') ? 1 : 0); } - if (!$mesg) { + if (isset($mesg) && !$mesg) { $result = $object->update($user); if ($result >= 0) { header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id); @@ -761,6 +796,10 @@ if (empty($reshook)) { $form = new Form($db); $htmlother = new FormOther($db); +$formproject = null; +if (isModEnabled('project')) { + $formproject = new FormProjets($db); +} $help_url = 'EN:Module_EMailing|FR:Module_Mailing|ES:Módulo_Mailing'; llxHeader( @@ -806,6 +845,17 @@ if ($action == 'create') { // aaa print ''.$langs->trans("MailTitle").''; + // Project + if (isModEnabled('project')) { + $langs->load("projects"); + print ''; + print '' . $langs->trans("Project") . ''; + print img_picto('', 'project', 'class="pictofixedwidth"') . $formproject->select_projects(-1, (string) $projectid, 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500 widthcentpercentminusxx'); + print ' '; + print ''; + print ''; + } + if (getDolGlobalInt('EMAILINGS_SUPPORT_ALSO_SMS')) { $arrayoftypes = array("email" => "Email", "sms" => "SMS"); print ''.$langs->trans("Type").''; @@ -1027,6 +1077,27 @@ if ($action == 'create') { // aaa } $morehtmlstatus .= ')   '; } + // Project + if (isModEnabled('project')) { + $langs->load("projects"); + $morehtmlref .= '
'; + if ($permissiontocreate) { + $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"'); + if ($action != 'classify') { + $morehtmlref .= '' . img_edit($langs->transnoentitiesnoconv('SetProject')) . ' '; + } + $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, -1, (string) $object->fk_project, ($action == 'classify' ? 'projectid' : 'none'), 0, 0, 0, 1, '', 'maxwidth300'); + } else { + if (!empty($object->fk_project)) { + $proj = new Project($db); + $proj->fetch($object->fk_project); + $morehtmlref .= $proj->getNomUrl(1); + if ($proj->title) { + $morehtmlref .= ' - ' . dol_escape_htmltag($proj->title) . ''; + } + } + } + } dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlstatus); @@ -1383,6 +1454,26 @@ if ($action == 'create') { // aaa $morehtmlstatus .= ')   '; } + // Project + if (isModEnabled('project')) { + $langs->load("projects"); + $morehtmlref .= '
'; + if ($permissiontocreate) { + $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"'); + $morehtmlref .= '' . img_edit($langs->transnoentitiesnoconv('SetProject')) . ' '; + $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, -1, (string) $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300'); + } else { + if (!empty($object->fk_project)) { + $proj = new Project($db); + $proj->fetch($object->fk_project); + $morehtmlref .= $proj->getNomUrl(1); + if ($proj->title) { + $morehtmlref .= ' - ' . dol_escape_htmltag($proj->title) . ''; + } + } + } + } + dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlstatus); print '
'; diff --git a/htdocs/comm/mailing/class/mailing.class.php b/htdocs/comm/mailing/class/mailing.class.php index 893e6896b84..959e4e8e032 100644 --- a/htdocs/comm/mailing/class/mailing.class.php +++ b/htdocs/comm/mailing/class/mailing.class.php @@ -4,6 +4,7 @@ * Copyright (C) 2005-2009 Regis Houssin * Copyright (C) 2024-2025 Frédéric France * Copyright (C) 2024 MDW + * Copyright (C) 2025 Jon Bendtsen * * 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 @@ -185,6 +186,11 @@ class Mailing extends CommonObject */ public $fk_project; + public $fields = array( + 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id'), + 'fk_project' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Fk project', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 10), + ); + const STATUS_DRAFT = 0; const STATUS_VALIDATED = 1; const STATUS_SENTPARTIALY = 2; diff --git a/htdocs/comm/mailing/list.php b/htdocs/comm/mailing/list.php index 1986ef75b48..d67b0ecd863 100644 --- a/htdocs/comm/mailing/list.php +++ b/htdocs/comm/mailing/list.php @@ -4,6 +4,7 @@ * Copyright (C) 2024 MDW * Copyright (C) 2024 Alexandre Spangaro * Copyright (C) 2024 Frédéric France + * Copyright (C) 2025 Jon Bendtsen * * 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 @@ -28,6 +29,9 @@ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php'; +if (isModEnabled('project')) { + require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php'; +} /** * @var Conf $conf @@ -71,6 +75,9 @@ $search_ref = GETPOST("search_ref", "alpha") ? GETPOST("search_ref", "alpha") : $search_messtype = GETPOST("search_messtype", "alpha"); $filteremail = GETPOST('filteremail', 'alpha'); +$search_refproject = GETPOST('search_refproject', 'alpha'); +$search_project = GETPOST('search_project', 'alpha'); + // Initialize a technical objects $object = new Mailing($db); $extrafields = new ExtraFields($db); @@ -135,6 +142,8 @@ if (empty($reshook)) { }*/ $search_ref = ''; $search_messtype = ''; + $search_refproject = ''; + $search_project = ''; $search_all = ''; $toselect = array(); $search_array_options = array(); @@ -157,6 +166,7 @@ if (empty($reshook)) { */ $form = new Form($db); +$projectstatic = new Project($db); $now = dol_now(); @@ -169,11 +179,13 @@ $morecss = array(); // -------------------------------------------------------------------- if ($filteremail) { $sql = "SELECT m.rowid, m.messtype, m.titre as title, m.nbemail, m.statut as status, m.date_creat as datec, m.date_envoi as date_envoi,"; - $sql .= " mc.statut as sendstatut"; + $sql .= " mc.statut as sendstatut,"; + $sql .= " pr.rowid as project_id, pr.ref as project_ref, pr.title as project_label"; $sqlfields = $sql; // $sql fields to remove for count total $sql .= " FROM ".MAIN_DB_PREFIX."mailing as m, ".MAIN_DB_PREFIX."mailing_cibles as mc"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as pr ON pr.rowid = m.fk_project"; $sql .= " WHERE m.rowid = mc.fk_mailing AND m.entity = ".$conf->entity; $sql .= " AND mc.email = '".$db->escape($filteremail)."'"; if ($search_ref) { @@ -185,6 +197,12 @@ if ($filteremail) { if ($search_all) { $sql .= " AND (m.titre LIKE '%".$db->escape($search_all)."%' OR m.sujet LIKE '%".$db->escape($search_all)."%' OR m.body LIKE '%".$db->escape($search_all)."%')"; } + if ($search_refproject) { + $sql .= natural_search('pr.ref', $search_refproject); + } + if ($search_project) { + $sql .= natural_search('pr.title', $search_project); + } if (!$sortorder) { $sortorder = "ASC"; } @@ -192,11 +210,13 @@ if ($filteremail) { $sortfield = "m.rowid"; } } else { - $sql = "SELECT m.rowid, m.messtype, m.titre as title, m.nbemail, m.statut as status, m.date_creat as datec, m.date_envoi as date_envoi"; + $sql = "SELECT m.rowid, m.messtype, m.titre as title, m.nbemail, m.statut as status, m.date_creat as datec, m.date_envoi as date_envoi,"; + $sql .= " pr.rowid as project_id, pr.ref as project_ref, pr.title as project_label"; $sqlfields = $sql; // $sql fields to remove for count total $sql .= " FROM ".MAIN_DB_PREFIX."mailing as m"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as pr ON pr.rowid = m.fk_project"; $sql .= " WHERE m.entity = ".((int) $conf->entity); if ($search_ref) { $sql .= " AND m.rowid = '".$db->escape($search_ref)."'"; @@ -207,6 +227,12 @@ if ($filteremail) { if ($search_all) { $sql .= " AND (m.titre LIKE '%".$db->escape($search_all)."%' OR m.sujet LIKE '%".$db->escape($search_all)."%' OR m.body LIKE '%".$db->escape($search_all)."%')"; } + if ($search_refproject) { + $sql .= natural_search('pr.ref', $search_refproject); + } + if ($search_project) { + $sql .= natural_search('pr.title', $search_project); + } if (!$sortorder) { $sortorder = "ASC"; } @@ -301,6 +327,12 @@ if ($search_ref != '') { if ($search_messtype != '') { $param .= '&search_type='.urlencode($search_messtype); } +if ($search_refproject != '') { + $param .= '&search_refproject='.urlencode($search_refproject); +} +if ($search_project != '') { + $param .= '&search_project='.urlencode($search_project); +} if ($optioncss != '') { $param .= '&optioncss='.urlencode($optioncss); } @@ -415,6 +447,12 @@ print ''; print ''; print ''; print ' '; + +// Project +print ''; +print ''; +print ''; + if (!$filteremail) { print ' '; } @@ -455,6 +493,9 @@ if (getDolGlobalInt('EMAILINGS_SUPPORT_ALSO_SMS')) { print_liste_field_titre("Title", $_SERVER["PHP_SELF"], "m.titre", $param, "", "", $sortfield, $sortorder); $totalarray['nbfield']++; print_liste_field_titre("DateCreation", $_SERVER["PHP_SELF"], "m.date_creat", $param, "", '', $sortfield, $sortorder, 'center '); + +// Project +print_liste_field_titre("Project", $_SERVER["PHP_SELF"], "project_label", $param, "", '', $sortfield, $sortorder); $totalarray['nbfield']++; if (!$filteremail) { $title = $langs->trans("NbOfEMails"); @@ -506,6 +547,10 @@ while ($i < $imaxinloop) { $object->id = $obj->rowid; $object->ref = $obj->rowid; + $projectstatic->id = $obj->project_id; + $projectstatic->ref = $obj->project_ref; + $projectstatic->title = $obj->project_label; + // Show here line of result $j = 0; print ''; @@ -546,6 +591,11 @@ while ($i < $imaxinloop) { print dol_print_date($db->jdate($obj->datec), 'day'); print ''; + // Project + print ''; + print ''.dol_escape_htmltag($projectstatic->title).''; + print ''; + // Nb of email if (!$filteremail) { print '';