alecpl
2011-05-21 bc8c2c57880523472b30f475d566a8133e2d2e20
commit | author | age
47124c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_json_output.php                                 |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2008-2010, The Roundcube Dev Team                       |
47124c 9  | Licensed under the GNU GPL                                            |
T 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
1d786c 19  $Id$
47124c 20
T 21 */
22
23
24 /**
25  * View class to produce JSON responses
26  *
27  * @package View
28  */
29 class rcube_json_output
30 {
5c461b 31     /**
A 32      * Stores configuration object.
33      *
34      * @var rcube_config
35      */
47124c 36     private $config;
ecb9fb 37     private $charset = RCMAIL_CHARSET;
47124c 38     private $texts = array();
T 39     private $commands = array();
0e99d3 40     private $callbacks = array();
69f18a 41     private $message = null;
47124c 42
115158 43     public $browser;
1ac543 44     public $env = array();
c8a21d 45     public $type = 'js';
47124c 46     public $ajax_call = true;
115158 47
A 48
47124c 49     /**
T 50      * Constructor
51      */
115158 52     public function __construct($task=null)
47124c 53     {
115158 54         $this->config  = rcmail::get_instance()->config;
A 55         $this->browser = new rcube_browser();
47124c 56     }
2eb794 57
A 58
47124c 59     /**
T 60      * Set environment variable
61      *
5c461b 62      * @param string $name Property name
A 63      * @param mixed $value Property value
47124c 64      */
T 65     public function set_env($name, $value)
66     {
67         $this->env[$name] = $value;
68     }
2eb794 69
A 70
47124c 71     /**
c719f3 72      * Issue command to set page title
T 73      *
5c461b 74      * @param string $title New page title
47124c 75      */
T 76     public function set_pagetitle($title)
77     {
c719f3 78         $name = $this->config->get('product_name');
e0480e 79         $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
47124c 80     }
2eb794 81
47124c 82
T 83     /**
84      * @ignore
85      */
86     function set_charset($charset)
87     {
88         // ignore: $this->charset = $charset;
89     }
90
91
92     /**
93      * Get charset for output
94      *
95      * @return string Output charset
96      */
97     function get_charset()
98     {
99         return $this->charset;
100     }
101
102
103     /**
104      * Register a template object handler
105      *
5c461b 106      * @param  string $obj Object name
A 107      * @param  string $func Function name to call
47124c 108      * @return void
T 109      */
110     public function add_handler($obj, $func)
111     {
112         // ignore
113     }
114
2eb794 115
47124c 116     /**
T 117      * Register a list of template object handlers
118      *
5c461b 119      * @param  array $arr Hash array with object=>handler pairs
47124c 120      * @return void
T 121      */
122     public function add_handlers($arr)
123     {
124         // ignore
125     }
2eb794 126
A 127
47124c 128     /**
T 129      * Call a client method
130      *
131      * @param string Method to call
132      * @param ... Additional arguments
133      */
134     public function command()
135     {
0e99d3 136         $cmd = func_get_args();
ad334a 137
0e99d3 138         if (strpos($cmd[0], 'plugin.') === 0)
T 139           $this->callbacks[] = $cmd;
140         else
141           $this->commands[] = $cmd;
47124c 142     }
ad334a 143
A 144
47124c 145     /**
T 146      * Add a localized label to the client environment
147      */
148     public function add_label()
149     {
5c2d6e 150         $args = func_get_args();
T 151         if (count($args) == 1 && is_array($args[0]))
152             $args = $args[0];
ad334a 153
5c2d6e 154         foreach ($args as $name) {
b2d8b8 155             $this->texts[$name] = rcube_label($name);
47124c 156         }
T 157     }
2eb794 158
47124c 159
T 160     /**
161      * Invoke display_message command
162      *
5c461b 163      * @param string  $message  Message to display
A 164      * @param string  $type     Message type [notice|confirm|error]
165      * @param array   $vars     Key-value pairs to be replaced in localized text
166      * @param boolean $override Override last set message
47124c 167      * @uses self::command()
T 168      */
69f18a 169     public function show_message($message, $type='notice', $vars=null, $override=true)
47124c 170     {
69f18a 171         if ($override || !$this->message) {
T 172             $this->message = $message;
07b95d 173             $msgtext = rcube_label_exists($message) ? rcube_label(array('name' => $message, 'vars' => $vars)) : $message;
T 174             $this->command('display_message', $msgtext, $type);
69f18a 175         }
47124c 176     }
2eb794 177
A 178
47124c 179     /**
T 180      * Delete all stored env variables and commands
181      */
f7a122 182     public function reset()
47124c 183     {
T 184         $this->env = array();
185         $this->texts = array();
186         $this->commands = array();
187     }
2eb794 188
A 189
c719f3 190     /**
T 191      * Redirect to a certain url
192      *
5c461b 193      * @param mixed $p Either a string with the action or url parameters as key-value pairs
A 194      * @param int $delay Delay in seconds
c719f3 195      * @see rcmail::url()
T 196      */
fde466 197     public function redirect($p = array(), $delay = 1)
c719f3 198     {
T 199         $location = rcmail::get_instance()->url($p);
200         $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
201         exit;
202     }
ad334a 203
A 204
47124c 205     /**
T 206      * Send an AJAX response to the client.
207      */
208     public function send()
209     {
210         $this->remote_response();
211         exit;
212     }
ad334a 213
A 214
47124c 215     /**
T 216      * Send an AJAX response with executable JS code
217      *
5c461b 218      * @param  string  $add Additional JS code
47124c 219      * @param  boolean True if output buffer should be flushed
T 220      * @return void
221      * @deprecated
222      */
cc97ea 223     public function remote_response($add='')
47124c 224     {
T 225         static $s_header_sent = false;
226
227         if (!$s_header_sent) {
228             $s_header_sent = true;
229             send_nocacheing_headers();
cc97ea 230             header('Content-Type: text/plain; charset=' . $this->get_charset());
47124c 231         }
T 232
233         // unset default env vars
234         unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
235
cc97ea 236         $rcmail = rcmail::get_instance();
10a6fc 237         $response['action'] = $rcmail->action;
A 238
239         if ($unlock = get_input_value('_unlock', RCUBE_INPUT_GPC)) {
240             $response['unlock'] = $unlock;
241         }
ad334a 242
cc97ea 243         if (!empty($this->env))
2eb794 244             $response['env'] = $this->env;
ad334a 245
cc97ea 246         if (!empty($this->texts))
2eb794 247             $response['texts'] = $this->texts;
47124c 248
0e99d3 249         // send function calls
cc97ea 250         $response['exec'] = $this->get_js_commands() . $add;
ad334a 251
0e99d3 252         if (!empty($this->callbacks))
2eb794 253             $response['callbacks'] = $this->callbacks;
cc97ea 254
2717f9 255         echo json_serialize($response);
47124c 256     }
2eb794 257
A 258
47124c 259     /**
T 260      * Return executable javascript code for all registered commands
261      *
262      * @return string $out
263      */
264     private function get_js_commands()
265     {
47fab0 266         $out = '';
2717f9 267
47124c 268         foreach ($this->commands as $i => $args) {
T 269             $method = array_shift($args);
270             foreach ($args as $i => $arg) {
2717f9 271                 $args[$i] = json_serialize($arg);
47124c 272             }
T 273
274             $out .= sprintf(
275                 "this.%s(%s);\n",
276                 preg_replace('/^parent\./', '', $method),
277                 implode(',', $args)
278             );
279         }
a4e6ed 280
47124c 281         return $out;
T 282     }
283 }