Browse Source

Ticket #4558 - Build-in API support: Favorites feature.

AntonLV 1 year ago
parent
commit
f24f6540ec

+ 92 - 4
inc/classes/BxDolFavorite.php

@@ -47,8 +47,15 @@ define('BX_DOL_FAVORITE_USAGE_DEFAULT', BX_DOL_FAVORITE_USAGE_BLOCK);
  */
  */
 class BxDolFavorite extends BxDolObject
 class BxDolFavorite extends BxDolObject
 {
 {
+    protected $_sType;
     protected $_sBaseUrl;
     protected $_sBaseUrl;
 
 
+    protected $_aFavorite;
+
+    protected $_aElementDefaults;
+    protected $_aElementDefaultsApi;
+    protected $_aElementParamsApi; //--- Params from DefaultsApi array to be passed to Api
+
     protected $_sFormObject;
     protected $_sFormObject;
     protected $_sFormDisplayAdd;
     protected $_sFormDisplayAdd;
     protected $_sFormDisplayListEdit;
     protected $_sFormDisplayListEdit;
@@ -61,10 +68,13 @@ class BxDolFavorite extends BxDolObject
 
 
         $this->_oQuery = new BxDolFavoriteQuery($this);
         $this->_oQuery = new BxDolFavoriteQuery($this);
 
 
+        $this->_sType = 'favorites';
         $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;
-        
+
+        $this->_aFavorite = [];
+
         $this->_sFormObject = 'sys_favorite';
         $this->_sFormObject = 'sys_favorite';
         $this->_sFormDisplayAdd = 'sys_favorite_add';
         $this->_sFormDisplayAdd = 'sys_favorite_add';
         $this->_sFormDisplayListEdit = 'sys_favorite_list_edit';
         $this->_sFormDisplayListEdit = 'sys_favorite_list_edit';
@@ -155,7 +165,7 @@ class BxDolFavorite extends BxDolObject
      */
      */
     public function actionFavorite()
     public function actionFavorite()
     {
     {
-        return echoJson($this->_getFavorite());
+        return echoJson($this->favorite());
     }
     }
     
     
     public function actionEditList()
     public function actionEditList()
@@ -261,10 +271,88 @@ class BxDolFavorite extends BxDolObject
         return (int)$this->_aSystem['is_public'] == 1;
         return (int)$this->_aSystem['is_public'] == 1;
     }
     }
 
 
-	/**
+    /**
      * Internal functions
      * Internal functions
      */
      */
-	
+    public function doFavorite()
+    {
+        if(!$this->isEnabled())
+            return ['code' => 1, 'message' => _t('_feature_err_not_enabled')];
+
+        $iObjectId = $this->getId();
+        $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
+        $iAuthorId = $this->_getAuthorId();
+
+        $bUndo = $this->isUndo();
+        $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
+        $bPerformUndo = $bPerformed && $bUndo ? true : false;
+
+        if(!$bPerformUndo && !$this->isAllowedFavorite())
+            return ['code' => 2, 'message' => $this->msgErrAllowedFavorite()];
+
+        if($bPerformed && !$bUndo)
+            return ['code' => 3, 'message' => _t('_favorite_err_duplicate_favorite')];
+
+        if(!$this->_oQuery->{($bPerformUndo ? 'un' : '') . 'doFavorite'}($iObjectId, $iAuthorId))
+            return ['code' => 4, 'message' => _t('_favorite_err_cannot_perform_action')];
+
+        if(!$bPerformUndo)
+            $this->isAllowedFavorite(true);
+
+        $this->_triggerValue($bPerformUndo ? -1 : 1);
+
+        bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'favorite', $iObjectId, $iAuthorId, ['favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId]);
+        /**
+         * @hooks
+         * @hookdef hook-report-undo 'favorite', 'favorite' - hook on add new object to favorites lists or remove object from favorites lists 
+         * - $unit_name - equals `favorite`
+         * - $action - can be  do/undo 
+         * - $object_id - not used
+         * - $sender_id - profile_id for favorite's author
+         * - $extra_params - array of additional params with the following array keys:
+         *      - `object_system` - [string] system name, ex: bx_posts
+         *      - `object_id` - [int] reported object id 
+         *      - `object_author_id` - [int] author's profile_id for reported object_id 
+         * @hook @ref hook-favorite-undo
+         */
+        bx_alert('favorite', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, ['object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId]);
+
+        $aFavorite = $this->_oQuery->getFavorite($iObjectId);
+
+        $aResult = [
+            'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
+            'code' => 0, 
+            'count' => $aFavorite['count'],
+            'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
+            'label_icon' => $this->_getIconDoFavorite(!$bPerformed),
+            'label_title' => _t($this->_getTitleDoFavorite(!$bPerformed)),
+            'favorited' => !$bPerformed,
+            'disabled' => !$bPerformed && !$bUndo
+        ];
+
+        $aResult['api'] = [
+            'is_favorited' => $aResult['favorited'],
+            'is_disabled' => $aResult['disabled'],
+            'icon' => $aResult['label_icon'],
+            'title' => $aResult['label_title'],
+            'counter' => $aFavorite
+        ];
+
+        return $aResult;
+    }
+
+    protected function _getFavorite($iObjectId = 0, $bForceGet = false)
+    {
+        if(!empty($this->_aFavorite) && !$bForceGet)
+            return $this->_aFavorite;
+
+        if(empty($iObjectId))
+            $iObjectId = $this->getId();
+
+        $this->_aFavorite = $this->_oQuery->getFavorite($iObjectId);
+        return $this->_aFavorite;
+    }
+
     protected function _getIconDoFavorite($bPerformed)
     protected function _getIconDoFavorite($bPerformed)
     {
     {
     	return $bPerformed && $this->isUndo() ?  'far fa-bookmark' : 'fas fa-bookmark';
     	return $bPerformed && $this->isUndo() ?  'far fa-bookmark' : 'fas fa-bookmark';

+ 119 - 76
template/scripts/BxBaseFavorite.php

@@ -20,8 +20,6 @@ class BxBaseFavorite extends BxDolFavorite
 
 
     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);
@@ -29,27 +27,32 @@ class BxBaseFavorite extends BxDolFavorite
         $this->_bCssJsAdded = false;
         $this->_bCssJsAdded = false;
 
 
         $this->_sJsObjClass = 'BxDolFavorite';
         $this->_sJsObjClass = 'BxDolFavorite';
-        $this->_sJsObjName = 'oFavorite' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
+        $this->_sJsObjName = 'oFavorite' . bx_gen_method_name($sSystem, ['_' , '-']) . $iId;
         $this->_sStylePrefix = 'bx-favorite';
         $this->_sStylePrefix = 'bx-favorite';
 
 
-        $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
-        $this->_aHtmlIds = array(
+        $sHtmlId = str_replace(['_' , ' '], ['-', '-'], $sSystem) . '-' . $iId;
+        $this->_aHtmlIds = [
             'main' => 'bx-favorite-' . $sHtmlId,
             'main' => 'bx-favorite-' . $sHtmlId,
             'counter' => 'bx-favorite-counter-' . $sHtmlId,
             'counter' => 'bx-favorite-counter-' . $sHtmlId,
             'do_link' => 'bx-favorite-do-link-' . $sHtmlId,
             'do_link' => 'bx-favorite-do-link-' . $sHtmlId,
             'by_popup' => 'bx-favorite-by-popup-' . $sHtmlId,
             'by_popup' => 'bx-favorite-by-popup-' . $sHtmlId,
             'do_popup' => 'bx-favorite-do-popup-' . $sHtmlId,
             'do_popup' => 'bx-favorite-do-popup-' . $sHtmlId,
             'do_form' => 'bx-favorite-do-form-' . $sHtmlId,
             'do_form' => 'bx-favorite-do-form-' . $sHtmlId,
-        );
+        ];
 
 
-        $this->_aElementDefaults = array(
+        $this->_aElementDefaults = [
             'show_do_favorite_as_button' => false,
             'show_do_favorite_as_button' => false,
             'show_do_favorite_as_button_small' => false,
             'show_do_favorite_as_button_small' => false,
             'show_do_favorite_icon' => true,
             'show_do_favorite_icon' => true,
             'show_do_favorite_label' => false,
             'show_do_favorite_label' => false,
             'show_counter' => true,
             'show_counter' => true,
             'show_counter_only' => true
             'show_counter_only' => true
-        );
+        ];
+
+        $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
+            'show_do_feature_as_button' => true,
+        ]);
+        $this->_aElementParamsApi = ['is_favorited'];
 
 
         $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('favorite_element_block.html');
         $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('favorite_element_block.html');
         $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('favorite_element_inline.html');
         $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('favorite_element_inline.html');
@@ -89,7 +92,7 @@ class BxBaseFavorite extends BxDolFavorite
         $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
         $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
         $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
         $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
 
 
-        $aFavorite = $this->_oQuery->getFavorite($this->getId());
+        $aFavorite = $this->_getFavorite();
 
 
         $sClass = 'sys-action-counter';
         $sClass = 'sys-action-counter';
         if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
         if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
@@ -109,6 +112,13 @@ class BxBaseFavorite extends BxDolFavorite
         ));
         ));
     }
     }
 
 
+    public function getCounterAPI($aParams = [])
+    {
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        return $this->_getFavorite();
+    }
+
     public function getElementBlock($aParams = array())
     public function getElementBlock($aParams = array())
     {
     {
         $aParams['usage'] = BX_DOL_FAVORITE_USAGE_BLOCK;
         $aParams['usage'] = BX_DOL_FAVORITE_USAGE_BLOCK;
@@ -167,28 +177,96 @@ class BxBaseFavorite extends BxDolFavorite
         ));
         ));
     }
     }
 
 
+    public function getElementAPI($aParams = [])
+    {
+        if(!($this->_bApi = bx_is_api()))
+            return;
+
+        if(!$this->isEnabled())
+            return bx_api_get_msg('_favorite_err_not_enabled');
+
+        $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
+
+        $iObjectId = $this->getId();
+        $iAuthorId = $this->_getAuthorId();
+
+        $aFavorite = $this->_oQuery->getFavorite($iObjectId);
+
+        $bCount = (int)$aFavorite['count'] != 0;
+        $isAllowedFavorite = $this->isAllowedFavorite();
+        $isAllowedFavoriteView = $this->isAllowedFavoriteView();
+        $aParams['is_favorited'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
+
+        //--- Do Favorite
+        $bDoFavorite = $this->_isShowDoFavorite($aParams, $isAllowedFavorite, $bCount);
+        $aDoFavorite = $bDoFavorite ? $this->_getDoFavorite($aParams, $isAllowedFavorite) : [];
+
+        //--- Counter
+        $bCounter = $this->_isShowCounter($aParams, $isAllowedFavorite, $isAllowedFavoriteView, $bCount);
+        $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
+            'show_counter_only' => false, 
+            'show_script' => false
+        ])) : [];
+
+        if(!$bDoFavorite && !$bCounter)
+            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' => $aDoFavorite,
+            'counter' => $aCounter
+        ];
+    }
+
+    public function getPerformedByAPI($aParams = [])
+    {
+        $aValues = $this->_oQuery->getPerformedBy($this->getId());
+
+        $aTmplUsers = [];
+        foreach($aValues as $mValue) {
+            $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
+
+            $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
+        }
+
+        return [
+            'performed_by' => $aTmplUsers
+        ];
+    }
+
     protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
     protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
     {
     {
-    	$bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
+        $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
         $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
         $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
         $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
         $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
-		$bDisabled = !$bAllowedFavorite || ($bFavorited  && !$this->isUndo());
+        $bDisabled = !$bAllowedFavorite || ($bFavorited  && !$this->isUndo());
 
 
         $sClass = '';
         $sClass = '';
-		if($bShowDoFavoriteAsButton)
-			$sClass = 'bx-btn';
-		else if ($bShowDoFavoriteAsButtonSmall)
-			$sClass = 'bx-btn bx-btn-small';
-
-		if($bDisabled)
-			$sClass .= $bShowDoFavoriteAsButton || $bShowDoFavoriteAsButtonSmall ? ' bx-btn-disabled' : 'bx-favorite-disabled';
-
-        return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFavorite($aParams), array(
-        	'id' => $this->_aHtmlIds['do_link'],
+        if($bShowDoFavoriteAsButton)
+            $sClass = 'bx-btn';
+        else if ($bShowDoFavoriteAsButtonSmall)
+            $sClass = 'bx-btn bx-btn-small';
+
+        if($bDisabled)
+            $sClass .= $bShowDoFavoriteAsButton || $bShowDoFavoriteAsButtonSmall ? ' bx-btn-disabled' : 'bx-favorite-disabled';
+
+        if($this->_bApi)
+            return [
+                'is_undo' => $this->isUndo(),
+                'is_favorited' => $bFavorited,
+                'is_disabled' => $bDisabled,
+                'title' => _t($this->_getTitleDoFavorite($bFavorited)),
+            ];
+
+        return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFavorite($aParams), [
+            'id' => $this->_aHtmlIds['do_link'],
             'class' => $this->_sStylePrefix . '-do-favorite ' . $sClass,
             'class' => $this->_sStylePrefix . '-do-favorite ' . $sClass,
             'title' => _t($this->_getTitleDoFavorite($bFavorited)),
             'title' => _t($this->_getTitleDoFavorite($bFavorited)),
-        	'onclick' => !$bDisabled ? $this->getJsClick() : ''
-        ));
+            'onclick' => !$bDisabled ? $this->getJsClick() : ''
+        ]);
     }
     }
 
 
     protected function _getCounterLabel($iCount)
     protected function _getCounterLabel($iCount)
