2007-05-28 23:17:15 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare - abstract base class for tracking (history log, notifications, ...)
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @copyright (c) 2007 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2008-05-10 14:06:15 +02:00
|
|
|
* @version $Id$
|
2007-05-28 23:17:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract base class for trackering:
|
|
|
|
* - logging all modifications of an entry
|
|
|
|
* - notifying users about changes in an entry
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-05-28 23:17:15 +02:00
|
|
|
* You need to extend these class in your application:
|
|
|
|
* 1. set the required class-vars: app, id_field
|
|
|
|
* 2. optional set class-vars: creator_field, assigned_field, check2prefs
|
|
|
|
* 3. implement the required methods: get_config, get_details
|
2008-01-30 19:58:00 +01:00
|
|
|
* 4. optionally re-implement: get_title, get_subject, get_body, get_attachments, get_link, get_notification_link, get_message
|
2007-05-28 23:17:15 +02:00
|
|
|
* They are all documented in this file via phpDocumentor comments.
|
|
|
|
*/
|
|
|
|
class bo_tracking
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Application we are tracking
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $app;
|
|
|
|
/**
|
|
|
|
* Name of the id-field, used as id in the history log (required!)
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $id_field;
|
|
|
|
/**
|
|
|
|
* Name of the field with the creator id, if the creator of an entry should be notified
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $creator_field;
|
|
|
|
/**
|
|
|
|
* Name of the field with the id(s) of assinged users, if they should be notified
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $assigned_field;
|
|
|
|
/**
|
|
|
|
* Can be used to map the following prefs to different names:
|
|
|
|
* - notify_creator - user wants to be notified for items he created
|
|
|
|
* - notify_assigned - user wants to be notified for items assigned to him
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $check2pref;
|
|
|
|
/**
|
|
|
|
* Translate field-name to 2-char history status
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $field2history = array();
|
|
|
|
/**
|
|
|
|
* Should the user (passed to the track method or current user if not passed) be used as sender or get_config('sender')
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $prefer_user_as_sender = true;
|
2007-06-10 10:50:03 +02:00
|
|
|
/**
|
|
|
|
* Should the current user be email-notified (about change he made himself)
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-06-10 10:50:03 +02:00
|
|
|
* Popup notifications are never send to the current user!
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $notify_current_user = false;
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Array with error-messages if track($data,$old) returns false
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $errors = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* instance of the historylog object for the app we are tracking
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var historylog
|
|
|
|
*/
|
|
|
|
var $historylog;
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Current user, can be set via bo_tracking::track(,,$user)
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-05-28 23:17:15 +02:00
|
|
|
* @access private
|
|
|
|
* @var int;
|
|
|
|
*/
|
|
|
|
var $user;
|
|
|
|
/**
|
|
|
|
* Saved user preferences, if send_notifications need to set an other language
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $save_prefs;
|
|
|
|
/**
|
|
|
|
* Datetime format of the currently notified user (send_notificaton)
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $datetime_format;
|
|
|
|
/**
|
|
|
|
* Offset to server-time of the currently notified user (send_notificaton)
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
var $tz_offset_s;
|
2007-11-27 06:49:25 +01:00
|
|
|
/**
|
|
|
|
* Should the class allow html content (for notifications)
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-11-27 06:49:25 +01:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $html_content_allow = false;
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-06-10 10:50:03 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @return bo_tracking
|
|
|
|
*/
|
2008-05-10 14:06:15 +02:00
|
|
|
function __construct()
|
2007-06-10 10:50:03 +02:00
|
|
|
{
|
2008-03-09 15:35:48 +01:00
|
|
|
|
2007-06-10 10:50:03 +02:00
|
|
|
}
|
2007-05-28 23:17:15 +02:00
|
|
|
|
2008-05-10 14:06:15 +02:00
|
|
|
function bo_tracking()
|
|
|
|
{
|
|
|
|
self::__construct
|
|
|
|
}
|
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Get a config value, which can depend on $data and $old
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-05-28 23:17:15 +02:00
|
|
|
* Need to be implemented in your extended tracking class!
|
|
|
|
*
|
2008-05-10 14:06:15 +02:00
|
|
|
* @abstract
|
2007-05-28 23:17:15 +02:00
|
|
|
* @param string $what possible values are:
|
|
|
|
* - 'copy' array of email addresses notifications should be copied too, can depend on $data
|
|
|
|
* - 'lang' string lang code for copy mail
|
|
|
|
* - 'subject' string subject line for the notification of $data,$old, defaults to link-title
|
|
|
|
* @param array $data current entry
|
|
|
|
* @param array $old=null old/last state of the entry or null for a new entry
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function get_config($name,$data,$old=null)
|
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
die('You need to extend the bo_tracking class, to be able to use it (abstract base class)!');
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Tracks the changes in one entry $data, by comparing it with the last version in $old
|
|
|
|
*
|
|
|
|
* @param array $data current entry
|
|
|
|
* @param array $old=null old/last state of the entry or null for a new entry
|
|
|
|
* @param int $user=null user who made the changes, default to current user
|
2007-06-21 18:25:08 +02:00
|
|
|
* @param boolean $deleted=null can be set to true to let the tracking know the item got deleted or undelted
|
2007-05-28 23:17:15 +02:00
|
|
|
* @return int/boolean false on error, integer number of changes logged or true for new entries ($old == null)
|
|
|
|
*/
|
2007-06-21 18:25:08 +02:00
|
|
|
function track($data,$old=null,$user=null,$deleted=null)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$this->user = !is_null($user) ? $user : $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
|
|
|
|
$changes = true;
|
|
|
|
|
2007-06-21 18:25:08 +02:00
|
|
|
if ($old && $this->field2history)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
2007-06-21 18:25:08 +02:00
|
|
|
$changes = $this->save_history($data,$old,$deleted);
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2007-06-21 18:25:08 +02:00
|
|
|
// do not run do_notifications if we have no changes
|
|
|
|
if ($changes && !$this->do_notifications($data,$old,$deleted))
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$changes = false;
|
|
|
|
}
|
|
|
|
return $changes;
|
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Save changes to the history log
|
|
|
|
*
|
|
|
|
* @internal use only track($data,$old)
|
|
|
|
* @param array $data current entry
|
|
|
|
* @param array $old=null old/last state of the entry or null for a new entry
|
2007-06-21 18:25:08 +02:00
|
|
|
* @param boolean $deleted=null can be set to true to let the tracking know the item got deleted or undelted
|
|
|
|
* @return int number of log-entries made
|
2007-05-28 23:17:15 +02:00
|
|
|
*/
|
2007-06-21 18:25:08 +02:00
|
|
|
function save_history($data,$old,$deleted=null)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$changes = 0;
|
|
|
|
foreach($this->field2history as $name => $status)
|
|
|
|
{
|
2007-06-13 23:37:05 +02:00
|
|
|
if ($old[$name] != $data[$name] && !(!$old[$name] && !$data[$name]))
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
if (!is_object($this->historylog))
|
|
|
|
{
|
|
|
|
require_once(EGW_API_INC.'/class.historylog.inc.php');
|
|
|
|
$this->historylog =& new historylog($this->app);
|
|
|
|
}
|
2007-06-13 23:37:05 +02:00
|
|
|
$this->historylog->add($status,$data[$this->id_field],
|
|
|
|
is_array($data[$name]) ? implode(',',$data[$name]) : $data[$name],
|
|
|
|
is_array($old[$name]) ? implode(',',$old[$name]) : $old[$name]);
|
2007-05-28 23:17:15 +02:00
|
|
|
++$changes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $changes;
|
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* sending all notifications for the changed entry
|
|
|
|
*
|
|
|
|
* @internal use only track($data,$old,$user)
|
|
|
|
* @param array $data current entry
|
|
|
|
* @param array $old=null old/last state of the entry or null for a new entry
|
2007-06-21 18:25:08 +02:00
|
|
|
* @param boolean $deleted=null can be set to true to let the tracking know the item got deleted or undelted
|
2007-05-28 23:17:15 +02:00
|
|
|
* @return boolean true on success, false on error (error messages are in $this->errors)
|
|
|
|
*/
|
2007-06-21 18:25:08 +02:00
|
|
|
function do_notifications($data,$old,$deleted=null)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$this->errors = $email_sent = array();
|
|
|
|
|
2007-06-10 10:50:03 +02:00
|
|
|
if (!$this->notify_current_user) // should we notify the current user about his own changes
|
|
|
|
{
|
|
|
|
//error_log("do_notificaton() adding user=$this->user to email_sent, to not notify him");
|
|
|
|
$email_sent[] = $GLOBALS['egw']->accounts->id2name($this->user,'account_email');
|
|
|
|
}
|
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
// entry creator
|
|
|
|
if ($this->creator_field && ($email = $GLOBALS['egw']->accounts->id2name($data[$this->creator_field],'account_email')) &&
|
2008-05-10 14:06:15 +02:00
|
|
|
!in_array($email, $email_sent))
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$this->send_notification($data,$old,$email,$data[$this->creator_field],'notify_creator');
|
2008-05-10 14:06:15 +02:00
|
|
|
$email_sent[] = $email;
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// assigned / responsible users
|
|
|
|
if ($this->assigned_field)
|
|
|
|
{
|
2007-06-11 15:43:12 +02:00
|
|
|
//error_log("bo_tracking::do_notifications() data[$this->assigned_field]=".print_r($data[$this->assigned_field],true).", old[$this->assigned_field]=".print_r($old[$this->assigned_field],true));
|
2007-05-28 23:17:15 +02:00
|
|
|
$assignees = $old_assignees = array();
|
|
|
|
if ($data[$this->assigned_field]) // current assignments
|
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
$assignees = is_array($data[$this->assigned_field]) ?
|
2007-05-28 23:17:15 +02:00
|
|
|
$data[$this->assigned_field] : explode(',',$data[$this->assigned_field]);
|
|
|
|
}
|
|
|
|
if ($old && $old[$this->assigned_field])
|
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
$old_assignees = is_array($old[$this->assigned_field]) ?
|
2007-05-28 23:17:15 +02:00
|
|
|
$old[$this->assigned_field] : explode(',',$old[$this->assigned_field]);
|
|
|
|
}
|
|
|
|
foreach(array_unique(array_merge($assignees,$old_assignees)) as $assignee)
|
|
|
|
{
|
2007-06-11 15:43:12 +02:00
|
|
|
//error_log("bo_tracking::do_notifications() assignee=$assignee, type=".$GLOBALS['egw']->accounts->get_type($assignee).", email=".$GLOBALS['egw']->accounts->id2name($assignee,'account_email'));
|
2007-05-28 23:17:15 +02:00
|
|
|
if (!$assignee) continue;
|
|
|
|
|
|
|
|
// item assignee is a user
|
|
|
|
if ($GLOBALS['egw']->accounts->get_type($assignee) == 'u')
|
|
|
|
{
|
|
|
|
if (($email = $GLOBALS['egw']->accounts->id2name($assignee,'account_email')) && !in_array($email, $email_sent))
|
|
|
|
{
|
2007-06-21 18:25:08 +02:00
|
|
|
$this->send_notification($data,$old,$email,$assignee,'notify_assigned',
|
|
|
|
in_array($assignee,$assignees) !== in_array($assignee,$old_assignees) || $deleted); // assignment changed
|
2008-05-10 14:06:15 +02:00
|
|
|
$email_sent[] = $email;
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // item assignee is a group
|
|
|
|
{
|
|
|
|
foreach($GLOBALS['egw']->accounts->members($assignee,true) as $u)
|
|
|
|
{
|
2007-06-20 22:53:54 +02:00
|
|
|
if (($email = $GLOBALS['egw']->accounts->id2name($u,'account_email')) && !in_array($email, $email_sent))
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
2007-06-21 18:25:08 +02:00
|
|
|
$this->send_notification($data,$old,$email,$u,'notify_assigned',
|
|
|
|
in_array($u,$assignees) !== in_array($u,$old_assignees) || $deleted); // assignment changed
|
2007-05-28 23:17:15 +02:00
|
|
|
$email_sent[] = $email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// notification copies
|
|
|
|
if (($copies = $this->get_config('copy',$data,$old)))
|
|
|
|
{
|
|
|
|
$lang = $this->get_config('lang',$data,$old);
|
|
|
|
foreach($copies as $email)
|
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
if (strchr($email,'@') !== false && !in_array($email, $email_sent))
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$this->send_notification($data,$old,$email,$lang,'notify_copy');
|
|
|
|
$email_sent[] = $email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore the user enviroment
|
|
|
|
if ($this->save_prefs) $GLOBALS['egw_info']['user'] = $this->save_prefs; unset($this->save_prefs);
|
|
|
|
if ($GLOBALS['egw_info']['user']['preferences']['common']['lang'] != $GLOBALS['egw']->translation->userlang)
|
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
$GLOBALS['egw']->translation->init();
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
return !count($this->errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sending a notification to the given email-address
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-06-10 10:50:03 +02:00
|
|
|
* Called by track() or externally for sending async notifications
|
2007-05-28 23:17:15 +02:00
|
|
|
*
|
|
|
|
* @param array $data current entry
|
|
|
|
* @param array $old=null old/last state of the entry or null for a new entry
|
|
|
|
* @param string $email address to send the notification to
|
|
|
|
* @param string $user_or_lang='en' user-id or 2 char lang-code for a non-system user
|
|
|
|
* @param string $check=null pref. to check if a notification is wanted
|
2007-06-21 18:25:08 +02:00
|
|
|
* @param boolean $assignment_changed=true the assignment of the user $user_or_lang changed
|
2007-05-28 23:17:15 +02:00
|
|
|
* @return boolean true on success or false on error (error-message is in $this->errors)
|
|
|
|
*/
|
2007-06-21 18:25:08 +02:00
|
|
|
function send_notification($data,$old,$email,$user_or_lang,$check=null,$assignment_changed=true)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
2007-06-11 15:43:12 +02:00
|
|
|
//error_log("bo_trackering::send_notification(,,'$email',$user_or_lang,$check)");
|
2007-05-28 23:17:15 +02:00
|
|
|
if (!$email) return false;
|
2007-11-22 09:29:16 +01:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
if (!$this->save_prefs) $this->save_prefs = $GLOBALS['egw_info']['user'];
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
if (is_numeric($user_or_lang)) // user --> read everything from his prefs
|
|
|
|
{
|
|
|
|
if ($user_or_lang != $this->user)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->preferences->preferences($user_or_lang);
|
|
|
|
$GLOBALS['egw_info']['user']['preferences'] = $GLOBALS['egw']->preferences->read_repository();
|
|
|
|
}
|
2007-06-21 18:25:08 +02:00
|
|
|
if ($check && $this->check2pref) $check = $this->check2pref[$check];
|
|
|
|
if ($check && !$GLOBALS['egw_info']['user']['preferences'][$this->app][$check])
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
return false; // no notification requested
|
|
|
|
}
|
2007-06-21 18:25:08 +02:00
|
|
|
if ($check && $GLOBALS['egw_info']['user']['preferences'][$this->app][$check] === 'assignment' && !$assignment_changed)
|
|
|
|
{
|
|
|
|
return false; // only notification about changed assignment requested
|
|
|
|
}
|
2007-11-25 09:14:50 +01:00
|
|
|
if($this->user == $user_or_lang && !$this->notify_current_user)
|
2008-05-10 14:06:15 +02:00
|
|
|
{
|
2007-11-22 09:29:16 +01:00
|
|
|
return false; // no popup for own actions
|
|
|
|
}
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// for the notification copy, we use the default-prefs plus the language from the the tracker config
|
|
|
|
$GLOBALS['egw_info']['user']['preferences'] = $GLOBALS['egw']->preferences->default;
|
|
|
|
$GLOBALS['egw_info']['user']['preferences']['common']['lang'] = $user_or_lang;
|
|
|
|
}
|
|
|
|
if ($lang != $GLOBALS['egw']->translation->userlang) // load the right language if needed
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->translation->init();
|
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-12-10 09:26:29 +01:00
|
|
|
// send over notification_app
|
|
|
|
if ($GLOBALS['egw_info']['apps']['notifications']['enabled']) {
|
2007-11-22 09:29:16 +01:00
|
|
|
// send via notification_app
|
2007-12-10 09:26:29 +01:00
|
|
|
$receiver = is_numeric($user_or_lang) ? $user_or_lang : $email;
|
2007-11-22 09:29:16 +01:00
|
|
|
try {
|
2008-01-30 19:58:00 +01:00
|
|
|
$notification = new notifications();
|
2007-12-10 09:26:29 +01:00
|
|
|
$notification->set_receivers(array($receiver));
|
2008-01-30 19:58:00 +01:00
|
|
|
$notification->set_message($this->get_body(false,$data,$old,false)); // set message as plaintext
|
|
|
|
$notification->set_message($this->get_body(true,$data,$old,false)); // and html
|
2007-12-10 09:26:29 +01:00
|
|
|
$notification->set_sender($this->get_sender($data,$old,true));
|
|
|
|
$notification->set_subject($this->get_subject($data,$old));
|
2008-01-30 19:58:00 +01:00
|
|
|
$notification->set_links(array($this->get_notification_link($data,$old)));
|
2007-12-10 09:26:29 +01:00
|
|
|
$attachments = $this->get_attachments($data,$old);
|
2007-11-22 09:29:16 +01:00
|
|
|
if(is_array($attachments)) { $notification->set_attachments($attachments); }
|
|
|
|
$notification->send();
|
|
|
|
}
|
|
|
|
catch (Exception $exception) {
|
|
|
|
$this->errors[] = $exception->getMessage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2008-01-30 19:58:00 +01:00
|
|
|
error_log('tracking: cannot send any notifications because notifications is not installed');
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
2007-06-10 10:50:03 +02:00
|
|
|
* Return date+time formatted for the currently notified user (prefs in $GLOBALS['egw_info']['user']['preferences'])
|
2007-05-28 23:17:15 +02:00
|
|
|
*
|
|
|
|
* @param int $timestamp
|
2007-06-10 10:50:03 +02:00
|
|
|
* @param boolean $do_time=true true=allways (default), false=never print the time, null=print time if != 00:00
|
2007-05-28 23:17:15 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2007-06-10 10:50:03 +02:00
|
|
|
function datetime($timestamp,$do_time=true)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
2007-06-10 10:50:03 +02:00
|
|
|
if (is_null($do_time))
|
|
|
|
{
|
|
|
|
$do_time = date('H:i',$timestamp+$this->tz_offset_s) != '00:00';
|
|
|
|
}
|
|
|
|
$format = $GLOBALS['egw_info']['user']['preferences']['common']['dateformat'];
|
|
|
|
if ($do_time) $format .= ' '.($GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] != 12 ? 'H:i' : 'h:i a');
|
|
|
|
|
|
|
|
//error_log("bo_tracking::datetime($timestamp,$do_time)=date('$format',$timestamp+$this->tz_offset_s)='".date($format,$timestamp+$this->tz_offset_s).'\')');
|
|
|
|
return date($format,$timestamp+3600 * $GLOBALS['egw_info']['user']['preferences']['common']['tz_offset']);
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Get sender address
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-06-03 13:31:01 +02:00
|
|
|
* The default implementation prefers depending on the prefer_user_as_sender class-var the user over
|
2008-05-10 14:06:15 +02:00
|
|
|
* what is returned by get_config('sender').
|
|
|
|
*
|
2007-06-06 10:18:45 +02:00
|
|
|
* @param int $user account_lid of user
|
2007-05-28 23:17:15 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
2007-12-10 09:26:29 +01:00
|
|
|
* @param bool $prefer_id returns the userid rather than email
|
|
|
|
* @return string or userid
|
2007-05-28 23:17:15 +02:00
|
|
|
*/
|
2007-12-10 09:26:29 +01:00
|
|
|
function get_sender($data,$old,$prefer_id=false)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
2007-06-03 13:31:01 +02:00
|
|
|
$sender = $this->get_config('sender',$data,$old);
|
2007-06-06 10:18:45 +02:00
|
|
|
//echo "<p>bo_tracking::get_sender() get_config('sender',...)='".htmlspecialchars($sender)."'</p>\n";
|
|
|
|
|
2008-05-10 14:06:15 +02:00
|
|
|
if (($this->prefer_user_as_sender || !$sender) && $this->user &&
|
2007-06-03 13:31:01 +02:00
|
|
|
($email = $GLOBALS['egw']->accounts->id2name($this->user,'account_email')))
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
$name = $GLOBALS['egw']->accounts->id2name($this->user,'account_fullname');
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-12-10 09:26:29 +01:00
|
|
|
if($prefer_id) {
|
|
|
|
$sender = $this->user;
|
|
|
|
} else {
|
|
|
|
$sender = $name ? $name.' <'.$email.'>' : $email;
|
|
|
|
}
|
2007-06-06 10:18:45 +02:00
|
|
|
}
|
|
|
|
elseif(!$sender)
|
|
|
|
{
|
|
|
|
$sender = 'eGroupWare '.lang($this->app).' <noreply@'.$GLOBALS['egw_info']['server']['mail_suffix'].'>';
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2007-06-06 10:18:45 +02:00
|
|
|
//echo "<p>bo_tracking::get_sender()='".htmlspecialchars($sender)."'</p>\n";
|
|
|
|
return $sender;
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
/**
|
|
|
|
* Get the title for a given entry, can be reimplemented
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_title($data,$old)
|
|
|
|
{
|
2008-03-25 17:42:23 +01:00
|
|
|
return egw_link::title($this->app,$data[$this->id_field]);
|
2008-01-30 19:58:00 +01:00
|
|
|
}
|
2007-05-28 23:17:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the subject for a given entry, can be reimplemented
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-05-28 23:17:15 +02:00
|
|
|
* Default implementation uses the link-title
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_subject($data,$old)
|
|
|
|
{
|
2008-03-25 17:42:23 +01:00
|
|
|
return egw_link::title($this->app,$data[$this->id_field]);
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the modified / new message (1. line of mail body) for a given entry, can be reimplemented
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-05-28 23:17:15 +02:00
|
|
|
* Default implementation does nothing
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_message($data,$old)
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a link to view the entry, can be reimplemented
|
2008-05-10 14:06:15 +02:00
|
|
|
*
|
2007-05-28 23:17:15 +02:00
|
|
|
* Default implementation checks get_config('link') (appending the id) or link::view($this->app,$id)
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
2007-06-10 10:50:03 +02:00
|
|
|
* @param string $allow_popup=false if true return array(link,popup-size) incl. session info an evtl. partial url (no host-part)
|
|
|
|
* @return string/array string with link (!$allow_popup) or array(link,popup-size), popup size is something like '640x480'
|
2007-05-28 23:17:15 +02:00
|
|
|
*/
|
2007-06-10 10:50:03 +02:00
|
|
|
function get_link($data,$old,$allow_popup=false)
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
|
|
|
if (($link = $this->get_config('link',$data,$old)))
|
|
|
|
{
|
|
|
|
if (strpos($link,$this->id_field.'=') === false)
|
|
|
|
{
|
|
|
|
$link .= '&'.$this->id_field.'='.$data[$this->id_field];
|
|
|
|
}
|
|
|
|
}
|
2007-06-13 23:37:05 +02:00
|
|
|
else
|
2007-05-28 23:17:15 +02:00
|
|
|
{
|
2008-03-25 17:42:23 +01:00
|
|
|
if (($view = egw_link::view($this->app,$data[$this->id_field])))
|
2007-06-13 23:37:05 +02:00
|
|
|
{
|
|
|
|
$link = $GLOBALS['egw']->link('/index.php',$view);
|
2008-03-25 17:42:23 +01:00
|
|
|
$popup = egw_link::is_popup($this->app,'view');
|
2007-06-13 23:37:05 +02:00
|
|
|
}
|
2007-06-10 10:50:03 +02:00
|
|
|
}
|
|
|
|
if ($link{0} == '/')
|
|
|
|
{
|
|
|
|
$link = ($_SERVER['HTTPS'] || $GLOBALS['egw_info']['server']['enforce_ssl'] ? 'https://' : 'http://').
|
|
|
|
($GLOBALS['egw_info']['server']['hostname'] ? $GLOBALS['egw_info']['server']['hostname'] : $_SERVER['HTTP_HOST']).$link;
|
|
|
|
}
|
|
|
|
if (!$allow_popup)
|
|
|
|
{
|
|
|
|
// remove the session-id in the notification mail!
|
|
|
|
$link = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',$link);
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-06-10 10:50:03 +02:00
|
|
|
if ($popup) $link .= '&nopopup=1';
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2007-06-10 10:50:03 +02:00
|
|
|
return $allow_popup ? array($link,$popup) : $link;
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
2008-01-30 19:58:00 +01:00
|
|
|
* Get a link for notifications to view the entry, can be reimplemented
|
2007-11-22 09:29:16 +01:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
|
|
|
* @return array with link
|
|
|
|
*/
|
|
|
|
function get_notification_link($data,$old)
|
|
|
|
{
|
|
|
|
if (!is_object($GLOBALS['egw']->link))
|
|
|
|
{
|
2008-01-30 19:58:00 +01:00
|
|
|
$GLOBALS['egw']->link = new bolink();
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
2008-03-25 17:42:23 +01:00
|
|
|
if($view = egw_link::view($this->app,$data[$this->id_field])) {
|
2008-01-30 19:58:00 +01:00
|
|
|
return array( 'text' => $this->get_title($data,$old),
|
|
|
|
'view' => $view,
|
2008-03-25 17:42:23 +01:00
|
|
|
'popup' => egw_link::is_popup($this->app,'view'),
|
2007-11-22 09:29:16 +01:00
|
|
|
);
|
|
|
|
}
|
2008-01-30 19:58:00 +01:00
|
|
|
return false;
|
2008-05-10 14:06:15 +02:00
|
|
|
}
|
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
2007-11-24 15:01:27 +01:00
|
|
|
* Get the body of the notification message, can be reimplemented
|
|
|
|
*
|
|
|
|
* @param boolean $html_email
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
2008-01-30 19:58:00 +01:00
|
|
|
* @param boolean $integrate_link to have links embedded inside the body
|
2007-11-24 15:01:27 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2008-01-30 19:58:00 +01:00
|
|
|
function get_body($html_email,$data,$old,$integrate_link = true)
|
2007-11-24 15:01:27 +01:00
|
|
|
{
|
|
|
|
$body = '';
|
|
|
|
if ($html_email)
|
2008-05-10 14:06:15 +02:00
|
|
|
{
|
2007-11-24 15:45:03 +01:00
|
|
|
$body = '<table cellspacing="2" cellpadding="0" border="0" width="100%">'."\n";
|
2007-11-24 15:01:27 +01:00
|
|
|
}
|
|
|
|
// new or modified message
|
|
|
|
if (($message = $this->get_message($data,$old)))
|
|
|
|
{
|
|
|
|
$body .= $this->format_line($html_email,'message',false,$message);
|
|
|
|
}
|
2008-01-30 19:58:00 +01:00
|
|
|
if ($integrate_link && ($link = $this->get_link($data,$old)))
|
2007-11-24 15:01:27 +01:00
|
|
|
{
|
|
|
|
$body .= $this->format_line($html_email,'link',false,lang('You can respond by visiting:'),$link);
|
|
|
|
}
|
|
|
|
foreach($this->get_details($data) as $name => $detail)
|
|
|
|
{
|
|
|
|
// if there's no old entry, the entry is not modified by definition
|
|
|
|
// if both values are '', 0 or null, we count them as equal too
|
|
|
|
$modified = $old && $data[$name] != $old[$name] && !(!$data[$name] && !$old[$name]);
|
|
|
|
//if ($modified) error_log("data[$name]=".print_r($data[$name],true).", old[$name]=".print_r($old[$name],true)." --> modified=".(int)$modified);
|
|
|
|
if (empty($detail['value']) && !$modified) continue; // skip unchanged, empty values
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-11-24 15:01:27 +01:00
|
|
|
$body .= $this->format_line($html_email,$detail['type'],$modified,
|
|
|
|
($detail['label'] ? $detail['label'].': ':'').$detail['value']);
|
|
|
|
}
|
|
|
|
if ($html_email)
|
|
|
|
{
|
2007-11-24 15:45:03 +01:00
|
|
|
$body .= "</table>\n";
|
2007-11-24 15:01:27 +01:00
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-11-24 15:01:27 +01:00
|
|
|
return $body;
|
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
|
|
|
* Format one line to the mail body
|
|
|
|
*
|
2008-05-10 14:06:15 +02:00
|
|
|
* @internal
|
2007-05-28 23:17:15 +02:00
|
|
|
* @param boolean $html_mail
|
|
|
|
* @param string $type 'link', 'message', 'summary', 'multiline', 'reply' and ''=regular content
|
|
|
|
* @param boolean $modified mark field as modified
|
|
|
|
* @param string $line
|
|
|
|
* @param string $link=null
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function format_line($html_mail,$type,$modified,$line,$link=null)
|
|
|
|
{
|
|
|
|
$content = '';
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
if ($html_mail)
|
|
|
|
{
|
2008-03-09 15:35:48 +01:00
|
|
|
if (!$this->html_content_allow) $line = html::htmlspecialchars($line); // XSS
|
2007-06-10 10:50:03 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
$color = $modified ? 'red' : false;
|
2008-01-30 19:58:00 +01:00
|
|
|
$size = '110%';
|
2007-05-28 23:17:15 +02:00
|
|
|
$bold = false;
|
|
|
|
$background = '#FFFFF1';
|
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case 'message':
|
|
|
|
$background = '#D3DCE3;';
|
|
|
|
$bold = true;
|
|
|
|
break;
|
|
|
|
case 'link':
|
|
|
|
$background = '#F1F1F1';
|
|
|
|
break;
|
|
|
|
case 'summary':
|
|
|
|
$background = '#F1F1F1';
|
|
|
|
$bold = true;
|
|
|
|
break;
|
|
|
|
case 'multiline':
|
2007-11-27 06:49:25 +01:00
|
|
|
// Only Convert nl2br on non-html content
|
|
|
|
$pos = strpos($line, '<br');
|
2008-05-10 14:06:15 +02:00
|
|
|
if ($pos===false)
|
2007-11-27 06:49:25 +01:00
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
$line = nl2br($line);
|
|
|
|
}
|
2007-05-28 23:17:15 +02:00
|
|
|
break;
|
|
|
|
case 'reply':
|
2008-05-10 14:06:15 +02:00
|
|
|
$background = '#F1F1F1';
|
2007-05-28 23:17:15 +02:00
|
|
|
break;
|
|
|
|
default:
|
2007-12-05 10:05:57 +01:00
|
|
|
$size = '100%';
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
$style = ($bold ? 'font-weight:bold;' : '').($size ? 'font-size:'.$size.';' : '').($color?'color:'.$color:'');
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
$content = '<tr style="background-color: '.$background.';"><td style="'.$style.'">';
|
|
|
|
}
|
|
|
|
else // text-mail
|
|
|
|
{
|
2008-05-10 14:06:15 +02:00
|
|
|
if ($type == 'reply') $content = str_repeat('-',64)."\n";
|
2007-06-10 10:50:03 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
if ($modified) $content .= '> ';
|
|
|
|
}
|
|
|
|
$content .= $line;
|
2007-06-10 10:50:03 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
if ($link)
|
|
|
|
{
|
|
|
|
$content .= ' ';
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-06-10 10:50:03 +02:00
|
|
|
if ($html_mail)
|
|
|
|
{
|
2007-12-10 09:26:29 +01:00
|
|
|
// the link is often too long for html boxes
|
|
|
|
// chunk-split allows to break lines if needed
|
2008-03-09 15:35:48 +01:00
|
|
|
$content .= html::a_href(chunk_split($link,40,'​'),$link,'','target="_blank"');
|
2007-06-10 10:50:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$content .= $link;
|
|
|
|
}
|
2007-05-28 23:17:15 +02:00
|
|
|
}
|
|
|
|
if ($html_mail) $content .= '</td></tr>';
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
$content .= "\n";
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2008-05-10 14:06:15 +02:00
|
|
|
|
2007-05-28 23:17:15 +02:00
|
|
|
/**
|
2008-01-30 19:58:00 +01:00
|
|
|
* Get the attachments for a notification
|
2007-05-28 23:17:15 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param array $old
|
|
|
|
* @return array with values for either 'content' or 'path' and optionally 'mimetype', 'filename' and 'encoding'
|
|
|
|
*/
|
|
|
|
function get_attachments($data,$old)
|
|
|
|
{
|
2007-11-22 09:29:16 +01:00
|
|
|
return array();
|
2007-06-10 10:50:03 +02:00
|
|
|
}
|
2007-12-04 18:35:17 +01:00
|
|
|
}
|