]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/ActiveControls/TCallbackResponseAdapter.php
Add Baculum
[bacula/bacula] / gui / baculum / framework / Web / UI / ActiveControls / TCallbackResponseAdapter.php
1 <?php
2 /**
3  * TCallbackResponseAdapter and TCallbackResponseWriter class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2013 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @version $Id: TCallbackResponseAdapter.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10  * @package System.Web.UI.ActiveControls
11  */
12
13 /**
14  * TCallbackResponseAdapter alters the THttpResponse's outputs.
15  *
16  * A TCallbackResponseWriter is used instead of the TTextWrite when
17  * createHtmlWriter is called. Each call to createHtmlWriter will create
18  * a new TCallbackResponseWriter. When flushContent() is called each
19  * instance of TCallbackResponseWriter's content is flushed.
20  *
21  * The callback response data can be set using the {@link setResponseData ResponseData}
22  * property.
23  *
24  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
25  * @version $Id: TCallbackResponseAdapter.php 3245 2013-01-07 20:23:32Z ctrlaltca $
26  * @package System.Web.UI.ActiveControls
27  * @since 3.1
28  */
29 class TCallbackResponseAdapter extends THttpResponseAdapter
30 {
31         /**
32          * @var TCallbackResponseWriter[] list of writers.
33          */
34         private $_writers=array();
35         /**
36          * @var mixed callback response data.
37          */
38         private $_data;
39
40         private $_redirectUrl=null;
41
42         /**
43          * Returns a new instance of THtmlWriter.
44          * An instance of TCallbackResponseWriter is created to hold the content.
45          * @param string writer class name.
46          * @param THttpResponse http response handler.
47          */
48         public function createNewHtmlWriter($type,$response)
49         {
50                 $writer = new TCallbackResponseWriter();
51                 $this->_writers[] = $writer;
52                 return parent::createNewHtmlWriter($type,$writer);
53         }
54
55         /**
56          * Flushes the contents in the writers.
57          */
58         public function flushContent()
59         {
60                 foreach($this->_writers as $writer)
61                         echo $writer->flush();
62                 parent::flushContent();
63         }
64
65         /**
66          * @param mixed callback response data.
67          */
68         public function setResponseData($data)
69         {
70                 $this->_data = $data;
71         }
72
73         /**
74          * @return mixed callback response data.
75          */
76         public function getResponseData()
77         {
78                 return $this->_data;
79         }
80
81         /**
82          * Delay the redirect until we process the rest of the page.
83          * @param string new url to redirect to.
84          */
85         public function httpRedirect($url)
86         {
87                 if($url[0]==='/')
88                         $url=$this->getRequest()->getBaseUrl().$url;
89                 $this->_redirectUrl=str_replace('&amp;','&',$url);
90         }
91
92         /**
93          * @return string new url for callback response to redirect to.
94          */
95         public function getRedirectedUrl()
96         {
97                 return $this->_redirectUrl;
98         }
99 }
100
101 /**
102  * TCallbackResponseWriter class.
103  *
104  * TCallbackResponseWriter class enclosed a chunck of content within a
105  * html comment boundary. This allows multiple chuncks of content to return
106  * in the callback response and update multiple HTML elements.
107  *
108  * The {@link setBoundary Boundary} property sets boundary identifier in the
109  * HTML comment that forms the boundary. By default, the boundary identifier
110  * is generated using microtime.
111  *
112  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
113  * @version $Id: TCallbackResponseAdapter.php 3245 2013-01-07 20:23:32Z ctrlaltca $
114  * @package System.Web.UI.ActiveControls
115  * @since 3.1
116  */
117 class TCallbackResponseWriter extends TTextWriter
118 {
119         /**
120          * @var string boundary ID
121          */
122         private $_boundary;
123
124         /**
125          * Constructor. Generates unique boundary ID using microtime.
126          */
127         public function __construct()
128         {
129                 $this->_boundary = sprintf('%x',crc32(microtime()));
130         }
131
132         /**
133          * @return string boundary identifier.
134          */
135         public function getBoundary()
136         {
137                 return $this->_boundary;
138         }
139
140         /**
141          * @param string boundary identifier.
142          */
143         public function setBoundary($value)
144         {
145                 $this->_boundary = $value;
146         }
147
148         /**
149          * Returns the text content wrapped within a HTML comment with boundary
150          * identifier as its comment content.
151          * @return string text content chunck.
152          */
153         public function flush()
154         {
155                 $content = parent::flush();
156                 if(empty($content))
157                         return "";
158                 return '<!--'.$this->getBoundary().'-->'.$content.'<!--//'.$this->getBoundary().'-->';
159         }
160 }
161