alecpl
2010-09-29 5c461bada970c336616b0c03c9036f89cab21d0c
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                     |
A 8  | Copyright (C) 2008-2010, Roundcube Dev. - Switzerland                 |
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 $env = array();
T 39     private $texts = array();
40     private $commands = array();
0e99d3 41     private $callbacks = array();
69f18a 42     private $message = null;
47124c 43
115158 44     public $browser;
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();
T 137         
138         if (strpos($cmd[0], 'plugin.') === 0)
139           $this->callbacks[] = $cmd;
140         else
141           $this->commands[] = $cmd;
47124c 142     }
T 143     
144     
145     /**
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];
153         
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;
173             $this->command(
174                 'display_message',
175                 rcube_label(array('name' => $message, 'vars' => $vars)),
176                 $type
177             );
178         }
47124c 179     }
2eb794 180
A 181
47124c 182     /**
T 183      * Delete all stored env variables and commands
184      */
f7a122 185     public function reset()
47124c 186     {
T 187         $this->env = array();
188         $this->texts = array();
189         $this->commands = array();
190     }
2eb794 191
A 192
c719f3 193     /**
T 194      * Redirect to a certain url
195      *
5c461b 196      * @param mixed $p Either a string with the action or url parameters as key-value pairs
A 197      * @param int $delay Delay in seconds
c719f3 198      * @see rcmail::url()
T 199      */
fde466 200     public function redirect($p = array(), $delay = 1)
c719f3 201     {
T 202         $location = rcmail::get_instance()->url($p);
203         $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
204         exit;
205     }
206     
47124c 207     
T 208     /**
209      * Send an AJAX response to the client.
210      */
211     public function send()
212     {
213         $this->remote_response();
214         exit;
215     }
216     
217     
218     /**
219      * Send an AJAX response with executable JS code
220      *
5c461b 221      * @param  string  $add Additional JS code
47124c 222      * @param  boolean True if output buffer should be flushed
T 223      * @return void
224      * @deprecated
225      */
cc97ea 226     public function remote_response($add='')
47124c 227     {
T 228         static $s_header_sent = false;
229
230         if (!$s_header_sent) {
231             $s_header_sent = true;
232             send_nocacheing_headers();
cc97ea 233             header('Content-Type: text/plain; charset=' . $this->get_charset());
47124c 234         }
T 235
236         // unset default env vars
237         unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
238
cc97ea 239         $rcmail = rcmail::get_instance();
T 240         $response = array('action' => $rcmail->action, 'unlock' => (bool)$_REQUEST['_unlock']);
241         
242         if (!empty($this->env))
2eb794 243             $response['env'] = $this->env;
cc97ea 244           
T 245         if (!empty($this->texts))
2eb794 246             $response['texts'] = $this->texts;
47124c 247
0e99d3 248         // send function calls
cc97ea 249         $response['exec'] = $this->get_js_commands() . $add;
0e99d3 250         
T 251         if (!empty($this->callbacks))
2eb794 252             $response['callbacks'] = $this->callbacks;
cc97ea 253
2717f9 254         echo json_serialize($response);
47124c 255     }
2eb794 256
A 257
47124c 258     /**
T 259      * Return executable javascript code for all registered commands
260      *
261      * @return string $out
262      */
263     private function get_js_commands()
264     {
47fab0 265         $out = '';
2717f9 266
47124c 267         foreach ($this->commands as $i => $args) {
T 268             $method = array_shift($args);
269             foreach ($args as $i => $arg) {
2717f9 270                 $args[$i] = json_serialize($arg);
47124c 271             }
T 272
273             $out .= sprintf(
274                 "this.%s(%s);\n",
275                 preg_replace('/^parent\./', '', $method),
276                 implode(',', $args)
277             );
278         }
a4e6ed 279
47124c 280         return $out;
T 281     }
282 }