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