<?php
/*
* ImiComponent.php
* Copyright ALM Software Technologies
* Created by fm
* Date created Jul 19, 2008
* Description: Base Itshomi component class
*
*/
class ImiComponent{
protected $comps = array(),$layout = 'CONTENT_TEXT',$task='',
$compPath="",$compName = "", $compEvents = null;
function __construct(){
$this->navigator = &$GLOBALS['navigator'];
}
function setLayout($nlayout){
$this->layout = $nlayout;
}
function compPath(){
return $this->compPath;
}
function setPath($p){
$this->compPath = $p;
}
function setTask($tsk){
$this->task = $tsk;
}
function setName($cname){
$this->compName = $cname;
}
/**
* get current task (usually value of user request for current query label. Example: if user
* submitted index.php?option=about and option is the current query label, then getTask would
* return 'about'
*/
function getTask(){
return $this->task;
}
/**
* adds a child component to the current component
*/
function addComponent($cname,&$component){
$this->comps[$cname]=&$component;
}
/**
* executes any composite or children components of the current component
*/
function play(){
foreach($this->comps as $component){
$component->play();
}
}
function getComponent($cname){
if(array_key_exists($cname,$this->comps))
return $this->comps[$cname];
return null;
}
/**
* loads content of given file, which must exist within the component's folder
*/
function getFile($fname){
return file_get_contents($this->compPath.$fname);
}
/**
* return specified class of current component. The class must be defined within the component's
* folder in a file with the same name as the class, and extension .php
* Throws 'Class not found' exception
*/
function &getClass($classname){
$compClass = &$this->navigator->getClass($this->compPath,$classname);
if($compClass instanceof ImiComponent)
$compClass->setPath($this->compPath);
return $compClass;
}
/**
* return given Framework's utility class
* Throws 'Class not found' exception
*/
function &getUtil($classname){
$compClass = &$this->navigator->getUtil($classname);
if($compClass instanceof ImiComponent)
$compClass->setPath($this->compPath);
return $compClass;
}
function &getInstance($cname){
$inst = &$this->navigator->getInstance($cname);
return $inst;
}
function addView($view){
$this->navigator->addView($view);
}
function addViewData($p,$view){
$this->navigator->addViewData($p,$view);
}
/**
* add component as listener
* @param $evName event name
* @param $listener component name
* @param $evType (optional)
* @param $viewid (optional)
*/
function addListener($evName,$listener,$evType=2,$viewid=0){
$this->navigator->addListener($evName,$listener,$evType=2,$viewid=0);
}
/**
* component quick installer. Use to install a component
* make sure to add the events your component must handle to $this->compEvents.
*/
function install(){
if(!is_array($this->compEvents)){
$this->addViewData('FEEDBACK','Could not find any event to install. Please add your events to $this->compEvents');
return;
}
foreach($this->compEvents as $ev){
$this->addListener($ev,$this->compName);
}
}
/**
*
* @param $url
* @param $title
* @param $label
* @param $access (user,admin)
* @param $position (top,left,right)
* @return unknown_type
*/
function addMenuItem($url,$title,$label,$access='user',$position='top'){
$atag = 'ADM_';
if($access=='user')
$atag = '';
$p = '_TOP';
if($position=='left')
$p = '_LEFT';
else
if($position=='right')
$p = '_RIGHT';
$html = '<a href="'.$url.'" title="'.$title.'">'.$label.'</a>';
$this->addViewData($atag.'XTRA_MNU'.$p,$html);
}
/**
* return current query label (example: the word between ? and = here: ?option= or ?event=)
*/
function webev(){
return $this->navigator->getCfg('def_webev_label');
}
/**
* return a given configuration entry
*/
function getCfg($key){
return $this->navigator->getCfg($key);
}
function checkClearance($ctype,$value){
switch($ctype){
case 'role':
if(!$_SESSION['LoggedIn'])
return false;
return ($_SESSION['roleid']==$value);
break;
}
}
/**
* throws exception if caller is not admin. Use in your components for access control
*/
function adminOnly(){
global $config;
if(!$this->checkClearance('role',$config['admrole'])){
throw new Exception('Access denied');
}
}
/** redirect to specified internal url */
function redirect($url)
{
if (!headers_sent())
{
header("Location: http://" . $_SERVER['HTTP_HOST']
. "/".dirname($_SERVER['PHP_SELF'])
. "/".$url);
}else
addlog("Redirect for ".$url." failed. Headers sent already");
}
/** redirect to specified external url */
function redirectOut($url)
{
if (!headers_sent())
{
header("Location: " . $url);
}else
addlog("Redirect for ".$url." failed. Headers sent already");
}
function onComponentAction($compName,$actionName){
}
function onEvent($ev){
}
/**
* error login with different categories of errors
*/
function addlog($msg,$type='sys',$level='error',$prefix=""){
$date = date("Y-m-d");
if(strlen($prefix)>0)
$prefix.='_';
switch($type){
case 'sys':
$fname = 'logs/sys_'.$prefix.$level.'_'.$date;
break;
case 'support':
$fname = 'logs/sup_'.$prefix.$level.'_'.$date;
break;
case 'user':
$fname = 'logs/use_'.$prefix.$level.'_'.$date;
break;
}
error_log("[".date("D Y/m/d H:i:s")."][".$level."]: ".$msg."\r\n",3,$fname);
}
}
?>