1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Horde_Core_Block_Collection implements Serializable
18: {
19: 20: 21: 22: 23: 24:
25: protected $_blocks = array();
26:
27: 28: 29: 30: 31:
32: protected $_layout;
33:
34: 35: 36: 37: 38: 39:
40: public function __construct(array $apps, $layout)
41: {
42: foreach ($apps as $app) {
43: $drivers = $GLOBALS['registry']->getAppDrivers($app, 'Block');
44: foreach ($drivers as $val) {
45: $tmp = new $val($app);
46: if ($tmp->enabled) {
47: $this->_blocks[$app][$val]['name'] = $tmp->getName();
48: }
49: }
50: }
51:
52: $this->_layout = $layout;
53: }
54:
55: 56: 57: 58: 59:
60: public function getLayout()
61: {
62: $layout = @unserialize($GLOBALS['prefs']->getValue($this->_layout));
63:
64: if (empty($layout)) {
65: $layout = array();
66:
67: if (isset($GLOBALS['conf']['portal']['fixed_blocks'])) {
68: foreach ($GLOBALS['conf']['portal']['fixed_blocks'] as $block) {
69: list($app, $type) = explode(':', $block, 2);
70: $layout[] = array(
71: array(
72: 'app' => $app,
73: 'params' => array(
74: 'type2' => $type,
75: 'params' => false
76: ),
77: 'height' => 1,
78: 'width' => 1
79: )
80: );
81: }
82: }
83: }
84:
85: return $layout;
86: }
87:
88: 89: 90: 91: 92:
93: public function getLayoutManager()
94: {
95: return new Horde_Core_Block_Layout_Manager($this);
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public function getBlock($app, $name, $params = null)
110: {
111: global $registry;
112:
113: if ($registry->isInactive($app)) {
114: throw new Horde_Exception(sprintf('%s is not activated.', $GLOBALS['registry']->get('name', $app)));
115: }
116:
117: $pushed = $registry->pushApp($app);
118:
119: if (!class_exists($name)) {
120: if ($pushed) {
121: $registry->popApp($app);
122: }
123: throw new Horde_Exception(sprintf('%s not found.', $name));
124: }
125:
126: if (is_null($params)) {
127: foreach ($this->getLayout() as $row) {
128: foreach ($row as $col) {
129: if (is_array($col) &&
130: (strcasecmp($col['params']['type2'], $name) === 0)) {
131: $params = $col['params']['params'];
132: break 2;
133: }
134: }
135: }
136: }
137:
138: $ob = new $name($app, $params);
139:
140: if ($pushed) {
141: $registry->popApp($app);
142: }
143:
144: if (is_null($ob)) {
145: throw new Horde_Exception(sprintf('%s not found.', $name));
146: }
147:
148: return $ob;
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function getBlocksList()
158: {
159: $blocks = array();
160:
161:
162: foreach ($this->_blocks as $app => $app_blocks) {
163: $app_name = $GLOBALS['registry']->get('name', $app);
164:
165: foreach ($app_blocks as $block_id => $block) {
166: $blocks[$app . ':' . $block_id] = $app_name . ': ' . $block['name'];
167: }
168: }
169:
170: return $blocks;
171: }
172:
173: 174: 175: 176: 177:
178: public function getFixedBlocks()
179: {
180: $layout = array();
181:
182:
183: return $layout;
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195:
196: public function getBlocksWidget($cur_app = null, $cur_block = null,
197: $onchange = false, $readonly = false)
198: {
199: $widget = '<select name=' . (!$readonly ? '"app"' : '"roapp"');
200:
201: if ($onchange) {
202: $widget .= ' onchange="document.blockform.action.value=\'save-resume\';document.blockform.submit()"';
203: }
204:
205: if ($readonly) {
206: $widget .= ' disabled="disabled"';
207: }
208:
209: $widget .= ">\n";
210:
211: foreach ($this->getBlocksList() as $id => $name) {
212: $widget .= sprintf(
213: "<option value=\"%s\"%s>%s</option>\n",
214: $id,
215: ($id == $cur_app . ':' . $cur_block) ? ' selected="selected"' : '',
216: $name
217: );
218: }
219: $widget .= "</select>\n";
220: if ($readonly) {
221: $widget .= '<input type="hidden" name="app" value="' . $cur_app . ':' . $cur_block . '" />' . "\n";
222: }
223:
224: return $widget;
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function getOptionType($app, $block, $param_id)
237: {
238: $this->getParams($app, $block);
239: return $this->_blocks[$app][$block]['params'][$param_id]['type2'];
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249: 250:
251: public function getOptionRequired($app, $block, $param_id)
252: {
253: $this->getParams($app, $block);
254: return isset($this->_blocks[$app][$block]['params'][$param_id]['required'])
255: ? $this->_blocks[$app][$block]['params'][$param_id]['required']
256: : true;
257: }
258:
259: 260: 261: 262: 263: 264: 265: 266: 267:
268: public function getOptionValues($app, $block, $param_id)
269: {
270: $this->getParams($app, $block);
271: return $this->_blocks[$app][$block]['params'][$param_id]['values'];
272: }
273:
274: 275: 276: 277: 278: 279: 280: 281: 282: 283:
284: public function getOptionsWidget($app, $block, $param_id, $val = null)
285: {
286: $widget = '';
287:
288: $this->getParams($app, $block);
289: $param = $this->_blocks[$app][$block]['params'][$param_id];
290: if (!isset($param['default'])) {
291: $param['default'] = '';
292: }
293:
294: switch ($param['type']) {
295: case 'boolean':
296: case 'checkbox':
297: $checked = !empty($val[$param_id]) ? ' checked="checked"' : '';
298: $widget = sprintf('<input type="checkbox" name="params[%s]"%s />', $param_id, $checked);
299: break;
300:
301: case 'enum':
302: $widget = sprintf('<select name="params[%s]">', $param_id);
303: foreach ($param['values'] as $key => $name) {
304: if (Horde_String::length($name) > 30) {
305: $name = substr($name, 0, 27) . '...';
306: }
307: $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
308: htmlspecialchars($key),
309: (isset($val[$param_id]) && $val[$param_id] == $key) ? ' selected="selected"' : '',
310: htmlspecialchars($name));
311: }
312:
313: $widget .= '</select>';
314: break;
315:
316: case 'multienum':
317: $widget = sprintf('<select multiple="multiple" name="params[%s][]">', $param_id);
318: foreach ($param['values'] as $key => $name) {
319: if (Horde_String::length($name) > 30) {
320: $name = substr($name, 0, 27) . '...';
321: }
322: $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
323: htmlspecialchars($key),
324: (isset($val[$param_id]) && in_array($key, $val[$param_id])) ? ' selected="selected"' : '',
325: htmlspecialchars($name));
326: }
327:
328: $widget .= '</select>';
329: break;
330:
331: case 'mlenum':
332:
333: if (is_array($val) && isset($val['__' . $param_id])) {
334: $firstval = $val['__' . $param_id];
335: } else {
336: $tmp = array_keys($param['values']);
337: $firstval = current($tmp);
338: }
339: $blockvalues = $param['values'][$firstval];
340: asort($blockvalues);
341:
342: $widget = sprintf('<select name="params[__%s]" onchange="document.blockform.action.value=\'save-resume\';document.blockform.submit()">', $param_id) . "\n";
343: foreach ($param['values'] as $key => $values) {
344: $name = Horde_String::length($key) > 30 ? Horde_String::substr($key, 0, 27) . '...' : $key;
345: $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
346: htmlspecialchars($key),
347: $key == $firstval ? ' selected="selected"' : '',
348: htmlspecialchars($name));
349: }
350: $widget .= "</select><br />\n";
351:
352: $widget .= sprintf("<select name=\"params[%s]\">\n", $param_id);
353: foreach ($blockvalues as $key => $name) {
354: $name = (Horde_String::length($name) > 30) ? Horde_String::substr($name, 0, 27) . '...' : $name;
355: $widget .= sprintf("<option value=\"%s\"%s>%s</option>\n",
356: htmlspecialchars($key),
357: $val[$param_id] == $key ? ' selected="selected"' : '',
358: htmlspecialchars($name));
359: }
360: $widget .= "</select><br />\n";
361: break;
362:
363: case 'int':
364: case 'text':
365: $widget = sprintf('<input type="text" name="params[%s]" value="%s" />', $param_id, !isset($val[$param_id]) ? $param['default'] : $val[$param_id]);
366: break;
367:
368: case 'password':
369: $widget = sprintf('<input type="password" name="params[%s]" value="%s" />', $param_id, !isset($val[$param_id]) ? $param['default'] : $val[$param_id]);
370: break;
371:
372: case 'error':
373: $widget = '<span class="form-error">' . $param['default'] . '</span>';
374: break;
375: }
376:
377: return $widget;
378: }
379:
380: 381: 382: 383: 384: 385: 386: 387:
388: public function getName($app, $block)
389: {
390: return isset($this->_blocks[$app][$block])
391: ? $this->_blocks[$app][$block]['name']
392: : sprintf(Horde_Core_Translation::t("Block \"%s\" of application \"%s\" not found."), $block, $app);
393: }
394:
395: 396: 397: 398: 399: 400: 401: 402:
403: public function getParams($app, $block)
404: {
405: if (!isset($this->_blocks[$app][$block])) {
406: return array();
407: }
408:
409: if (!isset($this->_blocks[$app][$block]['params'])) {
410: $blockOb = $this->getBlock($app, $block);
411: $this->_blocks[$app][$block]['params'] = $blockOb->getParams();
412: }
413:
414: if (isset($this->_blocks[$app][$block]['params']) &&
415: is_array($this->_blocks[$app][$block]['params'])) {
416: return array_keys($this->_blocks[$app][$block]['params']);
417: }
418:
419: return array();
420: }
421:
422: 423: 424: 425: 426: 427: 428: 429: 430:
431: public function getParamName($app, $block, $param)
432: {
433: $this->getParams($app, $block);
434: return $this->_blocks[$app][$block]['params'][$param]['name'];
435: }
436:
437: 438: 439: 440: 441: 442: 443: 444: 445:
446: public function getDefaultValue($app, $block, $param)
447: {
448: $this->getParams($app, $block);
449: return isset($this->_blocks[$app][$block]['params'][$param]['default'])
450: ? $this->_blocks[$app][$block]['params'][$param]['default']
451: : null;
452: }
453:
454: 455: 456: 457: 458: 459: 460: 461:
462: public function isEditable($app, $block)
463: {
464: $block = $this->getBlock($app, $block);
465: return $block->updateable || $block->getParams();
466: }
467:
468:
469:
470: public function serialize()
471: {
472: return json_encode(array(
473: $this->_blocks,
474: $this->_layout
475: ));
476: }
477:
478: public function unserialize($data)
479: {
480: list($this->_blocks, $this->_layout) = json_decode($data, true);
481: }
482:
483: }
484: