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