PageRenderTime 66ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/acidel/buykoala
PHP | 124 lines | 103 code | 10 blank | 11 comment | 31 complexity | c6b318c0b1b6bd1d3b023c6d09bc29a5 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. $config = $this->getbrandConfig();
  65. $title = strtolower($brand->getTitle());
  66. // there are 3sizes: small, medium, large
  67. $size = $config[$size];
  68. $w = $size['width'];
  69. $h = $size['height'];
  70. $subFolder = (string) $brand->getId() . $title;
  71. $length = strlen($subFolder);
  72. $path = Mage::getBaseDir('media') . DS . 'brand' . DS;
  73. if ($length == 1) {
  74. $firstFolder = $subFolder[0];
  75. } else {
  76. $firstFolder = $subFolder[0] . DS . $subFolder[1];
  77. }
  78. $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)
  79. . 'brand' . DS . $firstFolder . DS . $brand->getId().$title;
  80. if (file_exists($path . $firstFolder . DS . $brand->getId() .$title. '.jpg')) {
  81. $original = $path . $firstFolder . DS . $brand->getId() .$title. '.jpg';
  82. $cropImage = $path . $firstFolder . DS . $brand->getId() .$title. '_' . $w . '_' . $h . '.jpg';
  83. $image = $url . '_' . $w . '_' . $h . '.jpg';
  84. } else if (file_exists($path . $firstFolder . DS . $brand->getId() .$title. '.png')) {
  85. $original = $path . $firstFolder . DS . $brand->getId() .$title. '.png';
  86. $cropImage = $path . $firstFolder . DS . $brand->getId() .$title. '_' . $w . '_' . $h . '.png';
  87. $image = $url . '_' . $w . '_' . $h . '.png';
  88. } else if (file_exists($path . $firstFolder . DS . $brand->getId() .$title. '.jpeg')) {
  89. $original = $path . $firstFolder . DS . $brand->getId() .$title. '.jpeg';
  90. $cropImage = $path . $firstFolder . DS . $brand->getId() .$title. '_' . $w . '_' . $h . '.jpeg';
  91. $image = $url . '_' . $w . '_' . $h . '.jpeg';
  92. } else if (file_exists($path . $firstFolder . DS . $brand->getId() .$title. '.bmp')) {
  93. $original = $path . $firstFolder . DS . $brand->getId() .$title. '.bmp';
  94. $cropImage = $path . $firstFolder . DS . $brand->getId() .$title. '_' . $w . '_' . $h . '.bmp';
  95. $image = $url . '_' . $w . '_' . $h . '.bmp';
  96. } else {
  97. $original = $path . $firstFolder . DS . $brand->getId() .$title. '.gif';
  98. $cropImage = $path . $firstFolder . DS . $brand->getId() .$title. '_' . $w . '_' . $h . '.gif';
  99. $image = $url . '_' . $w . '_' . $h . '.gif';
  100. }
  101. if (@file_exists($cropImage)) {
  102. return $image;
  103. } else {
  104. $resizeObj = Mage::getModel('brand/resizeimage');
  105. $resizeObj->setFile($original);
  106. // *** 1) Initialise / load image
  107. // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
  108. $resizeObj->resizeImage($w, $h, 'crop');
  109. // *** 3) Save image
  110. $resizeObj->saveImage($cropImage, 100);
  111. return $image;
  112. }
  113. }
  114. }