BxBaseVoteServices.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System services related to Votes.
  11. */
  12. class BxBaseVoteServices extends BxDol
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function serviceDo($aParams)
  19. {
  20. if(is_string($aParams))
  21. $aParams = json_decode($aParams, true);
  22. if(!$aParams['s'] || !$aParams['o'])
  23. return ['code' => 1];
  24. $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
  25. if(!$oVote)
  26. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  27. $sMethod = 'serviceDo' . bx_gen_method_name($oVote->getType());
  28. if(method_exists($this, $sMethod))
  29. return $this->$sMethod($aParams, $oVote);
  30. $aResult = $oVote->vote($aParams);
  31. if((int)$aResult['code'] != 0)
  32. return $aResult;
  33. return [
  34. 'is_voted' => $aResult['voted'],
  35. 'is_disabled' => $aResult['disabled'],
  36. 'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
  37. 'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
  38. 'counter' => $oVote->getVote()
  39. ];
  40. }
  41. public function serviceDoReactions($aParams, &$oVote)
  42. {
  43. $aResult = $oVote->vote($aParams);
  44. if((int)$aResult['code'] != 0)
  45. return $aResult;
  46. $aDefault = $oVote->getReaction($oVote->getDefault());
  47. $aDefaultInfo = $oVote->getReaction($aDefault['name']);
  48. return [
  49. 'is_voted' => $aResult['voted'],
  50. 'is_disabled' => $aResult['disabled'],
  51. 'reaction' => $aResult['reaction'],
  52. 'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
  53. 'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
  54. 'counter' => $oVote->getVote()
  55. ];
  56. }
  57. }
  58. /** @} */