]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TConditional.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TConditional.php
1 <?php
2 /**
3  * TConditional class file.
4  *
5  * @author Qiang Xue <qiang.xue@gmail.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.WebControls
10  */
11
12 /**
13  * TConditional class.
14  *
15  * TConditional displays appropriate content based on the evaluation result
16  * of a PHP expression specified via {@link setCondition Condition}.
17  * If the result is true, it instantiates the template {@link getTrueTemplate TrueTemplate};
18  * otherwise, the template {@link getFalseTemplate FalseTemplate} is instantiated.
19  * The PHP expression is evaluated right before {@link onInit} stage of the control lifecycle.
20  *
21  * Since {@link setCondition Condition} is evaluated at a very early stage, it is recommended
22  * you set {@link setCondition Condition} in template and the expression should not refer to
23  * objects that are available on or after {@link onInit} lifecycle.
24  *
25  * A typical usage of TConditional is shown as following:
26  * <code>
27  * <com:TConditional Condition="$this->User->IsGuest">
28  *   <prop:TrueTemplate>
29  *     <a href="path/to/login">Login</a>
30  *   </prop:TrueTemplate>
31  *   <prop:FalseTemplate>
32  *     <a href="path/to/logout">Logout</a>
33  *   </prop:FalseTemplate>
34  * </com:TConditional>
35  * </code>
36  *
37  * TConditional is very light. It instantiates either {@link getTrueTemplate TrueTemplate}
38  * or {@link getFalseTemplate FalseTemplate}, but never both. And the condition is evaluated only once.
39  *
40  * @author Qiang Xue <qiang.xue@gmail.com>
41  * @package System.Web.UI.WebControls
42  * @since 3.1.1
43  */
44 class TConditional extends TControl
45 {
46         private $_condition='true';
47         private $_trueTemplate;
48         private $_falseTemplate;
49         private $_creatingChildren=false;
50
51         /**
52          * Processes an object that is created during parsing template.
53          * This method overrides the parent implementation by removing
54          * all contents enclosed in the template tag.
55          * @param string|TComponent text string or component parsed and instantiated in template
56          * @see createdOnTemplate
57          */
58         public function addParsedObject($object)
59         {
60                 if($this->_creatingChildren)
61                         parent::addParsedObject($object);
62         }
63
64         /**
65          * Creates child controls.
66          * This method overrides the parent implementation. It evaluates {@link getCondition Condition}
67          * and instantiate the corresponding template.
68          */
69         public function createChildControls()
70         {
71                 $this->_creatingChildren=true;
72                 $result=true;
73                 try
74                 {
75                         $result=$this->getTemplateControl()->evaluateExpression($this->_condition);
76                 }
77                 catch(Exception $e)
78                 {
79                         throw new TInvalidDataValueException('conditional_condition_invalid',$this->_condition,$e->getMessage());
80                 }
81                 if($result)
82                 {
83                         if($this->_trueTemplate)
84                                 $this->_trueTemplate->instantiateIn($this->getTemplateControl(),$this);
85                 }
86                 else if($this->_falseTemplate)
87                         $this->_falseTemplate->instantiateIn($this->getTemplateControl(),$this);
88                 $this->_creatingChildren=false;
89         }
90
91         /**
92          * @return string the PHP expression used for determining which template to use. Defaults to 'true', meaning using TrueTemplate.
93          */
94         public function getCondition()
95         {
96                 return $this->_condition;
97         }
98
99         /**
100          * Sets the PHP expression to be evaluated for conditionally displaying content.
101          * The context of the expression is the template control containing TConditional.
102          * @param string the PHP expression used for determining which template to use.
103          */
104         public function setCondition($value)
105         {
106                 $this->_condition=TPropertyValue::ensureString($value);
107         }
108
109         /**
110          * @return ITemplate the template applied when {@link getCondition Condition} is true.
111          */
112         public function getTrueTemplate()
113         {
114                 return $this->_trueTemplate;
115         }
116
117         /**
118          * @param ITemplate the template applied when {@link getCondition Condition} is true.
119          */
120         public function setTrueTemplate(ITemplate $value)
121         {
122                 $this->_trueTemplate=$value;
123         }
124
125         /**
126          * @return ITemplate the template applied when {@link getCondition Condition} is false.
127          */
128         public function getFalseTemplate()
129         {
130                 return $this->_falseTemplate;
131         }
132
133         /**
134          * @param ITemplate the template applied when {@link getCondition Condition} is false.
135          */
136         public function setFalseTemplate(ITemplate $value)
137         {
138                 $this->_falseTemplate=$value;
139         }
140 }
141