Wednesday, April 9, 2008

functions.inc.php

If you create this file in a folder called include, this file will be included in every page and the functions will be available. I've included like an example of this file with some functions that serve as an example of what it can be used for:


error_reporting(E_ALL);
ini_set('display_errors','On');
define('APPROVAL_LEVEL_APPROVED',2);
define('APPROVAL_LEVEL_CONDITIONAL',1);
define('APPROVAL_LEVEL_NOT_APPROVED',0);

define('APPROVAL_STATUS_APPROVED',2);
define('APPROVAL_STATUS_NOT_APPROVED',0);

function isLoggedIn(){
static $loggedIn = 0;
if ( $loggedIn === 0 ){
$user =& getUser();
if ( $user ) $loggedIn = true;
else $loggedIn = false;
}
return $loggedIn;
}

function &getUser(){
static $user = 0;
if ( $user === 0 ){
$auth =& Dataface_AuthenticationTool::getInstance();
$user = $auth->getLoggedInUser();
}
return $user;
}

// Checks whether the current user is an admin or not.
function isAdmin(){
$user =& getUser();
if ( !$user ) return false;
return ($user->val('role') == 'ADMIN');
}

function isBrokerCustomer(){
$user =& getUser();
if ( !$user ) return false;
return ($user->val('cookedRole') == 'BROKER CUSTOMER');
}

function getDealerID(){
static $dealer_id = -1;
$user =& getUser();
if ( !$user){
if ( $dealer_id == -1 ){
$res = mysql_query("select company_id from companies where host_name='".addslashes($_SERVER['HTTP_HOST'])."' limit 1", df_db());
if ( mysql_num_rows($res) == 0 ) $dealer_id = null;
else list($dealer_id) = mysql_fetch_row($res);
}
return $dealer_id;
}
return $user->val('dealerID');
}

function &getBrokerDealer(){
static $bd = 0;
if ( $bd === 0 ){
$id = getDealerID();
if ( $id ){
$bd = df_get_record('companies', array('company_id'=>$id));
}
}
return $bd;
}

function isBrokerDealer(){
$user =& getUser();
if ( !$user ) return false;
return ($user->val('cookedRole') == 'BROKER DEALER');
}

function isCompanyOwner(){
$user =& getUser();
if ( !$user ) return false;
return ($user->val('cookedRole') == 'COMPANY OWNER');
}

function isCompanyRep(){
$user =& getUser();
if ( !$user ) return false;
return ($user->val('cookedRole') == 'COMPANY REP');
}

function getCompanyID(){
$user =& getUser();
if ( !$user ) return null;
return $user->val('companyID');
}

function &getCompany(){
static $company = 0;
if ($company === 0 ){
$id = getCompanyID();
if ( $id ){
$company = df_get_record('companies', array('company_id'=>$id));
}
}
return $company;
}


function off($date){
$date = strtotime($date);
$offset = (strftime("%j")+strftime("%Y")*365)-
(strftime("%j",$date)+strftime("%Y",$date)*365);
if ($offset>7){
$offset = (strftime("%V")+strftime("%Y")*52)-
(strftime("%V",$date)+strftime("%Y",$date)*52);
$end=($offset!=0?($offset>1?$offset . " weeks ago":"a week ago"):"Today");
} else
$end=($offset!=0?($offset>1?"$offset days ago":"Yesterday"):"Today");
return strftime("%A, %B %e, %Y",$date)." - ". $end;
}


Here we have some common functions to help figure out what type of user is logged in.

No comments: