]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/ActiveControls/TActiveTableCell.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / ActiveControls / TActiveTableCell.php
1 <?php
2 /**
3  * TActiveTableCell class file
4  *
5  * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
6  * @link http://www.landwehr-software.de/
7  * @copyright Copyright &copy; 2009 LANDWEHR Computer und Software GmbH
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.ActiveControls
10  * @version $Id$
11  */
12
13 /**
14  * Includes the following used classes
15  */
16 Prado::using('System.Web.UI.WebControls.TTableRow');
17 Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
18 Prado::using('System.Web.UI.ActiveControls.TCallbackEventParameter');
19
20 /**
21  * TActiveTableCell class.
22  *
23  * TActiveTableCell is the active counterpart to the original {@link TTableCell} control
24  * and displays a table cell. The horizontal and vertical alignments of the cell
25  * are specified via {@link setHorizontalAlign HorizontalAlign} and
26  * {@link setVerticalAlign VerticalAlign} properties, respectively.
27  *
28  * TActiveTableCell allows the contents of the table cell to be changed during callback. When
29  * {@link onCellSelected CellSelected} property is set, selecting (clicking on) the cell will
30  * perform a callback request causing {@link onCellSelected OnCellSelected} event to be fired.
31  *
32  * It will also bubble the {@link onCellSelected OnCellSelected} event up to it's parent
33  * {@link TActiveTableRow} control which will fire up the event handlers if implemented.
34  *
35  * TActiveTableCell allows the client-side cell contents to be updated during a
36  * callback response by getting a new writer, invoking the render method and flushing the
37  * output, similar to a {@link TActivePanel} control.
38  * <code>
39  * function callback_request($sender, $param)
40  * {
41  *     $this->active_cell->render($param->getNewWriter());
42  * }
43  * </code>
44  *
45  * Please refer to the original documentation of the regular counterpart for usage.
46  *
47  * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
48  * @package System.Web.UI.ActiveControls
49  * @version $Id$
50  * @since 3.1.9
51  */
52 class TActiveTableCell extends TTableCell implements ICallbackEventHandler, IActiveControl
53 {
54
55         /**
56         * @var TTable parent row control containing the cell
57         */
58         private $_row;
59
60         /**
61          * Creates a new callback control, sets the adapter to TActiveControlAdapter.
62          */
63         public function __construct()
64         {
65                 parent::__construct();
66                         $this->setAdapter(new TActiveControlAdapter($this));
67         }
68
69         /**
70          * @return TBaseActiveCallbackControl standard callback control options.
71          */
72         public function getActiveControl()
73         {
74                 return $this->getAdapter()->getBaseActiveControl();
75         }
76
77         /**
78          * @return TCallbackClientSide client side request options.
79          */
80         public function getClientSide()
81         {
82                 return $this->getAdapter()->getBaseActiveControl()->getClientSide();
83         }
84
85         /**
86          * @return string corresponding javascript class name for this TActiveTableCell.
87          */
88         protected function getClientClassName()
89         {
90                 return 'Prado.WebUI.TActiveTableCell';
91         }
92
93         /**
94          * Raises the callback event. This method is required by {@link ICallbackEventHandler}
95          * interface. It will raise {@link onCellSelected OnCellSelected} event with a
96          * {@link TActiveTableCellEventParameter} containing the zero-based index of the
97          * TActiveTableCell.
98          * This method is mainly used by framework and control developers.
99          * @param TCallbackEventParameter the event parameter
100          */
101         public function raiseCallbackEvent($param)
102         {
103                 $parameter = new TActiveTableCellEventParameter($this->getResponse(), $param->getCallbackParameter(), $this->getCellIndex());
104                 $this->onCellSelected($parameter);
105                 $this->raiseBubbleEvent($this, $parameter);
106         }
107
108         /**
109          * This method is invoked when a callback is requested. The method raises
110          * 'OnCellSelected' event to fire up the event handlers. If you override this
111          * method, be sure to call the parent implementation so that the event
112          * handler can be invoked.
113          * @param TActiveTableCellEventParameter event parameter to be passed to the event handlers
114          */
115         public function onCellSelected($param)
116         {
117                 $this->raiseEvent('OnCellSelected', $this, $param);
118         }
119
120         /**
121          * Ensure that the ID attribute is rendered and registers the javascript code
122          * for initializing the active control if the event handler for the
123          * {@link onCellSelected OnCellSelected} event is set.
124          * @param THtmlWriter the writer responsible for rendering
125          */
126         protected function addAttributesToRender($writer)
127         {
128                 parent::addAttributesToRender($writer);
129                 $writer->addAttribute('id', $this->getClientID());
130                 if ($this->hasEventHandler('OnCellSelected'))
131                         $this->getActiveControl()->registerCallbackClientScript($this->getClientClassName(), $this->getPostBackOptions());
132         }
133
134         /**
135          * Renders and replaces the cell's content on the client-side. When render() is
136          * called before the OnPreRender event, such as when render() is called during
137          * a callback event handler, the rendering is defered until OnPreRender event
138          * is raised.
139          * @param THtmlWriter html writer
140          */
141         public function render($writer)
142         {
143                 if ($this->getHasPreRendered())
144                 {
145                         parent::render($writer);
146                         if ($this->getActiveControl()->canUpdateClientSide())
147                                 $this->getPage()->getCallbackClient()->replaceContent($this, $writer);
148                 }
149                 else {
150                         $this->getPage()->getAdapter()->registerControlToRender($this, $writer);
151                         // If we update a TActiveTableCell on callback, we shouldn't update all childs,
152                         // because the whole content will be replaced by the parent.
153                         if ($this->getHasControls())
154                         {
155                                 foreach ($this->findControlsByType('IActiveControl', false) as $control)
156                                         $control->getActiveControl()->setEnableUpdate(false);
157                         }
158                 }
159         }
160
161         /**
162          * Returns postback specifications for the table cell.
163          * This method is used by framework and control developers.
164          * @return array parameters about how the row defines its postback behavior.
165          */
166         protected function getPostBackOptions()
167         {
168                 $options['ID'] = $this->getClientID();
169                 $options['EventTarget'] = $this->getUniqueID();
170                 return $options;
171         }
172
173         /**
174          * Returns the zero-based index of the TActiveTableCell within the {@link TTableCellCollection}
175          * of the parent {@link TTableRow} control. Raises a {@link TConfigurationException} if the cell
176          * is no member of the cell collection.
177          * @return integer the zero-based index of the cell
178          */
179         public function getCellIndex()
180         {
181                 foreach ($this->getRow()->getCells() as $key => $row)
182                         if ($row == $this) return $key;
183                 throw new TConfigurationException('tactivetablecell_control_notincollection', get_class($this), $this->getUniqueID());
184         }
185
186         /**
187          * Returns the parent {@link TTableRow} control by looping through all parents until a {@link TTableRow}
188          * is found. Raises a {@link TConfigurationException} if no row control is found.
189          * @return TTableRow the parent row control
190          */
191         public function getRow()
192         {
193                 if ($this->_row === null)
194                 {
195                         $row = $this->getParent();
196                         while (!($row instanceof TTableRow) && $row !== null)
197                         {
198                                 $row = $row->getParent();
199                         }
200                         if ($row instanceof TTableRow) $this->_row = $row;
201                         else throw new TConfigurationException('tactivetablecell_control_outoftable', get_class($this), $this->getUniqueID());
202                 }
203                 return $this->_row;
204         }
205
206 }
207
208 /**
209  * TActiveTableCellEventParameter class.
210  *
211  * The TActiveTableCellEventParameter provides the parameter passed during the callback
212  * requestion in the {@link getCallbackParameter CallbackParameter} property. The
213  * callback response content (e.g. new HTML content) must be rendered
214  * using an THtmlWriter obtained from the {@link getNewWriter NewWriter}
215  * property, which returns a <b>NEW</b> instance of TCallbackResponseWriter.
216  *
217  * The {@link getSelectedCellIndex SelectedCellIndex} is a zero-based index of the
218  * TActiveTableCell , -1 if the cell is not part of the cell collection (this shouldn't
219  * happen though since an exception is thrown before).
220  *
221  * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
222  * @package System.Web.UI.ActiveControls
223  * @since 3.1.9
224  */
225 class TActiveTableCellEventParameter extends TCallbackEventParameter
226 {
227
228         /**
229         * @var integer the zero-based index of the cell.
230         */
231         private $_selectedCellIndex = -1;
232
233         /**
234          * Creates a new TActiveTableRowEventParameter.
235          */
236         public function __construct($response, $parameter, $index=-1)
237         {
238                 parent::__construct($response, $parameter);
239                 $this->_selectedCellIndex = $index;
240         }
241
242         /**
243          * Returns the zero-based index of the {@link TActiveTableCell} within the
244          * {@link TTableCellCollection} of the parent {@link TTableRow} control.
245          * @return integer the zero-based index of the cell.
246          */
247         public function getSelectedCellIndex()
248         {
249                 return $this->_selectedCellIndex;
250         }
251
252 }