BxBaseVoteServices.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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)
  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. $aDefault = $oVote->getReaction($oVote->getDefault());
  69. $aDefaultInfo = $oVote->getReaction($aDefault['name']);
  70. return [
  71. 'is_voted' => $aResult['voted'],
  72. 'is_disabled' => $aResult['disabled'],
  73. 'reaction' => $aResult['reaction'],
  74. 'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : $aDefaultInfo['emoji'],
  75. 'title' => !empty($aResult['label_title']) ? $aResult['label_title'] : '',
  76. 'counter' => $oVote->getVote()
  77. ];
  78. }
  79. /**
  80. * @page service Service Calls
  81. * @section bx_system_general System Services
  82. * @subsection bx_system_general-vote Vote
  83. * @subsubsection bx_system_general-get_performed_by get_performed_by
  84. *
  85. * @code bx_srv('system', 'get_performed_by', [[...]], 'TemplVoteServices'); @endcode
  86. * @code {{~system:get_performed_by:TemplVoteServices[[...]]~}} @endcode
  87. *
  88. * Gets a list of PerformedBy users.
  89. * @param $aParams an array with necessary parameters
  90. *
  91. * @see BxBaseVoteServices::serviceGetPerformedBy
  92. */
  93. /**
  94. * @ref bx_system_general-get_performed_by "get_performed_by"
  95. * @api @ref bx_system_general-get_performed_by "get_performed_by"
  96. */
  97. public function serviceGetPerformedBy($aParams)
  98. {
  99. if(is_string($aParams))
  100. $aParams = json_decode($aParams, true);
  101. if(!$aParams['s'] || !$aParams['o'])
  102. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  103. $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
  104. if(!$oVote)
  105. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  106. $sMethod = '_serviceGetPerformedBy' . bx_gen_method_name($oVote->getType());
  107. if(!method_exists($this, $sMethod))
  108. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  109. return $this->$sMethod($aParams, $oVote);
  110. }
  111. protected function _serviceGetPerformedByLikes($aParams, &$oVote)
  112. {
  113. $aValues = $oVote->getQueryObject()->getPerformedBy($oVote->getId());
  114. $aTmplUsers = [];
  115. foreach($aValues as $mValue) {
  116. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  117. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  118. }
  119. return [
  120. 'performed_by' => $aTmplUsers
  121. ];
  122. }
  123. protected function _serviceGetPerformedByReactions($aParams, &$oVote)
  124. {
  125. $aReactions = !empty($aParams['reaction']) ? [$aParams['reaction']] : $oVote->getReactions();
  126. $aResult = [];
  127. foreach($aReactions as $sReaction) {
  128. $aValues = $oVote->getQueryObject()->getPerformed(['type' => 'by', 'object_id' => $oVote->getId(), 'reaction'=> $sReaction]);
  129. $aTmplUsers = [];
  130. foreach($aValues as $mValue) {
  131. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  132. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  133. }
  134. $aResult[$sReaction] = $aTmplUsers;
  135. }
  136. return [
  137. 'performed_by' => $aResult
  138. ];
  139. }
  140. }
  141. /** @} */