BxBaseVoteServices.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  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 ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  30. return $this->$sMethod($aParams, $oVote);
  31. }
  32. protected function _serviceDoLikes($aParams, &$oVote)
  33. {
  34. $aResult = $oVote->vote($aParams);
  35. if((int)$aResult['code'] != 0)
  36. return $aResult;
  37. return [
  38. 'is_voted' => $aResult['voted'],
  39. 'is_disabled' => $aResult['disabled'],
  40. 'icon' => $aResult['label_emoji'],
  41. 'title' => $aResult['label_title'],
  42. 'counter' => $oVote->getVote()
  43. ];
  44. }
  45. protected function _serviceDoReactions($aParams, &$oVote)
  46. {
  47. $aResult = $oVote->vote($aParams);
  48. if((int)$aResult['code'] != 0)
  49. return $aResult;
  50. $aDefault = $oVote->getReaction($oVote->getDefault());
  51. $aDefaultInfo = $oVote->getReaction($aDefault['name']);
  52. return [
  53. 'is_voted' => $aResult['voted'],
  54. 'is_disabled' => $aResult['disabled'],
  55. 'reaction' => $aResult['reaction'],
  56. 'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
  57. 'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
  58. 'counter' => $oVote->getVote()
  59. ];
  60. }
  61. public function serviceGetPerformedBy($aParams)
  62. {
  63. if(is_string($aParams))
  64. $aParams = json_decode($aParams, true);
  65. if(!$aParams['s'] || !$aParams['o'])
  66. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  67. $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
  68. if(!$oVote)
  69. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  70. $sMethod = '_serviceGetPerformedBy' . bx_gen_method_name($oVote->getType());
  71. if(!method_exists($this, $sMethod))
  72. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  73. return $this->$sMethod($aParams, $oVote);
  74. }
  75. protected function _serviceGetPerformedByLikes($aParams, &$oVote)
  76. {
  77. $aValues = $oVote->getQueryObject()->getPerformedBy($oVote->getId());
  78. $aTmplUsers = [];
  79. foreach($aValues as $mValue) {
  80. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  81. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  82. }
  83. return [
  84. 'performed_by' => $aTmplUsers
  85. ];
  86. }
  87. protected function _serviceGetPerformedByReactions($aParams, &$oVote)
  88. {
  89. $sReaction = $aParams['reaction'];
  90. $aValues = $oVote->getQueryObject()->getPerformed(['type' => 'by', 'object_id' => $oVote->getId(), 'reaction'=> $sReaction]);
  91. $aTmplUsers = [];
  92. foreach($aValues as $mValue) {
  93. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  94. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  95. }
  96. return [
  97. 'performed_by' => [
  98. $sReaction => $aTmplUsers
  99. ]
  100. ];
  101. }
  102. }
  103. /** @} */