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
00035
00036
00037
00038 abstract class Mage_Core_Block_Abstract extends Varien_Object
00039 {
00040
00041
00042
00043
00044
00045
00046 protected $_name;
00047
00048
00049
00050
00051
00052
00053 protected $_layout;
00054
00055
00056
00057
00058
00059
00060 protected $_parent;
00061
00062
00063
00064
00065
00066
00067 protected $_alias;
00068
00069
00070
00071
00072
00073
00074 protected $_anonSuffix;
00075
00076
00077
00078
00079
00080
00081 protected $_children = array();
00082
00083
00084
00085
00086
00087
00088 protected $_sortedChildren = array();
00089
00090
00091
00092
00093
00094
00095 protected $_childrenHtmlCache = array();
00096
00097
00098
00099
00100
00101
00102 protected $_request;
00103
00104
00105
00106
00107
00108
00109 protected $_messagesBlock = null;
00110
00111
00112
00113
00114
00115
00116 protected $_isAnonymous = false;
00117
00118
00119
00120
00121
00122
00123 protected $_parentBlock;
00124
00125 protected static $_urlModel;
00126
00127
00128
00129
00130
00131
00132
00133
00134 protected function _construct()
00135 {
00136
00137
00138
00139 }
00140
00141
00142
00143
00144
00145
00146 public function getRequest()
00147 {
00148 if ($controller = Mage::app()->getFrontController()) {
00149 $this->_request = $controller->getRequest();
00150 }
00151 else {
00152 throw new Exception(Mage::helper('core')->__("Can't retrieve request object"));
00153 }
00154 return $this->_request;
00155 }
00156
00157
00158
00159
00160
00161
00162 public function getParentBlock()
00163 {
00164 return $this->_parentBlock;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 public function setParentBlock(Mage_Core_Block_Abstract $block)
00174 {
00175 $this->_parentBlock = $block;
00176 return $this;
00177 }
00178
00179
00180
00181
00182
00183
00184 public function getAction()
00185 {
00186 return Mage::app()->getFrontController()->getAction();
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 public function setLayout(Mage_Core_Model_Layout $layout)
00196 {
00197 $this->_layout = $layout;
00198 Mage::dispatchEvent('core_block_abstract_prepare_layout_before', array('block' => $this));
00199 $this->_prepareLayout();
00200 Mage::dispatchEvent('core_block_abstract_prepare_layout_after', array('block' => $this));
00201 return $this;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211 protected function _prepareLayout()
00212 {
00213 return $this;
00214 }
00215
00216
00217
00218
00219
00220
00221 public function getLayout()
00222 {
00223 return $this->_layout;
00224 }
00225
00226 public function getIsAnonymous()
00227 {
00228 return $this->_isAnonymous;
00229 }
00230
00231 public function setIsAnonymous($flag)
00232 {
00233 $this->_isAnonymous = $flag;
00234 return $this;
00235 }
00236
00237 public function getAnonSuffix()
00238 {
00239 return $this->_anonSuffix;
00240 }
00241
00242 public function setAnonSuffix($suffix)
00243 {
00244 $this->_anonSuffix = $suffix;
00245 return $this;
00246 }
00247
00248 public function getBlockAlias()
00249 {
00250 return $this->_alias;
00251 }
00252
00253 public function setBlockAlias($alias)
00254 {
00255 $this->_alias = $alias;
00256 return $this;
00257 }
00258
00259 public function getName()
00260 {
00261 return $this->_name;
00262 }
00263
00264 public function setName($name)
00265 {
00266 if (!empty($this->_name) && $this->getLayout()) {
00267 $this->getLayout()
00268 ->unsetBlock($this->_name)
00269 ->setBlock($name, $this);
00270 }
00271 $this->_name = $name;
00272 return $this;
00273 }
00274
00275 public function getSortedChildren()
00276 {
00277 return $this->_sortedChildren;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 public function setAttribute($name, $value=null)
00290 {
00291 return $this->setData($name, $value);
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301 public function setChild($alias, $block)
00302 {
00303 if (is_string($block)) {
00304 $block = $this->getLayout()->getBlock($block);
00305 }
00306
00307
00308
00309 if (!$block) {
00310 return $this;
00311 }
00312 if ($block->getIsAnonymous()) {
00313
00314 $suffix = $block->getAnonSuffix();
00315 if (empty($suffix)) {
00316 $suffix = 'child'.sizeof($this->_children);
00317 }
00318 $blockName = $this->getNameInLayout().'.'.$suffix;
00319
00320 if ($this->getLayout()) {
00321 $this->getLayout()
00322 ->unsetBlock($block->getNameInLayout())
00323 ->setBlock($blockName, $block);
00324 }
00325
00326 $block->setNameInLayout($blockName);
00327 $block->setIsAnonymous(false);
00328
00329 if (empty($alias)) {
00330 $alias = $blockName;
00331 }
00332 }
00333
00334 $block->setParentBlock($this);
00335 $block->setBlockAlias($alias);
00336
00337 $this->_children[$alias] = $block;
00338
00339 return $this;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348 public function unsetChild($alias)
00349 {
00350 if (isset($this->_children[$alias])) {
00351 unset($this->_children[$alias]);
00352 }
00353
00354 if (!empty($this->_sortedChildren)) {
00355 $key = array_search($alias, $this->_sortedChildren);
00356 if ($key!==false) {
00357 unset($this->_sortedChildren[$key]);
00358 }
00359 }
00360
00361 return $this;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 public function unsetCallChild($alias, $callback, $result, $params)
00384 {
00385 $child = $this->getChild($alias);
00386 if ($child) {
00387 $args = func_get_args();
00388 $alias = array_shift($args);
00389 $callback = array_shift($args);
00390 $result = (string)array_shift($args);
00391 if (!is_array($params)) {
00392 $params = $args;
00393 }
00394
00395 if ($result == call_user_func_array(array(&$child, $callback), $params)) {
00396 $this->unsetChild($alias);
00397 }
00398 }
00399 return $this;
00400 }
00401
00402
00403
00404
00405
00406
00407 public function unsetChildren()
00408 {
00409 $this->_children = array();
00410 $this->_sortedChildren = array();
00411 return $this;
00412 }
00413
00414
00415
00416
00417
00418
00419
00420 public function getChild($name='')
00421 {
00422 if (''===$name) {
00423 return $this->_children;
00424 } elseif (isset($this->_children[$name])) {
00425 return $this->_children[$name];
00426 }
00427 return false;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437 public function getChildHtml($name='', $useCache=true, $sorted=false)
00438 {
00439 if ('' === $name) {
00440 if ($sorted) {
00441 $children = array();
00442 foreach ($this->getSortedChildren() as $childName) {
00443 $children[$childName] = $this->getLayout()->getBlock($childName);
00444 }
00445 } else {
00446 $children = $this->getChild();
00447 }
00448 $out = '';
00449 foreach ($children as $child) {
00450 $out .= $this->_getChildHtml($child->getBlockAlias(), $useCache);
00451 }
00452 return $out;
00453 } else {
00454 return $this->_getChildHtml($name, $useCache);
00455 }
00456 }
00457
00458 public function getChildChildHtml($name, $childName = '', $useCache = true, $sorted = false)
00459 {
00460 if (empty($name)) {
00461 return '';
00462 }
00463 $child = $this->getChild($name);
00464 if (!$child) {
00465 return '';
00466 }
00467 return $child->getChildHtml($childName, $useCache, $sorted);
00468 }
00469
00470
00471
00472
00473
00474
00475 public function getSortedChildBlocks()
00476 {
00477 $children = array();
00478 foreach ($this->getSortedChildren() as $childName) {
00479 $children[$childName] = $this->getLayout()->getBlock($childName);
00480 }
00481 return $children;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491 protected function _getChildHtml($name, $useCache=true)
00492 {
00493 if ($useCache && isset($this->_childrenHtmlCache[$name])) {
00494 return $this->_childrenHtmlCache[$name];
00495 }
00496
00497 $child = $this->getChild($name);
00498
00499 if (!$child) {
00500 $html = '';
00501 } else {
00502 $this->_beforeChildToHtml($name, $child);
00503 $html = $child->toHtml();
00504 }
00505
00506 $this->_childrenHtmlCache[$name] = $html;
00507 return $html;
00508 }
00509
00510
00511
00512
00513
00514
00515
00516 protected function _beforeChildToHtml($name, $child)
00517 {
00518 }
00519
00520
00521
00522
00523
00524
00525
00526 public function getBlockHtml($name)
00527 {
00528 if (!($layout = $this->getLayout()) && !($layout = Mage::app()->getFrontController()->getAction()->getLayout())) {
00529 return '';
00530 }
00531 if (!($block = $layout->getBlock($name))) {
00532 return '';
00533 }
00534 return $block->toHtml();
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 public function insert($block, $siblingName='', $after=false, $alias='')
00547 {
00548 if (is_string($block)) {
00549 $block = $this->getLayout()->getBlock($block);
00550 }
00551 if (!$block) {
00552
00553
00554
00555
00556
00557 return $this;
00558 }
00559 if ($block->getIsAnonymous()) {
00560 $this->setChild('', $block);
00561 $name = $block->getNameInLayout();
00562 } elseif ('' != $alias) {
00563 $this->setChild($alias, $block);
00564 $name = $block->getNameInLayout();
00565 } else {
00566 $name = $block->getNameInLayout();
00567 $this->setChild($name, $block);
00568 }
00569
00570 if (''===$siblingName) {
00571 if ($after) {
00572 array_push($this->_sortedChildren, $name);
00573 }
00574 else {
00575 array_unshift($this->_sortedChildren, $name);
00576 }
00577 } else {
00578 $key = array_search($siblingName, $this->_sortedChildren);
00579 if (false!==$key) {
00580 if ($after) {
00581 $key++;
00582 }
00583 array_splice($this->_sortedChildren, $key, 0, $name);
00584 } else {
00585 if ($after) {
00586 array_push($this->_sortedChildren, $name);
00587 }
00588 else {
00589 array_unshift($this->_sortedChildren, $name);
00590 }
00591 }
00592 }
00593
00594 return $this;
00595 }
00596
00597
00598
00599
00600
00601
00602
00603
00604 public function append($block, $alias='')
00605 {
00606 $this->insert($block, '', true, $alias);
00607 return $this;
00608 }
00609
00610
00611
00612
00613
00614
00615 protected function _beforeToHtml()
00616 {
00617 return $this;
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627 final public function toHtml()
00628 {
00629 Mage::dispatchEvent('core_block_abstract_to_html_before', array('block' => $this));
00630
00631 if (Mage::getStoreConfig('advanced/modules_disable_output/'.$this->getModuleName())) {
00632 return '';
00633 }
00634
00635 if (!($html = $this->_loadCache())) {
00636 $translate = Mage::getSingleton('core/translate');
00637
00638 if ($this->hasData('translate_inline')) {
00639 $translate->setTranslateInline($this->getData('translate_inline'));
00640 }
00641
00642 $this->_beforeToHtml();
00643 $html = $this->_toHtml();
00644 $this->_saveCache($html);
00645
00646 if ($this->hasData('translate_inline')) {
00647 $translate->setTranslateInline(true);
00648 }
00649 }
00650
00651 $html = $this->_afterToHtml($html);
00652 Mage::dispatchEvent('core_block_abstract_to_html_after', array('block' => $this));
00653
00654 return $html;
00655 }
00656
00657
00658
00659
00660
00661
00662
00663 protected function _afterToHtml($html)
00664 {
00665 return $html;
00666 }
00667
00668
00669
00670
00671
00672
00673 protected function _toHtml()
00674 {
00675 return '';
00676 }
00677
00678
00679
00680
00681
00682
00683 protected function _getUrlModelClass()
00684 {
00685 return 'core/url';
00686 }
00687
00688
00689
00690
00691
00692
00693 protected function _getUrlModel()
00694 {
00695 return Mage::getModel($this->_getUrlModelClass());;
00696 }
00697
00698
00699
00700
00701
00702
00703
00704
00705 public function getUrl($route='', $params=array())
00706 {
00707 return $this->_getUrlModel()->getUrl($route, $params);
00708 }
00709
00710
00711
00712
00713
00714
00715
00716
00717 public function getUrlBase64($route='', $params=array())
00718 {
00719 return Mage::helper('core')->urlEncode($this->getUrl($route, $params));
00720 }
00721
00722
00723
00724
00725
00726
00727
00728
00729 public function getUrlEncoded($route = '', $params = array())
00730 {
00731 return Mage::helper('core')->urlEncode($this->getUrl($route, $params));
00732 }
00733
00734
00735
00736
00737
00738
00739
00740
00741 public function getSkinUrl($file=null, array $params=array())
00742 {
00743 return Mage::getDesign()->getSkinUrl($file, $params);
00744 }
00745
00746
00747
00748
00749
00750
00751 public function getMessagesBlock()
00752 {
00753 if (is_null($this->_messagesBlock)) {
00754 return $this->getLayout()->getMessagesBlock();
00755 }
00756 return $this->_messagesBlock;
00757 }
00758
00759
00760
00761
00762
00763
00764
00765 public function setMessagesBlock(Mage_Core_Block_Messages $block)
00766 {
00767 $this->_messagesBlock = $block;
00768 return $this;
00769 }
00770
00771
00772
00773
00774
00775
00776
00777 public function getHelper($type)
00778 {
00779 return $this->getLayout()->getBlockSingleton($type);
00780
00781 }
00782
00783
00784
00785
00786
00787
00788
00789 public function helper($name)
00790 {
00791 if ($this->getLayout()) {
00792 return $this->getLayout()->helper($name);
00793 }
00794 return Mage::helper($name);
00795 }
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805 public function formatDate($date=null, $format='short', $showTime=false)
00806 {
00807 return $this->helper('core')->formatDate($date, $format, $showTime);
00808 }
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818 public function formatTime($time=null, $format='short', $showDate=false)
00819 {
00820 return $this->helper('core')->formatTime($time, $format, $showDate);
00821 }
00822
00823
00824
00825
00826
00827
00828 public function getModuleName()
00829 {
00830 $module = $this->getData('module_name');
00831 if (is_null($module)) {
00832 $class = get_class($this);
00833 $module = substr($class, 0, strpos($class, '_Block'));
00834 $this->setData('module_name', $module);
00835 }
00836 return $module;
00837 }
00838
00839
00840
00841
00842
00843
00844 public function __()
00845 {
00846 $args = func_get_args();
00847 $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->getModuleName());
00848 array_unshift($args, $expr);
00849 return Mage::app()->getTranslator()->translate($args);
00850 }
00851
00852
00853
00854
00855
00856
00857 public function getCacheKey()
00858 {
00859 if (!$this->hasData('cache_key')) {
00860 $this->setCacheKey($this->getNameInLayout());
00861 }
00862 return $this->getData('cache_key');
00863 }
00864
00865
00866
00867
00868
00869
00870 public function getCacheTags()
00871 {
00872 if (!$this->hasData('cache_tags')) {
00873 $tags = array();
00874 } else {
00875 $tags = $this->getData('cache_tags');
00876 }
00877 $tags[] = 'block_html';
00878 return $tags;
00879 }
00880
00881
00882
00883
00884
00885
00886 public function getCacheLifetime()
00887 {
00888 if (!$this->hasData('cache_lifetime')) {
00889 return null;
00890 }
00891 return $this->getData('cache_lifetime');
00892 }
00893
00894
00895
00896
00897
00898
00899 protected function _loadCache()
00900 {
00901 if (is_null($this->getCacheLifetime()) || !Mage::app()->useCache('block_html')) {
00902 return false;
00903 }
00904 return Mage::app()->loadCache($this->getCacheKey());
00905 }
00906
00907
00908
00909
00910
00911
00912
00913 protected function _saveCache($data)
00914 {
00915 if (is_null($this->getCacheLifetime()) || !Mage::app()->useCache('block_html')) {
00916 return false;
00917 }
00918 Mage::app()->saveCache($data, $this->getCacheKey(), $this->getCacheTags(), $this->getCacheLifetime());
00919 return $this;
00920 }
00921
00922
00923
00924
00925
00926
00927
00928
00929 public function htmlEscape($data, $allowedTags = null)
00930 {
00931 return $this->helper('core')->htmlEscape($data, $allowedTags);
00932 }
00933
00934
00935
00936
00937
00938
00939
00940 public function urlEscape($data)
00941 {
00942 return $this->helper('core')->urlEscape($data);
00943 }
00944
00945
00946
00947
00948
00949
00950
00951
00952 public function jsQuoteEscape($data, $quote = '\'')
00953 {
00954 return $this->helper('core')->jsQuoteEscape($data, $quote);
00955 }
00956
00957 public function getNameInLayout()
00958 {
00959 return $this->_getData('name_in_layout');
00960 }
00961
00962 public function countChildren()
00963 {
00964 return count($this->_children);
00965 }
00966
00967
00968
00969
00970
00971
00972 protected function _beforeCacheUrl()
00973 {
00974 if (Mage::app()->useCache('block_html')) {
00975 Mage::app()->setUseSessionVar(true);
00976 }
00977 return $this;
00978 }
00979
00980
00981
00982
00983
00984
00985
00986 protected function _afterCacheUrl($html)
00987 {
00988 if (Mage::app()->useCache('block_html')) {
00989 Mage::app()->setUseSessionVar(false);
00990 Varien_Profiler::start('CACHE_URL');
00991 $html = Mage::getSingleton('core/url')->sessionUrlVar($html);
00992 Varien_Profiler::stop('CACHE_URL');
00993 }
00994 return $html;
00995 }
00996 }