/usr/share/php/Icinga/Web/Form/Decorator/FormNotifications.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Form;
/**
* Decorator to add a list of notifications at the top or bottom of a form
*/
class FormNotifications extends Zend_Form_Decorator_Abstract
{
/**
* Render form notifications
*
* @param string $content The html rendered so far
*
* @return string The updated html
*/
public function render($content = '')
{
$form = $this->getElement();
if (! $form instanceof Form) {
return $content;
}
$view = $form->getView();
if ($view === null) {
return $content;
}
$notifications = $this->recurseForm($form);
if (empty($notifications)) {
return $content;
}
$html = '<ul class="form-notifications">';
foreach (array(Form::NOTIFICATION_ERROR, Form::NOTIFICATION_WARNING, Form::NOTIFICATION_INFO) as $type) {
if (isset($notifications[$type])) {
$html .= '<li><ul class="notification-' . $this->getNotificationTypeName($type) . '">';
foreach ($notifications[$type] as $message) {
if (is_array($message)) {
list($message, $properties) = $message;
$html .= '<li' . $view->propertiesToString($properties) . '>'
. $view->escape($message)
. '</li>';
} else {
$html .= '<li>' . $view->escape($message) . '</li>';
}
}
$html .= '</ul></li>';
}
}
switch ($this->getPlacement()) {
case self::APPEND:
return $content . $html . '</ul>';
case self::PREPEND:
return $html . '</ul>' . $content;
}
}
/**
* Recurse the given form and return the notifications for it and all of its subforms
*
* @param Form $form The form to recurse
*
* @return array
*/
protected function recurseForm(Form $form)
{
$notifications = $form->getNotifications();
foreach ($form->getSubForms() as $subForm) {
foreach ($this->recurseForm($subForm) as $type => $messages) {
foreach ($messages as $message) {
$notifications[$type][] = $message;
}
}
}
return $notifications;
}
/**
* Return the name for the given notification type
*
* @param int $type
*
* @return string
*
* @throws ProgrammingError In case the given type is invalid
*/
protected function getNotificationTypeName($type)
{
switch ($type) {
case Form::NOTIFICATION_ERROR:
return 'error';
case Form::NOTIFICATION_WARNING:
return 'warning';
case Form::NOTIFICATION_INFO:
return 'info';
default:
throw new ProgrammingError('Invalid notification type "%s" provided', $type);
}
}
}
|