فهرست منبع

Ticket #5775 - Paid levels: Cancel related subscription when membership is downgraded. (#5785)

Anton 1 هفته پیش
والد
کامیت
6ee635f5cd

+ 9 - 5
modules/boonex/acl/classes/BxAclGridView.php

@@ -40,8 +40,6 @@ class BxAclGridView extends BxAclGridLevels
 
     public function performActionChoose()
     {
-        $CNF = &$this->_oModule->_oConfig->CNF;
-
         if(!$this->_iLogged)
             return $this->_bIsApi ? [] : echoJson([]);
 
@@ -51,12 +49,18 @@ class BxAclGridView extends BxAclGridLevels
 
         $aItem = $this->_oModule->_oDb->getPrices(['type' => 'by_id', 'value' => $aIds[0]]);
         if(!is_array($aItem) || empty($aItem) || (float)$aItem['price'] != 0)
-            return $this->_bIsApi ? [] : echoJson(array());
+            return $this->_bIsApi ? [] : echoJson([]);
 
-        $aResult = [];
+        $oAcl = BxDolAcl::getInstance();
         $iUserId = bx_get_logged_profile_id();
-        if(BxDolAcl::getInstance()->setMembership($iUserId, $aItem['level_id'], ['period' => $aItem['period'], 'period_unit' => $aItem['period_unit']], true))
+        $aMembershipInfo = $oAcl->getMemberMembershipInfo($iUserId);
+
+        $aResult = [];
+        if($oAcl->setMembership($iUserId, $aItem['level_id'], ['period' => $aItem['period'], 'period_unit' => $aItem['period_unit']], true)) {
+            $this->_oModule->cancelSubscription($iUserId, $aMembershipInfo);
+
             $aResult = ['grid' => $this->getCode(false), 'blink' => $aItem['id'], 'msg' => _t('_bx_acl_msg_performed')];
+        }
         else
             $aResult = ['msg' => _t('_bx_acl_err_cannot_perform')];
 

+ 46 - 31
modules/boonex/acl/classes/BxAclModule.php

@@ -627,37 +627,8 @@ class BxAclModule extends BxBaseModGeneralModule
             'added' => time()
         ]);
 
