Bläddra i källkod

Action objects: Reactions.

AntonLV 3 år sedan
förälder
incheckning
954d8fb592

+ 20 - 3
inc/classes/BxDolObject.php

@@ -7,6 +7,13 @@
  * @{
  */
 
+
+define('BX_DOL_OBJECT_ERR_NOT_AVAILABLE', 1);
+define('BX_DOL_OBJECT_ERR_ACCESS_DENIED', 2);
+define('BX_DOL_OBJECT_ERR_DUPLICATE', 3);
+define('BX_DOL_OBJECT_ERR_WRONG_DATE', 4);
+define('BX_DOL_OBJECT_ERR_CANNOT_PERFORM', 5);
+
 /**
  * Base class for all "Object" classes.
  * Child classes usually represents high level programming constructions to generate ready 'objects' functionality, like Comments, Votings, Forms.
@@ -139,9 +146,19 @@ class BxDolObject extends BxDolFactory implements iBxDolReplaceable
         return $this->_oQuery->isPerformed($iObjectId, $iAuthorId);
     }
 
-	/**
-	 * Interface functions for outer usage
-	 */
+    public function getVote($iObjectId = 0, $bForceGet = false)
+    {
+        return $this->_getVote($iObjectId, $bForceGet);
+    }
+
+    public function getTrack($iObjectId, $iAuthorId)
+    {
+        return $this->_getTrack($iObjectId, $iAuthorId);
+    }
+
+    /**
+     * Interface functions for outer usage
+     */
     public function getConditions($sMainTable, $sMainField)
     {
         if(!$this->isEnabled())

+ 18 - 13
inc/classes/BxDolVote.php

@@ -207,8 +207,18 @@ class BxDolVote extends BxDolObject
     public function actionVote()
     {
         if(!$this->isEnabled())
-            return echoJson(array('code' => 1, 'message' => _t('_vote_err_not_enabled')));
+            return echoJson(['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE, 'message' => _t('_vote_err_not_enabled')]);
 
+        $aVoteData = $this->_getVoteData();
+        $aRequestParamsData = $this->_getRequestParamsData();
+        if($aVoteData === false)
+            return echoJson(['code' => BX_DOL_OBJECT_ERR_WRONG_DATE]);
+
+        return echoJson($this->vote($aVoteData, $aRequestParamsData));
+    }
+
+    public function vote($aVoteData = [], $aRequestParamsData = [])
+    {
         $iObjectId = $this->getId();
         $iObjectAuthorId = $this->getObjectAuthorId($iObjectId);
         $iAuthorId = $this->_getAuthorId();
@@ -219,29 +229,24 @@ class BxDolVote extends BxDolObject
         $bPerformUndo = $bVoted && $bUndo;
 
         if(!$bPerformUndo && !$this->isAllowedVote())
-            return echoJson(array('code' => 2, 'message' => $this->msgErrAllowedVote()));
+            return ['code' => BX_DOL_OBJECT_ERR_ACCESS_DENIED, 'message' => $this->msgErrAllowedVote()];
 
         if($this->_isDuplicate($iObjectId, $iAuthorId, $iAuthorIp, $bVoted))
-            return echoJson(array('code' => 3, 'message' => _t('_vote_err_duplicate_vote')));
-
-        $aData = $this->_getVoteData();
-        $aParams = $this->_getRequestParamsData();
-        if($aData === false)
-            return echoJson(array('code' => 4));
+            return ['code' => BX_DOL_OBJECT_ERR_DUPLICATE, 'message' => _t('_vote_err_duplicate_vote')];
 
-        $iId = $this->_putVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, $bPerformUndo);
+        $iId = $this->_putVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, $bPerformUndo);
         if($iId === false)
-            return echoJson(array('code' => 5));
+            return ['code' => BX_DOL_OBJECT_ERR_CANNOT_PERFORM];
 
         if(!$bPerformUndo)
             $this->isAllowedVote(true);
 
         $this->_trigger();
 
-        bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'doVote', $iObjectId, $iAuthorId, array_merge(array('vote_id' => $iId, 'vote_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId), $aData));
-        bx_alert('vote', ($bPerformUndo ? 'un' : '') . 'do', $iId, $iAuthorId, array_merge(array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId), $aData));
+        bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'doVote', $iObjectId, $iAuthorId, array_merge(['vote_id' => $iId, 'vote_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId], $aVoteData));
+        bx_alert('vote', ($bPerformUndo ? 'un' : '') . 'do', $iId, $iAuthorId, array_merge(['object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId], $aVoteData));
 
-        echoJson($this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aData, !$bVoted, $aParams));
+        return $this->_returnVoteData($iObjectId, $iAuthorId, $iAuthorIp, $aVoteData, !$bVoted, $aRequestParamsData);
     }
 
     public function actionGetVotedBy()

+ 1 - 1
inc/utils.inc.php

@@ -2333,7 +2333,7 @@ function bx_api_check_origins()
 {
     if (isset($_SERVER['HTTP_ORIGIN']) && parse_url($_SERVER['HTTP_ORIGIN'], PHP_URL_HOST) != parse_url(BX_DOL_URL_ROOT, PHP_URL_HOST)) {
 
-        $aAllowedOrigins = ['http://app.una.io:3000', 'http://ci.una.io:3000', 'http://localhost:3000', 'https://una.io']; // TODO: separate config
+        $aAllowedOrigins = ['http://app.una.io:3000', 'http://ci.una.io:3000', 'http://localhost:3000', 'http://single.me:3000', 'https://una.io']; // TODO: separate config
         if (!in_array($_SERVER['HTTP_ORIGIN'], $aAllowedOrigins)) {
             header('HTTP/1.0 403 Forbidden');
             echo json_encode(['status' => 403, 'error' => _t("_Access denied")]);

+ 2 - 0
template/scripts/BxBaseVote.php

@@ -212,6 +212,8 @@ class BxBaseVote extends BxDolVote
 
         return [
             'type' => $this->_sType,
+            'system' => $this->_sSystem,
+            'object_id' => $this->_iId,
             'params' => $aParams,
             'action' => $aDoVote,
             'counter' => $aCounter,

+ 44 - 0
template/scripts/BxBaseVoteServices.php

@@ -0,0 +1,44 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+/**
+ * System services related to Votes.
+ */
+class BxBaseVoteServices extends BxDol
+{
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    public function serviceDo($aParams)
+    {
+        if(is_string($aParams))
+            $aParams = json_decode($aParams, true);
+
+        if(!$aParams['s'] || !$aParams['o'])
+            return ['code' => 1];
+
+        $oVote = BxDolVote::getObjectInstance($aParams['s'], $aParams['o']);
+        if(!$oVote)
+            return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
+
+        $aResult = $oVote->vote($aParams);
+        if((int)$aResult['code'] != 0)
+            return $aResult;
+
+        return [
+            'reaction' => $aResult['reaction'],
+            'icon' => !empty($aResult['label_emoji']) ? $aResult['label_emoji'] : '',
+            'counter' => $oVote->getVote()
+        ];
+    }
+}
+
+/** @} */

+ 18 - 0
template/scripts_templ/BxTemplVoteServices.php

@@ -0,0 +1,18 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxBaseVoteServices
+ */
+class BxTemplVoteServices extends BxBaseVoteServices
+{
+    
+}
+
+/** @} */