BxBaseVoteServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $aResult = $oVote->vote($aParams);
  46. return (int)$aResult['code'] != 0 ? $aResult : $aResult['api'];
  47. }
  48. /**
  49. * @page service Service Calls
  50. * @section bx_system_general System Services
  51. * @subsection bx_system_general-vote Vote
  52. * @subsubsection bx_system_general-get_performed_by get_performed_by
  53. *
  54. * @code bx_srv('system', 'get_performed_by', [[...]], 'TemplVoteServices'); @endcode
  55. * @code {{~system:get_performed_by:TemplVoteServices[[...]]~}} @endcode
  56. *
  57. * Gets a list of PerformedBy users.
  58. * @param $aParams an array with necessary parameters
  59. *
  60. * @see BxBaseVoteServices::serviceGetPerformedBy
  61. */
  62. /**
  63. * @ref bx_system_general-get_performed_by "get_performed_by"
  64. * @api @ref bx_system_general-get_performed_by "get_performed_by"
  65. */
  66. public function serviceGetPerformedBy($aParams)
  67. {
  68. if(is_string($aParams))
  69. $aParams = json_decode($aParams, true);
  70. if(!$aParams['s'] || !$aParams['o'])
  71. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  72. $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
  73. if(!$oVote || !$oVote->isEnabled())
  74. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  75. $sMethod = '_serviceGetPerformedBy' . bx_gen_method_name($oVote->getType());
  76. if(!method_exists($this, $sMethod))
  77. return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
  78. return $this->$sMethod($aParams, $oVote);
  79. }
  80. protected function _serviceGetPerformedByLikes($aParams, &$oVote)
  81. {
  82. $aValues = $oVote->getQueryObject()->getPerformedBy($oVote->getId());
  83. $aTmplUsers = [];
  84. foreach($aValues as $mValue) {
  85. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  86. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  87. }
  88. return [
  89. 'performed_by' => $aTmplUsers
  90. ];
  91. }
  92. protected function _serviceGetPerformedByReactions($aParams, &$oVote)
  93. {
  94. $aReactions = !empty($aParams['reaction']) ? [$aParams['reaction']] : $oVote->getReactions();
  95. $aResult = [];
  96. foreach($aReactions as $sReaction) {
  97. $aValues = $oVote->getQueryObject()->getPerformed(['type' => 'by', 'object_id' => $oVote->getId(), 'reaction'=> $sReaction]);
  98. $aTmplUsers = [];
  99. foreach($aValues as $mValue) {
  100. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  101. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  102. }
  103. $aResult[$sReaction] = $aTmplUsers;
  104. }
  105. return [
  106. 'performed_by' => $aResult
  107. ];
  108. }
  109. }
  110. /** @} */