BxAdsSearchResult.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Ads Ads
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxAdsSearchResult extends BxBaseModTextSearchResult
  12. {
  13. protected $sModule;
  14. function __construct($sMode = '', $aParams = array())
  15. {
  16. parent::__construct($sMode, $aParams);
  17. $this->sModule = 'bx_ads';
  18. $this->oModule = BxDolModule::getInstance($this->sModule);
  19. $CNF = &$this->oModule->_oConfig->CNF;
  20. $this->aCurrent = array(
  21. 'name' => $this->sModule,
  22. 'module_name' => $this->sModule,
  23. 'object_metatags' => $CNF['OBJECT_METATAGS'],
  24. 'title' => _t('_bx_ads_page_title_browse'),
  25. 'table' => $CNF['TABLE_ENTRIES'],
  26. 'ownFields' => array('id', 'url', 'title', 'text', 'thumb', 'author', 'added', 'price', 'quantity', 'notes_purchased'),
  27. 'searchFields' => array(),
  28. 'restriction' => array(
  29. 'author' => array('value' => '', 'field' => 'author', 'operator' => '='),
  30. 'featured' => array('value' => '', 'field' => 'featured', 'operator' => '<>'),
  31. 'category' => array('value' => '', 'field' => 'category', 'operator' => '='),
  32. 'status' => array('value' => 'active', 'field' => 'status', 'operator' => '='),
  33. 'statusAdmin' => array('value' => 'active', 'field' => 'status_admin', 'operator' => '='),
  34. ),
  35. 'paginate' => array('perPage' => getParam('bx_ads_per_page_browse'), 'start' => 0),
  36. 'sorting' => 'last',
  37. 'rss' => array(
  38. 'title' => '',
  39. 'link' => '',
  40. 'image' => '',
  41. 'profile' => 0,
  42. 'fields' => array (
  43. 'Guid' => 'link',
  44. 'Link' => 'link',
  45. 'Title' => 'title',
  46. 'DateTimeUTS' => 'added',
  47. 'Desc' => 'text',
  48. 'Image' => 'thumb'
  49. ),
  50. ),
  51. 'ident' => 'id',
  52. );
  53. $this->sFilterName = 'bx_ads_filter';
  54. $sSearchFields = getParam($CNF['PARAM_SEARCHABLE_FIELDS']);
  55. $this->aCurrent['searchFields'] = !empty($sSearchFields) ? explode(',', $sSearchFields) : '';
  56. $oProfileAuthor = null;
  57. switch ($sMode) {
  58. case 'author':
  59. if(!$this->_updateCurrentForAuthor($sMode, $aParams, $oProfileAuthor))
  60. $this->isError = true;
  61. break;
  62. case 'context':
  63. if(!$this->_updateCurrentForContext($sMode, $aParams, $oProfileAuthor))
  64. $this->isError = true;
  65. break;
  66. case 'favorite':
  67. if(!$this->_updateCurrentForFavorite($sMode, $aParams, $oProfileAuthor))
  68. $this->isError = true;
  69. break;
  70. case 'public':
  71. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']);
  72. $this->aCurrent['title'] = _t('_bx_ads_page_title_browse_recent');
  73. $this->aCurrent['rss']['link'] = 'modules/?r=ads/rss/' . $sMode;
  74. break;
  75. case 'featured':
  76. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']);
  77. $this->aCurrent['title'] = _t('_bx_ads_page_title_browse_featured');
  78. $this->aCurrent['restriction']['featured']['value'] = '0';
  79. $this->aCurrent['rss']['link'] = 'modules/?r=ads/rss/' . $sMode;
  80. $this->aCurrent['sorting'] = 'featured';
  81. break;
  82. case 'popular':
  83. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_POPULAR']);
  84. $this->aCurrent['title'] = _t('_bx_ads_page_title_browse_popular');
  85. $this->aCurrent['rss']['link'] = 'modules/?r=ads/rss/' . $sMode;
  86. $this->aCurrent['sorting'] = 'popular';
  87. break;
  88. case 'updated':
  89. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_UPDATED']);
  90. $this->aCurrent['title'] = _t('_bx_ads_page_title_browse_updated');
  91. $this->aCurrent['rss']['link'] = 'modules/?r=ads/rss/' . $sMode;
  92. $this->aCurrent['sorting'] = 'updated';
  93. break;
  94. case 'category':
  95. $this->sBrowseUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES']);
  96. if(!empty($aParams['category']))
  97. $this->aCurrent['restriction']['category']['value'] = $aParams['category'];
  98. $this->aCurrent['title'] = _t('_bx_ads_page_title_browse_category');
  99. $this->aCurrent['rss']['link'] = 'modules/?r=ads/rss/' . $sMode;
  100. $this->aCurrent['sorting'] = 'last';
  101. break;
  102. case '': // search results
  103. $this->sBrowseUrl = BX_DOL_SEARCH_KEYWORD_PAGE;
  104. $this->aCurrent['title'] = _t('_bx_ads');
  105. unset($this->aCurrent['paginate']['perPage'], $this->aCurrent['rss']);
  106. break;
  107. default:
  108. $sMode = '';
  109. $this->isError = true;
  110. }
  111. $this->processReplaceableMarkers($oProfileAuthor);
  112. $this->addConditionsForPrivateContent($CNF, $oProfileAuthor);
  113. if($this->oModule->_oConfig->isPromotion())
  114. $this->addConditionsForPromotion($CNF, $oProfileAuthor, $sMode, $aParams);
  115. if(in_array($this->_sMode, ['public', 'popular', 'updated', 'featured']))
  116. $this->addConditionsForSegmentation($CNF, $oProfileAuthor, $sMode, $aParams);
  117. $this->addCustomConditions($CNF, $oProfileAuthor, $sMode, $aParams);
  118. }
  119. function displayResultBlock()
  120. {
  121. $this->oModule->_oTemplate->addJs(['main.js']);
  122. return $this->oModule->_oTemplate->getJsCode('main') . parent::displayResultBlock();
  123. }
  124. function getAlterOrder()
  125. {
  126. $CNF = &$this->oModule->_oConfig->CNF;
  127. $aSql = array();
  128. switch ($this->aCurrent['sorting']) {
  129. case 'last':
  130. $aSql['order'] = ' ORDER BY `' . $CNF['TABLE_ENTRIES'] . '`.`added` DESC';
  131. break;
  132. case 'featured':
  133. $aSql['order'] = ' ORDER BY `' . $CNF['TABLE_ENTRIES'] . '`.`featured` DESC';
  134. break;
  135. case 'updated':
  136. $aSql['order'] = ' ORDER BY `' . $CNF['TABLE_ENTRIES'] . '`.`changed` DESC';
  137. break;
  138. case 'popular':
  139. $aSql['order'] = ' ORDER BY `' . $CNF['TABLE_ENTRIES'] . '`.`views` DESC';
  140. break;
  141. }
  142. return $aSql;
  143. }
  144. protected function addConditionsForPromotion($CNF, $oProfile, $sMode, $aParams)
  145. {
  146. $CNF = &$this->oModule->_oConfig->CNF;
  147. $iDay = $this->oModule->_oConfig->getDay();
  148. $fPromotionCpm = $this->oModule->_oConfig->getPromotionCpm();
  149. if(!isset($this->aCurrent['join']))
  150. $this->aCurrent['join'] = [];
  151. $this->aCurrent['join'][] = [
  152. 'type' => 'LEFT',
  153. 'table' => $CNF['TABLE_PROMO_TRACKER'],
  154. 'mainField' => $CNF['FIELD_ID'],
  155. 'on_sql' => $this->oModule->_oDb->prepareAsString("`{$CNF['TABLE_PROMO_TRACKER']}`.`entry_id`=`{$this->aCurrent['table']}`.`{$CNF['FIELD_ID']}` AND `{$CNF['TABLE_PROMO_TRACKER']}`.`date`=?", $iDay),
  156. 'joinFields' => ['impressions', 'clicks'],
  157. ];
  158. $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], [
  159. 'budget_total' => ['value' => 0, 'field' => 'budget_total', 'operator' => '<>'],
  160. ]);
  161. if(!isset($this->aCurrent['restriction_sql']))
  162. $this->aCurrent['restriction_sql'] = "";
  163. $this->aCurrent['restriction_sql'] .= " AND `{$this->aCurrent['table']}`.`{$CNF['FIELD_BUDGET_TOTAL']}` > ({$fPromotionCpm} * `{$this->aCurrent['table']}`.`impressions`)/1000";
  164. $this->aCurrent['restriction_sql'] .= " AND (ISNULL(`{$CNF['TABLE_PROMO_TRACKER']}`.`impressions`) OR `{$this->aCurrent['table']}`.`{$CNF['FIELD_BUDGET_DAILY']}` > ({$fPromotionCpm} * `{$CNF['TABLE_PROMO_TRACKER']}`.`impressions`)/1000)";
  165. }
  166. protected function addConditionsForSegmentation($CNF, $oProfile, $sMode, $aParams)
  167. {
  168. $iViewer = bx_get_logged_profile_id();
  169. if(!$iViewer)
  170. return;
  171. $oViewer = BxDolProfile::getInstance($iViewer);
  172. if(!$oViewer)
  173. return;
  174. $sViewerModule = $oViewer->getModule();
  175. if($sViewerModule != 'bx_persons')
  176. return;
  177. $aViewerInfo = bx_srv($sViewerModule, 'get_info', [$oViewer->getContentId(), false]);
  178. if(empty($aViewerInfo) || !is_array($aViewerInfo))
  179. return;
  180. $sWhereClause = "1";
  181. if(!empty($aViewerInfo['gender']))
  182. $sWhereClause .= $this->oModule->_oDb->prepareAsString(" AND IF(`seg_gender` <> 0, `seg_gender` & ?, 1)", $aViewerInfo['gender']);
  183. if(!empty($aViewerInfo['birthday'])) {
  184. $iAge = bx_birthday2age($aViewerInfo['birthday']);
  185. $sWhereClause .= $this->oModule->_oDb->prepareAsString(" AND IF(`seg_age_min` <> 0 AND `seg_age_max` <> 0 AND `seg_age_min` <= `seg_age_max`, `seg_age_min` <= ? AND `seg_age_max` >= ?, 1)", $iAge, $iAge);
  186. }
  187. if(!empty($aViewerInfo['location'])) {
  188. $aLocation = unserialize($aViewerInfo['location']);
  189. if(!empty($aLocation['country']))
  190. $sWhereClause .= $this->oModule->_oDb->prepareAsString(" AND IF(`seg_country` <> '', `seg_country` = ?, 1)", $aLocation['country']);
  191. }
  192. if(empty($sWhereClause))
  193. return;
  194. if(!isset($this->aCurrent['restriction_sql']))
  195. $this->aCurrent['restriction_sql'] = "";
  196. $this->aCurrent['restriction_sql'] .= " AND IF(`seg` = 1, " . $sWhereClause . ", 1)";
  197. }
  198. }
  199. /** @} */