Przeglądaj źródła

Ticket #3645 - Ads: optimize serviceUpdateCategoriesStats: The rest.

AntonLV 4 lat temu
rodzic
commit
8a74f8fdc0

+ 54 - 0
modules/boonex/ads/classes/BxAdsAlertsResponse.php

@@ -17,6 +17,60 @@ class BxAdsAlertsResponse extends BxBaseModTextAlertsResponse
 
         parent::__construct();
     }
+
+    public function response($oAlert)
+    {
+        $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);           	
+        if(!method_exists($this, $sMethod))
+            return;
+
+        $this->$sMethod($oAlert);
+    }
+    
+    protected function _processProfileApprove($oAlert)
+    {
+        $this->_processProfileChangeStatus($oAlert);
+    }
+
+    protected function _processProfileActivate($oAlert)
+    {
+        $this->_processProfileChangeStatus($oAlert);
+    }
+
+    protected function _processProfileDisapprove($oAlert)
+    {
+        $this->_processProfileChangeStatus($oAlert);
+    }
+
+    protected function _processProfileSuspend($oAlert)
+    {
+        $this->_processProfileChangeStatus($oAlert);
+    }
+
+    protected function _processProfileChangeStatus($oAlert)
+    {
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        $aEntries = $this->_oModule->_oDb->getEntriesBy([
+            'type' => 'author', 
+            'author' => $oAlert->iObject
+        ]);
+
+        $aCategories = [];
+        foreach($aEntries as $aEntry) {
+            if($aEntry[$CNF['FIELD_STATUS']] != 'active' || $aEntry[$CNF['FIELD_STATUS_ADMIN']] != 'active')
+                continue;
+
+            if(in_array($aEntry[$CNF['FIELD_CATEGORY']], $aCategories))
+                continue;
+
+            $aCategories[] = $aEntry[$CNF['FIELD_CATEGORY']];
+        }
+
+        if(!empty($aCategories))
+            foreach($aCategories as $iCategoryId)
+                $this->_oModule->serviceUpdateCategoriesStatsByCategory($iCategoryId);   
+    }
 }
 
 /** @} */

+ 9 - 2
modules/boonex/ads/classes/BxAdsDb.php

@@ -222,9 +222,16 @@ class BxAdsDb extends BxBaseModTextDb
                 break;
 
             case 'collect_stats':
-                $sCountClause = "SELECT COUNT(`te`.`id`) FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te` INNER JOIN `sys_profiles` AS `p` ON(`p`.`id` = `te`.`{$CNF['FIELD_AUTHOR']}` AND `p`.`status` = 'active') WHERE `tc`.`id`=`te`.`category` AND `te`.`status`='active' AND `te`.`status_admin`='active' AND (`te`.`allow_view_to`=" . BX_DOL_PG_ALL . " OR `te`.`allow_view_to`<0)";
+                $aMethod['params'][1] = [];
+
+                $sCountClause = "SELECT COUNT(`te`.`id`) FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te` INNER JOIN `sys_profiles` AS `p` ON (`p`.`id` = `te`.`" . $CNF['FIELD_AUTHOR'] . "` AND `p`.`status` = 'active') WHERE `tc`.`id`=`te`.`" . $CNF['FIELD_CATEGORY'] . "` AND `te`.`" . $CNF['FIELD_STATUS'] . "`='active' AND `te`.`" . $CNF['FIELD_STATUS_ADMIN'] . "`='active' AND (`te`.`" . $CNF['FIELD_ALLOW_VIEW_TO'] . "`=" . BX_DOL_PG_ALL . " OR `te`.`" . $CNF['FIELD_ALLOW_VIEW_TO'] . "`<0)";
                 $sSelectClause = "`tc`.`id`, (" . $sCountClause . ") AS `count`";
-                $sGroupClause = "`tc`.`id`";
+
+                if(isset($aParams['category_id'])) {
+                    $aMethod['params'][1]['category_id'] = $aParams['category_id'];
+
+                    $sWhereClause = " AND `tc`.`id`=:category_id";
+                }
                 break;
         }
 

