| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | Prado::using('System.Web.UI.TCompositeControl'); |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class TTemplateControl extends TCompositeControl |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | const EXT_TEMPLATE='.tpl'; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | private static $_template=array(); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | private $_localTemplate=null; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | private $_master=null; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | private $_masterClass=''; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | private $_contents=array(); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | private $_placeholders=array(); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | public function getTemplate() |
|---|
| 67 | { |
|---|
| 68 | if($this->_localTemplate===null) |
|---|
| 69 | { |
|---|
| 70 | $class=get_class($this); |
|---|
| 71 | if(!isset(self::$_template[$class])) |
|---|
| 72 | self::$_template[$class]=$this->loadTemplate(); |
|---|
| 73 | return self::$_template[$class]; |
|---|
| 74 | } |
|---|
| 75 | else |
|---|
| 76 | return $this->_localTemplate; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | public function setTemplate($value) |
|---|
| 86 | { |
|---|
| 87 | $this->_localTemplate=$value; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | public function getIsSourceTemplateControl() |
|---|
| 96 | { |
|---|
| 97 | if(($template=$this->getTemplate())!==null) |
|---|
| 98 | return $template->getIsSourceTemplate(); |
|---|
| 99 | else |
|---|
| 100 | return false; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | public function getTemplateDirectory() |
|---|
| 107 | { |
|---|
| 108 | if(($template=$this->getTemplate())!==null) |
|---|
| 109 | return $template->getContextPath(); |
|---|
| 110 | else |
|---|
| 111 | return ''; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | protected function loadTemplate() |
|---|
| 119 | { |
|---|
| 120 | Prado::trace("Loading template ".get_class($this),'System.Web.UI.TTemplateControl'); |
|---|
| 121 | $template=$this->getService()->getTemplateManager()->getTemplateByClassName(get_class($this)); |
|---|
| 122 | return $template; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | public function createChildControls() |
|---|
| 131 | { |
|---|
| 132 | if($tpl=$this->getTemplate()) |
|---|
| 133 | { |
|---|
| 134 | foreach($tpl->getDirective() as $name=>$value) |
|---|
| 135 | { |
|---|
| 136 | if(is_string($value)) |
|---|
| 137 | $this->setSubProperty($name,$value); |
|---|
| 138 | else |
|---|
| 139 | throw new TConfigurationException('templatecontrol_directive_invalid',get_class($this),$name); |
|---|
| 140 | } |
|---|
| 141 | $tpl->instantiateIn($this); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | public function registerContent($id,TContent $object) |
|---|
| 151 | { |
|---|
| 152 | if(isset($this->_contents[$id])) |
|---|
| 153 | throw new TConfigurationException('templatecontrol_contentid_duplicated',$id); |
|---|
| 154 | else |
|---|
| 155 | $this->_contents[$id]=$object; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | public function registerContentPlaceHolder($id,TContentPlaceHolder $object) |
|---|
| 165 | { |
|---|
| 166 | if(isset($this->_placeholders[$id])) |
|---|
| 167 | throw new TConfigurationException('templatecontrol_placeholderid_duplicated',$id); |
|---|
| 168 | else |
|---|
| 169 | $this->_placeholders[$id]=$object; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | public function getMasterClass() |
|---|
| 176 | { |
|---|
| 177 | return $this->_masterClass; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | public function setMasterClass($value) |
|---|
| 184 | { |
|---|
| 185 | $this->_masterClass=$value; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | public function getMaster() |
|---|
| 192 | { |
|---|
| 193 | return $this->_master; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | public function injectContent($id,$content) |
|---|
| 203 | { |
|---|
| 204 | if(isset($this->_placeholders[$id])) |
|---|
| 205 | { |
|---|
| 206 | $placeholder=$this->_placeholders[$id]; |
|---|
| 207 | $controls=$placeholder->getParent()->getControls(); |
|---|
| 208 | $loc=$controls->remove($placeholder); |
|---|
| 209 | $controls->insertAt($loc,$content); |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | throw new TConfigurationException('templatecontrol_placeholder_inexistent',$id); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | protected function initRecursive($namingContainer=null) |
|---|
| 224 | { |
|---|
| 225 | $this->ensureChildControls(); |
|---|
| 226 | if($this->_masterClass!=='') |
|---|
| 227 | { |
|---|
| 228 | $master=Prado::createComponent($this->_masterClass); |
|---|
| 229 | if(!($master instanceof TTemplateControl)) |
|---|
| 230 | throw new TInvalidDataValueException('templatecontrol_mastercontrol_invalid'); |
|---|
| 231 | $this->_master=$master; |
|---|
| 232 | $this->getControls()->clear(); |
|---|
| 233 | $this->getControls()->add($master); |
|---|
| 234 | $master->ensureChildControls(); |
|---|
| 235 | foreach($this->_contents as $id=>$content) |
|---|
| 236 | $master->injectContent($id,$content); |
|---|
| 237 | } |
|---|
| 238 | else if(!empty($this->_contents)) |
|---|
| 239 | throw new TConfigurationException('templatecontrol_mastercontrol_required',get_class($this)); |
|---|
| 240 | parent::initRecursive($namingContainer); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | ?> |
|---|