BxReviewsFormEntry.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Reviews Reviews
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxReviewsFormEntry extends BxBaseModTextFormEntry
  15. {
  16. protected $_sGhostTemplateCover = 'form_ghost_template_cover.html';
  17. public function __construct($aInfo, $oTemplate = false)
  18. {
  19. $this->MODULE = 'bx_reviews';
  20. parent::__construct($aInfo, $oTemplate);
  21. $CNF = &$this->_oModule->_oConfig->CNF;
  22. if ($this->_oModule->_oDb->getParam($CNF['PARAM_CONTEXT_CONTROL_ENABLE'])) {
  23. unset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]);
  24. } else {
  25. unset($this->aInputs[$CNF['FIELD_REVIEWED_PROFILE']]);
  26. }
  27. }
  28. public function insert ($aValsToAdd = [], $isIgnore = false)
  29. {
  30. $CNF = &$this->_oModule->_oConfig->CNF;
  31. if (($iReviewFor = $this->safeCustomPostToContext()))
  32. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $iReviewFor;
  33. $this->calcAvgVoting($aValsToAdd);
  34. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  35. return parent::insert ($aValsToAdd, $isIgnore);
  36. }
  37. public function update ($iContentId, $aValsToAdd = [], &$aTrackTextFieldsChanges = null)
  38. {
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. if (($iReviewFor = $this->safeCustomPostToContext()))
  41. $aValsToAdd[$CNF['FIELD_ALLOW_VIEW_TO']] = $iReviewFor;
  42. $this->calcAvgVoting($aValsToAdd);
  43. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  44. }
  45. protected function calcAvgVoting(&$aValsToAdd) {
  46. $CNF = &$this->_oModule->_oConfig->CNF;
  47. if (isset($this->aInputs[$CNF['FIELD_VOTING_OPTIONS']]) && !bx_is_empty_array($this->aInputs[$CNF['FIELD_VOTING_OPTIONS']]['value'])) {
  48. $iCount = 0;
  49. $iSum = 0;
  50. foreach ($this->aInputs[$CNF['FIELD_VOTING_OPTIONS']]['value'] as $iRating) {
  51. if ($iRating) {
  52. $iSum += $iRating;
  53. $iCount++;
  54. }
  55. }
  56. if (!$iCount) return '';
  57. $fRate = $iSum/$iCount;
  58. //need to scale it to 5 stars only
  59. $iMaxValue = $this->_oModule->_oDb->getParam($CNF['PARAM_MAX_STARS']);
  60. $fRate /= $iMaxValue / 5;
  61. $aValsToAdd[$CNF['FIELD_VOTING_AVG']] = $fRate;
  62. }
  63. }
  64. protected function safeCustomPostToContext() {
  65. $CNF = &$this->_oModule->_oConfig->CNF;
  66. if ($this->_oModule->_oDb->getParam($CNF['PARAM_CONTEXT_CONTROL_ENABLE'])) {
  67. $mixedFieldValue = $this->getCleanValue($CNF['FIELD_REVIEWED_PROFILE']);
  68. $iProfile = is_array($mixedFieldValue) ? intval($mixedFieldValue[0]) : intval($mixedFieldValue);
  69. if (!$iProfile)
  70. return BX_DOL_PG_ALL;
  71. $oProfile = BxDolProfile::getInstance($iProfile);
  72. $sModules = $this->_oModule->_oDb->getParam($CNF['PARAM_CONTEXT_MODULES_AVAILABLE']);
  73. $aModulesList = explode(',', $sModules);
  74. if (!in_array($oProfile->getModule(), $aModulesList))
  75. return BX_DOL_PG_ALL;
  76. return -$iProfile;
  77. } else return false;
  78. }
  79. protected function genCustomInputVotingOptions ($aInput)
  80. {
  81. return $this->_oModule->_oTemplate->getMultiVoting(isset($aInput['value']) ? $aInput['value'] : array(), true);
  82. }
  83. protected function genCustomInputReviewedProfile ($aInput)
  84. {
  85. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'suggest_profile_for_review/';
  86. $aInput['custom']['only_once'] = true;
  87. if (isset($aInput['value']) && !is_array($aInput['value'])) $aInput['value'] = [$aInput['value']];
  88. return $this->genCustomInputUsernamesSuggestions($aInput);
  89. }
  90. protected function genCustomViewRowValueProduct($aInput) {
  91. if (empty($aInput['value']))
  92. return null;
  93. $sProduct = bx_process_output($aInput['value']);
  94. $sProductUrlEncoded = $aInput['value'];
  95. $sProductSearchUrl = BX_DOL_URL_ROOT . $this->_oModule->_oConfig->CNF['URI_SEARCH_PRODUCT'];
  96. $sProductSearchUrl = bx_append_url_params($sProductSearchUrl, ['keyword' => $sProductUrlEncoded]);
  97. return '<a href="'.$sProductSearchUrl.'">'.$sProduct.'</a>';
  98. }
  99. protected function genCustomViewRowValueReviewedProfile($aInput) {
  100. $oProfile = BxDolProfile::getInstance($aInput['value']);
  101. if (!$oProfile)
  102. return null;
  103. return '<a href="'.$oProfile->getUrl().'">'.$oProfile->getDisplayName().'</a>';
  104. }
  105. }
  106. class BxReviewsFormEntryCheckerHelper extends BxDolFormCheckerHelper {
  107. public function passVotingOptions ($s) {
  108. if (!is_array($s))
  109. return false;
  110. return serialize($s);
  111. }
  112. public function passOneIntArray ($s) {
  113. if (is_array($s)) return intval($s[0]);
  114. else return intval($s);
  115. }
  116. public function displayVotingOptions ($s) {
  117. return unserialize($s);
  118. }
  119. }
  120. /** @} */