Skip to content

Instantly share code, notes, and snippets.

@NoiseEee
Last active August 29, 2015 13:58
Show Gist options
  • Save NoiseEee/10291712 to your computer and use it in GitHub Desktop.
Save NoiseEee/10291712 to your computer and use it in GitHub Desktop.
<?php
/*
* a class that writes a basic webpage with a javascript alert, and options
*
* @param string $destinationURL the webpage to be redirected to
* @param string $windowMessage the message presented to the user in the javascript alert
* @param string $windowAction what to do: redirect to a new page, close (when called in a popup), or a combination.
*/
class popupMessage {
private $destinationURL;
private $windowMessage;
private $windowAction;
protected $validActions = array("redirect","close","closeAndRedirect","redirectAndClose","closeAndReload","fullWindowRedirect","hideiframe");
function __construct($vMessage,$vURL,$vWindowAction="redirect") { //presents a javascript alert with a message and takes them to a destination
if($vMessage=="") {
throw new Exception('Missing a message or destination.');
}
$this->windowMessage = $vMessage;
$this->windowAction = $vWindowAction;
$this->destinationURL = $vURL;
if(!in_array($this->windowAction,$this->validActions)) {
throw new Exception('Invalid window action.');
}
$this->presentWindow($this->windowAction);
}
private function presentWindow($theWindowAction) {
echo("<html>\n");
echo("<head>\n");
echo("<title>System Message</title>\n");
echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
echo("<script type=\"text/javascript\">\n");
echo("alert('".addslashes($this->windowMessage)."');\n");
switch($theWindowAction) {
case "redirect":
$jsCommand = "location.replace('".$this->destinationURL."');\n";
break;
case "close":
$jsCommand = "window.close();";
break;
case "hideiframe":
$jsCommand = "
var iframe = window.parent.document.body.getElementsByTagName('iframe')[0];
console.log(iframe);
iframe.parentNode.style.display = 'none';
";
break;
case "closeAndRedirect":
case "redirectAndClose":
$jsCommand = "opener.location.replace('".$this->destinationURL."');\nwindow.close();\n";
break;
case "closeAndReload":
$jsCommand = "opener.location.reload(true);\n";
break;
case "fullWindowRedirect":
$jsCommand = "top.location.replace('".$this->destinationURL."');\n";
break;
}
echo $jsCommand;
echo("</script>\n");
echo("</head>\n");
echo("<body>\n");
echo("</body>\n");
echo("</html>\n");
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment