]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_json_output.php
Imported Upstream version 0.2~stable
[roundcube.git] / program / include / rcube_json_output.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_json_output.php                                 |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2008, RoundCube Dev. - Switzerland                      |
9  | Licensed under the GNU GPL                                            |
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
19  $Id:  $
20
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;
32     private $charset = 'UTF-8';
33     private $env = array();
34     private $texts = array();
35     private $commands = array();
36
37     public $type = 'js';
38     public $ajax_call = true;
39     
40     
41     /**
42      * Constructor
43      */
44     public function __construct($task)
45     {
46         $this->config = rcmail::get_instance()->config;
47     }
48     
49     
50     /**
51      * Set environment variable
52      *
53      * @param string Property name
54      * @param mixed Property value
55      */
56     public function set_env($name, $value)
57     {
58         $this->env[$name] = $value;
59     }
60     
61     /**
62      * Issue command to set page title
63      *
64      * @param string New page title
65      */
66     public function set_pagetitle($title)
67     {
68         $name = $this->config->get('product_name');
69         $this->command('set_pagetitle', JQ(empty($name) ? $title : $name.' :: '.$title));
70     }
71
72     /**
73      * @ignore
74      */
75     function set_charset($charset)
76     {
77         // ignore: $this->charset = $charset;
78     }
79
80
81     /**
82      * Get charset for output
83      *
84      * @return string Output charset
85      */
86     function get_charset()
87     {
88         return $this->charset;
89     }
90
91
92     /**
93      * Register a template object handler
94      *
95      * @param  string Object name
96      * @param  string Function name to call
97      * @return void
98      */
99     public function add_handler($obj, $func)
100     {
101         // ignore
102     }
103
104     /**
105      * Register a list of template object handlers
106      *
107      * @param  array Hash array with object=>handler pairs
108      * @return void
109      */
110     public function add_handlers($arr)
111     {
112         // ignore
113     }
114     
115     
116     /**
117      * Call a client method
118      *
119      * @param string Method to call
120      * @param ... Additional arguments
121      */
122     public function command()
123     {
124         $this->commands[] = func_get_args();
125     }
126     
127     
128     /**
129      * Add a localized label to the client environment
130      */
131     public function add_label()
132     {
133         $arg_list = func_get_args();
134         foreach ($arg_list as $i => $name) {
135             $this->texts[$name] = rcube_label($name);
136         }
137     }
138     
139
140     /**
141      * Invoke display_message command
142      *
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()
147      */
148     public function show_message($message, $type='notice', $vars=null)
149     {
150         $this->command(
151             'display_message',
152             rcube_label(array('name' => $message, 'vars' => $vars)),
153             $type
154         );
155     }
156     
157     /**
158      * Delete all stored env variables and commands
159      */
160     public function reset()
161     {
162         $this->env = array();
163         $this->texts = array();
164         $this->commands = array();
165     }
166     
167     /**
168      * Redirect to a certain url
169      *
170      * @param mixed Either a string with the action or url parameters as key-value pairs
171      * @see rcmail::url()
172      */
173     public function redirect($p = array(), $delay = 1)
174     {
175         $location = rcmail::get_instance()->url($p);
176         $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
177         exit;
178     }
179     
180     
181     /**
182      * Send an AJAX response to the client.
183      */
184     public function send()
185     {
186         $this->remote_response();
187         exit;
188     }
189     
190     
191     /**
192      * Send an AJAX response with executable JS code
193      *
194      * @param  string  Additional JS code
195      * @param  boolean True if output buffer should be flushed
196      * @return void
197      * @deprecated
198      */
199     public function remote_response($add='', $flush=false)
200     {
201         static $s_header_sent = false;
202
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";
208         }
209
210         // unset default env vars
211         unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
212
213         // send response code
214         echo $this->get_js_commands() . $add;
215
216         // flush the output buffer
217         if ($flush)
218             flush();
219     }
220     
221     
222     /**
223      * Return executable javascript code for all registered commands
224      *
225      * @return string $out
226      */
227     private function get_js_commands()
228     {
229         $out = '';
230         
231         if (sizeof($this->env))
232             $out .= 'this.set_env('.json_serialize($this->env).");\n";
233         
234         foreach($this->texts as $name => $text) {
235             $out .= sprintf("this.add_label('%s', '%s');\n", $name, JQ($text));
236         }
237
238         foreach ($this->commands as $i => $args) {
239             $method = array_shift($args);
240             foreach ($args as $i => $arg) {
241                 $args[$i] = json_serialize($arg);
242             }
243
244             $out .= sprintf(
245                 "this.%s(%s);\n",
246                 preg_replace('/^parent\./', '', $method),
247                 implode(',', $args)
248             );
249         }
250
251         return $out;
252     }
253 }
254
255