dolibarr/htdocs/core/class/notify.class.php

246 lines
8.8 KiB
PHP
Raw Normal View History

2004-10-20 23:06:45 +02:00
<?php
/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
2011-09-03 02:14:27 +02:00
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
2003-06-30 19:33:53 +02:00
*
* 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 2 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
2011-08-01 01:45:11 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2003-06-30 19:33:53 +02:00
*/
/**
2010-11-22 10:18:53 +01:00
* \file htdocs/core/class/notify.class.php
2011-06-19 16:35:16 +02:00
* \ingroup notification
* \brief File of class to manage notifications
2010-11-22 10:18:53 +01:00
*/
require_once(DOL_DOCUMENT_ROOT ."/lib/CMailFile.class.php");
2005-01-09 19:30:34 +01:00
2005-07-14 15:03:20 +02:00
/**
2010-11-22 10:18:53 +01:00
* \class Notify
* \brief Classe de gestion des notifications
*/
2005-01-09 19:30:34 +01:00
class Notify
2003-06-30 19:33:53 +02:00
{
2005-07-14 15:03:20 +02:00
var $id;
var $db;
var $error;
var $author;
var $ref;
var $date;
var $duree;
var $note;
2010-03-11 15:31:16 +01:00
var $fk_project;
2005-07-14 15:03:20 +02:00
2009-06-06 04:50:59 +02:00
// Les codes actions sont definis dans la table llx_notify_def
2005-07-14 15:03:20 +02:00
/**
2011-09-03 02:14:27 +02:00
* Constructor
*
* @param DoliDB $DB Database handler
2005-07-14 15:03:20 +02:00
*/
function Notify($DB)
2003-06-30 19:33:53 +02:00
{
2005-07-14 15:03:20 +02:00
$this->db = $DB ;
2003-06-30 19:33:53 +02:00
}
2005-07-14 15:03:20 +02:00
/**
2011-09-11 20:35:38 +02:00
* Renvoie le message signalant les notifications qui auront lieu sur
* un evenement pour affichage dans texte de confirmation evenement
*
* @param action Id of action in llx_c_action_trigger
* @param socid Id of third party
* @return string Message
*/
function confirmMessage($action,$socid)
{
global $langs;
$langs->load("mails");
2009-06-30 13:59:46 +02:00
$nb=$this->countDefinedNotifications($action,$socid);
2010-09-18 15:38:43 +02:00
if ($nb <= 0) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("NoNotificationsWillBeSent");
if ($nb == 1) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("ANotificationsWillBeSent");
if ($nb >= 2) $texte=img_object($langs->trans("Notifications"),'email').' '.$langs->trans("SomeNotificationsWillBeSent",$nb);
return $texte;
}
2009-06-30 13:59:46 +02:00
/**
* \brief Return number of notifications activated for action code and third party
* \param action Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
* \param socid Id of third party
* \return int <0 si ko, sinon nombre de notifications definies
*/
function countDefinedNotifications($action,$socid)
{
global $conf;
2011-06-19 16:35:16 +02:00
$num=-1;
2009-06-30 13:59:46 +02:00
$sql = "SELECT n.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."notify_def as n,";
$sql.= " ".MAIN_DB_PREFIX."socpeople as c,";
$sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
$sql.= " ".MAIN_DB_PREFIX."societe as s";
$sql.= " WHERE n.fk_contact = c.rowid";
$sql.= " AND a.rowid = n.fk_action";
$sql.= " AND n.fk_soc = s.rowid";
if (is_numeric($action)) $sql.= " AND n.fk_action = ".$action; // Old usage
else $sql.= " AND a.code = '".$action."'"; // New usage
$sql.= " AND a.entity = ".$conf->entity;
$sql.= " AND s.rowid = ".$socid;
dol_syslog("Notify.class::countDefinedNotifications $action, $socid");
$resql = $this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
}
else
{
$this->error=$this->db->error.' sql='.$sql;
return -1;
}
2009-06-30 13:59:46 +02:00
return $num;
}
/**
2009-07-30 00:03:20 +02:00
* \brief Check if notification are active for couple action/company.
* If yes, send mail and save trace into llx_notify.
* \param action Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
2009-07-30 00:03:20 +02:00
* \param socid Id of third party
* \param texte Message to send
* \param objet_type Type of object the notification deals on (facture, order, propal, order_supplier...). Just for log in llx_notify.
* \param objet_id Id of object the notification deals on
2009-07-30 00:03:20 +02:00
* \param file Attach a file
2010-06-03 00:50:49 +02:00
* \return int <0 if KO, or number of changes if OK
2005-07-14 15:03:20 +02:00
*/
function send($action, $socid, $texte, $objet_type, $objet_id, $file="")
2003-06-30 19:33:53 +02:00
{
2010-06-03 00:50:49 +02:00
global $conf,$langs,$mysoc,$dolibarr_main_url_root;
2005-07-14 15:03:20 +02:00
2009-07-30 00:03:20 +02:00
$langs->load("other");
dol_syslog("Notify::send action=$action, socid=$socid, texte=$texte, objet_type=$objet_type, objet_id=$objet_id, file=$file");
$sql = "SELECT s.nom, c.email, c.rowid as cid, c.name, c.firstname,";
$sql.= " a.rowid as adid, a.label, a.code, n.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c,";
$sql.= " ".MAIN_DB_PREFIX."c_action_trigger as a,";
$sql.= " ".MAIN_DB_PREFIX."notify_def as n,";
$sql.= " ".MAIN_DB_PREFIX."societe as s";
$sql.= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action";
$sql.= " AND n.fk_soc = s.rowid";
if (is_numeric($action)) $sql.= " AND n.fk_action = ".$action; // Old usage
else $sql.= " AND a.code = '".$action."'"; // New usage
$sql .= " AND s.rowid = ".$socid;
dol_syslog("Notify::send sql=".$sql);
2005-07-14 15:03:20 +02:00
$result = $this->db->query($sql);
if ($result)
{
$num = $this->db->num_rows($result);
$i = 0;
while ($i < $num) // For each notification couple defined (third party/actioncode)
2005-07-14 15:03:20 +02:00
{
$obj = $this->db->fetch_object($result);
$sendto = $obj->firstname . " " . $obj->name . " <".$obj->email.">";
$actiondefid = $obj->adid;
2005-07-14 15:03:20 +02:00
if (dol_strlen($sendto))
2005-07-14 15:03:20 +02:00
{
2010-06-03 00:50:49 +02:00
include_once(DOL_DOCUMENT_ROOT.'/lib/files.lib.php');
$application=($conf->global->MAIN_APPLICATION_TITLE?$conf->global->MAIN_APPLICATION_TITLE:'Dolibarr ERP/CRM');
$subject = '['.$application.'] '.$langs->transnoentitiesnoconv("DolibarrNotification");
$message = $langs->transnoentities("YouReceiveMailBecauseOfNotification",$application,$mysoc->name)."\n";
$message.= $langs->transnoentities("YouReceiveMailBecauseOfNotification2",$application,$mysoc->name)."\n";
2010-06-03 00:50:49 +02:00
$message.= "\n";
$message.= $texte;
// Add link
switch($objet_type)
{
case 'ficheinter':
$link=DOL_URL_ROOT.'/fichinter/fiche.php?id='.$objet_id;
break;
case 'propal':
$link=DOL_URL_ROOT.'/comm/propal.php?id='.$objet_id;
break;
case 'facture':
2011-06-20 14:35:25 +02:00
$link=DOL_URL_ROOT.'/compta/facture.php?facid='.$objet_id;
2010-06-03 00:50:49 +02:00
break;
case 'order':
$link=DOL_URL_ROOT.'/commande/fiche.php?facid='.$objet_id;
break;
case 'order_supplier':
$link=DOL_URL_ROOT.'/fourn/commande/fiche.php?facid='.$objet_id;
break;
}
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',$dolibarr_main_url_root);
if ($link) $message.="\n".$urlwithouturlroot.$link;
$filename = basename($file);
$mimefile=dol_mimetype($file);
$msgishtml=0;
2009-06-30 13:59:46 +02:00
$replyto = $conf->notification->email_from;
2005-07-14 15:03:20 +02:00
$mailfile = new CMailFile($subject,
$sendto,
$replyto,
$message,
array($file),
2010-06-03 00:50:49 +02:00
array($mimefile),
2011-09-17 21:49:50 +02:00
array($filename[count($filename)-1]),
'', '', 0, $msgishtml
);
2005-07-14 15:03:20 +02:00
if ( $mailfile->sendfile() )
{
$sendto = htmlentities($sendto);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_contact, objet_type, objet_id, email)";
2011-09-20 11:59:08 +02:00
$sql.= " VALUES (".$this->db->idate(mktime()).", ".$actiondefid.", ".$obj->cid.", '".$objet_type."', ".$objet_id.", '".$this->db->escape($obj->email)."')";
dol_syslog("Notify::send sql=".$sql);
2005-12-01 00:15:20 +01:00
if (! $this->db->query($sql) )
2005-07-14 15:03:20 +02:00
{
2009-06-06 04:50:59 +02:00
dol_print_error($this->db);
2005-07-14 15:03:20 +02:00
}
}
else
{
$this->error=$mailfile->error;
2010-07-15 01:10:56 +02:00
//dol_syslog("Notify::send ".$this->error, LOG_ERR);
2005-07-14 15:03:20 +02:00
}
}
$i++;
}
return $i;
2005-07-14 15:03:20 +02:00
}
else
{
$this->error=$this->db->error();
return -1;
2005-07-14 15:03:20 +02:00
}
2003-06-30 19:33:53 +02:00
}
2005-07-14 15:03:20 +02:00
}
2003-06-30 19:33:53 +02:00
?>