BxMessengerJotReactions.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Market Market
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxMessengerJotReactions extends BxTemplVoteReactions
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. function __construct($sSystem, $iId, $iInit = 1)
  16. {
  17. $this->_sModule = 'bx_messenger';
  18. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  19. parent::__construct($sSystem, $iId, $iInit);
  20. $this->_oQuery = &$this->_oModule->_oDb;
  21. }
  22. public function isAllowedVote($isPerformAction = false)
  23. {
  24. return true;
  25. }
  26. protected function _serviceDoReactions($aParams, &$oVote)
  27. {
  28. $aResult = $oVote->vote($aParams);
  29. if((int)$aResult['code'] != 0)
  30. return $aResult;
  31. $sDefault = $oVote->getDefault();
  32. $aDefault = $oVote->getReaction($sDefault);
  33. $aDefaultInfo = $oVote->getReaction($aDefault['name']);
  34. return [
  35. 'is_voted' => $aResult['voted'],
  36. 'is_disabled' => $aResult['disabled'],
  37. 'reaction' => $aResult['voted'] ? $aResult['reaction'] : $sDefault,
  38. 'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
  39. 'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
  40. 'counter' => $oVote->getVote()
  41. ];
  42. }
  43. public function vote($aVoteData = [], $aRequestParamsData = [])
  44. {
  45. $iObjectId = $this->getId();
  46. $iAuthorId = $this->_getAuthorId();
  47. $iAuthorIp = $this->_getAuthorIp();
  48. $bUndo = $this->isUndo();
  49. $bVoted = $this->_oQuery->isPerformed($iObjectId, $iAuthorId, $aVoteData['reaction']);
  50. $bPerformUndo = $bVoted && $bUndo;
  51. if(!$bPerformUndo && !$this->isAllowedVote())
  52. return ['code' => BX_DOL_OBJECT_ERR_ACCESS_DENIED, 'message' => $this->msgErrAllowedVote()];
  53. $iId = $this->_putVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, $bPerformUndo);
  54. if($iId === false)
  55. return ['code' => BX_DOL_OBJECT_ERR_CANNOT_PERFORM];
  56. if(!$bPerformUndo)
  57. $this->isAllowedVote(true);
  58. return $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);
  59. }
  60. }
  61. /** @} */