Merge pull request #14825 from atm-greg/fix_notification_system

Fix notification system
This commit is contained in:
Laurent Destailleur 2020-09-29 14:13:33 +02:00 committed by GitHub
commit 708ac91a39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 149 additions and 44 deletions

View File

@ -48,7 +48,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
// Load translation files required by the page
$langs->loadLangs(array("companies", "other", "commercial", "bills", "orders", "agenda"));
$langs->loadLangs(array("companies", "other", "commercial", "bills", "orders", "agenda", "mails"));
$action = GETPOST('action', 'aZ09');
$cancel = GETPOST('cancel', 'alpha');
@ -393,31 +393,36 @@ if (empty($reshook) && $action == 'add')
$moreparam = '';
if ($user->id != $object->userownerid) $moreparam = "filtert=-1"; // We force to remove filter so created record is visible when going back to per user view.
//Create eminder
//Create reminders
if ($addreminder == 'on'){
$actionCommReminder = new ActionCommReminder($db);
$dateremind = dol_time_plus_duree($datep, -$offsetvalue, 'i');
$dateremind = dol_time_plus_duree($datep, -$offsetvalue, $offsetunit);
$actionCommReminder->dateremind = $dateremind;
$actionCommReminder->typeremind = $remindertype;
$actionCommReminder->fk_user = $user;
$actionCommReminder->offsetunit = $offsetunit;
$actionCommReminder->offsetvalue = $offsetvalue;
$actionCommReminder->status = $actionCommReminder::STATUS_TODO;
$actionCommReminder->fk_actioncomm = $object->id;
if ($remindertype == 'email') $actionCommReminder->fk_email_template = $modelmail;
$res = $actionCommReminder->create($user);
// the notification must be created for every user assigned to the event
foreach ($object->userassigned as $userassigned)
{
$actionCommReminder->fk_user = $userassigned['id'];
$res = $actionCommReminder->create($user);
if ($res <= 0){
// If error
$error++;
$langs->load("errors");
$error = $langs->trans('ErrorReminderActionCommCreation').' '.$actionCommReminder->error;
setEventMessages($error, $actionCommReminder->errors, 'errors');
$action = 'create'; $donotclearsession = 1;
}
if ($res <= 0){
// If error
$db->rollback();
$langs->load("errors");
$error = $langs->trans('ErrorReminderActionCommCreation');
setEventMessages($error, null, 'errors');
$action = 'create'; $donotclearsession = 1;
break;
}
}
}
if ($error) {
@ -634,9 +639,51 @@ if (empty($reshook) && $action == 'update')
$categories = GETPOST('categories', 'array');
$object->setCategories($categories);
$object->loadReminders();
if (!empty($object->reminders) && $object->datep > dol_now())
{
foreach ($object->reminders as $reminder)
{
$reminder->delete($user);
}
$object->reminders = array();
}
//Create reminders
if ($addreminder == 'on' && $object->datep > dol_now()){
$actionCommReminder = new ActionCommReminder($db);
$dateremind = dol_time_plus_duree($datep, -$offsetvalue, $offsetunit);
$actionCommReminder->dateremind = $dateremind;
$actionCommReminder->typeremind = $remindertype;
$actionCommReminder->offsetunit = $offsetunit;
$actionCommReminder->offsetvalue = $offsetvalue;
$actionCommReminder->status = $actionCommReminder::STATUS_TODO;
$actionCommReminder->fk_actioncomm = $object->id;
if ($remindertype == 'email') $actionCommReminder->fk_email_template = $modelmail;
// the notification must be created for every user assigned to the event
foreach ($object->userassigned as $userassigned)
{
$actionCommReminder->fk_user = $userassigned['id'];
$res = $actionCommReminder->create($user);
if ($res <= 0){
// If error
$langs->load("errors");
$error = $langs->trans('ErrorReminderActionCommCreation');
setEventMessages($error, null, 'errors');
$action = 'create'; $donotclearsession = 1;
break;
}
}
}
unset($_SESSION['assignedtouser']);
$db->commit();
if (!$error) $db->commit();
else $db->rollback();
} else {
setEventMessages($object->error, $object->errors, 'errors');
$db->rollback();
@ -1236,7 +1283,7 @@ if ($action == 'create')
}
});
$("#selectremindertype").click(function(){
$("#selectremindertype").change(function(){
var selected_option = $("#selectremindertype option:selected").val();
if(selected_option == "email") {
$("#select_actioncommsendmodel_mail").closest("tr").show();
@ -1664,41 +1711,82 @@ if ($id > 0)
{
$filtreuserid = $user->id;
if ($user->rights->agenda->allactions->read) $filtreuserid = 0;
$object->loadReminders('', $filteruserid);
$object->loadReminders('', $filteruserid, false);
print '<hr>';
print '<table class="border tableforfield centpercent">';
print '<tr><td class="titlefieldcreate nowrap">'.$langs->trans("Reminders").'</td><td>';
if (count($object->reminders) > 0) {
if (count($object->reminders) > 0) {
$tmpuserstatic = new User($db);
$checked = 'checked';
$keys = array_keys($object->reminders);
$firstreminderId = array_shift($keys);
foreach ($object->reminders as $actioncommreminderid => $actioncommreminder) {
print $TRemindTypes[$actioncommreminder->typeremind];
if ($actioncommreminder->fk_user > 0) {
$tmpuserstatic->fetch($actioncommreminder->fk_user);
print ' ('.$tmpuser->getNomUrl(0, '', 0, 0, 16).')';
}
print ' - '.$actioncommreminder->offsetvalue.' '.$TDurationTypes[$actioncommreminder->offsetunit];
if ($actioncommreminder->status == $actioncommreminder::STATUS_TODO) {
print ' - <span class="opacitymedium">';
print $langs->trans("NotSent");
print ' </span>';
} elseif ($actioncommreminder->status == $actioncommreminder::STATUS_DONE) {
print ' - <span class="opacitymedium">';
print $langs->trans("Done");
print ' </span>';
}
}
}
$actionCommReminder = $object->reminders[$firstreminderId];
}
else
{
$checked = '';
$actionCommReminder = new ActionCommReminder($db);
$actionCommReminder->offsetvalue = 10;
$actionCommReminder->offsetunit = 'i';
$actionCommReminder->typeremind = 'email';
}
print '<tr><td>'.$langs->trans("AddReminder").'</td><td colspan="3"><input type="checkbox" id="addreminder" name="addreminder" '.$checked.'></td></tr>';
print '<div class="reminderparameters" '.(empty($checked) ? 'style="display: none;"' : '').'>';
print '<table class="border centpercent">';
//Reminder
print '<tr><td class="titlefieldcreate nowrap">'.$langs->trans("ReminderTime").'</td><td colspan="3">';
print '<input type="number" name="offsetvalue" value="'.$actionCommReminder->offsetvalue.'" size="5">';
print '</td></tr>';
//Time Type
print '<tr><td class="titlefieldcreate nowrap">'.$langs->trans("TimeType").'</td><td colspan="3">';
print $form->selectTypeDuration('offsetunit', $actionCommReminder->offsetunit);
print '</td></tr>';
//Reminder Type
$TRemindTypes = array();
if (!empty($conf->global->AGENDA_REMINDER_EMAIL)) $TRemindTypes['email'] = $langs->trans('EMail');
if (!empty($conf->global->AGENDA_REMINDER_BROWSER)) $TRemindTypes['browser'] = $langs->trans('BrowserPush');
print '<tr><td class="titlefieldcreate nowrap">'.$langs->trans("ReminderType").'</td><td colspan="3">';
print $form->selectarray('selectremindertype', $TRemindTypes, $actionCommReminder->typeremind);
print '</td></tr>';
$hide = '';
if ($actionCommReminder->typeremind == 'browser') $hide = 'style="display:none;"';
//Mail Model
print '<tr '.$hide.'><td class="titlefieldcreate nowrap">'.$langs->trans("EMailTemplates").'</td><td colspan="3">';
print $form->selectModelMail('actioncommsend', 'actioncomm_send', 1);
print '</td></tr>';
print '</table>';
print "\n".'<script type="text/javascript">';
print '$(document).ready(function () {
$("#addreminder").click(function(){
if (this.checked) {
$(".reminderparameters").show();
} else {
$(".reminderparameters").hide();
}
});
$("#selectremindertype").change(function(){
var selected_option = $("#selectremindertype option:selected").val();
if(selected_option == "email") {
$("#select_actioncommsendmodel_mail").closest("tr").show();
} else {
$("#select_actioncommsendmodel_mail").closest("tr").hide();
};
});
})';
print '</script>'."\n";
}
dol_fiche_end();
@ -1980,7 +2068,7 @@ if ($id > 0)
{
$filtreuserid = $user->id;
if ($user->rights->agenda->allactions->read) $filtreuserid = 0;
$object->loadReminders('', $filteruserid);
$object->loadReminders('', $filteruserid, false);
print '<tr><td class="titlefieldcreate nowrap">'.$langs->trans("Reminders").'</td><td>';
@ -1991,7 +2079,7 @@ if ($id > 0)
print $TRemindTypes[$actioncommreminder->typeremind];
if ($actioncommreminder->fk_user > 0) {
$tmpuserstatic->fetch($actioncommreminder->fk_user);
print ' ('.$tmpuser->getNomUrl(0, '', 0, 0, 16).')';
print ' ('.$tmpuserstatic->getNomUrl(0, '', 0, 0, 16).')';
}
print ' - '.$actioncommreminder->offsetvalue.' '.$TDurationTypes[$actioncommreminder->offsetunit];
if ($actioncommreminder->status == $actioncommreminder::STATUS_TODO) {
@ -2003,6 +2091,7 @@ if ($id > 0)
print $langs->trans("Done");
print ' </span>';
}
print '<br>';
}
}

View File

@ -925,6 +925,18 @@ class ActionComm extends CommonObject
}
}
if (!$error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
$sql .= " WHERE fk_actioncomm = ".$this->id;
$res = $this->db->query($sql);
if (!$res) {
$this->error = $this->db->lasterror();
$error++;
}
}
// Removed extrafields
if (!$error) {
$result = $this->deleteExtraFields();
@ -1958,9 +1970,10 @@ class ActionComm extends CommonObject
*
* @param string $type Type of reminder 'browser' or 'email'
* @param int $fk_user Id of user
* @param bool $onlypast true = get only past reminder, false = get all reminders linked to this
* @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
*/
public function loadReminders($type = '', $fk_user = 0)
public function loadReminders($type = '', $fk_user = 0, $onlypast = true)
{
global $conf, $langs, $user;
@ -1971,7 +1984,10 @@ class ActionComm extends CommonObject
//Select all action comm reminders for event
$sql = "SELECT rowid as id, typeremind, dateremind, status, offsetvalue, offsetunit, fk_user";
$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm_reminder";
$sql .= " WHERE fk_actioncomm = ".$this->id." AND dateremind <= '".$this->db->idate(dol_now())."'";
$sql .= " WHERE fk_actioncomm = ".$this->id;
if ($onlypast) {
$sql .= " AND dateremind <= '".$this->db->idate(dol_now())."'";
}
if ($type) {
$sql .= " AND typeremind ='".$this->db->escape($type)."'";
}