]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/ActiveControls/TActiveRepeater.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / ActiveControls / TActiveRepeater.php
1 <?php
2 /**
3  * TActiveRepeater class file
4  *
5  * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
6  * @package System.Web.UI.ActiveControls
7  * @since 3.1.9
8  */
9
10 /**
11  * TActiveRepeater class
12  *
13  * TActiveRepeater represents a data bound and updatable grid control which is the
14  * active counterpart to the original {@link TRepeater} control.
15  *
16  * This component can be used in the same way as the regular datagrid, the only
17  * difference is that the active repeater uses callbacks instead of postbacks
18  * for interaction.
19  *
20  * Please refer to the original documentation of the regular counterparts for usage.
21  *
22  * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
23  * @package System.Web.UI.ActiveControls
24  * @since 3.1.9
25  */
26 class TActiveRepeater extends TRepeater implements IActiveControl, ISurroundable {
27
28   /**
29    * @var string the tag used to render the surrounding container
30    */
31   protected $_surroundingTag='div';
32
33   /**
34    * Creates a new callback control, sets the adapter to
35    * TActiveControlAdapter.
36    */
37         public function __construct() {
38                 parent::__construct();
39                 $this->setAdapter(new TActiveControlAdapter($this));
40         }
41
42         /**
43          * @return TBaseActiveControl standard active control options.
44          */
45         public function getActiveControl() {
46                 return $this->getAdapter()->getBaseActiveControl();
47         }
48
49         /**
50          * Sets the data source object associated with the repeater control.
51          * In addition, the render method of all connected pagers is called so they
52          * get updated when the data source is changed. Also the repeater registers
53          * itself for rendering in order to get it's content replaced on client side.
54          * @param Traversable|array|string data source object
55          */
56         public function setDataSource($value) {
57                 parent::setDataSource($value);
58                 if($this->getActiveControl()->canUpdateClientSide()) {
59                         $this->renderPager();
60                         $this->getPage()->getAdapter()->registerControlToRender($this,$this->getResponse()->createHtmlWriter());
61                 }
62         }
63
64         /**
65          * Gets the tag used to render the surrounding container. Defaults to 'div'.
66          * @return string container tag
67          */
68         public function getSurroundingTag() {
69           return $this->_surroundingTag;
70         }
71
72         /**
73          * Sets the tag used to render the surrounding container.
74          * @param string $value container tag
75          */
76         public function setSurroundingTag($value) {
77           $this->_surroundingTag=TPropertyValue::ensureString($value);
78         }
79
80         /**
81          * Returns the id of the surrounding container.
82          * @return string container id
83          */
84         public function getSurroundingTagID() {
85                 return $this->getClientID().'_Container';
86         }
87
88         /**
89          * Renders the repeater.
90          * If the repeater did not pass the prerender phase yet, it will register itself for rendering later.
91          * Else it will call the {@link renderRepeater()} method which will do the rendering of the repeater.
92          * @param THtmlWriter writer for the rendering purpose
93          */
94         public function render($writer) {
95                 if($this->getHasPreRendered()) {
96                         $this->renderRepeater($writer);
97                         if($this->getActiveControl()->canUpdateClientSide()) $this->getPage()->getCallbackClient()->replaceContent($this->getSurroundingTagId(),$writer);
98                 }
99                 else {
100                         $this->getPage()->getAdapter()->registerControlToRender($this,$writer);
101                 }
102         }
103
104         /**
105          * Loops through all {@link TActivePager} on the page and registers the ones which are set to paginate
106          * the repeater for rendering. This is to ensure that the connected pagers are also rendered if the
107          * data source changed.
108          */
109         private function renderPager() {
110                 $pager=$this->getPage()->findControlsByType('TActivePager', false);
111                 foreach($pager as $item) {
112                         if($item->ControlToPaginate==$this->ID) {
113                                 $writer=$this->getResponse()->createHtmlWriter();
114                                 $this->getPage()->getAdapter()->registerControlToRender($item,$writer);
115                         }
116                 }
117         }
118
119         /**
120          * Renders the repeater by writing a {@link getSurroundingTag()} with the container id obtained
121          * from {@link getSurroundingTagID()} which will be called by the replacement method of the client
122          * script to update it's content.
123          * @param THtmlWriter writer for the rendering purpose
124          */
125         private function renderRepeater($writer) {
126                 $writer->addAttribute('id',$this->getSurroundingTagID());
127                 $writer->renderBeginTag($this->getSurroundingTag());
128                 parent::render($writer);
129                 $writer->renderEndTag();
130         }
131
132 }
133