<?php
// $Id: groupalertlib.php 17035 2009-03-01 21:27:49Z luciash $

/*
 groupalert is used to select user of groups to send alert email (groupware notification)
*/

if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
  header("location: index.php");
  exit;
}

class groupAlertLib extends TikiLib {

	function GroupAlertLib($db) {
		parent::TikiLib($db);
	}

	function AddGroup ($ObjectType, $ObjectNumber,$GroupName,$displayEachUser) {
		if ( $displayEachUser == "on" ) $displayEachUser='y';
		if ( $displayEachUser == "" ) $displayEachUser='n';

		$query ="delete from `tiki_groupalert` where ( `objectType`= ? and `objectId` = ?) ";
		$this->query($query,array($ObjectType,$ObjectNumber));
		if ( $GroupName != '' ) {
			$query = "insert into `tiki_groupalert` ( `groupName`,`objectType`,`objectId`,`displayEachuser` )  values (?,?,?,?)";
			$this->query($query,array($GroupName,$ObjectType,$ObjectNumber,$displayEachUser));
		}
	 return true;
	}

	function GetGroup ($ObjectType,$ObjectNumber) {
		$res= $this->getOne( "select `groupName` from `tiki_groupalert` where ( `objectType` = ? and `objectId` = ? )", array($ObjectType,$ObjectNumber));
		return $res ;
	}

	function GetShowEachUser($ObjectType,$ObjectNumber,$GroupName ) {
		return $this->getOne( "select `displayEachuser` from `tiki_groupalert` where ( `objectType` = ? and `objectId` = ? and `groupName` =? )", array($ObjectType,$ObjectNumber,$GroupName));
	}

	function Notify ( $ListUserToAlert,$URI ) {
		global $tikilib,$userlib ;
		if (!is_array($ListUserToAlert)) {
			return;
		}
		$project=$tikilib->get_preference ("browsertitle");
		$foo = parse_url($_SERVER["REQUEST_URI"]);
		$machine = $tikilib->httpPrefix() . dirname( $foo["path"] );
		$URL=$machine."/".$URI;
		foreach ( $ListUserToAlert as $user ){
		$email=$userlib->get_user_email($user);
		if ( ! empty($email) ){
		include_once ('lib/webmail/tikimaillib.php'); 
		$mail = new TikiMail();
		$mail->setText(tra("You are alerted by the server "). $project . "\n" . tra ("You can check the modifications at : ") .$URL );
		$mail->setSubject(tra("You are alerted of a changement on ").$project );
		$mail->send(array($email));
		}
	}
	}
}


global $tikilib,$dbTiki;
$groupalertlib = new groupAlertLib($dbTiki);
?>