4 +-----------------------------------------------------------------------+
5 | program/include/rcube_json_output.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2008, 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 = 'UTF-8';
33 private $env = array();
34 private $texts = array();
35 private $commands = array();
38 public $ajax_call = true;
44 public function __construct($task)
47 $this->config = rcmail::get_instance()->config;
52 * Set environment variable
54 * @param string Property name
55 * @param mixed Property value
57 public function set_env($name, $value)
59 $this->env[$name] = $value;
65 public function set_pagetitle($title)
67 $name = $this->config->get('product_name');
68 $this->command('set_pagetitle', JQ(empty($name) ? $title : $name.' :: '.$title));
74 function set_charset($charset)
76 // ignore: $this->charset = $charset;
81 * Get charset for output
83 * @return string Output charset
85 function get_charset()
87 return $this->charset;
92 * Register a template object handler
94 * @param string Object name
95 * @param string Function name to call
98 public function add_handler($obj, $func)
104 * Register a list of template object handlers
106 * @param array Hash array with object=>handler pairs
109 public function add_handlers($arr)
116 * Call a client method
118 * @param string Method to call
119 * @param ... Additional arguments
121 public function command()
123 $this->commands[] = func_get_args();
128 * Add a localized label to the client environment
130 public function add_label()
132 $arg_list = func_get_args();
133 foreach ($arg_list as $i => $name) {
134 $this->texts[$name] = rcube_label($name);
140 * Invoke display_message command
142 * @param string Message to display
143 * @param string Message type [notice|confirm|error]
144 * @param array Key-value pairs to be replaced in localized text
145 * @uses self::command()
147 public function show_message($message, $type='notice', $vars=null)
151 rcube_label(array('name' => $message, 'vars' => $vars)),
157 * Delete all stored env variables and commands
159 public public function reset()
161 $this->env = array();
162 $this->texts = array();
163 $this->commands = array();
168 * Send an AJAX response to the client.
170 public function send()
172 $this->remote_response();
178 * Send an AJAX response with executable JS code
180 * @param string Additional JS code
181 * @param boolean True if output buffer should be flushed
185 public function remote_response($add='', $flush=false)
187 static $s_header_sent = false;
189 if (!$s_header_sent) {
190 $s_header_sent = true;
191 send_nocacheing_headers();
192 header('Content-Type: application/x-javascript; charset=' . $this->get_charset());
193 print '/** ajax response ['.date('d/M/Y h:i:s O')."] **/\n";
196 // unset default env vars
197 unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
199 // send response code
200 echo $this->get_js_commands() . $add;
202 // flush the output buffer
209 * Return executable javascript code for all registered commands
211 * @return string $out
213 private function get_js_commands()
217 if (sizeof($this->env))
218 $out .= 'this.set_env('.json_serialize($this->env).");\n";
220 foreach($this->texts as $name => $text) {
221 $out .= sprintf("this.add_label('%s', '%s');\n", $name, JQ($text));
224 foreach ($this->commands as $i => $args) {
225 $method = array_shift($args);
226 foreach ($args as $i => $arg) {
227 $args[$i] = json_serialize($arg);
232 preg_replace('/^parent\./', '', $method),