-        if($bImmediate && !empty($aMembershipInfo['transaction_id'])) {
-            $oPayments = BxDolPayments::getInstance();
-
-            $sLicenseOld = $aMembershipInfo['transaction_id'];
-            $aLicenseOld = $this->_oDb->getLicenses(['type' => 'by_license', 'license' => $sLicenseOld]);
-
-            $sOrderOld = '';
-            if(empty($aLicenseOld) || !is_array($aLicenseOld)) {
-                $aOrders = $oPayments->getOrdersInfo(['license' => $sLicenseOld]);
-                if(!empty($aOrders) && is_array($aOrders)) {
-                    $aOrder = reset($aOrders);
-                    $aPendings = $oPayments->getPendingOrdersInfo(['id' => $aOrder['pending_id']]);
-                    if(!empty($aPendings) && is_array($aPendings)) {
-                        $aPending = reset($aPendings);
-                        if($aPending['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
-                            $sOrderOld = $aPending['order'];
-                    }
-                }    
-            }
-            else if($aLicenseOld['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
-                $sOrderOld = $aLicenseOld['order'];
-
-            if(!empty($sOrderOld) && $sOrderOld != $sOrder && (($aResult = $oPayments->cancelSubscription($sOrderOld)) === false || (int)$aResult['code'] != 0)) {
-                sendMailTemplate($CNF['ETEMPLATE_SBS_CANCEL_REQUIRED'], 0, $iClientId, [
-                    'level' => _t($aMembershipInfo['name']),
-                    'order' => $sOrderOld,
-                    'date_starts' => $this->_oConfig->formatDate($aMembershipInfo['date_starts']),
-                    'date_expires' => $this->_oConfig->formatDate($aMembershipInfo['date_expires']),
-                ], BX_EMAIL_NOTIFY, true);
-            }
-        }
+        if($bImmediate)
+            $this->cancelSubscription($iClientId, $aMembershipInfo, $sOrder);
 
         return $aItem;
     }
@@ -705,6 +676,50 @@ class BxAclModule extends BxBaseModGeneralModule
             $oProfile->getUnit()
         );
     }
+
+    public function cancelSubscription($iUserId, $aMembership = [], $sOrder = '')
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        if(!$aMembership || !is_array($aMembership))
+            $aMembership = BxDolAcl::getInstance()->getMemberMembershipInfo($iUserId);
+
+        $sLicense = '';
+        if(!$aMembership || !is_array($aMembership) || !($sLicense = $aMembership['transaction_id'] ?? false))
+            return;
+
+        $oPayments = BxDolPayments::getInstance();
+
+        $aLicense = $this->_oDb->getLicenses([
+            'type' => 'by_license', 
+            'license' => $sLicense
+        ]);
+
+        $sOrderDb = '';
+        if(!$aLicense || !is_array($aLicense)) {
+            $aOrders = $oPayments->getOrdersInfo(['license' => $sLicense]);
+            if($aOrders && is_array($aOrders)) {
+                $aOrder = reset($aOrders);
+
+                $aPendings = $oPayments->getPendingOrdersInfo(['id' => $aOrder['pending_id']]);
+                if($aPendings && is_array($aPendings)) {
+                    $aPending = reset($aPendings);
+                    if($aPending['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
+                        $sOrderDb = $aPending['order'];
+                }
+            }    
+        }
+        else if($aLicense['type'] == BX_ACL_LICENSE_TYPE_RECURRING)
+            $sOrderDb = $aLicense['order'];
+
+        if($sOrderDb && (!$sOrder || $sOrder != $sOrderDb) && (($aResult = $oPayments->cancelSubscription($sOrderDb)) === false || (int)$aResult['code'] != 0))
+            sendMailTemplate($CNF['ETEMPLATE_SBS_CANCEL_REQUIRED'], 0, $iUserId, [
+                'level' => _t($aMembership['name']),
+                'order' => $sOrderDb,
+                'date_starts' => $this->_oConfig->formatDate($aMembership['date_starts']),
+                'date_expires' => $this->_oConfig->formatDate($aMembership['date_expires']),
+            ], BX_EMAIL_NOTIFY, true);
+    }
 }
 
 /** @} */

+ 30 - 26
modules/boonex/acl/classes/BxAclTemplate.php

@@ -129,33 +129,37 @@ class BxAclTemplate extends BxBaseModGeneralTemplate
 
     public function displayLevelIcon($mixedValue)
     {
-        $bTmplVarsImage = $bTmplVarsIcon = false;
-        $aTmplVarsImage = $aTmplVarsIcon = array();
-        if(is_numeric($mixedValue) && (int)$mixedValue != 0) {
-            $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
-
-            $bTmplVarsImage = true;
-            $aTmplVarsImage = array(
-                'src' => $oStorage->getFileUrlById((int)$mixedValue),
-            );
-        }
-        else {
-            $bTmplVarsIcon = true;
-            $aTmplVarsIcon = array(
-                'name' => $mixedValue
-            );
-        }
+        list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstance()->getIcon($mixedValue);
 
-    	return $this->parseHtmlByName('level_icon.html', array(
-    	    'bx_if:show_image' => array(
-    	        'condition' => $bTmplVarsImage,
-    	        'content' => $aTmplVarsImage
-    	    ),
-    	    'bx_if:show_icon' => array(
-    	        'condition' => $bTmplVarsIcon,
-    	        'content' => $aTmplVarsIcon
-    	    )
-    	));
+        $bTmplVarsIcon = $bTmplVarsImage = $bTmplVarsIconHtml = false;
+        $aTmplVarsIcon = $aTmplVarsImage = $aTmplVarsIconHtml = [];
+        if(($bTmplVarsIcon = $sIcon !== false))
+            $aTmplVarsIcon = [
+                'name' => $sIcon
+            ];
+        else if(($bTmplVarsImage = $sIconUrl !== false))
+            $aTmplVarsImage = [
+                'src' => $sIconUrl,
+            ];
+        else if(($bTmplVarsIconHtml = $sIconHtml !== false))
+            $aTmplVarsIconHtml = [
+                'icon_html' => $sIconHtml
+            ];
+
+        return $this->parseHtmlByName('level_icon.html', [
+            'bx_if:show_image' => [
+                'condition' => $bTmplVarsImage,
+                'content' => $aTmplVarsImage
+            ],
+            'bx_if:show_icon' => [
+                'condition' => $bTmplVarsIcon,
+                'content' => $aTmplVarsIcon
+            ],
+            'bx_if:show_icon_html' => [
+                'condition' => $bTmplVarsIconHtml,
+                'content' => $aTmplVarsIconHtml 
+            ]
+        ]);
     }
 
     public function getJsCode($sType, $aParams = [], $bWrap = true)

+ 4 - 0
modules/boonex/acl/template/css/view.css

@@ -8,6 +8,10 @@ div.bx-acl-level-icon {
 	overflow: hidden;
 }
 
+div.bx-acl-level-icon svg {
+	display: inline-block;
+}
+
 div.bx-acl-level-icon .sys-icon {
 	line-height: 32px;
 	font-size: 24px;

+ 3 - 0
modules/boonex/acl/template/level_icon.html

@@ -5,4 +5,7 @@
     <bx_if:show_icon>
         <i class="sys-icon __name__"></i>
     </bx_if:show_icon>
+    <bx_if:show_icon_html>
+        __icon_html__
+    </bx_if:show_icon_html>
 </div>