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)
46 $this->config = rcmail::get_instance()->config;
51 * Set environment variable
53 * @param string Property name
54 * @param mixed Property value
56 public function set_env($name, $value)
58 $this->env[$name] = $value;
62 * Issue command to set page title
64 * @param string New page title
66 public function set_pagetitle($title)
68 $name = $this->config->get('product_name');
69 $this->command('set_pagetitle', JQ(empty($name) ? $title : $name.' :: '.$title));
75 function set_charset($charset)
77 // ignore: $this->charset = $charset;
82 * Get charset for output
84 * @return string Output charset
86 function get_charset()
88 return $this->charset;
93 * Register a template object handler
95 * @param string Object name
96 * @param string Function name to call
99 public function add_handler($obj, $func)
105 * Register a list of template object handlers
107 * @param array Hash array with object=>handler pairs
110 public function add_handlers($arr)
117 * Call a client method
119 * @param string Method to call
120 * @param ... Additional arguments
122 public function command()
124 $this->commands[] = func_get_args();
129 * Add a localized label to the client environment
131 public function add_label()
133 $arg_list = func_get_args();
134 foreach ($arg_list as $i => $name) {
135 $this->texts[$name] = rcube_label($name);
141 * Invoke display_message command
143 * @param string Message to display
144 * @param string Message type [notice|confirm|error]
145 * @param array Key-value pairs to be replaced in localized text
146 * @uses self::command()
148 public function show_message($message, $type='notice', $vars=null)
152 rcube_label(array('name' => $message, 'vars' => $vars)),
158 * Delete all stored env variables and commands
160 public function reset()
162 $this->env = array();
163 $this->texts = array();
164 $this->commands = array();
168 * Redirect to a certain url
170 * @param mixed Either a string with the action or url parameters as key-value pairs
173 public function redirect($p = array(), $delay = 1)
175 $location = rcmail::get_instance()->url($p);
176 $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
182 * Send an AJAX response to the client.
184 public function send()
186 $this->remote_response();
192 * Send an AJAX response with executable JS code
194 * @param string Additional JS code
195 * @param boolean True if output buffer should be flushed
199 public function remote_response($add='', $flush=false)
201 static $s_header_sent = false;
203 if (!$s_header_sent) {
204 $s_header_sent = true;
205 send_nocacheing_headers();
206 header('Content-Type: application/x-javascript; charset=' . $this->get_charset());
207 print '/** ajax response ['.date('d/M/Y h:i:s O')."] **/\n";
210 // unset default env vars
211 unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
213 // send response code
214 echo $this->get_js_commands() . $add;
216 // flush the output buffer
223 * Return executable javascript code for all registered commands
225 * @return string $out
227 private function get_js_commands()
231 if (sizeof($this->env))
232 $out .= 'this.set_env('.json_serialize($this->env).");\n";
234 foreach($this->texts as $name => $text) {
235 $out .= sprintf("this.add_label('%s', '%s');\n", $name, JQ($text));
238 foreach ($this->commands as $i => $args) {
239 $method = array_shift($args);
240 foreach ($args as $i => $arg) {
241 $args[$i] = json_serialize($arg);
246 preg_replace('/^parent\./', '', $method),