1
0
Эх сурвалжийг харах

Ticket #4561 - Ads: Enhancements. Ppoint 3.

AntonLV 2 жил өмнө
parent
commit
6fc9a1cdbe

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

@@ -73,6 +73,40 @@ class BxAdsAlertsResponse extends BxBaseModTextAlertsResponse
             foreach($aCategories as $iCategoryId)
                 $this->_oModule->serviceUpdateCategoriesStatsByCategory($iCategoryId);   
     }
+
+    protected function _processBxTimelineGetView($oAlert)
+    {
+        if(!$this->_oModule->_oConfig->isPromotion())
+            return;
+
+        $bDynamicMode = isset($oAlert->aExtras['params']['dynamic_mode']) && (bool)$oAlert->aExtras['params']['dynamic_mode'] === true;
+
+        $sJs = $this->_oModule->_oTemplate->addJs(['main.js'], $bDynamicMode);
+        $oAlert->aExtras['content_before'] .= ($bDynamicMode ? $sJs : '') . $this->_oModule->_oTemplate->getJsCode('main');
+    }
+
+    protected function _processBxTimelineGetExternalPost($oAlert)
+    {
+        if(!$this->_oModule->_oConfig->isPromotion())
+            return;
+
+        $aEntries = $this->_oModule->_oDb->getEntriesBy(['type' => 'promotion']);
+        if(empty($aEntries) || !is_array($aEntries))
+            return;
+
+        $iId = $this->_oModule->_oConfig->getRandomWeightedItem($aEntries);
+
+        $sModule = 'bx_timeline';
+        $aEvent = bx_srv($sModule, 'get_events_by_descriptor', [$this->MODULE, 'added', $iId]);
+        if(empty($aEvent) || !is_array($aEvent))
+            return;
+
+        $mixedEvent = bx_srv($sModule, 'get_post', [$aEvent, $oAlert->aExtras['params']]);
+        if(empty($mixedEvent))
+            return;
+
+        $oAlert->aExtras['override_result'] = $mixedEvent;
+    }
 }
 
 /** @} */

+ 8 - 0
modules/boonex/ads/classes/BxAdsConfig.php

@@ -432,6 +432,14 @@ class BxAdsConfig extends BxBaseModTextConfig
     {
         return 'bx_ads_shopify_' . bx_site_hash() . '.php';
     }
+    
+    public function getRandomWeightedItem($aWeightedValues)
+    {
+        $iRand = mt_rand(1, (int)array_sum($aWeightedValues));
+        foreach($aWeightedValues as $iKey => $iValue)
+            if(($iRand -= $iValue) <= 0)
+                return $iKey;
+    }
 }
 
 /** @} */

+ 18 - 0
modules/boonex/ads/classes/BxAdsDb.php

