PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Vilpjsc/Brand/Helper/Data.php

https://bitbucket.org/acidel/buykoala
PHP | 125 lines | 103 code | 10 blank | 12 comment | 31 complexity | 34cdb1d6fe4e87928e381b1eb5e3c248 MD5 | raw file
  1. <?php
  2. /**
  3. * Vilpjsc Brand
  4. *
  5. * @category Vilpjsc
  6. * @package Vilpjsc_Brand
  7. * @author An <paul.aan@facebook.com>
  8. */
  9. class Vilpjsc_Brand_Helper_Data extends Mage_Core_Helper_Abstract {
  10. public function getBrandofProduct($_product) {
  11. $attribute = $_product->getResource()->getAttribute("manufacturer");
  12. if ($attribute->usesSource()) {
  13. $options = $attribute->getSource()->getAllOptions(false);
  14. if (is_numeric($options[0]['value']))
  15. $brand = Mage::getModel("brand/brand")->load($options[0]['value'], 'manufacturer_option_id');
  16. if ($brand != NULL)
  17. return $brand;
  18. }
  19. }
  20. public function getbrandConfig() {
  21. $small['width'] = Mage::getStoreConfig('vilpbrand/small/width');
  22. $small['height'] = Mage::getStoreConfig('vilpbrand/small/height');
  23. $medium['width'] = Mage::getStoreConfig('vilpbrand/medium/width');
  24. $medium['height'] = Mage::getStoreConfig('vilpbrand/medium/height');
  25. $large['width'] = Mage::getStoreConfig('vilpbrand/large/width');
  26. $large['height'] = Mage::getStoreConfig('vilpbrand/large/height');
  27. return array('small' => $small, 'medium' => $medium, 'large' => $large);
  28. }
  29. public function deleteBrandImage($brand) {
  30. $title = strtolower($brand->getTitle());
  31. $subFolder = strtolower((string) $brand->getId() . $title);
  32. $length = strlen($subFolder);
  33. $path = Mage::getBaseDir('media') . DS . 'brand' . DS;
  34. if ($length == 1) {
  35. $firstFolder = $subFolder[0];
  36. } else {
  37. $firstFolder = $subFolder[0] . DS . $subFolder[1];
  38. }
  39. foreach ($this->getbrandConfig() as $size) {
  40. if (file_exists($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.jpg') ||
  41. file_exists($path . $firstFolder . DS . $brand->getId().$title . '.jpg')) {
  42. unlink($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.jpg');
  43. unlink($path . $firstFolder . DS . $brand->getId().$title . '.jpg');
  44. } else if (file_exists($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.png') ||
  45. file_exists($path . $firstFolder . DS . $brand->getId().$title . '.png')) {
  46. unlink($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.png');
  47. unlink($path . $firstFolder . DS . $brand->getId().$title . '.png');
  48. } else if (file_exists($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.jpeg') ||
  49. file_exists($path . $firstFolder . DS . $brand->getId().$title . '.jpeg')) {
  50. unlink($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.jpeg');
  51. unlink($path . $firstFolder . DS . $brand->getId().$title . '.jpeg');
  52. } else if (file_exists($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.bmp') ||
  53. file_exists($path . $firstFolder . DS . $brand->getId().$title . '.bmp')) {
  54. unlink($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.bmp');
  55. unlink($path . $firstFolder . DS . $brand->getId().$title . '.bmp');
  56. } else if(file_exists($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.gif') ||
  57. file_exists($path . $firstFolder . DS . $brand->getId().$title . '.gif')){
  58. unlink($path . $firstFolder . DS . $brand->getId().$title . '_' . $size['width'] . '_' . $size['height'] . '.gif');
  59. unlink($path . $firstFolder . DS . $brand->getId().$title . '.gif');
  60. }
  61. }
  62. }
  63. public function getBrandImage($brand, $size = "small") {
  64. // here we change default DS parameter with manual '/' data seprator.
  65. $config = $this->getbrandConfig();
  66. $title = strtolower($brand->getTitle());
  67. // there are 3sizes: small, medium, large
  68. $size = $config[$size];
  69. $w = $size['width'];
  70. $h = $size['height'];
  71. $subFolder = (string) $brand->getId() . $title;
  72. $length = strlen($subFolder);
  73. $path = Mage::getBaseDir('media') . '/' . 'brand' . '/';
  74. if ($length == 1) {
  75. $firstFolder = $subFolder[0];
  76. } else {
  77. $firstFolder = $subFolder[0] . '/' . $subFolder[1];
  78. }
  79. $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)
  80. . 'brand' . '/' . $firstFolder . '/' . $brand->getId().$title;
  81. if (file_exists($path . $firstFolder . '/' . $brand->getId() .$title. '.jpg')) {
  82. $original = $path . $firstFolder . '/' . $brand->getId() .$title. '.jpg';
  83. $cropImage = $path . $firstFolder . '/' . $brand->getId() .$title. '_' . $w . '_' . $h . '.jpg';
  84. $image = $url . '_' . $w . '_' . $h . '.jpg';
  85. } else if (file_exists($path . $firstFolder . '/' . $brand->getId() .$title. '.png')) {
  86. $original = $path . $firstFolder . '/' . $brand->getId() .$title. '.png';
  87. $cropImage = $path . $firstFolder . '/' . $brand->getId() .$title. '_' . $w . '_' . $h . '.png';
  88. $image = $url . '_' . $w . '_' . $h . '.png';
  89. } else if (file_exists($path . $firstFolder . '/' . $brand->getId() .$title. '.jpeg')) {
  90. $original = $path . $firstFolder . '/' . $brand->getId() .$title. '.jpeg';
  91. $cropImage = $path . $firstFolder . '/' . $brand->getId() .$title. '_' . $w . '_' . $h . '.jpeg';
  92. $image = $url . '_' . $w . '_' . $h . '.jpeg';
  93. } else if (file_exists($path . $firstFolder . '/' . $brand->getId() .$title. '.bmp')) {
  94. $original = $path . $firstFolder . '/' . $brand->getId() .$title. '.bmp';
  95. $cropImage = $path . $firstFolder . '/' . $brand->getId() .$title. '_' . $w . '_' . $h . '.bmp';
  96. $image = $url . '_' . $w . '_' . $h . '.bmp';
  97. } else {
  98. $original = $path . $firstFolder . '/' . $brand->getId() .$title. '.gif';
  99. $cropImage = $path . $firstFolder . '/' . $brand->getId() .$title. '_' . $w . '_' . $h . '.gif';
  100. $image = $url . '_' . $w . '_' . $h . '.gif';
  101. }
  102. if (@file_exists($cropImage)) {
  103. return $image;
  104. } else {
  105. $resizeObj = Mage::getModel('brand/resizeimage');
  106. $resizeObj->setFile($original);
  107. // *** 1) Initialise / load image
  108. // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
  109. $resizeObj->resizeImage($w, $h, 'crop');
  110. // *** 3) Save image
  111. $resizeObj->saveImage($cropImage, 100);
  112. return $image;
  113. }
  114. }
  115. }