]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/BConditional.php
baculum: Stop using hidden fields to store item identifiers
[bacula/bacula] / gui / baculum / protected / Web / Portlets / BConditional.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('System.Web.UI.TTemplateControl');
24 Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
25
26 class BConditional extends TTemplateControl implements IActiveControl {
27
28         const BCONDITION = 'BCondition';
29         const TYPE_TPL_FALSE = 0;
30         const TYPE_TPL_TRUE = 1;
31
32         private $item_true_template;
33         private $item_false_template;
34
35         public function __construct() {
36                 parent::__construct();
37                 $this->setAdapter(new TActiveControlAdapter($this));
38         }
39
40         public function getActiveControl() {
41                 return $this->getAdapter()->getBaseActiveControl();
42         }
43
44         public function onLoad($param) {
45                 $this->prepareControlContent();
46                 parent::onLoad($param);
47         }
48
49         public function bubbleEvent($sender,$param) {
50                 if ($param instanceof TCommandEventParameter) {
51                         $this->raiseBubbleEvent($this, $param);
52                         return true;
53                 } else {
54                         return false;
55                 }
56         }
57
58         private function createItemInternal($item_type) {
59                 $item = $this->createItem($item_type);
60                 if (!is_null($item)) {
61                         $this->getControls()->add($item);
62                 }
63                 return $item;
64         }
65
66         protected function createItem($item_type) {
67                 $template = null;
68                 $item = null;
69                 switch ($item_type) {
70                         case self::TYPE_TPL_TRUE: {
71                                 $template = $this->getTrueTemplate();
72                                 break;
73                         }
74                         case self::TYPE_TPL_FALSE: {
75                                 $template = $this->getFalseTemplate();
76                                 break;
77                         }
78                 }
79                 if (!is_null($template)) {
80                         $item = new BConditionalItem;
81                         $item->setItemType($item_type);
82                         $item->setTemplate($template);
83                         $item->setTemplateControl($this);
84                         $item->setData($this->getTemplateControl());
85                 }
86                 return $item;
87         }
88
89         public function getTrueTemplate() {
90                 return $this->item_true_template;
91         }
92
93         public function setTrueTemplate($template) {
94                 if ($template instanceof ITemplate) {
95                         $this->item_true_template = $template;
96                 }
97         }
98
99         public function getFalseTemplate() {
100                 return $this->item_false_template;
101         }
102
103         public function setFalseTemplate($template) {
104                 if ($template instanceof ITemplate) {
105                         $this->item_false_template = $template;
106                 }
107         }
108
109         public function setBCondition($value) {
110                 settype($value, 'bool');
111                 $this->setViewState(self::BCONDITION, $value);
112         }
113
114         public function getBCondition() {
115                 $value = $this->getViewState(self::BCONDITION);
116                 return $value;
117         }
118
119         public function dataBind() {
120                 $this->dataBindProperties();
121                 $this->prepareControlContent();
122         }
123
124         public function prepareControlContent() {
125                 if ($this->getBCondition() === true) {
126                         $this->createItemInternal(self::TYPE_TPL_TRUE);
127                 } else {
128                         $this->createItemInternal(self::TYPE_TPL_FALSE);
129                 }
130         }
131 }
132
133 class BConditionalItem extends TTemplateControl implements IDataRenderer, INamingContainer {
134         private $item_type;
135         private $data;
136
137         public function getItemType() {
138                 return $this->item_type;
139         }
140
141         public function setItemType($type) {
142                 $this->item_type = $type;
143         }
144
145         public function getData() {
146                 return $this->data;
147         }
148
149         public function setData($data) {
150                 $this->data = $data;
151         }
152
153         public function bubbleEvent($sender,$param) {
154                 if ($param instanceof TCommandEventParameter) {
155                         $this->raiseBubbleEvent($this, $param);
156                         return true;
157                 } else {
158                         return false;
159                 }
160         }
161 }
162 ?>