@@ -218,63 +296,18 @@ class BxBaseFavorite extends BxDolFavorite
         ));
         ));
     }
     }
     
     
-    protected function _getFavorite()
+    public function favorite()
     {
     {
-        if (!$this->isEnabled())
+        if(!$this->isEnabled())
            return array('code' => 1, 'message' => _t('_favorite_err_not_enabled'));
            return array('code' => 1, 'message' => _t('_favorite_err_not_enabled'));
 
 
         $iAuthorId = $this->_getAuthorId();
         $iAuthorId = $this->_getAuthorId();
         $iObjectId = $this->getId();
         $iObjectId = $this->getId();
         $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
         $iObjectAuthorId = $this->_oQuery->getObjectAuthorId($iObjectId);
-        
-        
-        if (!isset($this->_aSystem['table_lists']) || $this->_aSystem['table_lists'] == ''){
-            $bUndo = $this->isUndo();
-            $bPerformed = $this->isPerformed($iObjectId, $iAuthorId);
-            $bPerformUndo = $bPerformed && $bUndo ? true : false;
-
-            if(!$bPerformUndo && !$this->isAllowedFavorite())
-                return array('code' => 2, 'message' => $this->msgErrAllowedFavorite());
-
-            if($bPerformed && !$bUndo)
-                return array('code' => 3, 'message' => _t('_favorite_err_duplicate_favorite'));
-
-            if(!$this->_oQuery->{($bPerformUndo ? 'un' : '') . 'doFavorite'}($iObjectId, $iAuthorId))
-                return array('code' => 4, 'message' => _t('_favorite_err_cannot_perform_action'));
 
 
-            if(!$bPerformUndo)
-                $this->isAllowedFavorite(true);
-
-            $this->_triggerValue($bPerformUndo ? -1 : 1);
-
-            bx_alert($this->_sSystem, ($bPerformUndo ? 'un' : '') . 'favorite', $iObjectId, $iAuthorId, array('favorite_author_id' => $iAuthorId, 'object_author_id' => $iObjectAuthorId));
-            /**
-             * @hooks
-             * @hookdef hook-report-undo 'favorite', 'favorite' - hook on add new object to favorites lists or remove object from favorites lists 
-             * - $unit_name - equals `favorite`
-             * - $action - can be  do/undo 
-             * - $object_id - not used
-             * - $sender_id - profile_id for favorite's author
-             * - $extra_params - array of additional params with the following array keys:
-             *      - `object_system` - [string] system name, ex: bx_posts
-             *      - `object_id` - [int] reported object id 
-             *      - `object_author_id` - [int] author's profile_id for reported object_id 
-             * @hook @ref hook-favorite-undo
-             */
-            bx_alert('favorite', ($bPerformUndo ? 'un' : '') . 'do', 0, $iAuthorId, array('object_system' => $this->_sSystem, 'object_id' => $iObjectId, 'object_author_id' => $iObjectAuthorId));
+        if(!isset($this->_aSystem['table_lists']) || $this->_aSystem['table_lists'] == '')
+            return $this->doFavorite();
 
 
-            $aFavorite = $this->_oQuery->getFavorite($iObjectId);
-            return array(
-                'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
-                'code' => 0, 
-                'count' => $aFavorite['count'],
-                'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
-                'label_icon' => $this->_getIconDoFavorite(!$bPerformed),
-                'label_title' => _t($this->_getTitleDoFavorite(!$bPerformed)),
-                'disabled' => !$bPerformed && !$bUndo
-            );
-        }
-        
         $oForm = $this->_getFormObject($this->_sFormDisplayAdd);
         $oForm = $this->_getFormObject($this->_sFormDisplayAdd);
         $oForm->setId($this->_aHtmlIds['do_form']);
         $oForm->setId($this->_aHtmlIds['do_form']);
         $oForm->setName($this->_aHtmlIds['do_form']);
         $oForm->setName($this->_aHtmlIds['do_form']);
@@ -342,10 +375,10 @@ class BxBaseFavorite extends BxDolFavorite
             $bPerformed = count($aList) > 0;
             $bPerformed = count($aList) > 0;
             $aFavorite = $this->_oQuery->getFavorite($iObjectId);
             $aFavorite = $this->_oQuery->getFavorite($iObjectId);
             return array(
             return array(
-        	    'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
-        	    'code' => 0, 
-        	    'count' => $aFavorite['count'],
-        	    'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
+                'eval' => $this->getJsObjectName() . '.onFavorite(oData, oElement)',
+                'code' => 0, 
+                'count' => $aFavorite['count'],
+                'countf' => (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '',
                 'label_icon' => $this->_getIconDoFavorite($bPerformed), 
                 'label_icon' => $this->_getIconDoFavorite($bPerformed), 
                 'label_title' => _t($this->_getTitleDoFavorite($bPerformed)), 
                 'label_title' => _t($this->_getTitleDoFavorite($bPerformed)), 
                 'disabled' => false
                 'disabled' => false
@@ -436,6 +469,16 @@ class BxBaseFavorite extends BxDolFavorite
             'bx_repeat:list' => $aTmplFavorites
             'bx_repeat:list' => $aTmplFavorites
         ));
         ));
     }
     }
+
+    protected function _isShowDoFavorite($aParams, $isAllowedFavorite, $bCount)
+    {
+        return (!isset($aParams['show_do_favorite']) || (bool)$aParams['show_do_favorite'] === true) && $isAllowedFavorite;
+    }
+
+    protected function _isShowCounter($aParams, $isAllowedFavorite, $isAllowedFavoriteView, $bCount)
+    {
+        return isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true && $isAllowedFavoriteView && ($isAllowedFavorite || $bCount);
+    }
 }
 }
 
 
 /** @} */
 /** @} */

+ 88 - 0
template/scripts/BxBaseFavoriteServices.php

@@ -0,0 +1,88 @@
+<?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 'Favorites' engine.
+ */
+class BxBaseFavoriteServices extends BxDol
+{
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-favorites Favorites
+     * @subsubsection bx_system_general-perform perform
+     * 
+     * @code bx_srv('system', 'perform', [[...]], 'TemplFavoriteServices'); @endcode
+     * @code {{~system:perform:TemplFavoriteServices[[...]]~}} @endcode
+     * 
+     * Performs Perform action (favorite, unfavorite, etc) with favorites object.
+     * @param $aParams an array with necessary parameters 
+     * 
+     * @see BxBaseFavoriteServices::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];
+
+        $oFavorite = BxDolFavorite::getObjectInstance($aParams['s'], $aParams['o']);
+        if(!$oFavorite || !$oFavorite->isEnabled())
+            return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
+
+        $aResult = $oFavorite->doFavorite($aParams);
+        return (int)$aResult['code'] != 0 ? $aResult : $aResult['api'];
+    }
+
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-favorites Favorites
+     * @subsubsection bx_system_general-get_performed_by get_performed_by
+     * 
+     * @code bx_srv('system', 'get_performed_by', [[...]], 'TemplFavoriteServices'); @endcode
+     * @code {{~system:get_performed_by:TemplFavoriteServices[[...]]~}} @endcode
+     * 
+     * Gets a list of PerformedBy users.
+     * @param $aParams an array with necessary parameters 
+     * 
+     * @see BxBaseFavoriteServices::serviceGetPerformedBy
+     */
+    /** 
+     * @ref bx_system_general-get_performed_by "get_performed_by"
+     * @api @ref bx_system_general-get_performed_by "get_performed_by"
+     */
+    public function serviceGetPerformedBy($aParams)
+    {
+        if(is_string($aParams))
+            $aParams = json_decode($aParams, true);
+
+        if(!$aParams['s'] || !$aParams['o'])
+            return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
+
+        $oFavorite = BxDolFavorite::getObjectInstance($aParams['s'], $aParams['o']);
+        if(!$oFavorite || !$oFavorite->isEnabled())
+            return ['code' => BX_DOL_OBJECT_ERR_NOT_AVAILABLE];
+
+        return $oFavorite->getPerformedByAPI($aParams);
+    }
+}
+
+/** @} */

+ 1 - 1
template/scripts/BxBaseFeature.php

@@ -128,7 +128,7 @@ class BxBaseFeature extends BxDolFeature
             return;
             return;
 
 
         if(!$this->isEnabled())
         if(!$this->isEnabled())
-            return bx_api_get_msg('_vote_err_not_enabled');
+            return bx_api_get_msg('_feature_err_not_enabled');
 
 
         $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
         $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
 
 

+ 3 - 3
template/scripts/BxBaseFeatureServices.php

@@ -23,13 +23,13 @@ class BxBaseFeatureServices extends BxDol
      * @subsection bx_system_general-features Features
      * @subsection bx_system_general-features Features
      * @subsubsection bx_system_general-perform perform
      * @subsubsection bx_system_general-perform perform
      * 
      * 
-     * @code bx_srv('system', 'perform', [[...]], 'TemplServiceFeatures'); @endcode
-     * @code {{~system:perform:TemplServiceFeatures[[...]]~}} @endcode
+     * @code bx_srv('system', 'perform', [[...]], 'TemplFeatureServices'); @endcode
+     * @code {{~system:perform:TemplFeatureServices[[...]]~}} @endcode
      * 
      * 
      * Performs Perform action (featured, unfeatured, etc) with features object.
      * Performs Perform action (featured, unfeatured, etc) with features object.
      * @param $aParams an array with necessary parameters 
      * @param $aParams an array with necessary parameters 
      * 
      * 
-     * @see BxBaseServiceFeatures::servicePerform
+     * @see BxBaseFeatureServices::servicePerform
      */
      */
     /** 
     /** 
      * @ref bx_system_general-perform "perform"
      * @ref bx_system_general-perform "perform"

+ 3 - 0
template/scripts/BxBaseServices.php

@@ -107,6 +107,9 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             'GetPerformedBy' => 'BxBaseVoteServices',
             'GetPerformedBy' => 'BxBaseVoteServices',
 
 
             'Perform' => 'BxBaseFeatureServices',
             'Perform' => 'BxBaseFeatureServices',
+            
+            'Perform' => 'BxBaseFavoriteServices',
+            'GetPerformedBy' => 'BxBaseFavoriteServices',
 
 
             'Perform' => 'BxBaseServiceConnections',
             'Perform' => 'BxBaseServiceConnections',
             'Perform' => 'BxBaseServiceRecommendations',
             'Perform' => 'BxBaseServiceRecommendations',

+ 15 - 0
template/scripts_templ/BxTemplFavoriteServices.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 BxTemplFavoriteServices extends BxBaseFavoriteServices
+{
+
+}
+
+/** @} */