@@ -205,6 +205,24 @@ class BxAdsDb extends BxBaseModTextDb
                 $sWhereClause .= " AND UNIX_TIMESTAMP() - `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_CHANGED'] . "` > :days";
                 break;
 
+            case 'promotion':
+                $aMethod['name'] = 'getPairs';
+                $aMethod['params'][1] = 'id';
+                $aMethod['params'][2] = 'weight';
+                $aMethod['params'][3] = [
+                    'today' => $this->_oConfig->getDay(),
+                    'promotion_cpm' => $this->_oConfig->getPromotionCpm(),
+                ];
+
+                $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` AS `id`, CAST(`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_DAILY'] . "` AS UNSIGNED) AS `weight`";
+                $sJoinClause .= " LEFT JOIN `" . $CNF['TABLE_PROMO_TRACKER'] . "` AS `tpt` ON `tpt`.`entry_id`=`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` AND `tpt`.`date` = :today";
+                //--- Check Total Budget
+                $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_TOTAL'] . "` <> 0 AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_TOTAL'] . "` > (:promotion_cpm * `" . $CNF['TABLE_ENTRIES'] . "`.`impressions`)/1000";
+                //--- Check Daily Budget
+                $sWhereClause .= " AND (ISNULL(`tpt`.`impressions`) OR `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_BUDGET_DAILY'] . "` > (:promotion_cpm * `tpt`.`impressions`)/1000)";
+                $sOrderClause = "";
+                break;
+
             default:
                 return parent::getEntriesBy($aParams);
         }

+ 34 - 1
modules/boonex/ads/classes/BxAdsModule.php

@@ -644,6 +644,22 @@ class BxAdsModule extends BxBaseModTextModule
         return $mixedResult;
     }
 
+    public function serviceGetTimelinePost($aEvent, $aBrowseParams = [])
+    {
+        $CNF = &$this->_oConfig->CNF;
+
+        $mixedResult = parent::serviceGetTimelinePost($aEvent, $aBrowseParams);
+        if(!$mixedResult)
+            return $mixedResult;
+        
+        $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
+
+        if($this->_oConfig->isSources() && !empty($aContentInfo[$CNF['FIELD_URL']]))
+            $mixedResult['url'] = $aContentInfo[$CNF['FIELD_URL']];
+
+        return $mixedResult;
+    }
+
     public function serviceGetNotificationsData()
     {
         $sModule = $this->_aModule['name'];
@@ -2427,7 +2443,24 @@ class BxAdsModule extends BxBaseModTextModule
                 'text' => $aResult['text']
             )) . ($bDynamic ? $sInclude : '');
         }
-        
+
+        if($this->_oConfig->isSources() && !empty($aContentInfo[$CNF['FIELD_URL']]))
+            $aResult['url'] = $aContentInfo[$CNF['FIELD_URL']];
+
+        if($this->_oConfig->isPromotion()) {
+            $sJsObject = $this->_oConfig->getJsObject('main');
+            $sHtmlId = $this->_oConfig->getHtmlIds('unit') . $aContentInfo[$CNF['FIELD_ID']] . '-' . time() . '-' . mt_rand(0, 100);
+
+            $aResult = array_merge($aResult, [
+                'url' => 'javascript:void(0)',
+                'onclick' => 'return ' . $sJsObject . '.registerClick(this, ' . $aContentInfo[$CNF['FIELD_ID']] . ')',
+                'raw' => $this->_oTemplate->parseHtmlByName('timeline_post_promotion.html', [
+                    'html_id' => $sHtmlId,
+                    'js_code' => $this->_oTemplate->_wrapInTagJsCode($sJsObject . '.registerTrakerForTimeline(' . $aContentInfo[$CNF['FIELD_ID']] . ', \'' . $sHtmlId . '\');')
+                ])
+            ]);
+        }
+
         return $aResult;
     }
 

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

@@ -680,7 +680,10 @@ INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
 ('profile', 'suspend', @iHandler),
 ('profile', 'delete', @iHandler),
 
-('bx_ads_videos_mp4', 'transcoded', @iHandler);
+('bx_ads_videos_mp4', 'transcoded', @iHandler),
+
+('bx_timeline', 'get_view', @iHandler);
+('bx_timeline', 'get_external_post', @iHandler);
 
 
 -- CRON

+ 23 - 0
modules/boonex/ads/js/main.js

@@ -39,6 +39,29 @@ BxAdsMain.prototype.registerTraker = function(iId) {
     });
 };
 
+BxAdsMain.prototype.registerTrakerForTimeline = function(iId, sHtmlId) {
+    var $this = this;
+
+    if(typeof window.glBxAdsTracked === 'undefined')
+        window.glBxAdsTracked = [];
+
+    $(window).on('load scroll', function() {
+        if(glBxAdsTracked[sHtmlId])
+            return;
+
+        var oEvent = $('#' + sHtmlId).parents('.bx-tl-item:first');
+        if(!oEvent || oEvent.length == 0)
+            return;
+
+        oEvent = oEvent.get(0);
+        if(oEvent && $this.isVisible(oEvent)) {
+            $this.registerImpression(oEvent, iId);
+
+            glBxAdsTracked[sHtmlId] = true;
+        }
+    });
+};
+
 BxAdsMain.prototype.registerImpression = function(oElement, iId) {
     jQuery.get (
         this._sActionsUrl + 'register_impression/' + iId,

+ 2 - 0
modules/boonex/ads/template/timeline_post_promotion.html

@@ -0,0 +1,2 @@
+<div id="__html_id__" class="bx-ads-promotion-anchor hidden"></div>
+__js_code__