Ok this is my solution:
Method DoAction calls in two extra javascript files: admin.errorMsg.js and AntzBase.js. Admin.errorMsg contains a function to build the error message into the dom:
Code: Select all
function showErrorMsg(msg, rootUrl){
var cont = document.getElementById('MainContent');
var pCont = document.getElementsByClassName('pagecontainer');
pCont = pCont[0];
var objs = document.getElementsByClassName('navt_menu');
var navt = objs[0];
var div = document.createElement('div');
var div2 = document.createElement('div');
var img = document.createElement('img');
var span = document.createElement('span');
span.innerHTML = ' '+msg;
img.src = rootUrl+'/admin/themes/default/images/icons/system/stop.gif';
img.alt = 'Error';
img.title = 'Error';
img.className = 'systemicon';
div.className = 'pageerrorcontainer';
div.id = 'errorMsg';
div2.className = 'pageoverflow';
div2.appendChild(img);
div2.appendChild(span);
div.appendChild(div2);
appendAfter(navt, div);
}
// function derived from http://book.itzero.com/read/others/McGraw.Hill.Osborne.JavaScript.2.0.The.Complete.Reference.Second.Edition.eBook-LiB_html/8166final/LiB0073.html
function appendAfter(existingNode, newNode)
{
if (existingNode.parentNode)
{
if (existingNode.nextSibling)
existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
else
existingNode.parentNode.appendChild(newNode);
}
}
Then at the end of DoAction, I add the following code, which will call the function showErrorMsg, suppying with the error messages that are assembled by my Core class "Antz":
Code: Select all
<?php
// time to add our error messages
$errorMsg = Antz::getError();
if($errorMsg != '')
echo '<__script__ type="text/javascript" language="javascript">showErrorMsg(\''.$errorMsg.'\', \''.$this->config['root_url'].'\')</__script>';
The file AntzBase.js just contain an object called "AntzBase", which contains useful methods, in particular here:
AntzBase.clearError(), which is called when the user clicks "OK" on the error message. This simply sets display="none" on the error message container (#errorMsg).
Code: Select all
clearError : function(){
if(document.getElementById('errorMsg'))
document.getElementById('errorMsg').style.display = 'none';
},
... so now I can add error messages to the page at any point in the execution of my module, simply by using Antz::addError('This is the error message');
Simple!