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 abstract class Mage_Core_Helper_Abstract
00033 {
00034
00035
00036
00037
00038
00039 protected $_moduleName;
00040
00041
00042
00043
00044
00045
00046 protected $_request;
00047
00048
00049
00050
00051
00052
00053 protected $_layout;
00054
00055
00056
00057
00058
00059
00060 protected function _getRequest()
00061 {
00062 if (!$this->_request) {
00063 $this->_request = Mage::app()->getRequest();
00064 }
00065 return $this->_request;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 protected function _loadCache($id)
00075 {
00076 return Mage::app()->loadCache($id);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 protected function _saveCache($data, $id, $tags=array(), $lifeTime=false)
00088 {
00089 Mage::app()->saveCache($data, $id, $tags, $lifeTime);
00090 return $this;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 protected function _removeCache($id)
00100 {
00101 Mage::app()->removeCache($id);
00102 return $this;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 protected function _cleanCache($tags=array())
00112 {
00113 Mage::app()->cleanCache($tags);
00114 return $this;
00115 }
00116
00117
00118
00119
00120
00121
00122 protected function _getModuleName()
00123 {
00124 if (!$this->_moduleName) {
00125 $class = get_class($this);
00126 $this->_moduleName = substr($class, 0, strpos($class, '_Helper'));
00127 }
00128 return $this->_moduleName;
00129 }
00130
00131
00132
00133
00134
00135
00136 public function __()
00137 {
00138 $args = func_get_args();
00139 $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->_getModuleName());
00140 array_unshift($args, $expr);
00141 return Mage::app()->getTranslator()->translate($args);
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151 public function htmlEscape($data, $allowedTags = null)
00152 {
00153 if (is_array($data)) {
00154 $result = array();
00155 foreach ($data as $item) {
00156 $result[] = $this->htmlEscape($item);
00157 }
00158 } else {
00159
00160 if (strlen($data)) {
00161 if (is_array($allowedTags) and !empty($allowedTags)) {
00162 $allowed = implode('|', $allowedTags);
00163 $result = preg_replace('/<([\/\s\r\n]*)(' . $allowed . ')([\/\s\r\n]*)>/si', '##$1$2$3##', $data);
00164 $result = htmlspecialchars($result);
00165 $result = preg_replace('/##([\/\s\r\n]*)(' . $allowed . ')([\/\s\r\n]*)##/si', '<$1$2$3>', $result);
00166 } else {
00167 $result = htmlspecialchars($data);
00168 }
00169 } else {
00170 $result = $data;
00171 }
00172 }
00173 return $result;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 public function urlEscape($data)
00183 {
00184 return htmlspecialchars($data);
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 public function jsQuoteEscape($data, $quote='\'')
00195 {
00196 if (is_array($data)) {
00197 $result = array();
00198 foreach ($data as $item) {
00199 $result[] = str_replace($quote, '\\'.$quote, $item);
00200 }
00201 return $result;
00202 }
00203 return str_replace($quote, '\\'.$quote, $data);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213 protected function _getUrl($route, $params = array())
00214 {
00215 return Mage::getUrl($route, $params);
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 public function setLayout($layout)
00225 {
00226 $this->_layout = $layout;
00227 return $this;
00228 }
00229
00230
00231
00232
00233
00234
00235 public function getLayout()
00236 {
00237 return $this->_layout;
00238 }
00239
00240
00241
00242
00243
00244
00245
00246 public function urlEncode($url)
00247 {
00248 return strtr(base64_encode($url), '+/=', '-_,');
00249 }
00250
00251
00252
00253
00254
00255
00256
00257 public function urlDecode($url)
00258 {
00259 $url = base64_decode(strtr($url, '-_,', '+/='));
00260 return Mage::getSingleton('core/url')->sessionUrlVar($url);
00261 }
00262
00263
00264
00265
00266
00267
00268
00269 public function translateArray($arr = array())
00270 {
00271 foreach ($arr as $k => $v) {
00272 if (is_array($v)) {
00273 $v = self::translateArray($v);
00274 } elseif ($k === 'label') {
00275 $v = self::__($v);
00276 }
00277 $arr[$k] = $v;
00278 }
00279 return $arr;
00280 }
00281 }