Răsfoiți Sursa

Ticket #4558 - Build-in API support: Features.

AntonLV 2 ani în urmă
părinte
comite
86eb08bfc2

+ 27 - 11
inc/classes/BxDolFeature.php

@@ -36,8 +36,13 @@ define('BX_DOL_FEATURED_USAGE_DEFAULT', BX_DOL_FEATURED_USAGE_BLOCK);
  */
  */
 class BxDolFeature extends BxDolObject
 class BxDolFeature extends BxDolObject
 {
 {
+    protected $_sType;
     protected $_sBaseUrl;
     protected $_sBaseUrl;
 
 
+    protected $_aElementDefaults;
+    protected $_aElementDefaultsApi;
+    protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
+
     protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     protected function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
     {
     {
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
@@ -46,6 +51,7 @@ class BxDolFeature extends BxDolObject
 
 
         $this->_oQuery = new BxDolFeatureQuery($this);
         $this->_oQuery = new BxDolFeatureQuery($this);
 
 
+        $this->_sType = 'features';
         $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
         $this->_sBaseUrl = BxDolPermalinks::getInstance()->permalink($this->_aSystem['base_url']);
         if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
         if(get_mb_substr($this->_sBaseUrl, 0, 4) != 'http')
             $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
             $this->_sBaseUrl = BX_DOL_URL_ROOT . $this->_sBaseUrl;
@@ -108,7 +114,7 @@ class BxDolFeature extends BxDolObject
      */
      */
     public function actionFeature()
     public function actionFeature()
     {
     {
-        return echoJson($this->_doFeature());
+        return echoJson($this->doFeature());
     }
     }
 
 
     public function actionGetFeatureBy()
     public function actionGetFeatureBy()
@@ -140,13 +146,13 @@ class BxDolFeature extends BxDolObject
         return (int)$this->_aSystem['is_undo'] == 1;
         return (int)$this->_aSystem['is_undo'] == 1;
     }
     }
 
 
-	/**
+    /**
      * Internal functions
      * Internal functions
      */
      */
-	protected function _doFeature()
+    public function doFeature()
     {
     {
-        if (!$this->isEnabled())
-           return array('code' => 1, 'message' => _t('_feature_err_not_enabled'));
+        if(!$this->isEnabled())
+            return array('code' => 1, 'message' => _t('_feature_err_not_enabled'));
 
 
         $iObjectId = $this->getId();
         $iObjectId = $this->getId();
         $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
         $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
@@ -160,7 +166,7 @@ class BxDolFeature extends BxDolObject
             return array('code' => 2, 'message' => $this->msgErrAllowedFeature());
             return array('code' => 2, 'message' => $this->msgErrAllowedFeature());
 
 
         if($bPerformed && !$bUndo)
         if($bPerformed && !$bUndo)
-        	return array('code' => 3, 'message' => _t('_feature_err_duplicate_feature'));
+            return array('code' => 3, 'message' => _t('_feature_err_duplicate_feature'));
 
 
         if(!$this->_triggerValue($bPerformUndo ? 0 : time()))
         if(!$this->_triggerValue($bPerformUndo ? 0 : time()))
             return array('code' => 4, 'message' => _t('_feature_err_cannot_perform_action'));
             return array('code' => 4, 'message' => _t('_feature_err_cannot_perform_action'));
@@ -204,13 +210,23 @@ class BxDolFeature extends BxDolObject
          */
          */
         bx_alert('feature', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
         bx_alert('feature', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
 
 
-        return array(
-        	'eval' => $this->getJsObjectName() . '.onFeature(oData, oElement)',
-        	'code' => 0, 
-        	'label_icon' => $this->_getIconDoFeature(!$bPerformed),
+        $aResult = [
+            'eval' => $this->getJsObjectName() . '.onFeature(oData, oElement)',
+            'code' => 0, 
+            'label_icon' => $this->_getIconDoFeature(!$bPerformed),
             'label_title' => _t($this->_getTitleDoFeature(!$bPerformed)),
             'label_title' => _t($this->_getTitleDoFeature(!$bPerformed)),
+            'featured' => !$bPerformed,
             'disabled' => !$bPerformed && !$bUndo
             'disabled' => !$bPerformed && !$bUndo
-        );
+        ];
+
+        $aResult['api'] = [
+            'is_featured' => $aResult['featured'],
+            'is_disabled' => $aResult['disabled'],
+            'icon' => $aResult['label_icon'],
+            'title' => $aResult['label_title'],
+        ];
+        
+        return $aResult;
     }
     }
 
 
     protected function _getIconDoFeature($bPerformed)
     protected function _getIconDoFeature($bPerformed)

+ 12 - 2
modules/base/general/classes/BxBaseModGeneralMenuViewActions.php

@@ -363,7 +363,12 @@ class BxBaseModGeneralMenuViewActions extends BxTemplMenuCustom
             $aObjectOptions = array_merge($aObjectOptions, $aParams['object_options']);
             $aObjectOptions = array_merge($aObjectOptions, $aParams['object_options']);
 
 
         if($this->_bIsApi)
         if($this->_bIsApi)
-            return $oObject->getElementApi($aObjectOptions);
+            return [
+                'id' => $aItem['id'],
+                'name' => $aItem['name'],
+                'display_type' => 'element',
+                'data' => $oObject->getElementApi($aObjectOptions)
+            ];
 
 
         $sResult = $oObject->getElementBlock($aObjectOptions);
         $sResult = $oObject->getElementBlock($aObjectOptions);
         if(empty($sResult))
         if(empty($sResult))
@@ -400,7 +405,12 @@ class BxBaseModGeneralMenuViewActions extends BxTemplMenuCustom
             $aObjectOptions = array_merge($aObjectOptions, $aParams['object_options']);
             $aObjectOptions = array_merge($aObjectOptions, $aParams['object_options']);
 
 
         if($this->_bIsApi)
         if($this->_bIsApi)
-            return $oObject->getElementApi($aObjectOptions);
+            return [
+                'id' => $aItem['id'],
+                'name' => $aItem['name'],
+                'display_type' => 'element',
+                'data' => $oObject->getElementApi($aObjectOptions)
+            ];
 
 
         $sResult = $oObject->getElementBlock($aObjectOptions);
         $sResult = $oObject->getElementBlock($aObjectOptions);
         if(empty($sResult))
         if(empty($sResult))

+ 54 - 4
template/scripts/BxBaseFeature.php

@@ -20,8 +20,6 @@ class BxBaseFeature extends BxDolFeature
 
 
     protected $_aHtmlIds;
     protected $_aHtmlIds;
 
 
-    protected $_aElementDefaults;
-
     public function __construct($sSystem, $iId, $iInit = 1, $oTemplate = false)
     public function __construct($sSystem, $iId, $iInit = 1, $oTemplate = false)
     {
     {
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
         parent::__construct($sSystem, $iId, $iInit, $oTemplate);
@@ -44,6 +42,10 @@ class BxBaseFeature extends BxDolFeature
             'show_do_feature_icon' => true,
             'show_do_feature_icon' => true,
             'show_do_feature_label' => false
             'show_do_feature_label' => false
         );
         );
+        $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
+            'show_do_feature_as_button' => true,
+        ]);
+        $this->_aElementParamsApi = ['is_featured'];
 
 
         $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('feature_element_block.html');
         $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('feature_element_block.html');
         $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('feature_element_inline.html');
         $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('feature_element_inline.html');
@@ -98,8 +100,8 @@ class BxBaseFeature extends BxDolFeature
         $bShowDoFeatureAsButtonSmall = isset($aParams['show_do_feature_as_button_small']) && $aParams['show_do_feature_as_button_small'] == true;
         $bShowDoFeatureAsButtonSmall = isset($aParams['show_do_feature_as_button_small']) && $aParams['show_do_feature_as_button_small'] == true;
         $bShowDoFeatureAsButton = !$bShowDoFeatureAsButtonSmall && isset($aParams['show_do_feature_as_button']) && $aParams['show_do_feature_as_button'] == true;
         $bShowDoFeatureAsButton = !$bShowDoFeatureAsButtonSmall && isset($aParams['show_do_feature_as_button']) && $aParams['show_do_feature_as_button'] == true;
 
 
-		$iObjectId = $this->getId();
-		$iAuthorId = $this->_getAuthorId();
+        $iObjectId = $this->getId();
+        $iAuthorId = $this->_getAuthorId();
         $aFeature = $this->_oQuery->getFeature($iObjectId);
         $aFeature = $this->_oQuery->getFeature($iObjectId);
         $bCount = (int)$aFeature['count'] != 0;
         $bCount = (int)$aFeature['count'] != 0;
 
 
@@ -120,6 +122,41 @@ class BxBaseFeature extends BxDolFeature
         ));
         ));
     }
     }
 
 
+    public function getElementAPI($aParams = [])
+    {
+        if(!($this->_bApi = bx_is_api()))
+            return;
+
+        if(!$this->isEnabled())
+            return bx_api_get_msg('_vote_err_not_enabled');
+
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $iObjectId = $this->getId();
+        $iAuthorId = $this->_getAuthorId();
+
+        $aFeature = $this->_oQuery->getFeature($iObjectId);
+
+        $bCount = (int)$aFeature['count'] != 0;
+        $isAllowedFeature = $this->isAllowedFeature();
+        $aParams['is_featured'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
+
+        //--- Do Feature
+        $bDoFeature = $this->_isShowDoFeature($aParams, $isAllowedFeature, $bCount);
+        $aDoFeature = $bDoFeature ? $this->_getDoFeature($aParams, $isAllowedFeature) : [];
+
+        if(!$bDoFeature)
+            return bx_api_get_msg('');
+
+        return [
+            'type' => $this->_sType,
+            'system' => $this->_sSystem,
+            'object_id' => $this->_iId,
+            'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
+            'action' => $aDoFeature
+        ];
+    }
+
     protected function _getDoFeature($aParams = array(), $bAllowedFeature = true)
     protected function _getDoFeature($aParams = array(), $bAllowedFeature = true)
     {
     {
     	$bFeatured = isset($aParams['is_featured']) && $aParams['is_featured'] === true;
     	$bFeatured = isset($aParams['is_featured']) && $aParams['is_featured'] === true;
@@ -136,6 +173,14 @@ class BxBaseFeature extends BxDolFeature
         if($bDisabled)
         if($bDisabled)
             $sClass .= $bShowDoFeatureAsButton || $bShowDoFeatureAsButtonSmall ? ' bx-btn-disabled' : 'bx-feature-disabled';
             $sClass .= $bShowDoFeatureAsButton || $bShowDoFeatureAsButtonSmall ? ' bx-btn-disabled' : 'bx-feature-disabled';
 
 
+        if($this->_bApi)
+            return [
+                'is_undo' => $this->isUndo(),
+                'is_featured' => $bFeatured,
+                'is_disabled' => $bDisabled,
+                'title' => _t($this->_getTitleDoFeature($bFeatured)),
+            ];
+
         return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFeature($aParams), array(
         return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFeature($aParams), array(
             'id' => $this->_aHtmlIds['do_link'],
             'id' => $this->_aHtmlIds['do_link'],
             'class' => $this->_sStylePrefix . '-do-feature ' . $sClass,
             'class' => $this->_sStylePrefix . '-do-feature ' . $sClass,
@@ -165,6 +210,11 @@ class BxBaseFeature extends BxDolFeature
             )
             )
         ));
         ));
     }
     }
