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