4 +-----------------------------------------------------------------------+
5 | program/include/rcube_json_output.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Class to handle HTML page output using a skin template. |
13 | Extends rcube_html_page class from rcube_shared.inc |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 +-----------------------------------------------------------------------+
25 * View class to produce JSON responses
29 class rcube_json_output
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;
40 public $ajax_call = true;
46 public function __construct($task)
48 $this->config = rcmail::get_instance()->config;
53 * Set environment variable
55 * @param string Property name
56 * @param mixed Property value
58 public function set_env($name, $value)
60 $this->env[$name] = $value;
64 * Issue command to set page title
66 * @param string New page title
68 public function set_pagetitle($title)
70 $name = $this->config->get('product_name');
71 $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
77 function set_charset($charset)
79 // ignore: $this->charset = $charset;
84 * Get charset for output
86 * @return string Output charset
88 function get_charset()
90 return $this->charset;
95 * Register a template object handler
97 * @param string Object name
98 * @param string Function name to call
101 public function add_handler($obj, $func)
107 * Register a list of template object handlers
109 * @param array Hash array with object=>handler pairs
112 public function add_handlers($arr)
119 * Call a client method
121 * @param string Method to call
122 * @param ... Additional arguments
124 public function command()
126 $cmd = func_get_args();
128 if (strpos($cmd[0], 'plugin.') === 0)
129 $this->callbacks[] = $cmd;
131 $this->commands[] = $cmd;
136 * Add a localized label to the client environment
138 public function add_label()
140 $args = func_get_args();
141 if (count($args) == 1 && is_array($args[0]))
144 foreach ($args as $name) {
145 $this->texts[$name] = rcube_label($name);
151 * Invoke display_message command
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()
159 public function show_message($message, $type='notice', $vars=null, $override=true)
161 if ($override || !$this->message) {
162 $this->message = $message;
165 rcube_label(array('name' => $message, 'vars' => $vars)),
172 * Delete all stored env variables and commands
174 public function reset()
176 $this->env = array();
177 $this->texts = array();
178 $this->commands = array();
182 * Redirect to a certain url
184 * @param mixed Either a string with the action or url parameters as key-value pairs
187 public function redirect($p = array(), $delay = 1)
189 $location = rcmail::get_instance()->url($p);
190 $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
196 * Send an AJAX response to the client.
198 public function send()
200 $this->remote_response();
206 * Send an AJAX response with executable JS code
208 * @param string Additional JS code
209 * @param boolean True if output buffer should be flushed
213 public function remote_response($add='')
215 static $s_header_sent = false;
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";
224 // unset default env vars
225 unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
227 $rcmail = rcmail::get_instance();
228 $response = array('action' => $rcmail->action, 'unlock' => (bool)$_REQUEST['_unlock']);
230 if (!empty($this->env))
231 $response['env'] = $this->env;
233 if (!empty($this->texts))
234 $response['texts'] = $this->texts;
236 // send function calls
237 $response['exec'] = $this->get_js_commands() . $add;
239 if (!empty($this->callbacks))
240 $response['callbacks'] = $this->callbacks;
242 echo json_serialize($response);
247 * Return executable javascript code for all registered commands
249 * @return string $out
251 private function get_js_commands()
255 foreach ($this->commands as $i => $args) {
256 $method = array_shift($args);
257 foreach ($args as $i => $arg) {
258 $args[$i] = json_serialize($arg);
263 preg_replace('/^parent\./', '', $method),