source: subversion/trunk/roundcubemail/program/include/rcube_json_output.php @ 2905

Last change on this file since 2905 was 2905, checked in by alec, 4 years ago
  • remove redundant JQ() use
File size: 6.7 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/include/rcube_json_output.php                                 |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Class to handle HTML page output using a skin template.             |
13 |   Extends rcube_html_page class from rcube_shared.inc                 |
14 |                                                                       |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17 +-----------------------------------------------------------------------+
18
19 $Id:  $
20
21*/
22
23
24/**
25 * View class to produce JSON responses
26 *
27 * @package View
28 */
29class rcube_json_output
30{
31    private $config;
32    private $charset = RCMAIL_CHARSET;
33    private $env = array();
34    private $texts = array();
35    private $commands = array();
36    private $callbacks = array();
37    private $message = null;
38
39    public $type = 'js';
40    public $ajax_call = true;
41   
42   
43    /**
44     * Constructor
45     */
46    public function __construct($task)
47    {
48        $this->config = rcmail::get_instance()->config;
49    }
50   
51   
52    /**
53     * Set environment variable
54     *
55     * @param string Property name
56     * @param mixed Property value
57     */
58    public function set_env($name, $value)
59    {
60        $this->env[$name] = $value;
61    }
62   
63    /**
64     * Issue command to set page title
65     *
66     * @param string New page title
67     */
68    public function set_pagetitle($title)
69    {
70        $name = $this->config->get('product_name');
71        $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
72    }
73
74    /**
75     * @ignore
76     */
77    function set_charset($charset)
78    {
79        // ignore: $this->charset = $charset;
80    }
81
82
83    /**
84     * Get charset for output
85     *
86     * @return string Output charset
87     */
88    function get_charset()
89    {
90        return $this->charset;
91    }
92
93
94    /**
95     * Register a template object handler
96     *
97     * @param  string Object name
98     * @param  string Function name to call
99     * @return void
100     */
101    public function add_handler($obj, $func)
102    {
103        // ignore
104    }
105
106    /**
107     * Register a list of template object handlers
108     *
109     * @param  array Hash array with object=>handler pairs
110     * @return void
111     */
112    public function add_handlers($arr)
113    {
114        // ignore
115    }
116   
117   
118    /**
119     * Call a client method
120     *
121     * @param string Method to call
122     * @param ... Additional arguments
123     */
124    public function command()
125    {
126        $cmd = func_get_args();
127       
128        if (strpos($cmd[0], 'plugin.') === 0)
129          $this->callbacks[] = $cmd;
130        else
131          $this->commands[] = $cmd;
132    }
133   
134   
135    /**
136     * Add a localized label to the client environment
137     */
138    public function add_label()
139    {
140        $args = func_get_args();
141        if (count($args) == 1 && is_array($args[0]))
142            $args = $args[0];
143       
144        foreach ($args as $name) {
145            $this->texts[$name] = rcube_label($name);
146        }
147    }
148   
149
150    /**
151     * Invoke display_message command
152     *
153     * @param string Message to display
154     * @param string Message type [notice|confirm|error]
155     * @param array Key-value pairs to be replaced in localized text
156     * @param boolean Override last set message
157     * @uses self::command()
158     */
159    public function show_message($message, $type='notice', $vars=null, $override=true)
160    {
161        if ($override || !$this->message) {
162            $this->message = $message;
163            $this->command(
164                'display_message',
165                rcube_label(array('name' => $message, 'vars' => $vars)),
166                $type
167            );
168        }
169    }
170   
171    /**
172     * Delete all stored env variables and commands
173     */
174    public function reset()
175    {
176        $this->env = array();
177        $this->texts = array();
178        $this->commands = array();
179    }
180   
181    /**
182     * Redirect to a certain url
183     *
184     * @param mixed Either a string with the action or url parameters as key-value pairs
185     * @see rcmail::url()
186     */
187    public function redirect($p = array(), $delay = 1)
188    {
189        $location = rcmail::get_instance()->url($p);
190        $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
191        exit;
192    }
193   
194   
195    /**
196     * Send an AJAX response to the client.
197     */
198    public function send()
199    {
200        $this->remote_response();
201        exit;
202    }
203   
204   
205    /**
206     * Send an AJAX response with executable JS code
207     *
208     * @param  string  Additional JS code
209     * @param  boolean True if output buffer should be flushed
210     * @return void
211     * @deprecated
212     */
213    public function remote_response($add='')
214    {
215        static $s_header_sent = false;
216
217        if (!$s_header_sent) {
218            $s_header_sent = true;
219            send_nocacheing_headers();
220            header('Content-Type: text/plain; charset=' . $this->get_charset());
221            print '/** ajax response ['.date('d/M/Y h:i:s O')."] **/\n";
222        }
223
224        // unset default env vars
225        unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
226
227        $rcmail = rcmail::get_instance();
228        $response = array('action' => $rcmail->action, 'unlock' => (bool)$_REQUEST['_unlock']);
229       
230        if (!empty($this->env))
231          $response['env'] = $this->env;
232         
233        if (!empty($this->texts))
234          $response['texts'] = $this->texts;
235
236        // send function calls
237        $response['exec'] = $this->get_js_commands() . $add;
238       
239        if (!empty($this->callbacks))
240          $response['callbacks'] = $this->callbacks;
241
242        echo json_serialize($response);
243    }
244   
245   
246    /**
247     * Return executable javascript code for all registered commands
248     *
249     * @return string $out
250     */
251    private function get_js_commands()
252    {
253        $out = '';
254       
255        foreach ($this->commands as $i => $args) {
256            $method = array_shift($args);
257            foreach ($args as $i => $arg) {
258                $args[$i] = json_serialize($arg);
259            }
260
261            $out .= sprintf(
262                "this.%s(%s);\n",
263                preg_replace('/^parent\./', '', $method),
264                implode(',', $args)
265            );
266        }
267
268        return $out;
269    }
270}
271
272
Note: See TracBrowser for help on using the repository browser.