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