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

256 lines
9.2 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 3 of the License, or
2003-06-30 19:33:53 +02:00
* (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 .'/core/class/CMailFile.class.php';
2005-01-09 19:30:34 +01:00
2005-07-14 15:03:20 +02:00
/**
2012-01-27 16:12:11 +01:00
* Class to manage notifications
2010-11-22 10:18:53 +01:00
*/
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 __construct($db)
2003-06-30 19:33:53 +02:00
{
$this->db = $db;
2003-06-30 19:33:53 +02:00
}
2005-07-14 15:03:20 +02:00
/**
* Return message that say how many notification will occurs on requested event.
* This is to show confirmation messages before event is done.
2011-09-11 20:35:38 +02:00
*
* @param string $action Id of action in llx_c_action_trigger
* @param int $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
/**
* Return number of notifications activated for action code and third party
*
* @param string $action Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
* @param int $socid Id of third party
* @return int <0 if KO, nb of notifications sent if OK
*/
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
2012-02-01 11:32:55 +01:00
$sql.= " AND s.entity IN (".getEntity('societe', 1).")";
$sql.= " AND s.rowid = ".$socid;
2011-11-06 14:23:09 +01:00
dol_syslog("Notify.class::countDefinedNotifications ".$action.", ".$socid." sql=".$sql);
$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;
}
/**
2012-02-21 00:18:48 +01:00
* Check if notification are active for couple action/company.
* If yes, send mail and save trace into llx_notify.
*
* @param string $action Code of action in llx_c_action_trigger (new usage) or Id of action in llx_c_action_trigger (old usage)
* @param int $socid Id of third party
* @param string $texte Message to send
* @param string $objet_type Type of object the notification deals on (facture, order, propal, order_supplier...). Just for log in llx_notify.
* @param int $objet_id Id of object the notification deals on
* @param string $file Attach a file
* @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(get_class($this)."::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.lastname, 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->lastname . " <".$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
{
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2010-06-03 00:50:49 +02:00
$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
2012-12-12 14:46:13 +01:00
$link='';
2010-06-03 00:50:49 +02:00
switch($objet_type)
{
case 'ficheinter':
2012-12-12 14:46:13 +01:00
$link='/fichinter/fiche.php?id='.$objet_id;
2010-06-03 00:50:49 +02:00
break;
case 'propal':
2012-12-12 14:46:13 +01:00
$link='/comm/propal.php?id='.$objet_id;
2010-06-03 00:50:49 +02:00
break;
case 'facture':
2012-12-12 14:46:13 +01:00
$link='/compta/facture.php?facid='.$objet_id;
2010-06-03 00:50:49 +02:00
break;
case 'order':
$link='/commande/fiche.php?id='.$objet_id;
2010-06-03 00:50:49 +02:00
break;
case 'order_supplier':
$link='/fourn/commande/fiche.php?id='.$objet_id;
2010-06-03 00:50:49 +02:00
break;
}
2012-12-12 14:46:13 +01:00
// Define $urlwithroot
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
if ($link) $message.="\n".$urlwithroot.$link;
2010-06-03 00:50:49 +02:00
$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
2012-02-21 00:18:48 +01:00
$mailfile = new CMailFile(
$subject,
2012-05-06 00:45:15 +02:00
$sendto,
$replyto,
$message,
array($file),
array($mimefile),
array($filename[count($filename)-1]),
'',
'',
0,
$msgishtml
2012-02-21 00:18:48 +01:00
);
2005-07-14 15:03:20 +02:00
2012-02-21 00:18:48 +01:00
if ($mailfile->sendfile())
2005-07-14 15:03:20 +02:00
{
2012-04-30 15:01:25 +02:00
$now=dol_now();
2005-07-14 15:03:20 +02:00
$sendto = htmlentities($sendto);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."notify (daten, fk_action, fk_contact, objet_type, objet_id, email)";
2012-04-30 15:01:25 +02:00
$sql.= " VALUES (".$this->db->idate($now).", ".$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
?>