00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Core_Model_Layout extends Varien_Simplexml_Config
00035 {
00036
00037
00038
00039
00040
00041
00042 protected $_update;
00043
00044
00045
00046
00047
00048
00049 protected $_blocks = array();
00050
00051
00052
00053
00054
00055
00056 protected $_output = array();
00057
00058
00059
00060
00061
00062
00063 protected $_area;
00064
00065
00066
00067
00068
00069
00070 protected $_helpers = array();
00071
00072
00073
00074
00075
00076
00077 protected $_directOutput = false;
00078
00079
00080
00081
00082
00083
00084 public function __construct($data=array())
00085 {
00086 $this->_elementClass = Mage::getConfig()->getModelClassName('core/layout_element');
00087 $this->setXml(simplexml_load_string('<layout/>', $this->_elementClass));
00088 parent::__construct($data);
00089 }
00090
00091
00092
00093
00094
00095
00096 public function getUpdate()
00097 {
00098 if (!$this->_update) {
00099 $this->_update = Mage::getModel('core/layout_update');
00100 }
00101 return $this->_update;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 public function setArea($area)
00111 {
00112 $this->_area = $area;
00113 return $this;
00114 }
00115
00116
00117
00118
00119
00120
00121 public function getArea()
00122 {
00123 return $this->_area;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 public function setDirectOutput($flag)
00133 {
00134 $this->_directOutput = $flag;
00135 return $this;
00136 }
00137
00138
00139
00140
00141
00142
00143 public function getDirectOutput()
00144 {
00145 return $this->_directOutput;
00146 }
00147
00148
00149
00150
00151
00152
00153 public function generateXml()
00154 {
00155 $xml = $this->getUpdate()->asSimplexml();
00156 $removeInstructions = $xml->xpath("//remove");
00157 foreach ($removeInstructions as $infoNode) {
00158 $attributes = $infoNode->attributes();
00159 if ($acl = (string)$attributes->acl && !Mage::getSingleton('admin/session')->isAllowed($acl)) {
00160 $block->addAttribute('ignore', true);
00161 }
00162 if ($blockName = (string)$attributes->name) {
00163 $ignoreNodes = $xml->xpath("//block[@name='".$blockName."']");
00164 foreach ($ignoreNodes as $block) {
00165 $block->addAttribute('ignore', true);
00166 }
00167 $ignoreNodes = $xml->xpath("//reference[@name='".$blockName."']");
00168 foreach ($ignoreNodes as $block) {
00169 $block->addAttribute('ignore', true);
00170 }
00171 }
00172 }
00173
00174 $this->setXml($xml);
00175 return $this;
00176 }
00177
00178
00179
00180
00181
00182
00183 public function generateBlocks($parent=null)
00184 {
00185 if (empty($parent)) {
00186 $parent = $this->getNode();
00187 }
00188 foreach ($parent as $node) {
00189 $attributes = $node->attributes();
00190 if ((bool)$attributes->ignore) {
00191 continue;
00192 }
00193 switch ($node->getName()) {
00194 case 'block':
00195 $this->_generateBlock($node, $parent);
00196 $this->generateBlocks($node);
00197 break;
00198
00199 case 'reference':
00200 $this->generateBlocks($node);
00201 break;
00202
00203 case 'action':
00204 $this->_generateAction($node, $parent);
00205 break;
00206 }
00207 }
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217 protected function _generateBlock($node, $parent)
00218 {
00219
00220 if (!empty($node['class'])) {
00221 $className = (string)$node['class'];
00222 } else {
00223 $className = Mage::getConfig()->getBlockClassName((string)$node['type']);
00224 }
00225
00226 $blockName = (string)$node['name'];
00227 $_profilerKey = 'BLOCK: '.$blockName;
00228 Varien_Profiler::start($_profilerKey);
00229 $block = $this->addBlock($className, $blockName);
00230 if (!$block) {
00231 return $this;
00232 }
00233
00234 if (!empty($node['parent'])) {
00235 $parentBlock = $this->getBlock((string)$node['parent']);
00236 } else {
00237 $parentName = $parent->getBlockName();
00238 if (!empty($parentName)) {
00239 $parentBlock = $this->getBlock($parentName);
00240 }
00241 }
00242 if (!empty($parentBlock)) {
00243 $alias = isset($node['as']) ? (string)$node['as'] : '';
00244 if (isset($node['before'])) {
00245 $sibling = (string)$node['before'];
00246 if ('-'===$sibling) {
00247 $sibling = '';
00248 }
00249 $parentBlock->insert($block, $sibling, false, $alias);
00250 } elseif (isset($node['after'])) {
00251 $sibling = (string)$node['after'];
00252 if ('-'===$sibling) {
00253 $sibling = '';
00254 }
00255 $parentBlock->insert($block, $sibling, true, $alias);
00256 } else {
00257 $parentBlock->append($block, $alias);
00258 }
00259 }
00260 if (!empty($node['template'])) {
00261 $block->setTemplate((string)$node['template']);
00262 }
00263
00264 if (!empty($node['output'])) {
00265 $method = (string)$node['output'];
00266 $this->addOutputBlock($blockName, $method);
00267 }
00268 Varien_Profiler::stop($_profilerKey);
00269
00270 return $this;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 protected function _generateAction($node, $parent)
00281 {
00282 if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
00283 if (!Mage::getStoreConfigFlag($configPath)) {
00284 return $this;
00285 }
00286 }
00287
00288 $method = (string)$node['method'];
00289 if (!empty($node['block'])) {
00290 $parentName = (string)$node['block'];
00291 } else {
00292 $parentName = $parent->getBlockName();
00293 }
00294
00295 $_profilerKey = 'BLOCK ACTION: '.$parentName.' -> '.$method;
00296 Varien_Profiler::start($_profilerKey);
00297
00298 if (!empty($parentName)) {
00299 $block = $this->getBlock($parentName);
00300 }
00301 if (!empty($block)) {
00302
00303 $args = (array)$node->children();
00304 unset($args['@attributes']);
00305
00306 foreach ($args as $key => $arg) {
00307 if (($arg instanceof Mage_Core_Model_Layout_Element)) {
00308 if (isset($arg['helper'])) {
00309 $helperName = explode('/', (string)$arg['helper']);
00310 $helperMethod = array_pop($helperName);
00311 $helperName = implode('/', $helperName);
00312 $arg = $arg->asArray();
00313 unset($arg['@']);
00314 $args[$key] = call_user_func_array(array(Mage::helper($helperName), $helperMethod), $arg);
00315 } else {
00316
00317
00318
00319 $arr = array();
00320 foreach($arg as $subkey => $value) {
00321 $arr[(string)$subkey] = (string)$value;
00322 }
00323 if (!empty($arr)) {
00324 $args[$key] = $arr;
00325 }
00326 }
00327 }
00328 }
00329
00330 if (isset($node['json'])) {
00331 $json = explode(' ', (string)$node['json']);
00332 foreach ($json as $arg) {
00333 $args[$arg] = Zend_Json::decode($args[$arg]);
00334 }
00335 }
00336
00337 $this->_translateLayoutNode($node, $args);
00338 call_user_func_array(array($block, $method), $args);
00339 }
00340
00341 Varien_Profiler::stop($_profilerKey);
00342
00343 return $this;
00344 }
00345
00346
00347
00348
00349
00350
00351
00352 protected function _translateLayoutNode($node, &$args)
00353 {
00354 if (isset($node['translate'])) {
00355 $items = explode(' ', (string)$node['translate']);
00356 foreach ($items as $arg) {
00357 if (isset($node['module'])) {
00358 $args[$arg] = Mage::helper((string)$node['module'])->__($args[$arg]);
00359 }
00360 else {
00361 $args[$arg] = __($args[$arg]);
00362 }
00363 }
00364 }
00365 }
00366
00367
00368
00369
00370
00371
00372
00373 public function setBlock($name, $block)
00374 {
00375 $this->_blocks[$name] = $block;
00376 return $this;
00377 }
00378
00379
00380
00381
00382
00383
00384 public function unsetBlock($name)
00385 {
00386 $this->_blocks[$name] = null;
00387 unset($this->_blocks[$name]);
00388 return $this;
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 public function createBlock($type, $name='', array $attributes = array())
00400 {
00401 try {
00402 $block = $this->_getBlockInstance($type, $attributes);
00403 } catch (Exception $e) {
00404 Mage::logException($e);
00405 return false;
00406 }
00407
00408 if (empty($name) || '.'===$name{0}) {
00409 $block->setIsAnonymous(true);
00410 if (!empty($name)) {
00411 $block->setAnonSuffix(substr($name, 1));
00412 }
00413 $name = 'ANONYMOUS_'.sizeof($this->_blocks);
00414 }
00415 elseif (isset($this->_blocks[$name])) {
00416 Mage::throwException(Mage::helper('core')->__('Block with name "%s" already exists', $name));
00417 }
00418
00419 $block->setType($type);
00420 $block->setNameInLayout($name);
00421 $block->addData($attributes);
00422 $block->setLayout($this);
00423
00424 $this->_blocks[$name] = $block;
00425
00426 return $this->_blocks[$name];
00427 }
00428
00429
00430
00431
00432
00433
00434
00435
00436 public function addBlock($block, $blockName)
00437 {
00438 try {
00439 $block = $this->_getBlockInstance($block);
00440 } catch (Exception $e) {
00441 return false;
00442 }
00443
00444 $block->setNameInLayout($blockName);
00445 $block->setLayout($this);
00446 $this->_blocks[$blockName] = $block;
00447
00448 return $block;
00449 }
00450
00451 protected function _getBlockInstance($block, array $attributes=array())
00452 {
00453 if (is_string($block)) {
00454 if (strpos($block, '/')!==false) {
00455 if (!$block = Mage::getConfig()->getBlockClassName($block)) {
00456 Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
00457 }
00458 }
00459 $fileName = mageFindClassFile($block);
00460 if ($fileName!==false) {
00461
00462 $block = new $block($attributes);
00463 }
00464 }
00465 if (!$block instanceof Mage_Core_Block_Abstract) {
00466 Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
00467 }
00468 return $block;
00469 }
00470
00471
00472
00473
00474
00475
00476
00477 public function getAllBlocks()
00478 {
00479 return $this->_blocks;
00480 }
00481
00482
00483
00484
00485
00486
00487
00488 public function getBlock($name)
00489 {
00490 if (isset($this->_blocks[$name])) {
00491 return $this->_blocks[$name];
00492 } else {
00493 return false;
00494 }
00495 }
00496
00497
00498
00499
00500
00501
00502
00503 public function addOutputBlock($blockName, $method='toHtml')
00504 {
00505
00506 $this->_output[$blockName] = array($blockName, $method);
00507 return $this;
00508 }
00509
00510 public function removeOutputBlock($blockName)
00511 {
00512 unset($this->_output[$blockName]);
00513 return $this;
00514 }
00515
00516
00517
00518
00519
00520
00521 public function getOutput()
00522 {
00523 $out = '';
00524 if (!empty($this->_output)) {
00525 foreach ($this->_output as $callback) {
00526 $out .= $this->getBlock($callback[0])->$callback[1]();
00527 }
00528 }
00529
00530 return $out;
00531 }
00532
00533
00534
00535
00536
00537
00538 public function getMessagesBlock()
00539 {
00540 if ($block = $this->getBlock('messages')) {
00541 return $block;
00542 }
00543 return $this->createBlock('core/messages', 'messages');
00544 }
00545
00546
00547
00548
00549
00550
00551
00552 public function getBlockSingleton($type)
00553 {
00554 if (!isset($this->_helpers[$type])) {
00555 if (!$className = Mage::getConfig()->getBlockClassName($type)) {
00556 Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type));
00557 }
00558
00559 $helper = new $className();
00560 if ($helper) {
00561 if ($helper instanceof Mage_Core_Block_Abstract) {
00562 $helper->setLayout($this);
00563 }
00564 $this->_helpers[$type] = $helper;
00565 }
00566 }
00567 return $this->_helpers[$type];
00568 }
00569
00570
00571
00572
00573
00574
00575
00576 public function helper($name)
00577 {
00578 $helper = Mage::app()->getHelper($name);
00579 if (!$helper) {
00580 return false;
00581 }
00582 return $helper->setLayout($this);
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 }