1: <?php
2: /**
3: * An abstract class representing a single block in the portal/block display.
4: *
5: * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Mike Cochrane <mike@graftonhall.co.nz>
11: * @author Jan Schneider <jan@horde.org>
12: * @author Michael Slusarz <slusarz@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
15: * @package Core
16: */
17: abstract class Horde_Core_Block
18: {
19: /**
20: * Is this block enabled?
21: *
22: * @var boolean
23: */
24: public $enabled = true;
25:
26: /**
27: * Whether this block has changing content.
28: *
29: * @var boolean
30: */
31: public $updateable = false;
32:
33: /**
34: * Application that this block originated from.
35: *
36: * @var string
37: */
38: protected $_app;
39:
40: /**
41: * Block name. Should be set in the constructor.
42: *
43: * @var string
44: */
45: protected $_name = '';
46:
47: /**
48: * Block specific parameters.
49: *
50: * @var array
51: */
52: protected $_params = array();
53:
54: /**
55: * Constructor.
56: *
57: * @param string $app The application name.
58: * @param array|boolean $params Any parameters the block needs. If false,
59: * the default parameter will be used.
60: */
61: public function __construct($app, $params = array())
62: {
63: $this->_app = $app;
64:
65: // @todo: we can't simply merge the default values and stored values
66: // because empty parameter values are not stored at all, so they would
67: // always be overwritten by the defaults.
68: if ($params === false) {
69: foreach ($this->getParams() as $name => $param) {
70: $this->_params[$name] = $param['default'];
71: }
72: } else {
73: $this->_params = $params;
74: }
75: }
76:
77: /**
78: * Returns the application that this block belongs to.
79: *
80: * @return string The application name.
81: */
82: public function getApp()
83: {
84: return $this->_app;
85: }
86:
87: /**
88: * Return the block name.
89: *
90: * @return string The block name.
91: */
92: public function getName()
93: {
94: return $this->_name;
95: }
96:
97: /**
98: * Returns any settable parameters for this block.
99: * It does *not* reference $this->_params; that is for runtime
100: * parameters (the choices made from these options).
101: *
102: * @return array The block's configurable parameters.
103: */
104: public function getParams()
105: {
106: return $this->_call('_params', array());
107: }
108:
109: /**
110: * Returns the parameters needed by block.
111: *
112: * @return array The block's parameters.
113: */
114: protected function _params()
115: {
116: return array();
117: }
118:
119: /**
120: * Returns a hash of block parameters and their configured values.
121: *
122: * @return array Parameter values.
123: */
124: public function getParamValues()
125: {
126: return $this->_params;
127: }
128:
129: /**
130: * Returns the text to go in the title of this block.
131: *
132: * This function handles the changing of current application as
133: * needed so code is executed in the scope of the application the
134: * block originated from.
135: *
136: * @return string The block's title.
137: */
138: public function getTitle()
139: {
140: return $this->_call('_title', '');
141: }
142:
143: /**
144: * Returns the title to go in this block.
145: *
146: * @return string The block title.
147: */
148: protected function _title()
149: {
150: return $this->_name;
151: }
152:
153: /**
154: * Returns the content for this block.
155: *
156: * This function handles the changing of current application as
157: * needed so code is executed in the scope of the application the
158: * block originated from.
159: *
160: * @return string The block's content.
161: */
162: public function getContent()
163: {
164: return $this->_call('_content', '');
165: }
166:
167: /**
168: * Returns this block's content.
169: *
170: * @return string The block's content.
171: */
172: abstract protected function _content();
173:
174: /**
175: * The data to send on an AJAX update request.
176: *
177: * @param Horde_Variables $vars The form variables for the request.
178: *
179: * @return string Update data.
180: */
181: public function getAjaxUpdate(Horde_Variables $vars)
182: {
183: return $this->_call('_ajaxUpdate', '', $vars);
184: }
185:
186: /**
187: * Returns this block's content for AJAX updates.
188: *
189: * @param Horde_Variables $vars The form variables for the request.
190: *
191: * @return string The update content.
192: */
193: protected function _ajaxUpdate(Horde_Variables $vars)
194: {
195: return '';
196: }
197:
198: /**
199: * Return the URL to use for AJAX update requests.
200: *
201: * @return Horde_Url The update URL.
202: */
203: protected function _ajaxUpdateUrl()
204: {
205: $ajax_url = Horde::getServiceLink('ajax', 'horde');
206: $ajax_url->pathInfo = 'blockUpdate';
207: $ajax_url->add('blockid', get_class($this));
208:
209: return $ajax_url;
210: }
211:
212: /**
213: * Calls the application driver in the proper context.
214: *
215: * @param string $name string
216: * @param mixed $default
217: * @param mixed $args
218: *
219: * @return mixed
220: */
221: protected function _call($name, $default, $args = null)
222: {
223: try {
224: $pushed = $GLOBALS['registry']->pushApp($this->getApp(), array(
225: 'check_perms' => true,
226: 'logintasks' => false
227: ));
228: } catch (Horde_Exception $e) {
229: return $default;
230: }
231:
232: try {
233: $ret = is_null($args)
234: ? $this->$name()
235: : call_user_func($name, $args);
236: } catch (Horde_Exception $e) {
237: $ret = $default;
238: }
239:
240: if ($pushed) {
241: $GLOBALS['registry']->popApp();
242: }
243:
244: return $ret;
245: }
246:
247: }
248: