1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL-2). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl.
7 *
8 * @category  Horde
9 * @copyright 2012-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl LGPL-2
11 * @package   Horde
12 */
13
14/**
15 * Defines the AJAX actions used in Horde.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2012-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/lgpl LGPL-2
21 * @package   Horde
22 */
23class Horde_Ajax_Application_Handler extends Horde_Core_Ajax_Application_Handler
24{
25    /**
26     * AJAX action: Update topbar.
27     *
28     * @return Horde_Core_Ajax_Response_HordeCore  Response object.
29     */
30    public function topbarUpdate()
31    {
32        global $injector, $registry;
33
34        $pushed = $registry->pushApp($this->vars->app);
35        $topbar = $injector->getInstance('Horde_Core_Factory_Topbar')
36            ->create('Horde_Tree_Renderer_Menu', array('nosession' => true));
37        $hash = $topbar->getHash();
38        $tree = $topbar->getTree();
39        if ($pushed) {
40            $registry->popApp();
41        }
42
43        if ($this->vars->hash == $hash) {
44            return false;
45        }
46
47        $node_defs = $tree->renderNodeDefinitions();
48        $node_defs->hash = $hash;
49
50        if (isset($node_defs->files)) {
51            $jsfiles = $node_defs->files;
52            unset($node_defs->files);
53        } else {
54            $jsfiles = array();
55        }
56
57        $ob = new Horde_Core_Ajax_Response_HordeCore($node_defs);
58        $ob->jsfiles = $jsfiles;
59
60        return $ob;
61    }
62
63    /**
64     * AJAX action: Update sidebar.
65     *
66     * @return object  See Horde_Core_Tree_Renderer_Javascript#renderNodeDefinitions().
67     */
68    public function sidebarUpdate()
69    {
70        return $GLOBALS['injector']->getInstance('Horde_Core_Sidebar')->getTree()->renderNodeDefinitions();
71    }
72
73    /**
74     * AJAX action: Auto-update portal block. To be called automatically by
75     * the block, with no user selected options.
76     *
77     * @return string  The full HTML needed to render the block content.
78     */
79    public function blockAutoUpdate()
80    {
81        $html = '';
82
83        if (isset($this->vars->app) && isset($this->vars->blockid)) {
84            try {
85                $block = $GLOBALS['injector']
86                    ->getInstance('Horde_Core_Factory_BlockCollection')
87                    ->create()
88                    ->getBlock($this->vars->app, $this->vars->blockid);
89                if (!empty($block->autoUpdateMethod) && is_callable(array($block, $block->autoUpdateMethod))) {
90                    $html = call_user_func_array(array($block, $block->autoUpdateMethod), isset($this->vars->options) ? array($this->vars->options) : array());
91                } else {
92                    $html = $block->getContent(isset($this->vars->options) ? $this->vars->options : null);
93                }
94            } catch (Exception $e) {
95                $html = $e->getMessage();
96            }
97        }
98
99        return $html;
100    }
101
102    /**
103     * AJAX action: Refresh portal block. Manually refresh the block content,
104     * may $this->vars may contain user selected/provided values.
105     *
106     * @return string  The full HTML needed to render the block content.
107     */
108    public function blockRefresh()
109    {
110        $html = '';
111        if (!isset($this->vars->app)) {
112            $this->vars->set('app', 'horde');
113        }
114        if (isset($this->vars->blockid)) {
115            try {
116                $html = $GLOBALS['injector']
117                    ->getInstance('Horde_Core_Factory_BlockCollection')
118                    ->create()
119                    ->getBlock($this->vars->app, $this->vars->blockid)
120                    ->refreshContent($this->vars);
121            } catch (Exception $e) {
122                $html = $e->getMessage();
123            }
124        }
125        return $html;
126    }
127
128    /**
129     * AJAX action: Update portal block data. To be used when the block can
130     * refresh using only JSON data. I.e., this data would need to be parsed
131     * by the block code to be rendered.
132     *
133     * @return Horde_Core_Response
134     */
135    public function blockUpdate()
136    {
137        if (isset($this->vars->blockid)) {
138            try {
139                return $GLOBALS['injector']
140                    ->getInstance('Horde_Core_Factory_BlockCollection')
141                    ->create()
142                    ->getBlock($this->vars->app, $this->vars->blockid)
143                    ->getAjaxUpdate($this->vars);
144            } catch (Exception $e) {
145                return $e->getMessage();
146            }
147        }
148
149        return '';
150    }
151
152}
153