ProductAttributes.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ /VukK0Hre6WTrofQaK8GMWWb9OA4n6/hc5mCM+ioXXA=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.6
  11. */
  12. /**
  13. * product attributes view helper class
  14. */
  15. namespace Ppb\View\Helper;
  16. use Cube\View\Helper\AbstractHelper,
  17. Ppb\Service\CustomFields as CustomFieldsService;
  18. class ProductAttributes extends AbstractHelper
  19. {
  20. /**
  21. *
  22. * custom fields service
  23. *
  24. * @var \Ppb\Service\CustomFields
  25. */
  26. protected $_customFieldsService;
  27. /**
  28. *
  29. * product attributes input array
  30. *
  31. * @var array
  32. */
  33. protected $_productAttributes;
  34. /**
  35. *
  36. * get custom fields table service
  37. *
  38. * @return \Ppb\Service\CustomFields
  39. */
  40. public function getCustomFieldsService()
  41. {
  42. if (!$this->_customFieldsService instanceof CustomFieldsService) {
  43. $this->setCustomFieldsService(
  44. new CustomFieldsService());
  45. }
  46. return $this->_customFieldsService;
  47. }
  48. /**
  49. *
  50. * set custom fields table service
  51. *
  52. * @param \Ppb\Service\CustomFields $customFieldsService
  53. *
  54. * @return $this
  55. */
  56. public function setCustomFieldsService(CustomFieldsService $customFieldsService)
  57. {
  58. $this->_customFieldsService = $customFieldsService;
  59. return $this;
  60. }
  61. /**
  62. *
  63. * main helper method
  64. *
  65. * @param mixed $productAttributes
  66. *
  67. * @return $this
  68. */
  69. public function productAttributes($productAttributes = null)
  70. {
  71. if ($productAttributes !== null) {
  72. $this->setProductAttributes(
  73. $productAttributes);
  74. }
  75. return $this;
  76. }
  77. /**
  78. *
  79. * set product attributes input array
  80. *
  81. * @param mixed $productAttributes
  82. *
  83. * @return $this
  84. */
  85. public function setProductAttributes($productAttributes)
  86. {
  87. $this->_productAttributes = \Ppb\Utility::unserialize($productAttributes);
  88. return $this;
  89. }
  90. /**
  91. *
  92. * get product attributes input array
  93. *
  94. * @return array
  95. */
  96. public function getProductAttributes()
  97. {
  98. return $this->_productAttributes;
  99. }
  100. /**
  101. *
  102. * format product attributes corresponding for the sale listing for display purposes
  103. *
  104. * @param string $separator
  105. *
  106. * @return string|null
  107. */
  108. public function display($separator = '; ')
  109. {
  110. $productAttributes = $this->getProductAttributes();
  111. if (count($productAttributes) > 0) {
  112. $output = array();
  113. $customFieldsService = $this->getCustomFieldsService();
  114. $translate = $this->getTranslate();
  115. foreach ($productAttributes as $key => $value) {
  116. $customField = $customFieldsService->findBy('id', $key);
  117. $multiOptions = \Ppb\Utility::unserialize($customField['multiOptions']);
  118. $multiOptions = array_filter(array_combine($multiOptions['key'], $multiOptions['value']));
  119. $multiOptionsValue = (isset($multiOptions[$value])) ? $multiOptions[$value] : 'N/A';
  120. $output[] = $translate->_($customField['label']) . ': ' . $translate->_($multiOptionsValue);
  121. }
  122. return implode($separator, $output);
  123. }
  124. return null;
  125. }
  126. }