+ 3 - 3
modules/boonex/ads/classes/BxAdsFormsEntryHelper.php

@@ -59,7 +59,7 @@ class BxAdsFormsEntryHelper extends BxBaseModTextFormsEntryHelper
         if(!empty($s))
             return $s;
 
-        $this->_oModule->serviceUpdateCategoriesStats();
+        $this->_oModule->serviceUpdateCategoriesStats($iContentId);
 
         return '';
     }
@@ -70,7 +70,7 @@ class BxAdsFormsEntryHelper extends BxBaseModTextFormsEntryHelper
         if(!empty($s))
             return $s;
 
-        $this->_oModule->serviceUpdateCategoriesStats();
+        $this->_oModule->serviceUpdateCategoriesStats($aContentInfo);
 
         return '';
     }
@@ -81,7 +81,7 @@ class BxAdsFormsEntryHelper extends BxBaseModTextFormsEntryHelper
         if(!empty($s))
             return $s;
 
-        $this->_oModule->serviceUpdateCategoriesStats();
+        $this->_oModule->serviceUpdateCategoriesStats($aContentInfo);
 
         return '';
     }

+ 29 - 3
modules/boonex/ads/classes/BxAdsModule.php

@@ -288,9 +288,26 @@ class BxAdsModule extends BxBaseModTextModule
     	);
     }
 
-    public function serviceUpdateCategoriesStats()
+    public function serviceUpdateCategoriesStats($mixedContentInfo = false)
     {
-        $aStats = $this->_oDb->getCategories(array('type' => 'collect_stats'));
+        $aContentInfo = [];
+        if(!empty($mixedContentInfo))
+            $aContentInfo = !is_array($mixedContentInfo) ? $this->_oDb->getContentInfoById((int)$mixedContentInfo) : $mixedContentInfo;
+
+        $iCategoryId = 0;
+        if($aContentInfo && !empty($aContentInfo['category']))
+            $iCategoryId = (int)$aContentInfo['category'];
+
+        return $this->serviceUpdateCategoriesStatsByCategory($iCategoryId);
+    }
+
+    public function serviceUpdateCategoriesStatsByCategory($iCategoryId = 0)
+    {
+        $aParams = ['type' => 'collect_stats'];
+        if($iCategoryId)
+            $aParams['category_id'] = (int)$iCategoryId;
+
+        $aStats = $this->_oDb->getCategories($aParams);
         if(empty($aStats) || !is_array($aStats))
             return true;
 
@@ -1465,7 +1482,14 @@ class BxAdsModule extends BxBaseModTextModule
 
         return CHECK_ACTION_RESULT_ALLOWED;
     }
-    
+
+    public function onPublished($iContentId)
+    {
+        $this->serviceUpdateCategoriesStats($iContentId);
+
+        parent::onPublished($iContentId);
+    }
+
     public function onOfferAdded($iOfferId, &$aResult)
     {
         $CNF = &$this->_oConfig->CNF;
@@ -1824,6 +1848,8 @@ class BxAdsModule extends BxBaseModTextModule
         if(!$this->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => $sStatus), array($CNF['FIELD_ID'] => $iContentId)))
             return array('msg' => _t('_bx_ads_txt_err_cannot_perform_action'));
 
+        $this->serviceUpdateCategoriesStats($aContentInfo);
+
         return array(
             'reload' => 1
         );

+ 4 - 0
modules/boonex/ads/install/sql/enable.sql

@@ -617,6 +617,10 @@ SET @iHandler := LAST_INSERT_ID();
 
 INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
 ('system', 'save_setting', @iHandler),
+('profile', 'approve', @iHandler),
+('profile', 'activate', @iHandler),
+('profile', 'disapprove', @iHandler),
+('profile', 'suspend', @iHandler),
 ('profile', 'delete', @iHandler),
 
 ('bx_ads_videos_mp4', 'transcoded', @iHandler);