i am including it in the autoload function at the top.
Please see by the Globalfunctions file that brings up syntax errors if i do a include_one or a require_once
class GlobalFunctions{
private $query_runner_obj = null;
function __construct(){
$this->query_runner_obj = new QueryRunner();
}
/*Simple function that checks if a key exists in an array and if not creates it with the passed argument as default
*
*/
public static function param_set(&$arr,$key,$default=""){
if(!isset($arr[$key])){
$arr[$key] = $default;
}
}
public static function getCategoryName()
{
$cat_names = array();
$cat_names = $cats_obj->getCat_Name();
return $cat_names;
}
public function getImageList($filename)
{
$image = array();
$sql = sprintf("select small_image, large_image, description from gallery_images where directory = '%s' ORDER BY item_id", $filename);
$qres_obj = $this->do_query($sql);
if( ($qres_obj->get_status() === true) && ($qres_obj->get_record_count() > 0) )
{
$result_set = $qres_obj->get_result_set();//get the result set
foreach ($result_set as $result_item) {
$ret = array();
$ret["small_image"] = $result_item["small_image"];
$ret["large_image"] = $result_item["large_image"];
$ret["description"] = $result_item["description"];
$image[] = $ret;
}
}//for each
else{
throw new Exception("no images in that directory=".$qres_obj->get_status().", erorr msg=". $qres_obj->get_error_message());
}
return $image;
}
/**
* Display function to topic listings on places like the homepage, magazine content etc
* @param $cats_obj - Categories object
* @param $cat_id - category id
* @param $articles - array of articles
* @param $topic_style - valid css for the div topic
*/
public static function displayCategoryListing(&$cats_obj,$cat_id, &$articles, $topic_style=""){
$found_one = false;
$cat_dir_name = $cats_obj->get_dir_name($cat_id);
$cat_name = $cats_obj->get_display_name($cat_id);
$cat_id = $cat_id;
foreach ($articles as $result_item) {//bit inefficienet this but shouldnt be that bad
if($result_item["article_category_id"] == $cat_id){
if(!$found_one){
echo "<div class=\"topic\" style=\"".$topic_style."\" >";
echo "<h3><a href=\"/".$cat_dir_name."\">".$cat_name."</a></h3>";
$found_one = true;
}
$image = "";
if($result_item["is_free"]){
$image = "<img src=\"/images/free_icon.gif\" alt=\"free content\" class=\"free\"/>";
}
echo "<h4>$image<a href=\"/$cat_dir_name/index.php?articleid=${result_item["item_id"]}\">${result_item["title"]}</a></h4>";
echo "<p>${result_item["summary_paragraph"]}...<a href=\"/$cat_dir_name/index.php?articleid=${result_item["item_id"]}\">»</a></p>";
}//if
}//foreach
if($found_one){
echo "<p><a href=\"/$cat_dir_name/\"><span class=\"red\">more $cat_name »</span></a></p>";
echo "</div><!--topic-->";
}//if
}//funct
/*Execute a query using the query runner object and return a QueryResult object
*/
public function do_query($query){
//echo $query;
$qres = $this->query_runner_obj->do_query($query);
return $qres;
}
/**
*Parses a mysql date time string "yyyy-mm-dd hh:mm:ss" and returns a timestamp value
*hh is in 24 hour format
*@returns a value generated by mktime function
*/
public static function parseMySQLDateTime($date) {
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
$hour = substr($date, 11, 2);
$minute = substr($date, 14, 2);
$second = substr($date, 17, 2);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
/**
* Truncates strings to show ellipsis if passes string is too big else return the string
*/
public static function ellipsis($s, $max_length=20){
$ret = $s;
$spcIdx = 0;//see later
if ( strlen($s) > $max_length ){//needs proccessing
$ret = substr($s, 0, ($max_length-3));
//find the last space less than 20 chars in if there is one
$ret = strrev($ret);
$spcIdx = strpos($ret," ");
if ($spcIdx <= 20){
$ret = substr($ret, $spcIdx-strlen($ret));
//$ret = right($ret, strlen($ret)-$spcIdx);
}
$ret = strrev($ret);//reverse back
$ret = $ret."...";
}//if
return $ret;
}//func
/**
* Gets hotspot text for the supplied area.
* This function
* @param name - the name of the hotspot e.g. "aboutus"
* @randomize - return one of the possible active items randomly (ignores date ordering)
* @returns the latest online hotspot text for the supplied area or empty string
*/
public function get_hotspot($name, $randomize=false){
$ret = "";
$sql = sprintf("select body from hotspot_area ha join hotspot_item hi on ha.hotspot_area_id = hi.hotspot_area_id join content_item ci on ci.item_id = hi.item_id " .
"where ha.hotspot_name = '%s' and workflow_status = 1 and (start_date < now() and end_date > now())" , $name);
if($randomize){
//is random
}else{//not random so get the latest active one
$sql = $sql." order by start_date desc limit 1";
}
$qres_obj = $this->do_query($sql);
if( ($qres_obj->get_status() === true) && ($qres_obj->get_record_count() > 0) ){
$result_set = $qres_obj->get_result_set();//get the result set
$row_index = 1;
$row_counter = 0;
if($randomize){//get a random column
$row_index = rand(1, $qres_obj->get_record_count());
}
//if random then will get one for get_record_count() possibilities else will always get first row
foreach ($result_set as $result_item) {
$row_counter++;
//throw new Exception("$row_counter, $row_index");
if($row_index == $row_counter){
$ret = $result_item["body"];
break;//only one row
}
}//for each
}elseif ($qres_obj->get_status() === false){
throw new Exception("bad query to get hotspot, status=".$qres_obj->get_status().", erorr msg=". $qres_obj->get_error_message());
}
return $ret;
}//funct
/*
* trims the contents of an assocaitive array
* ?does this work for non assoc?
*/
public static function array_trim(&$arr){
foreach ($arr as $key => $value) {
$attributes[$key] = trim($value);
}
}//funct
/**
* Checks if the passed list of array keys exist the passed array and are non emtpy i.e. = ""
* Used for form submission to check if certain items are present
* @returns true or false
*/
public static function check_fields_exist($arr, $list){
$tok = strtok($list, ",");
while ($tok !== false) {
if( isset($arr[$tok]) && (!is_array($arr[$tok])) && (!is_object($arr[$tok])) ){
$val = trim(strval($arr[$tok]));//convert to string
if(strlen($val) == 0){
return false;//fail
}
}//if
$tok = strtok(",");//get next token
}//while
return true;//all is good
}
/**
*Wrapper for how db strings are escapes
*@param $use_slashes - set to true if addslashes is to be used else set to false if '' is to be used, change default to make things easy if db is changed
*@returns the escaped string
*/
public static function dbEscape($s, $use_slashes=true){
if($use_slashes){
return addslashes($s);
}else{
return str_replace("'", "''", $s);// sql server and sybase
}
}//funct
/**
*Proccess Form Field String Content
*Replaces all non standard ascii chars with numerical entity equivalents e.g. &#xxx; form
*Idea is that all fields that will be shown on the web site are escaped with this and placed into the database in
*the escaped format. The problem is if argument s is allowed to have html markup. In this case we cant escape html chars e.g. <
*so this could break the display page on the site also " if used in an html tag attribute
*@returns proccessed string
*/
public static function escapeFormField($s, $contains_html=false){
$char = "";
$ret = "";//what gets returned
for($i=0 ; $i < strlen($s) ; $i++){
$char = substr($s, $i, 1);//get a char at a time
$code = ord($char); //character code
switch($code){
case 124: { //the | char - dont know why this is special - perhaps mac thing
$char = "&#".$code .";";
break;
}
case 60: {
if(!$contains_html)$char = "<";
break;
}
case 62: {
if(!$contains_html)$char = ">";
break;
}
/*case 39: {
if(!$contains_html)$char = "'";
break;
}*/
case 34: {
if(!$contains_html)$char = """;
break;
}
default:{
if( $code >= 127 ){
$char = "&#".$code .";";
}
}
}
$ret = $ret . $char;
}//for
return $ret;
}//function
/*Checks passed in email for validity
*@returns true or false
*/
public static function isEmailValid($email) {
if (!(eregi ("^[\'_a-z0-9-]+(\.[\'_a-z0-9-]+)*@[a-z0-9-]+\.[a-z0-9-]+(\.[a-z0-9-]+)*$", $email))) {
return false;
}
return true;
}//funct
public static function getCountryName()
{
$row = array();
$sql = "select countryname from iso_country order by countryname";
global $request;//defined in Application.php
$gf_obj = $request["gf_obj"];//the global helper object
$qres_obj = $gf_obj->do_query($sql);
if( ($qres_obj->get_status() === true) && ($qres_obj->get_record_count() > 0) ){
$result_set = $qres_obj->get_result_set();//get the result set
foreach ($result_set as $result_item)
{
$row[] = $result_item["countryname"];
}
}
else
{
$row = null;
}
return $row;
}
public static function addOnClickEvent($name, $section)
{
echo "here";
$today = date("D M j G:i:s T Y");
$sql = "insert into hotspot_tracking (date, hotspot_name, page_section) values('".$today."', '".$name."', '".$section."')";
$result = $db->query($sql);
if (!$result) {die('Bad Query: ' . mysql_error());}
}
}
?>