+
+    protected function _isShowDoFeature($aParams, $isAllowedFeature, $bCount)
+    {
+        return (!isset($aParams['show_do_feature']) || (bool)$aParams['show_do_feature'] === true) && $isAllowedFeature;
+    }
 }
 }
 
 
 /** @} */
 /** @} */

+ 55 - 0
template/scripts/BxBaseFeatureServices.php

@@ -0,0 +1,55 @@
+<?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 for 'Features' engine.
+ */
+class BxBaseFeatureServices extends BxDol
+{
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-features Features
+     * @subsubsection bx_system_general-perform perform
+     * 
+     * @code bx_srv('system', 'perform', [[...]], 'TemplServiceFeatures'); @endcode
+     * @code {{~system:perform:TemplServiceFeatures[[...]]~}} @endcode
+     * 
+     * Performs Perform action (featured, unfeatured, etc) with features object.
+     * @param $aParams an array with necessary parameters 
+     * 
+     * @see BxBaseServiceFeatures::servicePerform
+     */
+    /** 
+     * @ref bx_system_general-perform "perform"
+     * @api @ref bx_system_general-perform "perform"
+     */
+    public function servicePerform($aParams)
+    {
+        if(is_string($aParams))
+            $aParams = json_decode($aParams, true);
+
+        if(!$aParams['s'] || !$aParams['o'])
+            return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
+
+        $oFeature = BxDolFeature::getObjectInstance($aParams['s'], $aParams['o']);
+        if(!$oFeature || !$oFeature->isEnabled())
+            return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
+
+        $aResult = $oFeature->doFeature($aParams);
+        return (int)$aResult['code'] != 0 ? $aResult : $aResult['api'];
+    }
+}
+
+/** @} */

+ 2 - 0
template/scripts/BxBaseServices.php

@@ -103,6 +103,8 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             'Do' => 'BxBaseVoteServices',
             'Do' => 'BxBaseVoteServices',
             'GetPerformedBy' => 'BxBaseVoteServices',
             'GetPerformedBy' => 'BxBaseVoteServices',
 
 
+            'Perform' => 'BxBaseFeatureServices',
+
             'Perform' => 'BxBaseServiceConnections',
             'Perform' => 'BxBaseServiceConnections',
             'Perform' => 'BxBaseServiceRecommendations',
             'Perform' => 'BxBaseServiceRecommendations',
             'GetDataApi' => 'BxBaseCmtsServices',
             'GetDataApi' => 'BxBaseCmtsServices',

+ 15 - 0
template/scripts_templ/BxTemplFeatureServices.php

@@ -0,0 +1,15 @@
+<?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
+ * @{
+ */
+
+class BxTemplFeatureServices extends BxBaseFeatureServices
+{
+
+}
+
+/** @} */