Skip to content

Instantly share code, notes, and snippets.

@luniki
Created June 20, 2011 09:22
Show Gist options
  • Save luniki/1035357 to your computer and use it in GitHub Desktop.
Save luniki/1035357 to your computer and use it in GitHub Desktop.
AlertView4Stud.IP
<?php
/*
* AlertView.php - render an alert message to the user
*
* Copyright (c) 2011 mlunzena@uos.de
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
class AlertView
{
public $message, $buttons;
function __construct($message, $defaultButton, $alternateButton = null,
$otherButtons = null /*, ... */)
{
$this->message = $message;
$this->buttons = array('default' => $defaultButton);
if ($alternateButton) {
$this->buttons['alternate'] = $alternateButton;
}
if ($otherButtons) {
$buttons = func_get_args();
$this->buttons['others'] = array_slice($buttons, 3);
}
}
function addButton($button)
{
}
function numberOfButtons()
{
}
function render()
{
global $template_factory;
return $template_factory->render('shared/alert', array('alert' => $this));
}
function toString()
{
return $this->render();
}
}
<?
extract($alert->buttons, EXTR_PREFIX_ALL, 'b');
?>
<div class="modalshadow">
<div class="messagebox messagebox_modal">
<?= formatReady($alert->message) ?>
<div style="margin-top: 1em;">
<form action="<?= $b_default->action ?>" method="<?= $b_default->method ?>" style="display: inline;">
<?= CSRFProtection::tokenTag() ?>
<?= makeButton($b_default->title, 'input') ?>
</form>
<? if (isset($b_alternate)) : ?>
<form action="<?= $b_alternate->action ?>" method="<?= $b_alternate->method ?>" style="display: inline;">
<?= CSRFProtection::tokenTag() ?>
<?= makeButton($b_alternate->title, 'input') ?>
</form>
<? endif ?>
<? if (sizeof($b_alternate)) : ?>
<? foreach ($b_alternate as $b_other) : ?>
<form action="<?= $b_other->action ?>" method="<?= $b_other->method ?>" style="display: inline;">
<?= CSRFProtection::tokenTag() ?>
<?= makeButton($b_other->title, 'input') ?>
</form>
<? endforeach ?>
<? endif ?>
</div>
</div>
</div>
<?php
/*
* alert_view_test.php - TODO
*
* Copyright (c) 2011 mlunzena
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
require_once 'lib/classes/AlertView.php';
require_once 'lib/language.inc.php';
class AlertViewTest extends UnitTestCase
{
function setUp()
{
$this->original_factory = $GLOBALS['template_factory'];
$GLOBALS['template_factory'] = new Flexi_TemplateFactory(dirname(__FILE__) . '/../../../templates');
}
function tearDown()
{
$GLOBALS['template_factory'] = $this->original_factory;
}
function makeButton($title, $action, $method)
{
return (object) compact("title", "action", "method");
}
function testTokenGeneration()
{
$default = $this->makeButton("ok", "/button/ok", "post");
$alternate = $this->makeButton("cancel", "/button/cancel", "post");
$other1 = $this->makeButton("other 1", "/button/other1", "post");
$other2 = $this->makeButton("other 2", "/button/other2", "post");
$alert = new AlertView("my message", $default, $alternate, $other1, $other2);
var_dump($alert->render());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment