".$str."";
* }
*
*
* echo "
";
*
* //** create some errors and messages randomly
* $errors = array();
* for($i=0; $i<10; $i++){ list($errors[] , $data ) = message_handling::doSample(); }
* list($head, $body) = message_handling::renderMessages($errors, "./error.html", array(".alert"=>"background-color:black;color:white;"));
*
* //** now we test to append new errors to the file
* $errors = array();
* for($i=0; $i<10; $i++){ list($errors[] , $data ) = message_handling::doSample(); }
* list($head, $body) = message_handling::renderMessages($errors, "./error.html", array(".alert"=>"background-color:#DDF;color:red;font-style:normal"));
* echo $head.$body;
*
* //** out direct to the file
* $errors = array();
* for($i=0; $i<10; $i++){ list($errors[] , $data ) = message_handling::doSample(); }
* list($head, $body) = message_handling::renderMessages($errors);
* echo $head.$body;
* echo "
*
* ";
* //* ***********************************************
* // end example
*/
if(!defined("TARGET_WINDOW_NAME")){
define("TARGET_WINDOW_NAME", "_messages" );
}
class message_handling{
/** ***********************************************
* reports message in a file and returns a linkstring to the file
* or the str of rendered messages
*
* @param array $messages[] each message: array(string $type, string $message, string $info)
* @param string $toFile can be "./name.html" or "name.html" for relative path or absolute like dirname(__FILE__)."/name.html" no "../" allowed
* @param array $moreStyles -- see function getStyles()
* @param $prefix will be prepend to each $str to be translated
*
* @return $headStr with style+script $bodyStr
* or if isset $toFile
* @return $emptyHeadStr , $linkStr to open a separete Windows called messages
* ***********************************************
*/
function renderMessages($msgs, $toFile=false, $moreStyles=array(), $prefix=""){
static $runOnce = false;
static $fileRunOnce = false;
if(!(is_array($msgs)) ||count($msgs)==0){
return ""; //** --
}
$headStr = ($runOnce) ? "" : message_handling::_getStyle($moreStyles) . message_handling::_getScript();
$bodyStr ="";
$cnt = array();
$loop = "";
foreach($msgs as $msg){
if(!isset($cnt[$msg[0]])){ $cnt[$msg[0]]=0; }
$cnt[$msg[0]]++;
$more = (trim($msg[2])!="")? "..." : "" ;
$hand = (trim($msg[2])!="")? "hand" : "" ;
$loop .= "";
$loop .= "
".message_handling::T_($prefix.$msg[0]."_".$msg[1])." $more
{$msg[2]}
";
$loop .= "
\n";
}
$tabs = " ".message_handling::T_($prefix."show_all_messages")." ";
$title = "";
foreach($cnt as $type=>$count){
$title = message_handling::T_($prefix.$type)."=$count ";
$tabs .= " ".message_handling::T_($prefix.$type)." $count ";
}
$bodyStr .= " $tabs
";
$bodyStr .= $loop;
$bodyStr .= "
\n
\n";
$runOnce = false;
if($toFile && trim($toFile)!=""){
$toFile = str_replace("../", "", $toFile);
$toFile = ( dirname($toFile)==".") ? dirname($_SERVER["REQUEST_URI"])."/".str_replace("./", "", $toFile) : $toFile ; //if relative
$url = str_replace(realpath($_SERVER["DOCUMENT_ROOT"]), "", $toFile); //if absolute
$file = str_replace("//", "/", realpath($_SERVER["DOCUMENT_ROOT"])."/". $url );
if(is_writable(dirname($file))){
$append = ($fileRunOnce) ? FILE_APPEND : 0 ;
file_put_contents($file, "\n\n$headStr\n\n".date("h:i:s Y-m-d")."
\n$bodyStr\n\n\n", $append );
$fileRunOnce = true;
return array("", "" . message_handling::T_($prefix."Open_Messages") . "" ); //** --
}
}
$runOnce = true;
return array($headStr, $bodyStr);
}
/** ***********************************************
* call an external translation function if exists.
* the function must be declared by define("EXTERNAL_TRANSLATE_FUNCTION_NAME", "funcname");
* @param $str
* @return translated $str
* ***********************************************
*/
function T_($str){
if(function_exists(EXTERNAL_TRANSLATE_FUNCTION_NAME)){
return call_user_func(EXTERNAL_TRANSLATE_FUNCTION_NAME, $str );
}else{
return $str;
}
}
/* ***********************************************
* create the needed script to fold and sort items exactly once
* @param -
* @return $headStr
* ***********************************************
*/
function _getScript(){
$headStr = "\n";
return $headStr;
}
/** ***********************************************
* create the needed style exactly once
* @param array of additional styles key is classname with preceeding dot '.alert', value is the part between {} :color:red;
* @return $headStr
* ***********************************************
*/
function _getStyle($moreStyles=array()){
$headStr ="";
$headStr .= " \n";
return $headStr;
}
/** ***********************************************
* a dummy function to test this class
* @param -
* @return messageArray($type, $message, $information);
* ***********************************************
*/
function doSample(){
$sample = array("notice", "warning", "error", "alert");
$trace = array(print_r($GLOBALS, true), print_r($_ENV, true), print_r($_SERVER, true), print_r(debug_backtrace(), true) );
$messages = "HARLEZ-VOUS FRANCAIS? - Can you drive a French motorcycle?
EX POST FUCTO - Lost in the mail
IDIOS AMIGOS - We're wild and crazy guys
VENI, VIPI, VICI - I came, I'm a very important person, I conquered
COGITO, EGGO SUM - I think, therefore I waffle
RIGOR MORRIS - The cat is dead
RESPONDEZ S'IL VOUS PLAID - Honk if you're Scottish
QUE SERA SERF - Life is feudal
LE ROI EST MORT, JIVE LE ROI - The king is dead. No kidding
POSH MORTEM - Death styles of the rich and famous
PRO BOZO PUBLICO - Support your local clown
MONAGE A TROIS - I am three years old
FELIX NAVIDAD - Our cat has a boat
HASTE CUISINE - Fast French food
VENI, VIDI, VICE - I came, I saw, I partied
QUIP PRO QUO - A fast retort
ALOHA OY - Love; greetings, farewell; from such a pain you should never know
MAZEL TON - Tons of good luck
APRES MOE LE DELUGE - Curly and Larry got wet
PORT-KOCHERE - Sacramental wine
ICH LIEBE RICH - I'm really crazy about having dough
FUI GENERIS - What's mine is mine
VISA LA FRANCE - Don't leave your chateau without it
CA VA SANS DIRT - And that's not gossip
MERCI RIEN - Thanks for nothin'
AMICUS PURIAE - Platonic friend
";
$msg = explode("\n", $messages);
$normalData ="";
return( array(
array(
$sample[ rand(0,3) ],
$msg[ rand(0,count($msg)-1) ],
$trace[ rand(0, count($trace)-1) ]
) ,
$normalData )
);
}
//** end class
}
?>