Advertisement
Guest User

Untitled

a guest
Dec 20th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. <?php
  2. class S1902_Mgyumesphere_Helper_Data extends Mage_Core_Helper_Abstract{
  3.  
  4. //settings
  5. private $_modulename = 's1902_mgyumesphere';
  6.  
  7. public function initHeadBlock(){
  8. return Mage::app()->getLayout()
  9. ->createBlock('s1902_mgyumesphere/head')
  10. ->setTemplate('s1902_mgyumesphere/head.phtml')
  11. ->toHtml();
  12. }
  13.  
  14. public function getControllerName(){
  15. return Mage::app()->getRequest()->getControllerName();
  16. }
  17. public function getActionName(){
  18. return Mage::app()->getRequest()->getActionName();
  19. }
  20.  
  21. public function getRouteName(){
  22. return Mage::app()->getRequest()->getRouteName();
  23. }
  24.  
  25. public function getModuleName(){
  26. return Mage::app()->getRequest()->getModuleName();
  27. }
  28.  
  29. public function getModel($modelname,$namespacemodule = null){
  30. if($namespacemodule === null){
  31. $namespacemodule = $this->getModuleName();
  32. }
  33. $model = Mage::getModel($namespacemodule.'/'.$modelname);
  34. return $model;
  35. }
  36.  
  37. public function clearCheckout(){
  38. Mage::getSingleton('checkout/session')->clear();
  39. }
  40.  
  41. public function getCurrentProduct(){
  42. return Mage::registry('current_product');
  43. }
  44.  
  45. public function getCartPriceTotals(){
  46. /*assoc data: subtotal, grand_total, discount, tax
  47. example: $cartpricetotals['grand_total']->getValue();
  48. */
  49. return Mage::getSingleton('checkout/session')->getQuote()->getTotals();
  50. }
  51.  
  52. public function getFreeShippingSubTotal(){
  53. return Mage::getStoreConfig("carriers/freeshipping/free_shipping_subtotal");
  54. }
  55.  
  56. public function getCartItems($onlyVisible = true){
  57. $checkoutItems = array();
  58. $checkoutSession = Mage::getSingleton('checkout/session');
  59. $onQuote = null;
  60. if($onlyVisible){
  61. $onQuote = $checkoutSession->getQuote()->getAllVisibleItems();
  62. }
  63. else{
  64. $onQuote = $checkoutSession->getQuote()->getAllItems();
  65. }
  66. foreach ($onQuote as $item) {
  67. $_item = Mage::getModel('catalog/product')->load($item->getProductId());
  68.  
  69. array_push($checkoutItems, $_item->getData());
  70. }
  71. return $checkoutItems;
  72. }
  73.  
  74. public function getBaseUrl(){
  75. return Mage::getBaseUrl();
  76. }
  77.  
  78. public function getUrl($url){
  79. $url = strtolower($url);
  80. switch($url){
  81. case 'skin':
  82. Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
  83. break;
  84. case 'js':
  85. return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
  86. break;
  87. case 'media':
  88. return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
  89. break;
  90. case 'store':
  91. return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
  92. break;
  93. default:
  94. return $this->getBaseUrl();
  95. break;
  96. }
  97. }
  98.  
  99. public function manageSession($key, $val=null){
  100. if($val === null){
  101. return Mage::getSingleton('core/session')->getData($key);
  102. }
  103. else{
  104. Mage::getSingleton('core/session')->setData($key,$val);
  105. }
  106. }
  107.  
  108. public function unsetSession($key){
  109. return Mage::getSingleton('core/session')->unsetData($key);
  110. }
  111.  
  112. public function getProdBySku($sku){
  113. return Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
  114. }
  115.  
  116. public function getProdById($id){
  117. return Mage::getModel('catalog/product')->load($id);
  118. }
  119.  
  120. public function setProdAttr(&$product,&$attr,&$value){
  121. $product->setData($attr,$value)
  122. ->getResource()
  123. ->saveAttribute($product,$attr);
  124. }
  125.  
  126.  
  127. //new
  128. public function moneyFormat($number, $options = array()){
  129.  
  130. $options_copy = array('decimals'=>0,
  131. 'dec_point'=>'.',
  132. 'thousands'=>',',
  133. 'currency_sym'=>true,
  134. 'currency_pos'=>0);
  135.  
  136. if(!isset($options['decimal'])){
  137. $options['decimal'] = $options_copy['decimal'];
  138. }
  139. if(!isset($options['dec_point'])){
  140. $options['dec_point'] = $options_copy['dec_point'];
  141. }
  142. if(!isset($options['thousands'])){
  143. $options['thousands'] = $options_copy['thousands'];
  144. }
  145. if(!isset($options['currency_sym'])){
  146. $options['currency_sym'] = $options_copy['currency_sym'];
  147. }
  148. if(!isset($options['currency_pos'])){
  149. $options['currency_pos'] = $options_copy['currency_pos'];
  150. }
  151.  
  152. $rawNumber = intval(Mage::helper('core')->currency(intval($number),false,false));
  153.  
  154. $formattedNumber = number_format($number, $options['decimal'],$options['dec_point'],$options['thousands']);
  155.  
  156. if($options['currency_sym']){
  157. $currencySymbol = $this->moneySymbol();
  158. if(!is_bool($options['currency_sym'])){
  159. $currency_symbol = $options['currency_sym'];
  160. }
  161.  
  162. $moneyFormattedNumber = null;
  163. if($options['currency_pos'] <= 0){
  164. $formattedNumber = $currencySymbol . ' ' . $formattedNumber;
  165. }
  166. else{
  167. $formattedNumber = $formattedNumber . ' ' . $currencySymbol;
  168. }
  169. }
  170.  
  171. return $formattedNumber;
  172.  
  173. }
  174.  
  175. //new
  176. public function moneySymbol($store = null){
  177. if($store === null){
  178. $store = Mage::app()->getStore();
  179. }
  180. return Mage::app()->getLocale()->currency($store->getCurrentCurrencyCode())->getSymbol();
  181. }
  182.  
  183.  
  184. /*
  185. *deprecated
  186. */
  187. public function formatMoney($number, $hasSymbol = true, $hasNoDecimalAndSymbol=true){
  188. return Mage::helper('core')->currency($number, $hasNoDecimalAndSymbol,true);
  189. }
  190.  
  191. public function currency($number, $hasDecimalAndSymbol = false){
  192. return Mage::helper('core')->currency((int)$number, $hasNoDecimalAndSymbol,false);
  193. }
  194.  
  195. public function getMoneySym(){
  196. return Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
  197. }
  198.  
  199. public function log($prefix, $dump=true, $midfix='', $filename="mage.log"){
  200. if($dump){
  201. Mage::log($this->dump($prefix), $midfix, $filename);
  202. }
  203. else{
  204. Mage::log($prefix, $midfix, $filename);
  205. }
  206. }
  207.  
  208. public function dump($var, $label=null, $echo = false){
  209. if($echo){
  210. Zend_Debug::dump($var, $label, $echo);
  211. }
  212. else{
  213. return Zend_Debug::dump($var, $label, $echo);
  214. }
  215. }
  216.  
  217. public function getAttrByCode($attrCode){
  218. $attr_model = Mage::getModel('catalog/resource_eav_attribute');
  219. return $attr_model->loadByCode('catalog_product', $attrCode);
  220. }
  221.  
  222. public function getSelectAttrVals($attrCode){
  223. $attribute = $this->getAttrByCode('filter_category');
  224. $attributeOptionsModel= Mage::getModel('eav/entity_attribute_source_table') ;
  225. $attributeOptionsModel->setAttribute($attribute);
  226.  
  227. $options = $attributeOptionsModel->getAllOptions(false);
  228. return $options;
  229. }
  230.  
  231. public function selectAttrValExists($attrCode, $value){
  232.  
  233. foreach($this->getSelectAttrVals($attrCode) as $option){
  234. if ($option['label'] == $value){
  235. return $option;
  236. }
  237. }
  238.  
  239. return false;
  240. }
  241.  
  242. public function addSelectAttrVal($attrCode, $argValue)
  243. {
  244. $attribute_model = Mage::getModel('eav/entity_attribute');
  245. $attributeOptionsModel= Mage::getModel('eav/entity_attribute_source_table') ;
  246.  
  247. $attribute = $this->getAttrByCode($attrCode);
  248.  
  249. $attributeOptionsModel->setAttribute($attribute);
  250. $options = $attributeOptionsModel->getAllOptions(false);
  251.  
  252. if(!$this->selectAttrValExists($attrCode, $argValue)){
  253. $value['option'] = array($argValue,$argValue);
  254. $result = array('value' => $value);
  255. $attribute->setData('option',$result);
  256. $attribute->save();
  257. }
  258.  
  259. foreach($options as $option){
  260. if ($option['label'] == $argValue){
  261. return $option;
  262. }
  263. }
  264. return true;
  265. }
  266.  
  267. public function getCatById($catid){
  268. return Mage::getModel('catalog/category')->load($catid);
  269. }
  270.  
  271. public function getProdCats($prod){
  272. $categories = array();
  273. $catids = $prod->getCategoryIds();
  274. foreach ($catids as $category_id){
  275. array_push($categories, Mage::getModel('catalog/category')->load($category_id));
  276. }
  277. return $categories;
  278. }
  279.  
  280. public function getSelectAttrOptionId($attrCode,$optionLabel){
  281. $productModel = Mage::getModel('catalog/product');
  282. $attr = $productModel->getResource()->getAttribute($attrCode);
  283. if ($attr->usesSource()) {
  284. return $attr->getSource()->getOptionId($optionLabel);
  285. }
  286. else{
  287. return null;
  288. }
  289. }
  290.  
  291. public function setProdSelectAttrOptions($product,$attrCode,$values = array()){
  292. $existSelection = $this->getSelectedMultiSelectOptions($product,$attrCode,true);
  293. $valuesWant = array();
  294. $startcommit = false;
  295.  
  296. foreach($values as $value){
  297. array_push($valuesWant,(int)$this->getSelectAttrOptionId($attrCode,$value));
  298. }
  299.  
  300. if(!is_array($valuesWant) || !is_array($existSelection)){
  301. return null;
  302. }
  303.  
  304. $arrdiff1 = array_diff($valuesWant,$existSelection);
  305. $arrdiff2 = array_diff($existSelection,$valuesWant);
  306.  
  307. if(count($arrdiff1) != 0 || count($arrdiff2) != 0){
  308. $startcommit = true;
  309. }
  310.  
  311. if($startcommit){
  312. if(count($valuesWant) == 0){
  313. $product->setData($attrCode,array());
  314. }
  315. else{
  316. $product->setData($attrCode,$valuesWant);
  317. }
  318. $product->save();
  319. }
  320. else{
  321. return null;
  322. }
  323.  
  324. }
  325.  
  326. public function getSelectedMultiSelectOptions($product, $attrCode,$returnIdsOnly = false){
  327. $selectedIds = $product->getData($attrCode);
  328. if($returnIdsOnly){
  329. return explode(',',$selectedIds);
  330. }
  331. $options = array();
  332. foreach($this->getSelectAttrVals($attrCode) as $attr){
  333. if(in_array((int)$attr['value'],explode(',',$selectedIds))){
  334. array_push($options,$attr);
  335. }
  336. }
  337. return $options;
  338. }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement