Location: PHPKode > projects > Cynus > cynus/signal_func.php
<?php
/**
 * @file signal_func.php -- Provides capabilities for signals and handlers.
 * @Id $Id: signal_func.php,v 1.6 2004/06/25 19:13:05 jason Exp $
 *
 * Cynus - a web-based content manager
 * Copyright (C) 2003 Brett and Jason Profitt
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */



/*************************************
Issue Signal: void issue_signal(string $signal [, string/array $args])
Used to issue a signal to the other functions. What happens is this
will check the `signals` table to see if it needs to call any other functions
when a signal is received. This is useful when you want a certain action to
be performed when another part of Cynus does something you have no control
over, such as deleting a user.
**************************************/
function issue_signal($signal) {
	global $config;
	/*so what we need to do is check around for handlers to this signal
	if we find them, we'll just run through them in order*/
	if(func_num_args() > 1) {
		$args=func_get_arg(1);
	}
	$query="SELECT * from `$config[sql_prefix]handlers` WHERE (`signal`='$signal' AND `active`='1')";
	$result=mysql_query($query);
	while($each_handler=mysql_fetch_assoc($result)) {
		#so we have a handler, let's include the files
		if($each_handler['includes'] != '') {
			$files=split("\|", $each_handler['includes']);
			foreach($files as $file) {
				#getting the filename right.
				if($each_handler['module'] != 'base') {$module="modules/$each_handler[module]/";	}
				else{$module='';}
				require_once($module . $file);
			}
		}
		eval("$each_handler[handler]($args);");
	}
}

/*************************************
Register Handler: bool register_handler(string $signal, string $name,  string $module
	string $handler, string $includes, int $active)
Registers a handler for a signal. $signal is the signal issued by a function, and what 
Cynus will reference.  $name is a nice name for what the handler is. It really isn't
too important, but it helps keep things organized, and gives modules something to 
look at if somebody makes a module to control active signals. $module is just what module will be
calling the handler. If it is part of the base, it will be "base". $handler is what function
will be called. This should be something unique, and use both the module name, and "signal" or "handler"
in the name. An example could be "groups_building_deleted_signal()" for a signal handler set
by the groups module that does something when a building is deleted. $includes is a list
of files that need to be included, separated by pipes (|). For example, if you need to include
groups_signal_func.php and groups_func.php, you would have 
$includes='groups_signal_func.php|groups_func.php'. $active is just if you want it active
or not, this is usually 1. Returns true if it was successful, false otherwise.
*************************************/
function register_handler($signal, $name, $module, $handler, $includes, $active) {
	global $config;
	#we don't need to check, because this is the user's job.
	$query="INSERT into `$config[sql_prefix]handlers` (`signal`, `name`, `handler`, `includes`, `active`) ".
		   "VALUES ('$signal', '$name', '$handler', '$includes', '$active')";
	#and do our return based on if the query went through or not
	if(mysql_query($query)) {
		return true;
	}
	else{
		return false;
	}
}
?>
Return current item: Cynus