1
0

BxBaseVoteServices.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /**
  19. * @page service Service Calls
  20. * @section bx_system_general System Services
  21. * @subsection bx_system_general-vote Vote
  22. * @subsubsection bx_system_general-do do
  23. *
  24. * @code bx_srv('system', 'do', [[...]], 'TemplVoteServices'); @endcode
  25. * @code {{~system:do:TemplVoteServices[[...]]~}} @endcode
  26. *
  27. * Performs Do (Vote) action
  28. * @param $aParams an array with necessary parameters
  29. *
  30. * @see BxBaseVoteServices::serviceDo
  31. */
  32. /**
  33. * @ref bx_system_general-do "do"
  34. * @api @ref bx_system_general-do "do"
  35. */
  36. public function serviceDo($aParams)
  37. {
  38. if(is_string($aParams))
  39. $aParams = json_decode($aParams, true);
  40. if(!$aParams['s'] || !$aParams['o'])
  41. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  42. $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
  43. if(!$oVote || !$oVote->isEnabled())
  44. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  45. $sMethod = '_serviceDo' . bx_gen_method_name($oVote->getType());
  46. if(!method_exists($this, $sMethod))
  47. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  48. return $this->$sMethod($aParams, $oVote);
  49. }
  50. protected function _serviceDoLikes($aParams, &$oVote)
  51. {
  52. $aResult = $oVote->vote($aParams);
  53. if((int)$aResult['code'] != 0)
  54. return $aResult;
  55. return [
  56. 'is_voted' => $aResult['voted'],
  57. 'is_disabled' => $aResult['disabled'],
  58. 'icon' => $aResult['label_emoji'],
  59. 'title' => $aResult['label_title'],
  60. 'counter' => $oVote->getVote()
  61. ];
  62. }
  63. protected function _serviceDoReactions($aParams, &$oVote)
  64. {
  65. $aResult = $oVote->vote($aParams);
  66. if((int)$aResult['code'] != 0)
  67. return $aResult;
  68. $sDefault = $oVote->getDefault();
  69. $aDefault = $oVote->getReaction($sDefault);
  70. $aDefaultInfo = $oVote->getReaction($aDefault['name']);
  71. return [
  72. 'is_voted' => $aResult['voted'],
  73. 'is_disabled' => $aResult['disabled'],
  74. 'reaction' => $aResult['voted'] ? $aResult['reaction'] : $sDefault,
  75. 'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
  76. 'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
  77. 'counter' => $oVote->getVote()
  78. ];
  79. }
  80. /**
  81. * @page service Service Calls
  82. * @section bx_system_general System Services
  83. * @subsection bx_system_general-vote Vote
  84. * @subsubsection bx_system_general-get_performed_by get_performed_by
  85. *
  86. * @code bx_srv('system', 'get_performed_by', [[...]], 'TemplVoteServices'); @endcode
  87. * @code {{~system:get_performed_by:TemplVoteServices[[...]]~}} @endcode
  88. *
  89. * Gets a list of PerformedBy users.
  90. * @param $aParams an array with necessary parameters
  91. *
  92. * @see BxBaseVoteServices::serviceGetPerformedBy
  93. */
  94. /**
  95. * @ref bx_system_general-get_performed_by "get_performed_by"
  96. * @api @ref bx_system_general-get_performed_by "get_performed_by"
  97. */
  98. public function serviceGetPerformedBy($aParams)
  99. {
  100. if(is_string($aParams))
  101. $aParams = json_decode($aParams, true);
  102. if(!$aParams['s'] || !$aParams['o'])
  103. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  104. $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
  105. if(!$oVote || !$oVote->isEnabled())
  106. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  107. $sMethod = '_serviceGetPerformedBy' . bx_gen_method_name($oVote->getType());
  108. if(!method_exists($this, $sMethod))
  109. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  110. return $this->$sMethod($aParams, $oVote);
  111. }
  112. protected function _serviceGetPerformedByLikes($aParams, &$oVote)
  113. {
  114. $aValues = $oVote->getQueryObject()->getPerformedBy($oVote->getId());
  115. $aTmplUsers = [];
  116. foreach($aValues as $mValue) {
  117. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  118. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  119. }
  120. return [
  121. 'performed_by' => $aTmplUsers
  122. ];
  123. }
  124. protected function _serviceGetPerformedByReactions($aParams, &$oVote)
  125. {
  126. $aReactions = !empty($aParams['reaction']) ? [$aParams['reaction']] : $oVote->getReactions();
  127. $aResult = [];
  128. foreach($aReactions as $sReaction) {
  129. $aValues = $oVote->getQueryObject()->getPerformed(['type' => 'by', 'object_id' => $oVote->getId(), 'reaction'=> $sReaction]);
  130. $aTmplUsers = [];
  131. foreach($aValues as $mValue) {
  132. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  133. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  134. }
  135. $aResult[$sReaction] = $aTmplUsers;
  136. }
  137. return [
  138. 'performed_by' => $aResult
  139. ];
  140. }
  141. }
  142. /** @} */