1: <?php
2: /**
3: * Copyright 2006-2012 Horde LLC (http://www.horde.org/)
4: *
5: * @author Chuck Hagenbuch <chuck@horde.org>
6: * @category Horde
7: * @package View
8: * @subpackage Helper
9: */
10:
11: /**
12: * View helper for displaying Horde block objects.
13: *
14: * @author Chuck Hagenbuch <chuck@horde.org>
15: * @category Horde
16: * @package View
17: * @subpackage Helper
18: */
19: class Horde_View_Helper_Block extends Horde_View_Helper_Base
20: {
21: /**
22: * Blocks that have already been fetched.
23: *
24: * @var array
25: */
26: protected $_blockCache = array();
27:
28: /**
29: * Returns the title of the specified block.
30: *
31: * @param string $block The name of the block to get the title for.
32: * @param mixed $arg1 The first argument to the Block constructor.
33: *
34: * @return string The requested Block's title.
35: * @throws Horde_View_Exception
36: * @throws InvalidArgumentException
37: */
38: public function blockTitle()
39: {
40: list($block, $params) = $this->_args(func_get_args());
41: return $this->_block($block, $params)->getTitle();
42: }
43:
44: /**
45: * Returns the content of the specified block.
46: *
47: * @param string $block The name of the block to get the content for.
48: * @param mixed $arg1 The first argument to the Block constructor.
49: *
50: * @return string The requested Block's content.
51: * @throws Horde_View_Exception
52: * @throws InvalidArgumentException
53: */
54: public function blockContent()
55: {
56: list($block, $params) = $this->_args(func_get_args());
57: return $this->_block($block, $params)->getContent();
58: }
59:
60: /**
61: * Instantiates and caches Block objects.
62: *
63: * @param string $block The name of the block to fetch.
64: * @param array $params Any arguments to the Block constructor.
65: *
66: * @return Horde_Core_Block The requested Block object.
67: * @throws Horde_View_Exception
68: */
69: protected function _block($block, $params)
70: {
71: $hash = sha1(serialize(array($block, $params)));
72:
73: if (!isset($this->_blockCache[$hash])) {
74: try {
75: $this->_blockCache[$hash] = $GLOBALS['injector']
76: ->getInstance('Horde_Core_Factory_BlockCollection')
77: ->create()
78: ->getBlock($block, $params);
79: } catch (Exception $e) {
80: throw new Horde_View_Exception($e);
81: }
82: }
83:
84: return $this->_blockCache[$hash];
85: }
86:
87: /**
88: * Parses any argument style for the Block-fetching functions.
89: *
90: * @param array $args
91: */
92: protected function _args($args)
93: {
94: $argc = count($args);
95:
96: if ($argc == 1) {
97: if (is_array($args[0])) {
98: $args = $args[0];
99: $argc = count($args);
100: }
101: }
102:
103: if ($argc < 2) {
104: throw new InvalidArgumentException('You must provide at least an application name and a block name.');
105: }
106: $app = array_shift($args);
107: $block = array_shift($args);
108:
109: return array($app, $block, $args);
110: }
111: }
112: