BxAdsMenuView.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Ads Ads
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * View entry menu
  13. */
  14. class BxAdsMenuView extends BxBaseModTextMenuView
  15. {
  16. /**
  17. * Array with check_sum => JS_code pairs of all JS codes
  18. * which should be added to the page.
  19. */
  20. protected $_aJsCodes;
  21. public function __construct($aObject, $oTemplate = false)
  22. {
  23. $this->MODULE = 'bx_ads';
  24. parent::__construct($aObject, $oTemplate);
  25. $this->_aJsCodes = array();
  26. $this->addMarkers(array(
  27. 'js_object' => $this->_oModule->_oConfig->getJsObject('entry')
  28. ));
  29. }
  30. public function getCode ()
  31. {
  32. return parent::getCode() . $this->getJsCode();
  33. }
  34. public function getJsCode()
  35. {
  36. if(empty($this->_aJsCodes) || !is_array($this->_aJsCodes))
  37. return '';
  38. return implode('', $this->_aJsCodes);
  39. }
  40. protected function _isVisible ($a)
  41. {
  42. $CNF = &$this->_oModule->_oConfig->CNF;
  43. if(!parent::_isVisible($a))
  44. return false;
  45. $oPayment = BxDolPayments::getInstance();
  46. $bResult = false;
  47. switch ($a['name']) {
  48. case 'add-to-cart':
  49. if((float)$this->_aContentInfo[$CNF['FIELD_PRICE']] == 0 || (int)$this->_aContentInfo[$CNF['FIELD_QUANTITY']] <= 0)
  50. break;
  51. $aCommodity = $this->_oModule->_oDb->getCommodity([
  52. 'sample' => 'entry_id',
  53. 'entry_id' => $this->_aContentInfo[$CNF['FIELD_ID']],
  54. 'type' => BX_ADS_COMMODITY_TYPE_PRODUCT,
  55. 'latest' => true
  56. ]);
  57. if(empty($aCommodity) || !is_array($aCommodity))
  58. break;
  59. $iAuthorId = (int)$this->_aContentInfo[$CNF['FIELD_AUTHOR']];
  60. $aJs = $oPayment->getAddToCartJs($iAuthorId, $this->MODULE, $aCommodity['id'], 1, true);
  61. if(empty($aJs) || !is_array($aJs))
  62. break;
  63. list($sJsCode, $sJsMethod) = $aJs;
  64. $sJsCodeCheckSum = md5($sJsCode);
  65. if(!isset($this->_aJsCodes[$sJsCodeCheckSum]))
  66. $this->_aJsCodes[$sJsCodeCheckSum] = $sJsCode;
  67. $bResult = true;
  68. $this->addMarkers(array(
  69. 'add_to_cart_title' => _t('_bx_ads_menu_item_title_add_to_cart', BxDolPayments::getInstance()->getCurrencySign($iAuthorId), $this->_aContentInfo[$CNF['FIELD_PRICE']]),
  70. 'add_to_cart_onclick' => $sJsMethod
  71. ));
  72. break;
  73. case 'interested':
  74. $bResult = $this->_aContentInfo[$CNF['FIELD_AUTHOR']] != bx_get_logged_profile_id();
  75. break;
  76. case 'make-offer':
  77. $bResult = $this->_oModule->isAllowedMakeOffer($this->_aContentInfo);
  78. break;
  79. case 'view-offers':
  80. $bResult = $this->_oModule->isAllowedViewOffers($this->_aContentInfo);
  81. break;
  82. case 'approve':
  83. $bResult = $this->_oModule->isAllowedApprove($this->_aContentInfo);
  84. break;
  85. case 'shipped':
  86. $bResult = $this->_oModule->isAllowedMarkShipped($this->_aContentInfo);
  87. break;
  88. case 'received':
  89. $bResult = $this->_oModule->isAllowedMarkReceived($this->_aContentInfo);
  90. break;
  91. default:
  92. $bResult = true;
  93. }
  94. return $bResult;
  95. }
  96. }
  97. /** @} */