getPrefixSpecial(true) instead of
* $event->Prefix_Special as usual. This is due PHP
* is converting "." symbols in variable names during
* submit info "_". $event->getPrefixSpecial optional
* 1st parameter returns correct corrent Prefix_Special
* for variables beeing submitted such way (e.g. variable
* name that will be converted by PHP: "users.read_only_id"
* will be submitted as "users_read_only_id".
*
* 2. When using $this->Application-LinkVar on variables submitted
* from form which contain $Prefix_Special then note 1st item. Example:
* LinkVar($event->getPrefixSpecial(true).'_varname',$event->Prefix_Special.'_varname')
*
*/
/**
* Default event handler. Mostly abstract class
*
*/
class kEventHandler extends kBase {
/**
* In case if event should be handled with mehod,
* which name differs from event name, then it
* should be specified here.
* key - event name, value - event method
*
* @var Array
* @access protected
*/
var $eventMethods = Array();
/**
* Defines mapping vs event names and permission names
*
* @var Array
*/
var $permMapping = Array();
/**
* Define alternative event processing method names
*
* @see $eventMethods
* @access protected
*/
function mapEvents()
{
}
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
$this->permMapping = Array(
'OnCreate' => Array('self' => 'add', 'subitem' => 'add|edit'),
'OnDelete' => Array('self' => 'delete', 'subitem' => 'edit|add'),
'OnEdit' => Array('self' => 'add|edit'),
'OnPreCreate' => Array('self' => 'add'),
'OnPreSaveAndGoToTab' => Array('self' => 'add|edit'),
// theese event do not harm, but just in case check them too :)
'OnCancelEdit' => Array('self' => 'add|edit'),
'OnCancel' => Array('self' => 'add|edit', 'subitem' => 'add|edit'),
'OnSetSorting' => Array('self' => 'view', 'subitem' => 'view'),
);
}
function getPrefixSpecial()
{
trigger_error('Usage of getPrefixSpecial() this method is forbidden in kEventHandler class children. Use $event->getPrefixSpecial(true); instead', E_USER_ERROR);
}
/**
* Set's prefix and special
*
* @param string $prefix
* @param string $special
* @access public
*/
function Init($prefix,$special)
{
parent::Init($prefix,$special);
$this->mapEvents();
$this->mapPermissions();
}
/**
* Process Event
*
* @param kEvent $event
* @access public
*/
function processEvent(&$event)
{
$event_name=$event->Name;
if( isset($this->eventMethods[$event_name]) ) $event_name=$this->eventMethods[$event_name];
if( method_exists($this,$event_name) )
{
$this->$event_name($event);
}
else
{
trigger_error('event '.$event->Name.' not implemented in class '.get_class($this).'', E_USER_ERROR);
}
}
/**
* Sample dummy event
*
* @param kEvent $event
* @access protected
*/
function OnBuild(&$event)
{
/*echo 'building:
';
print_pre($event);*/
}
/**
* Returns to previous template in opener stack
*
* @param kEvent $event
*/
function OnGoBack(&$event)
{
$event->SetRedirectParam('opener', 'u');
}
/**
* Apply some special processing to
* object beeing recalled before using
* it in other events that call prepareObject
*
* @param Object $object
* @param kEvent $event
* @access protected
*/
function prepareObject(&$object, &$event)
{
// processing here
}
/**
* Creates new event as child of
* event passed as $event param
*
* @param kEvent $event
* @access protected
*/
function &inheritEvent(&$event, $name=null)
{
$child_event = new kEvent();
$child_event->MasterEvent =& $event;
$child_event->Prefix=$event->Prefix;
$child_event->Special=$event->Special;
$child_event->Prefix_Special=$event->Prefix_Special;
$child_event->Name = $name;
return $child_event;
}
/**
* Created url part for this module
*
* @param kEvent $event
*/
function BuildEnv(&$event)
{
$prefix_special = $event->getPrefixSpecial();
$url_params = $event->getEventParam('url_params');
$pass_events = $event->getEventParam('pass_events');
$query_vars = $this->Application->getUnitOption($event->Prefix, 'QueryString');
$event_key = array_search('event', $query_vars);
if ($event_key) {
// pass through event of this prefix
unset($query_vars[$event_key]);
}
if (!getArrayValue($url_params, $prefix_special.'_event')) {
// if empty event, then remove it from url
unset( $url_params[$prefix_special.'_event'] );
}
//if pass events is off and event is not implicity passed
if ( !$pass_events && !isset($url_params[$prefix_special.'_event']) )
{
unset($url_params[$prefix_special.'_event']); // remove event from url if requested
//otherwise it will use value from get_var
}
if(!$query_vars) return true;
$processed_params = Array();
foreach($query_vars as $index => $var_name)
{
//if value passed in params use it, otherwise use current from application
$var_name = $prefix_special.'_'.$var_name;
$processed_params[$var_name] = isset( $url_params[$var_name] ) ? $url_params[$var_name] : $this->Application->GetVar($var_name);
if ( isset($url_params[$var_name]) ) unset( $url_params[$var_name] );
}
$ret = '';
if($processed_params[$prefix_special.'_Page'] > 1)
{
$ret .= $processed_params[$prefix_special.'_Page'].'/';
}
if ($processed_params[$prefix_special.'_id']) {
// this allows to fill 3 cache records with one query (see this method for details)
$category_id = isset($url_params['m_cat_id']) ? $url_params['m_cat_id'] : $this->Application->GetVar('m_cat_id');
$category_filename = $this->Application->getFilename('c', $category_id);
// if template is also item template of category, then remove template
$template = getArrayValue($url_params, 't');
$item_template = $this->Application->getCache('item_templates', $category_id);
if ($template == $item_template || strtolower($template) == '__default__') {
unset($url_params['t']);
}
// get item's filename
$filename = $this->Application->getFilename($event->Prefix, $processed_params[$prefix_special.'_id'] );
if($filename !== false) $ret .= $filename.'/';
}
if( getArrayValue($processed_params, $prefix_special.'_Reviews_Page') > 1)
{
if($processed_params[$prefix_special.'_id']) $ret = rtrim($ret, '/');
$ret .= '_'.$processed_params[$prefix_special.'_Reviews_Page'].'/';
}
$event->setEventParam('url_params', $url_params);
$event->setEventParam('env_string', strtolower($ret) );
}
/**
* Process mod_rewrite url part left after previous parser
*
* @param kEvent $event
*/
function ParseEnv(&$event)
{
// /_
$url_parts = $event->getEventParam('url_parts');
$defaults = Array('id' => 0, 'Page' => 1, 'Reviews_Page' => 1);
foreach ($defaults as $var_name => $var_value)
{
$this->Application->SetVar($event->getPrefixSpecial().'_'.$var_name, $var_value);
}
if (!$url_parts) {
// $event->status = erFAIL;
return false;
}
$ret = '';
$url_part = array_shift($url_parts);
// match module page
if( is_numeric($url_part) )
{
$this->Application->SetVar( $event->getPrefixSpecial().'_Page', $url_part);
$url_part = $url_parts ? array_shift($url_parts) : '';
}
if (!$url_part) {
$event->setEventParam('url_parts', $url_parts);
// $event->status = erFAIL;
return true;
}
// match module reviews page
if( preg_match('/(.*)_([\d]+)$/', $url_part, $rets) )
{
$url_part = $rets[1];
$this->Application->SetVar( $event->getPrefixSpecial().'_Reviews_Page', $rets[2]);
}
// match item's filename
$db =& $this->Application->GetADODBConnection();
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT item_table.'.$id_field.'
FROM '.$table.' item_table
LEFT JOIN '.TABLE_PREFIX.'CategoryItems cat_items ON item_table.ResourceId = cat_items.ItemResourceId
WHERE (item_table.Filename = '.$db->qstr($url_part).') AND (cat_items.CategoryId = '.$this->Application->GetVar('m_cat_id').')';
$item_id = $db->GetOne($sql);
if($item_id !== false)
{
$this->Application->SetVar($event->getPrefixSpecial().'_id', $item_id);
}
else
{
array_unshift($url_parts, $url_part);
$event->status = erFAIL;
}
$event->setEventParam('url_parts', $url_parts);
}
/**
* Checks permissions of user
*
* @param kEvent $event
*/
function CheckPermission(&$event)
{
if ($this->Application->GetVar('u_id') == -1) {
// "root" has all permissions
return true;
}
$main_prefix = $this->Application->GetTopmostPrefix($event->Prefix);
$section = $this->Application->getUnitOption($main_prefix, 'Section');
if (!$section) {
trigger_error('Permission section not specified for prefix '.$main_prefix.'', E_USER_ERROR);
}
$pefix_type = ($main_prefix == $event->Prefix) ? 'self' : 'subitem';
$perm_mapping = getArrayValue($this->permMapping, $event->Name);
if (!$perm_mapping[$pefix_type]) {
trigger_error('Permission mappings not defined for event '.$main_prefix.':'.$event->Name.'', E_USER_ERROR);
}
$perm_status = false;
if ($perm_mapping[$pefix_type] === true) {
// event is defined in mapping but is not checked by permissions
return true;
}
$check_perms = explode('|', $perm_mapping[$pefix_type]);
foreach ($check_perms as $perm_name) {
// check if at least one of required permissions is set
$perm_name = $section.'.'.$perm_name;
$perm_status = $this->Application->CheckPermission($perm_name, 1);
if (($perm_name == $section.'.add') && $perm_status && ($main_prefix == $event->Prefix)) {
// main item, add permission allowed, but ID is > 0, then deny permission
// how to get id here
}
if ($perm_status) {
return $perm_status;
}
}
if (!$perm_status) {
// permission not found -> redirect to "no permissions" template
$event->setEventParam('no_permissions_template', 'no_permissions');
// for debugging purposes
$event->SetRedirectParam('section', $section);
$event->SetRedirectParam('main_prefix', $main_prefix);
$event->SetRedirectParam('event_name', $event->Name);
}
return $perm_status;
}
}
?>