Просмотр исходного кода

Ticket #4148 - Build-in API support: Payments.

AntonLV 3 лет назад
Родитель
Сommit
03dddc79d9

+ 1 - 1
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -3202,7 +3202,7 @@ class BxBaseModGeneralModule extends BxDolModule
             'icon' => $oProfile->getIcon(),
             'thumb' => $oProfile->getThumb(),
             'avatar' => $oProfile->getAvatar(),
-            'unit' => $oProfile->getUnit(),
+            'unit' => $oProfile->{'getUnit' . ($this->_bIsApi ? 'API' : '')}(),
             'active' => $oProfile->isActive(),
         );
     }

+ 6 - 2
modules/base/general/classes/BxBaseModGeneralTemplate.php

@@ -15,12 +15,16 @@
 class BxBaseModGeneralTemplate extends BxDolModuleTemplate
 {
     protected $MODULE;
-    
+
+    protected $_bIsApi;
+
     public $aMethodsToCallAddJsCss = array('entry', 'unit');
 
-    function __construct(&$oConfig, &$oDb)
+    public function __construct(&$oConfig, &$oDb)
     {
         parent::__construct($oConfig, $oDb);
+
+        $this->_bIsApi = bx_is_api();
     }
 
     public function addLocationBase()

+ 5 - 1
modules/base/payment/classes/BxBaseModPaymentCart.php

@@ -14,15 +14,19 @@ class BxBaseModPaymentCart extends BxDol
     protected $MODULE;
     protected $_oModule;
 
+    protected $_bIsApi;
+
     protected $_bSingleSeller;
     protected $_iSingleSeller;
 
-    function __construct()
+    public function __construct()
     {
         parent::__construct();
 
         $this->_oModule = BxDolModule::getInstance($this->MODULE);
 
+        $this->_bIsApi = bx_is_api();
+
         $this->_bSingleSeller = $this->_oModule->_oConfig->isSingleSeller();
 
         $this->_iSingleSeller = 0;

+ 2 - 2
modules/base/profile/classes/BxBaseModProfileModule.php

@@ -2083,8 +2083,8 @@ class BxBaseModProfileModule extends BxBaseModGeneralModule implements iBxDolCon
             'image' => $this->serviceGetThumb($iId),
             'cover' => $this->serviceGetCover($iId),
         ];
-        
-        if ($aParams['template'] == 'unit_wo_info')
+
+        if(isset($aParams['template']) && $aParams['template'] == 'unit_wo_info')
             return $aResult;
 
         if(isset($aParams['extended']) && $aParams['extended'] === true)

+ 25 - 11
modules/boonex/payment/classes/BxPaymentCart.php

@@ -53,9 +53,16 @@ class BxPaymentCart extends BxBaseModPaymentCart
     	if(bx_get('seller_id') !== false)
             return '';
 
-        return array(
-            'content' => $this->_oModule->_oTemplate->displayBlockCarts($iUserId)
-        );
+        $mixedContent = $this->_oModule->_oTemplate->displayBlockCarts($iUserId);
+
+        if($this->_bIsApi)
+            return [
+                bx_api_get_block('grid', $mixedContent)
+            ];
+
+        return [
+            'content' => $mixedContent
+        ];
     }
 
     /**
@@ -80,25 +87,32 @@ class BxPaymentCart extends BxBaseModPaymentCart
         $CNF = &$this->_oModule->_oConfig->CNF;
 
     	if(!$this->_bSingleSeller && bx_get('seller_id') === false)
-            return '';
+            return $this->_bIsApi ? [bx_api_get_msg('')] : '';
 
     	$iUserId = $this->_oModule->getProfileId();
         if(empty($iUserId))
-            return array(
+            return $this->_bIsApi ? [bx_api_get_msg(_t($CNF['T']['ERR_REQUIRED_LOGIN']))] : [
             	'content' => MsgBox(_t($CNF['T']['ERR_REQUIRED_LOGIN']))
-            );
+            ];
 
     	$iSellerId = !$this->_bSingleSeller ? bx_process_input(bx_get('seller_id'), BX_DATA_INT) : $this->_oModule->_oConfig->getSiteAdmin();
     	if(empty($iSellerId))
-            return array(
+            return $this->_bIsApi ? [bx_api_get_msg(_t($CNF['T']['ERR_UNKNOWN_VENDOR']))] : [
                 'content' => MsgBox(_t($CNF['T']['ERR_UNKNOWN_VENDOR']))
-            );
+            ];
+
+        $mixedContent = $this->_oModule->_oTemplate->displayBlockCart($iUserId, $iSellerId);
+
+        if($this->_bIsApi)
+            return [
+                bx_api_get_block('grid', $mixedContent)
+            ];
 
         $aSeller = $this->_oModule->getProfileInfo($iSellerId);
-        return array(
+        return [
             'title' => _t($CNF['T']['BLOCK_TITLE_CART'], $aSeller['name']),
-            'content' => $this->_oModule->_oTemplate->displayBlockCart($iUserId, $iSellerId)
-        );
+            'content' => $mixedContent
+        ];
     }
 
     /**

+ 51 - 0
modules/boonex/payment/classes/BxPaymentGridCart.php

@@ -154,6 +154,57 @@ class BxPaymentGridCart extends BxBaseModPaymentGridCarts
     	return $sActions . parent::_getActions ($sType, $sActionData, $isSmall, $isDisabled, $isPermanentState, $aRow);
     }
 
+    protected function _getActionsAPI($sType)
+    {
+        if($sType != 'bulk' || empty($this->_aQueryAppend['seller_id']))
+            return parent::_getActionsAPI($sType);
+
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        $iClientId = (int)$this->_aQueryAppend['client_id'];
+        $iSellerId = (int)$this->_aQueryAppend['seller_id'];
+        $aCartInfo = $this->_oModule->getObjectCart()->getInfo(BX_PAYMENT_TYPE_SINGLE, $iClientId, $iSellerId);
+
+        $bCreditsOnly = $this->_oModule->_oConfig->isCreditsOnly();
+
+        $bPayForCredits = true;
+        if(!empty($aCartInfo['items']) && is_array($aCartInfo['items']))
+            foreach($aCartInfo['items'] as $aItem) {
+                $aModule = $this->_oModule->_oDb->getModuleById((int)$aItem['module_id']);
+                if($aModule['name'] != $CNF['MODULE_CREDITS']) {
+                    $bPayForCredits = false;
+                    break;
+                }
+            }
+
+        $aActions = [];
+        $aProviders = $this->_oModule->_oDb->getVendorInfoProvidersSingle($iSellerId);
+        foreach($aProviders as $aProvider) {
+            $bProviderCredits = $aProvider['name'] == $CNF['OBJECT_PP_CREDITS'];
+
+            /*
+             * Hide all non-Credits payment providers when 'credits only' mode is enabled 
+             * and purchasing items aren't credits.
+             */
+            if($bCreditsOnly && !$bProviderCredits && !$bPayForCredits)
+                continue;
+
+            /*
+             * Hide Credits payment provider when paying for credits.
+             */
+            if($bProviderCredits && $bPayForCredits)
+                continue;
+            
+            $aActions[] = [
+                'title'=> _t('_bx_payment_grid_action_title_crt_checkout', _t($CNF['T']['TXT_CART_PROVIDER'] . $aProvider['name'])),
+                'icon' => '',
+                'icon_only' => 0,
+                'confirm' => 0,
+            ];
+        }
+        return $aActions;
+    }    
+
     protected function _getDataArray($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
     {
         if(empty($this->_aQueryAppend['client_id']) || empty($this->_aQueryAppend['seller_id']))

+ 10 - 4
modules/boonex/payment/classes/BxPaymentTemplate.php

@@ -11,7 +11,7 @@
 
 class BxPaymentTemplate extends BxBaseModPaymentTemplate
 {
-	protected $_sLangsPrefix;
+    protected $_sLangsPrefix;
 
     function __construct(&$oConfig, &$oDb)
     {
@@ -188,15 +188,18 @@ class BxPaymentTemplate extends BxBaseModPaymentTemplate
     	return $this->_displaySubscriptionData('change_billing', $iId);
     }
 
-	public function displayBlockCarts($iClientId)
+    public function displayBlockCarts($iClientId)
     {
     	$oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_carts'));
         if(!$oGrid)
             return MsgBox(_t($this->_sLangsPrefix . 'msg_no_results'));
 
-		$oGrid->addQueryParam('client_id', $iClientId);
+        $oGrid->addQueryParam('client_id', $iClientId);
+        
+        if($this->_bIsApi)
+            return $oGrid->getCodeAPI();
 
-		$this->addJsCssCart(BX_PAYMENT_TYPE_SINGLE);
+        $this->addJsCssCart(BX_PAYMENT_TYPE_SINGLE);
         return $this->displayJsCode('cart') . $oGrid->getCode();
     }
 
@@ -209,6 +212,9 @@ class BxPaymentTemplate extends BxBaseModPaymentTemplate
         $oGrid->addQueryParam('client_id', $iClientId);
         $oGrid->addQueryParam('seller_id', $iSellerId);
 
+        if($this->_bIsApi)
+            return $oGrid->getCodeAPI();
+
         $this->addJsCssCart(BX_PAYMENT_TYPE_SINGLE, $iSellerId);
         return $this->displayJsCode('cart') . $oGrid->getCode();
     }

+ 51 - 1
template/scripts/BxBaseGrid.php

@@ -285,6 +285,36 @@ class BxBaseGrid extends BxDolGrid
         return $this->_oTemplate->parseHtmlByName('grid.html', $aVars);
     }
 
+    /**
+     * Get grid code API.
+     * @return array
+     */
+    public function getCodeAPI()
+    {
+        $sFilter = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['filter_get'])));
+        $sOrderField = bx_unicode_urldecode(bx_process_input(bx_get($this->_aOptions['order_get_field'])));
+        $sOrderDir = 0 === strcasecmp('desc', bx_get($this->_aOptions['order_get_dir'])) ? 'DESC' : 'ASC';
+
+        $iStart = 0;
+        if($this->_aOptions['paginate_get_start'] && ($iStartGet = (int)bx_get($this->_aOptions['paginate_get_start'])) >= 0)
+            $iStart = $iStartGet;
+
+        $iPerPage = 10;
+        if($this->_aOptions['paginate_get_per_page'] && ($iPerPageGet = (int)bx_get($this->_aOptions['paginate_get_per_page'])) > 0)
+            $iPerPage = $iPerPageGet;
+        else if($this->_aOptions['paginate_per_page'])
+            $iPerPage = (int)$this->_aOptions['paginate_per_page']; 
+
+        return [
+            'header' => $this->_getRowHeadAPI(),
+            'data' => $this->_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage),
+            'actions' => [
+                'independent' => $this->_getActionsAPI('independent'), 
+                'bulk' => $this->_getActionsAPI('bulk'),
+            ]
+        ];
+    }
+
     /**
      * Reset query params, like filter and page number
      */
@@ -296,7 +326,7 @@ class BxBaseGrid extends BxDolGrid
         }
     }
 
-    protected function _getRowHead ()
+    protected function _getRowHead()
     {
         $aRet = array();
         foreach ($this->_aOptions['fields'] as $sKey => $a) {
@@ -311,6 +341,17 @@ class BxBaseGrid extends BxDolGrid
         return $aRet;
     }
 
+    protected function _getRowHeadAPI()
+    {
+        $aHeader = [];
+        foreach($this->_aOptions['fields'] as $sKey => $aField)
+            $aHeader[] = [
+                'title' => bx_process_output($aField['title'])
+            ];
+
+        return $aHeader;
+    }
+
     protected function _getCellHeaderDefault ($sKey, $aField)
     {
         $sHeader = bx_process_output($aField['title']);
@@ -595,6 +636,15 @@ class BxBaseGrid extends BxDolGrid
         return $sRet;
     }
 
+    protected function _getActionsAPI ($sType)
+    {
+        $sActionsType = 'actions_' . $sType;
+        if(empty($this->_aOptions[$sActionsType]) || !is_array($this->_aOptions[$sActionsType]))
+            return [];
+
+        return $this->_aOptions[$sActionsType];
+    }
+
     protected function _getActionDefault ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
     {
         if ($a['icon_only'] && empty($a['attr']['title']) && !empty($a['title']))

+ 5 - 0
template/scripts/BxBasePage.php

@@ -495,6 +495,7 @@ class BxBasePage extends BxDolPage
                 'avatar' => $o->getAvatar(),
                 'info' => $o->getInfo(),
                 'notifications' => 0,
+                'cart' => 0,
                 'active' => $o->isActive(),
                 'status' => $o->getStatus(),
             ];
@@ -508,6 +509,10 @@ class BxBasePage extends BxDolPage
             $sModuleNotifications = 'bx_notifications';
             if(BxDolRequest::serviceExists($sModuleNotifications, 'get_unread_notifications_num'))
                 $a['user']['notifications'] = bx_srv($sModuleNotifications, 'get_unread_notifications_num', [$o->id()]);
+
+            $oPayments = BxDolPayments::getInstance();
+            if($oPayments->isActive())
+                $a['user']['cart'] = $oPayments->getCartItemsCount();
         }
 
         return $a;