summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Widget/AbstractWidget.php
blob: 109054852671e108c35e01ebf0551224c0b60685 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Widget;

use Icinga\Exception\ProgrammingError;
use Icinga\Application\Icinga;
use Exception;
use Zend_View_Abstract;

/**
 * Web widgets MUST extend this class
 *
 * AbstractWidget implements getters and setters for widget options stored in
 * the protected options array. If you want to allow options for your own
 * widget, you have to set a default value (may be null) for each single option
 * in this array.
 *
 * Please have a look at the available widgets in this folder to get a better
 * idea on what they should look like.
 *
 * @copyright  Copyright (c) 2013 Icinga-Web Team <info@icinga.com>
 * @author     Icinga-Web Team <info@icinga.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */
abstract class AbstractWidget
{
    /**
     * If you are going to access the current view with the view() function,
     * its instance is stored here for performance reasons.
     *
     * @var Zend_View_Abstract
     */
    protected static $view;

    // TODO: Should we kick this?
    protected $properties = array();

    /**
     * Getter for widget properties
     *
     * @param  string $key The option you're interested in
     *
     * @throws ProgrammingError for unknown property name
     *
     * @return mixed
     */
    public function __get($key)
    {
        if (array_key_exists($key, $this->properties)) {
            return $this->properties[$key];
        }

        throw new ProgrammingError(
            'Trying to get invalid "%s" property for %s',
            $key,
            get_class($this)
        );
    }

    /**
     * Setter for widget properties
     *
     * @param  string $key The option you want to set
     * @param  string $val The new value going to be assigned to this option
     *
     * @throws ProgrammingError for unknown property name
     *
     * @return mixed
     */
    public function __set($key, $val)
    {
        if (array_key_exists($key, $this->properties)) {
            $this->properties[$key] = $val;
            return;
        }

        throw new ProgrammingError(
            'Trying to set invalid "%s" property in %s. Allowed are: %s',
            $key,
            get_class($this),
            empty($this->properties)
                ? 'none'
                : implode(', ', array_keys($this->properties))
        );
    }

    abstract public function render();

    /**
     * Access the current view
     *
     * Will instantiate a new one if none exists
     * // TODO: App->getView
     *
     * @return Zend_View_Abstract
     */
    protected function view()
    {
        if (self::$view === null) {
            self::$view = Icinga::app()->getViewRenderer()->view;
        }

        return self::$view;
    }

    /**
     * Cast this widget to a string. Will call your render() function
     *
     * @return string
     */
    public function __toString()
    {
        try {
            $html = $this->render();
        } catch (Exception $e) {
            return htmlspecialchars($e->getMessage());
        }
        return (string) $html;
    }
}