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