PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Block/Abstract.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 1188 lines | 617 code | 94 blank | 477 comment | 76 complexity | 8e03dd64a7c2e03e80b907cd3f70438b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Base Content Block class
  28. *
  29. * For block generation you must define Data source class, data source class method,
  30. * parameters array and block template
  31. *
  32. * @category Mage
  33. * @package Mage_Core
  34. * @author Magento Core Team <core@magentocommerce.com>
  35. */
  36. abstract class Mage_Core_Block_Abstract extends Varien_Object
  37. {
  38. const CACHE_GROUP = 'block_html';
  39. /**
  40. * Block name in layout
  41. *
  42. * @var string
  43. */
  44. protected $_nameInLayout;
  45. /**
  46. * Parent layout of the block
  47. *
  48. * @var Mage_Core_Model_Layout
  49. */
  50. protected $_layout;
  51. /**
  52. * Parent block
  53. *
  54. * @var Mage_Core_Block_Abstract
  55. */
  56. protected $_parent;
  57. /**
  58. * Short alias of this block to be refered from parent
  59. *
  60. * @var string
  61. */
  62. protected $_alias;
  63. /**
  64. * Suffix for name of anonymous block
  65. *
  66. * @var string
  67. */
  68. protected $_anonSuffix;
  69. /**
  70. * Contains references to child block objects
  71. *
  72. * @var array
  73. */
  74. protected $_children = array();
  75. /**
  76. * Sorted children list
  77. *
  78. * @var array
  79. */
  80. protected $_sortedChildren = array();
  81. /**
  82. * Children blocks HTML cache array
  83. *
  84. * @var array
  85. */
  86. protected $_childrenHtmlCache = array();
  87. /**
  88. * Arbitrary groups of child blocks
  89. *
  90. * @var array
  91. */
  92. protected $_childGroups = array();
  93. /**
  94. * Request object
  95. *
  96. * @var Zend_Controller_Request_Http
  97. */
  98. protected $_request;
  99. /**
  100. * Messages block instance
  101. *
  102. * @var Mage_Core_Block_Messages
  103. */
  104. protected $_messagesBlock = null;
  105. /**
  106. * Whether this block was not explicitly named
  107. *
  108. * @var boolean
  109. */
  110. protected $_isAnonymous = false;
  111. /**
  112. * Parent block
  113. *
  114. * @var Mage_Core_Block_Abstract
  115. */
  116. protected $_parentBlock;
  117. /**
  118. * Block html frame open tag
  119. * @var string
  120. */
  121. protected $_frameOpenTag;
  122. /**
  123. * Block html frame close tag
  124. * @var string
  125. */
  126. protected $_frameCloseTag;
  127. protected static $_urlModel;
  128. /**
  129. * @var Varien_Object
  130. */
  131. private static $_transportObject;
  132. /**
  133. * Internal constructor, that is called from real constructor
  134. *
  135. * Please override this one instead of overriding real __construct constructor
  136. *
  137. */
  138. protected function _construct()
  139. {
  140. /**
  141. * Please override this one instead of overriding real __construct constructor
  142. */
  143. }
  144. /**
  145. * Retrieve request object
  146. *
  147. * @return Mage_Core_Controller_Request_Http
  148. */
  149. public function getRequest()
  150. {
  151. $controller = Mage::app()->getFrontController();
  152. if ($controller) {
  153. $this->_request = $controller->getRequest();
  154. } else {
  155. throw new Exception(Mage::helper('core')->__("Can't retrieve request object"));
  156. }
  157. return $this->_request;
  158. }
  159. /**
  160. * Retrieve parent block
  161. *
  162. * @return Mage_Core_Block_Abstract
  163. */
  164. public function getParentBlock()
  165. {
  166. return $this->_parentBlock;
  167. }
  168. /**
  169. * Set parent block
  170. *
  171. * @param Mage_Core_Block_Abstract $block
  172. * @return Mage_Core_Block_Abstract
  173. */
  174. public function setParentBlock(Mage_Core_Block_Abstract $block)
  175. {
  176. $this->_parentBlock = $block;
  177. return $this;
  178. }
  179. /**
  180. * Retrieve current action object
  181. *
  182. * @return Mage_Core_Controller_Varien_Action
  183. */
  184. public function getAction()
  185. {
  186. return Mage::app()->getFrontController()->getAction();
  187. }
  188. /**
  189. * Set layout object
  190. *
  191. * @param Mage_Core_Model_Layout $layout
  192. * @return Mage_Core_Block_Abstract
  193. */
  194. public function setLayout(Mage_Core_Model_Layout $layout)
  195. {
  196. $this->_layout = $layout;
  197. Mage::dispatchEvent('core_block_abstract_prepare_layout_before', array('block' => $this));
  198. $this->_prepareLayout();
  199. Mage::dispatchEvent('core_block_abstract_prepare_layout_after', array('block' => $this));
  200. return $this;
  201. }
  202. /**
  203. * Preparing global layout
  204. *
  205. * You can redefine this method in child classes for changin layout
  206. *
  207. * @return Mage_Core_Block_Abstract
  208. */
  209. protected function _prepareLayout()
  210. {
  211. return $this;
  212. }
  213. /**
  214. * Retrieve layout object
  215. *
  216. * @return Mage_Core_Model_Layout
  217. */
  218. public function getLayout()
  219. {
  220. return $this->_layout;
  221. }
  222. /**
  223. * Check if block is using auto generated (Anonymous) name
  224. * @return bool
  225. */
  226. public function getIsAnonymous()
  227. {
  228. return $this->_isAnonymous;
  229. }
  230. public function setIsAnonymous($flag)
  231. {
  232. $this->_isAnonymous = $flag;
  233. return $this;
  234. }
  235. public function getAnonSuffix()
  236. {
  237. return $this->_anonSuffix;
  238. }
  239. public function setAnonSuffix($suffix)
  240. {
  241. $this->_anonSuffix = $suffix;
  242. return $this;
  243. }
  244. public function getBlockAlias()
  245. {
  246. return $this->_alias;
  247. }
  248. public function setBlockAlias($alias)
  249. {
  250. $this->_alias = $alias;
  251. return $this;
  252. }
  253. /**
  254. * Set block's name in layout and unsets previous link if such exists.
  255. *
  256. * @param $name
  257. * @return Mage_Core_Block_Abstract
  258. */
  259. public function setNameInLayout($name)
  260. {
  261. if (!empty($this->_nameInLayout) && $this->getLayout()) {
  262. $this->getLayout()->unsetBlock($this->_nameInLayout)
  263. ->setBlock($name, $this);
  264. }
  265. $this->_nameInLayout = $name;
  266. return $this;
  267. }
  268. public function getSortedChildren()
  269. {
  270. return $this->_sortedChildren;
  271. }
  272. /**
  273. * Set block attribute value
  274. *
  275. * Wrapper for method "setData"
  276. *
  277. * @param string $name
  278. * @param mixed $value
  279. * @return Mage_Core_Block_Abstract
  280. */
  281. public function setAttribute($name, $value=null)
  282. {
  283. return $this->setData($name, $value);
  284. }
  285. /**
  286. * Set child block
  287. *
  288. * @param string $name
  289. * @param Mage_Core_Block_Abstract $block
  290. * @return Mage_Core_Block_Abstract
  291. */
  292. public function setChild($alias, $block)
  293. {
  294. if (is_string($block)) {
  295. $block = $this->getLayout()->getBlock($block);
  296. }
  297. if (!$block) {
  298. return $this;
  299. }
  300. if ($block->getIsAnonymous()) {
  301. $suffix = $block->getAnonSuffix();
  302. if (empty($suffix)) {
  303. $suffix = 'child'.sizeof($this->_children);
  304. }
  305. $blockName = $this->getNameInLayout().'.'.$suffix;
  306. if ($this->getLayout()) {
  307. $this->getLayout()->unsetBlock($block->getNameInLayout())
  308. ->setBlock($blockName, $block);
  309. }
  310. $block->setNameInLayout($blockName);
  311. $block->setIsAnonymous(false);
  312. if (empty($alias)) {
  313. $alias = $blockName;
  314. }
  315. }
  316. $block->setParentBlock($this);
  317. $block->setBlockAlias($alias);
  318. $this->_children[$alias] = $block;
  319. return $this;
  320. }
  321. /**
  322. * Unset child block
  323. *
  324. * @param string $name
  325. * @return Mage_Core_Block_Abstract
  326. */
  327. public function unsetChild($alias)
  328. {
  329. if (isset($this->_children[$alias])) {
  330. unset($this->_children[$alias]);
  331. }
  332. if (!empty($this->_sortedChildren)) {
  333. $key = array_search($alias, $this->_sortedChildren);
  334. if ($key!==false) {
  335. unset($this->_sortedChildren[$key]);
  336. }
  337. }
  338. return $this;
  339. }
  340. /**
  341. * Call a child and unset it, if callback matched result
  342. *
  343. * $params will pass to child callback
  344. * $params may be array, if called from layout with elements with same name, for example:
  345. * ...<foo>value_1</foo><foo>value_2</foo><foo>value_3</foo>
  346. *
  347. * Or, if called like this:
  348. * ...<foo>value_1</foo><bar>value_2</bar><baz>value_3</baz>
  349. * - then it will be $params1, $params2, $params3
  350. *
  351. * It is no difference anyway, because they will be transformed in appropriate way.
  352. *
  353. * @param string $alias
  354. * @param string $callback
  355. * @param mixed $result
  356. * @param array $params
  357. * @return Mage_Core_Block_Abstract
  358. */
  359. public function unsetCallChild($alias, $callback, $result, $params)
  360. {
  361. $child = $this->getChild($alias);
  362. if ($child) {
  363. $args = func_get_args();
  364. $alias = array_shift($args);
  365. $callback = array_shift($args);
  366. $result = (string)array_shift($args);
  367. if (!is_array($params)) {
  368. $params = $args;
  369. }
  370. if ($result == call_user_func_array(array(&$child, $callback), $params)) {
  371. $this->unsetChild($alias);
  372. }
  373. }
  374. return $this;
  375. }
  376. /**
  377. * Unset all children blocks
  378. *
  379. * @return Mage_Core_Block_Abstract
  380. */
  381. public function unsetChildren()
  382. {
  383. $this->_children = array();
  384. $this->_sortedChildren = array();
  385. return $this;
  386. }
  387. /**
  388. * Retrieve child block by name
  389. *
  390. * @param string $name
  391. * @return mixed
  392. */
  393. public function getChild($name='')
  394. {
  395. if (''===$name) {
  396. return $this->_children;
  397. } elseif (isset($this->_children[$name])) {
  398. return $this->_children[$name];
  399. }
  400. return false;
  401. }
  402. /**
  403. * Retrieve child block HTML
  404. *
  405. * @param string $name
  406. * @param boolean $useCache
  407. * @return string
  408. */
  409. public function getChildHtml($name='', $useCache=true, $sorted=false)
  410. {
  411. if ('' === $name) {
  412. if ($sorted) {
  413. $children = array();
  414. foreach ($this->getSortedChildren() as $childName) {
  415. $children[$childName] = $this->getLayout()->getBlock($childName);
  416. }
  417. } else {
  418. $children = $this->getChild();
  419. }
  420. $out = '';
  421. foreach ($children as $child) {
  422. $out .= $this->_getChildHtml($child->getBlockAlias(), $useCache);
  423. }
  424. return $out;
  425. } else {
  426. return $this->_getChildHtml($name, $useCache);
  427. }
  428. }
  429. public function getChildChildHtml($name, $childName = '', $useCache = true, $sorted = false)
  430. {
  431. if (empty($name)) {
  432. return '';
  433. }
  434. $child = $this->getChild($name);
  435. if (!$child) {
  436. return '';
  437. }
  438. return $child->getChildHtml($childName, $useCache, $sorted);
  439. }
  440. /**
  441. * Obtain sorted child blocks
  442. *
  443. * @return array
  444. */
  445. public function getSortedChildBlocks()
  446. {
  447. $children = array();
  448. foreach ($this->getSortedChildren() as $childName) {
  449. $children[$childName] = $this->getLayout()->getBlock($childName);
  450. }
  451. return $children;
  452. }
  453. /**
  454. * Retrieve child block HTML
  455. *
  456. * @param string $name
  457. * @param boolean $useCache
  458. * @return string
  459. */
  460. protected function _getChildHtml($name, $useCache=true)
  461. {
  462. if ($useCache && isset($this->_childrenHtmlCache[$name])) {
  463. return $this->_childrenHtmlCache[$name];
  464. }
  465. $child = $this->getChild($name);
  466. if (!$child) {
  467. $html = '';
  468. } else {
  469. $this->_beforeChildToHtml($name, $child);
  470. $html = $child->toHtml();
  471. }
  472. $this->_childrenHtmlCache[$name] = $html;
  473. return $html;
  474. }
  475. /**
  476. * Prepare child block before generate html
  477. *
  478. * @param string $name
  479. * @param Mage_Core_Block_Abstract $child
  480. */
  481. protected function _beforeChildToHtml($name, $child)
  482. {
  483. }
  484. /**
  485. * Retrieve block html
  486. *
  487. * @param string $name
  488. * @return string
  489. */
  490. public function getBlockHtml($name)
  491. {
  492. if (!($layout = $this->getLayout()) && !($layout = Mage::app()->getFrontController()->getAction()->getLayout())) {
  493. return '';
  494. }
  495. if (!($block = $layout->getBlock($name))) {
  496. return '';
  497. }
  498. return $block->toHtml();
  499. }
  500. /**
  501. * Insert child block
  502. *
  503. * @param Mage_Core_Block_Abstract|string $block
  504. * @param string $siblingName
  505. * @param boolean $after
  506. * @param string $alias
  507. * @return object $this
  508. */
  509. public function insert($block, $siblingName='', $after=false, $alias='')
  510. {
  511. if (is_string($block)) {
  512. $block = $this->getLayout()->getBlock($block);
  513. }
  514. if (!$block) {
  515. /*
  516. * if we don't have block - don't throw exception because
  517. * block can simply removed using layout method remove
  518. */
  519. //Mage::throwException(Mage::helper('core')->__('Invalid block name to set child %s: %s', $alias, $block));
  520. return $this;
  521. }
  522. if ($block->getIsAnonymous()) {
  523. $this->setChild('', $block);
  524. $name = $block->getNameInLayout();
  525. } elseif ('' != $alias) {
  526. $this->setChild($alias, $block);
  527. $name = $block->getNameInLayout();
  528. } else {
  529. $name = $block->getNameInLayout();
  530. $this->setChild($name, $block);
  531. }
  532. if (''===$siblingName) {
  533. if ($after) {
  534. array_push($this->_sortedChildren, $name);
  535. } else {
  536. array_unshift($this->_sortedChildren, $name);
  537. }
  538. } else {
  539. $key = array_search($siblingName, $this->_sortedChildren);
  540. if (false!==$key) {
  541. if ($after) {
  542. $key++;
  543. }
  544. array_splice($this->_sortedChildren, $key, 0, $name);
  545. } else {
  546. if ($after) {
  547. array_push($this->_sortedChildren, $name);
  548. } else {
  549. array_unshift($this->_sortedChildren, $name);
  550. }
  551. }
  552. }
  553. return $this;
  554. }
  555. /**
  556. * Append child block
  557. *
  558. * @param Mage_Core_Block_Abstract|string $block
  559. * @param string $alias
  560. * @return Mage_Core_Block_Abstract
  561. */
  562. public function append($block, $alias='')
  563. {
  564. $this->insert($block, '', true, $alias);
  565. return $this;
  566. }
  567. /**
  568. * Make sure specified block will be registered in the specified child groups
  569. *
  570. * @param string $groupName
  571. * @param Mage_Core_Block_Abstract $child
  572. */
  573. public function addToChildGroup($groupName, Mage_Core_Block_Abstract $child)
  574. {
  575. if (!isset($this->_childGroups[$groupName])) {
  576. $this->_childGroups[$groupName] = array();
  577. }
  578. if (!in_array($child->getBlockAlias(), $this->_childGroups[$groupName])) {
  579. $this->_childGroups[$groupName][] = $child->getBlockAlias();
  580. }
  581. }
  582. /**
  583. * Add self to the specified group of parent block
  584. *
  585. * @param string $groupName
  586. * @return Mage_Core_Block_Abstract
  587. */
  588. public function addToParentGroup($groupName)
  589. {
  590. $this->getParentBlock()->addToChildGroup($groupName, $this);
  591. return $this;
  592. }
  593. /**
  594. * Get a group of child blocks
  595. *
  596. * Returns an array of <alias> => <block>
  597. * or an array of <alias> => <callback_result>
  598. * The callback currently supports only $this methods and passes the alias as parameter
  599. *
  600. * @param string $groupName
  601. * @param string $callback
  602. * @param bool $skipEmptyResults
  603. * @return array
  604. */
  605. public function getChildGroup($groupName, $callback = null, $skipEmptyResults = true)
  606. {
  607. $result = array();
  608. if (!isset($this->_childGroups[$groupName])) {
  609. return $result;
  610. }
  611. foreach ($this->getSortedChildBlocks() as $block) {
  612. $alias = $block->getBlockAlias();
  613. if (in_array($alias, $this->_childGroups[$groupName])) {
  614. if ($callback) {
  615. $row = $this->$callback($alias);
  616. if (!$skipEmptyResults || $row) {
  617. $result[$alias] = $row;
  618. }
  619. } else {
  620. $result[$alias] = $block;
  621. }
  622. }
  623. }
  624. return $result;
  625. }
  626. /**
  627. * Get a value from child block by specified key
  628. *
  629. * @param string $alias
  630. * @param string $key
  631. * @return mixed
  632. */
  633. public function getChildData($alias, $key = '')
  634. {
  635. $child = $this->getChild($alias);
  636. if ($child) {
  637. return $child->getData($key);
  638. }
  639. }
  640. /**
  641. * Before rendering html, but after trying to load cache
  642. *
  643. * @return Mage_Core_Block_Abstract
  644. */
  645. protected function _beforeToHtml()
  646. {
  647. return $this;
  648. }
  649. /**
  650. * Specify block output frame tags
  651. *
  652. * @param $openTag
  653. * @param $closeTag
  654. * @return Mage_Core_Block_Abstract
  655. */
  656. public function setFrameTags($openTag, $closeTag=null)
  657. {
  658. $this->_frameOpenTag = $openTag;
  659. if ($closeTag) {
  660. $this->_frameCloseTag = $closeTag;
  661. } else {
  662. $this->_frameCloseTag = '/'.$openTag;
  663. }
  664. return $this;
  665. }
  666. /**
  667. * Produce and return block's html output
  668. *
  669. * It is a final method, but you can override _toHmtl() method in descendants if needed
  670. *
  671. * @return string
  672. */
  673. final public function toHtml()
  674. {
  675. Mage::dispatchEvent('core_block_abstract_to_html_before', array('block' => $this));
  676. if (Mage::getStoreConfig('advanced/modules_disable_output/'.$this->getModuleName())) {
  677. return '';
  678. }
  679. $html = $this->_loadCache();
  680. if ($html === false) {
  681. $translate = Mage::getSingleton('core/translate');
  682. /* @var $translate Mage_Core_Model_Translate */
  683. if ($this->hasData('translate_inline')) {
  684. $translate->setTranslateInline($this->getData('translate_inline'));
  685. }
  686. $this->_beforeToHtml();
  687. $html = $this->_toHtml();
  688. $this->_saveCache($html);
  689. if ($this->hasData('translate_inline')) {
  690. $translate->setTranslateInline(true);
  691. }
  692. }
  693. $html = $this->_afterToHtml($html);
  694. /**
  695. * Check framing options
  696. */
  697. if ($this->_frameOpenTag) {
  698. $html = '<'.$this->_frameOpenTag.'>'.$html.'<'.$this->_frameCloseTag.'>';
  699. }
  700. /**
  701. * Use single transport object instance for all blocks
  702. */
  703. if (self::$_transportObject === null) {
  704. self::$_transportObject = new Varien_Object;
  705. }
  706. self::$_transportObject->setHtml($html);
  707. Mage::dispatchEvent('core_block_abstract_to_html_after', array('block' => $this, 'transport' => self::$_transportObject));
  708. $html = self::$_transportObject->getHtml();
  709. return $html;
  710. }
  711. /**
  712. * Processing block html after rendering
  713. *
  714. * @param string $html
  715. * @return string
  716. */
  717. protected function _afterToHtml($html)
  718. {
  719. return $html;
  720. }
  721. /**
  722. * Override this method in descendants to produce html
  723. *
  724. * @return string
  725. */
  726. protected function _toHtml()
  727. {
  728. return '';
  729. }
  730. /**
  731. * Enter description here...
  732. *
  733. * @return string
  734. */
  735. protected function _getUrlModelClass()
  736. {
  737. return 'core/url';
  738. }
  739. /**
  740. * Enter description here...
  741. *
  742. * @return Mage_Core_Model_Url
  743. */
  744. protected function _getUrlModel()
  745. {
  746. return Mage::getModel($this->_getUrlModelClass());
  747. }
  748. /**
  749. * Generate url by route and parameters
  750. *
  751. * @param string $route
  752. * @param array $params
  753. * @return string
  754. */
  755. public function getUrl($route='', $params=array())
  756. {
  757. return $this->_getUrlModel()->getUrl($route, $params);
  758. }
  759. /**
  760. * Generate base64-encoded url by route and parameters
  761. *
  762. * @param string $route
  763. * @param array $params
  764. * @return string
  765. */
  766. public function getUrlBase64($route='', $params=array())
  767. {
  768. return Mage::helper('core')->urlEncode($this->getUrl($route, $params));
  769. }
  770. /**
  771. * Generate url-encoded url by route and parameters
  772. *
  773. * @param string $route
  774. * @param array $params
  775. * @return string
  776. */
  777. public function getUrlEncoded($route = '', $params = array())
  778. {
  779. return Mage::helper('core')->urlEncode($this->getUrl($route, $params));
  780. }
  781. /**
  782. * Retrieve url of skins file
  783. *
  784. * @param string $file path to file in skin
  785. * @param array $params
  786. * @return string
  787. */
  788. public function getSkinUrl($file=null, array $params=array())
  789. {
  790. return Mage::getDesign()->getSkinUrl($file, $params);
  791. }
  792. /**
  793. * Retrieve messages block
  794. *
  795. * @return Mage_Core_Block_Messages
  796. */
  797. public function getMessagesBlock()
  798. {
  799. if (is_null($this->_messagesBlock)) {
  800. return $this->getLayout()->getMessagesBlock();
  801. }
  802. return $this->_messagesBlock;
  803. }
  804. /**
  805. * Set messages block
  806. *
  807. * @param Mage_Core_Block_Messages $block
  808. * @return Mage_Core_Block_Abstract
  809. */
  810. public function setMessagesBlock(Mage_Core_Block_Messages $block)
  811. {
  812. $this->_messagesBlock = $block;
  813. return $this;
  814. }
  815. /**
  816. * Enter description here...
  817. *
  818. * @param string $type
  819. * @return Mage_Core_Block_Abstract
  820. */
  821. public function getHelper($type)
  822. {
  823. return $this->getLayout()->getBlockSingleton($type);
  824. //return $this->helper($type);
  825. }
  826. /**
  827. * Enter description here...
  828. *
  829. * @param string $name
  830. * @return Mage_Core_Block_Abstract
  831. */
  832. public function helper($name)
  833. {
  834. if ($this->getLayout()) {
  835. return $this->getLayout()->helper($name);
  836. }
  837. return Mage::helper($name);
  838. }
  839. /**
  840. * Retrieve formating date
  841. *
  842. * @param string $date
  843. * @param string $format
  844. * @param bool $showTime
  845. * @return string
  846. */
  847. public function formatDate($date=null, $format='short', $showTime=false)
  848. {
  849. return $this->helper('core')->formatDate($date, $format, $showTime);
  850. }
  851. /**
  852. * Retrieve formating time
  853. *
  854. * @param string $time
  855. * @param string $format
  856. * @param bool $showDate
  857. * @return string
  858. */
  859. public function formatTime($time=null, $format='short', $showDate=false)
  860. {
  861. return $this->helper('core')->formatTime($time, $format, $showDate);
  862. }
  863. /**
  864. * Retrieve module name of block
  865. *
  866. * @return string
  867. */
  868. public function getModuleName()
  869. {
  870. $module = $this->getData('module_name');
  871. if (is_null($module)) {
  872. $class = get_class($this);
  873. $module = substr($class, 0, strpos($class, '_Block'));
  874. $this->setData('module_name', $module);
  875. }
  876. return $module;
  877. }
  878. /**
  879. * Translate block sentence
  880. *
  881. * @return string
  882. */
  883. public function __()
  884. {
  885. $args = func_get_args();
  886. $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->getModuleName());
  887. array_unshift($args, $expr);
  888. return Mage::app()->getTranslator()->translate($args);
  889. }
  890. /**
  891. * @deprecated after 1.4.0.0-rc1
  892. * @see self::escapeHtml()
  893. */
  894. public function htmlEscape($data, $allowedTags = null)
  895. {
  896. return $this->escapeHtml($data, $allowedTags);
  897. }
  898. /**
  899. * Escape html entities
  900. *
  901. * @param mixed $data
  902. * @param array $allowedTags
  903. * @return string
  904. */
  905. public function escapeHtml($data, $allowedTags = null)
  906. {
  907. return $this->helper('core')->escapeHtml($data, $allowedTags);
  908. }
  909. /**
  910. * Wrapper for standart strip_tags() function with extra functionality for html entities
  911. *
  912. * @param string $data
  913. * @param string $allowableTags
  914. * @param bool $allowHtmlEntities
  915. * @return string
  916. */
  917. public function stripTags($data, $allowableTags = null, $allowHtmlEntities = false)
  918. {
  919. return $this->helper('core')->stripTags($data, $allowableTags, $allowHtmlEntities);
  920. }
  921. /**
  922. * @deprecated after 1.4.0.0-rc1
  923. * @see self::escapeUrl()
  924. */
  925. public function urlEscape($data)
  926. {
  927. return $this->escapeUrl($data);
  928. }
  929. /**
  930. * Escape html entities in url
  931. *
  932. * @param string $data
  933. * @return string
  934. */
  935. public function escapeUrl($data)
  936. {
  937. return $this->helper('core')->escapeUrl($data);
  938. }
  939. /**
  940. * Escape quotes in java scripts
  941. *
  942. * @param mixed $data
  943. * @param string $quote
  944. * @return mixed
  945. */
  946. public function jsQuoteEscape($data, $quote = '\'')
  947. {
  948. return $this->helper('core')->jsQuoteEscape($data, $quote);
  949. }
  950. /**
  951. * Alias for getName method.
  952. *
  953. * @return string
  954. */
  955. public function getNameInLayout()
  956. {
  957. return $this->_nameInLayout;
  958. }
  959. /**
  960. * Get chilren blocks count
  961. * @return int
  962. */
  963. public function countChildren()
  964. {
  965. return count($this->_children);
  966. }
  967. /**
  968. * Prepare url for save to cache
  969. *
  970. * @return Mage_Core_Block_Abstract
  971. */
  972. protected function _beforeCacheUrl()
  973. {
  974. if (Mage::app()->useCache(self::CACHE_GROUP)) {
  975. Mage::app()->setUseSessionVar(true);
  976. }
  977. return $this;
  978. }
  979. /**
  980. * Replace URLs from cache
  981. *
  982. * @param string $html
  983. * @return string
  984. */
  985. protected function _afterCacheUrl($html)
  986. {
  987. if (Mage::app()->useCache(self::CACHE_GROUP)) {
  988. Mage::app()->setUseSessionVar(false);
  989. Varien_Profiler::start('CACHE_URL');
  990. $html = Mage::getSingleton('core/url')->sessionUrlVar($html);
  991. Varien_Profiler::stop('CACHE_URL');
  992. }
  993. return $html;
  994. }
  995. /**
  996. * Get cache key informative items
  997. * Provide string array key to share specific info item with FPC placeholder
  998. *
  999. * @return array
  1000. */
  1001. public function getCacheKeyInfo()
  1002. {
  1003. return array(
  1004. $this->getNameInLayout()
  1005. );
  1006. }
  1007. /**
  1008. * Get Key for caching block content
  1009. *
  1010. * @return string
  1011. */
  1012. public function getCacheKey()
  1013. {
  1014. if ($this->hasData('cache_key')) {
  1015. return $this->getData('cache_key');
  1016. }
  1017. /**
  1018. * don't prevent recalculation by saving generated cache key
  1019. * because of ability to render single block instance with different data
  1020. */
  1021. $key = $this->getCacheKeyInfo();
  1022. //ksort($key); // ignore order
  1023. $key = array_values($key); // ignore array keys
  1024. $key = implode('|', $key);
  1025. $key = sha1($key);
  1026. return $key;
  1027. }
  1028. /**
  1029. * Get tags array for saving cache
  1030. *
  1031. * @return array
  1032. */
  1033. public function getCacheTags()
  1034. {
  1035. if (!$this->hasData('cache_tags')) {
  1036. $tags = array();
  1037. } else {
  1038. $tags = $this->getData('cache_tags');
  1039. }
  1040. $tags[] = self::CACHE_GROUP;
  1041. return $tags;
  1042. }
  1043. /**
  1044. * Get block cache life time
  1045. *
  1046. * @return int
  1047. */
  1048. public function getCacheLifetime()
  1049. {
  1050. if (!$this->hasData('cache_lifetime')) {
  1051. return null;
  1052. }
  1053. return $this->getData('cache_lifetime');
  1054. }
  1055. /**
  1056. * Load block html from cache storage
  1057. *
  1058. * @return string | false
  1059. */
  1060. protected function _loadCache()
  1061. {
  1062. if (is_null($this->getCacheLifetime()) || !Mage::app()->useCache(self::CACHE_GROUP)) {
  1063. return false;
  1064. }
  1065. return Mage::app()->loadCache($this->getCacheKey());
  1066. }
  1067. /**
  1068. * Save block content to cache storage
  1069. *
  1070. * @param string $data
  1071. * @return Mage_Core_Block_Abstract
  1072. */
  1073. protected function _saveCache($data)
  1074. {
  1075. if (is_null($this->getCacheLifetime()) || !Mage::app()->useCache(self::CACHE_GROUP)) {
  1076. return false;
  1077. }
  1078. Mage::app()->saveCache($data, $this->getCacheKey(), $this->getCacheTags(), $this->getCacheLifetime());
  1079. return $this;
  1080. }
  1081. }