BxAdsTemplate.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /*
  12. * Module representation.
  13. */
  14. class BxAdsTemplate extends BxBaseModTextTemplate
  15. {
  16. function __construct(&$oConfig, &$oDb)
  17. {
  18. $this->MODULE = 'bx_ads';
  19. parent::__construct($oConfig, $oDb);
  20. $this->aMethodsToCallAddJsCss[] = 'categories';
  21. }
  22. public function entryPromotionGrowth($aContentInfo)
  23. {
  24. $CNF = &$this->_oConfig->CNF;
  25. $sDateFrom = date('Y-m-d', $aContentInfo[$CNF['FIELD_ADDED']]);
  26. $sDateTo = date('Y-m-d', time());
  27. $aForm = [
  28. 'form_attrs' => [
  29. 'id' => 'bx_chart_controls',
  30. 'action' => ''
  31. ],
  32. 'inputs' => [
  33. 'object' => [
  34. 'type' => 'select',
  35. 'name' => 'object',
  36. 'caption' => _t('_sys_chart_growth_object'),
  37. 'info' => '',
  38. 'value' => '',
  39. 'values' => [],
  40. 'required' => '0',
  41. 'attrs' => [
  42. 'id' => 'bx_chart_growth_objects',
  43. 'onchange' => 'oBxDolChartGrowth.loadData()'
  44. ],
  45. ],
  46. 'date_from' => [
  47. 'type' => 'datepicker',
  48. 'name' => 'date_from',
  49. 'caption' => _t('_sys_chart_growth_date_from'),
  50. 'info' => '',
  51. 'value' => $sDateFrom,
  52. 'values' => [],
  53. 'required' => '0',
  54. 'attrs' => [
  55. 'id' => 'bx_chart_growth_date_from',
  56. 'onchange' => 'oBxDolChartGrowth.loadData()'
  57. ],
  58. ],
  59. 'date_to' => [
  60. 'type' => 'datepicker',
  61. 'name' => 'date_to',
  62. 'caption' => _t('_sys_chart_growth_date_to'),
  63. 'info' => '',
  64. 'value' => $sDateTo,
  65. 'values' => [],
  66. 'required' => '0',
  67. 'attrs' => [
  68. 'id' => 'bx_chart_growth_date_to',
  69. 'onchange' => 'oBxDolChartGrowth.loadData()'
  70. ],
  71. ]
  72. ]
  73. ];
  74. foreach($CNF['OBJECT_PROMOTION_CHARTS'] as $sChart) {
  75. $aChart = BxDolChartQuery::getChartObject($sChart);
  76. if(!$aChart)
  77. continue;
  78. $aForm['inputs']['object']['values'][] = ['key' => $sChart, 'value' => _t($aChart['title'])];
  79. }
  80. $oForm = new BxTemplFormView($aForm);
  81. $oTemplate = BxDolTemplate::getInstance();
  82. $oTemplate->addJs(['chart.min.js', 'BxDolChartGrowth.js']);
  83. $oTemplate->addCss(['chart.css']);
  84. if($this->_bIsApi){
  85. return [bx_api_get_block('chart', ['type' => 'line', 'title' => 'Promotion for ' . $aContentInfo[$CNF['FIELD_TITLE']], 'form' => $oForm->getCodeApi(), 'endpoint' => 'system/get_data_by_interval/TemplChartServices', 'params' =>['object' => $aForm['inputs']['object']['values'][0]['key'], 'date_from' => $sDateFrom, 'date_to' => $sDateTo, 'content_id' => $aContentInfo[$CNF['FIELD_ID']]]])];
  86. }
  87. return $oTemplate->parseHtmlByName('chart_growth.html', [
  88. 'controls' => $oForm->getCode(),
  89. 'date_from' => $sDateFrom,
  90. 'date_to' => $sDateTo,
  91. 'request_params' => json_encode([
  92. 'content_id' => $aContentInfo[$CNF['FIELD_ID']]
  93. ]),
  94. ]);
  95. }
  96. public function entryPromotionSummary($aContentInfo)
  97. {
  98. $CNF = &$this->_oConfig->CNF;
  99. $aTmplVarsDataLabels = $aTmplVarsDataSet = [];
  100. foreach(['impressions', 'clicks'] as $sItem) {
  101. $sTitle = bx_html_attribute(_t($CNF['T']['chart_label_' . $sItem]));
  102. $iValue = (int)$aContentInfo[$sItem];
  103. $aTmplVarsDataLabels[] = $sTitle . ' - ' . $iValue;
  104. $aTmplVarsDataSet['data'][] = $iValue;
  105. $aTmplVarsDataSet['backgroundColor'][] = '#' . dechex(rand(0x000000, 0xFFFFFF));
  106. }
  107. if($this->_bIsApi){
  108. return [bx_api_get_block('chart', ['type' => 'pie', 'title' => 'Summary', 'labels' => $aTmplVarsDataLabels, 'data' => $aTmplVarsDataSet])];
  109. }
  110. $this->addJs(['chart.min.js']);
  111. $this->addCss(['chart.css']);
  112. return $this->parseHtmlByName('chart_stats.html', [
  113. 'chart_id' => $this->MODULE . '_chart_summary',
  114. 'chart_data' => json_encode([
  115. 'labels' => $aTmplVarsDataLabels,
  116. 'datasets' => [$aTmplVarsDataSet]
  117. ])
  118. ]);
  119. }
  120. public function entryPromotionRoi($aContentInfo)
  121. {
  122. $CNF = &$this->_oConfig->CNF;
  123. $aTmplVarsDataLabels = $aTmplVarsDataSet = [];
  124. //--- Get income local.
  125. $sTitleLocal = bx_html_attribute(_t($CNF['T']['chart_label_roi_local']));
  126. $iValueLocal = (int)$this->_oDb->getLicense(['type' => 'entry_id_income', 'entry_id' => $aContentInfo[$CNF['FIELD_ID']]]);
  127. $aTmplVarsDataLabels[] = $sTitleLocal . ' - ' . $iValueLocal;
  128. $aTmplVarsDataSet['data'][] = $iValueLocal;
  129. $aTmplVarsDataSet['backgroundColor'][] = '#' . dechex(rand(0x000000, 0xFFFFFF));
  130. //--- Get income external (from source)
  131. $iValueSource = 0;
  132. if($this->_oConfig->isSources()) {
  133. $oSource = $this->_oModule->getObjectSource($aContentInfo[$CNF['FIELD_SOURCE_TYPE']], $aContentInfo[$CNF['FIELD_AUTHOR']]);
  134. if($oSource !== false && !empty($aContentInfo[$CNF['FIELD_SOURCE']])) {
  135. $aOrders = $oSource->getOrders([
  136. 'sample' => 'sales_by_product_id',
  137. 'product_id' => $aContentInfo[$CNF['FIELD_SOURCE']],
  138. 'date_from' => $aContentInfo[$CNF['FIELD_ADDED']]
  139. ]);
  140. $sTitleSource = bx_html_attribute(_t($CNF['T']['chart_label_roi_source']));
  141. foreach($aOrders as $aOrder)
  142. $iValueSource += $aOrder['amount'];
  143. $aTmplVarsDataLabels[] = $sTitleSource . ' - ' . $iValueSource;
  144. $aTmplVarsDataSet['data'][] = $iValueSource;
  145. $aTmplVarsDataSet['backgroundColor'][] = '#' . dechex(rand(0x000000, 0xFFFFFF));
  146. }
  147. }
  148. //--- Get investments
  149. $sTitleInvestment = bx_html_attribute(_t($CNF['T']['chart_label_roi_investment']));
  150. $iValueInvestment = (int)$this->_oDb->getPromotionLicense(['type' => 'entry_id_outcome', 'entry_id' => $aContentInfo[$CNF['FIELD_ID']]]);
  151. $aTmplVarsDataLabels[] = $sTitleInvestment . ' - ' . $iValueInvestment;
  152. $aTmplVarsDataSet['data'][] = $iValueInvestment;
  153. $aTmplVarsDataSet['backgroundColor'][] = '#' . dechex(rand(0x000000, 0xFFFFFF));
  154. $fRoi = 100 * ($iValueLocal + $iValueSource - $iValueInvestment)/$iValueInvestment;
  155. if($this->_bIsApi){
  156. return [bx_api_get_block('chart', ['type' => 'pie', 'title' => 'Roi', 'labels' => $aTmplVarsDataLabels, 'data' => $aTmplVarsDataSet, 'text' => 'ROI: ' . $fRoi . '%'])];
  157. }
  158. $this->addJs(['chart.min.js']);
  159. $this->addCss(['chart.css']);
  160. return $this->parseHtmlByName('entry-roi.html', [
  161. 'chart' => $this->parseHtmlByName('chart_stats.html', [
  162. 'chart_id' => $this->MODULE . '_chart_roi',
  163. 'chart_data' => json_encode([
  164. 'labels' => $aTmplVarsDataLabels,
  165. 'datasets' => [$aTmplVarsDataSet]
  166. ])
  167. ]),
  168. 'roi' => round($fRoi, 1)
  169. ]);
  170. }
  171. public function entryOfferAccepted($iUserId, $aContent, $aOffer)
  172. {
  173. $CNF = &$this->_oConfig->CNF;
  174. $oPayments = BxDolPayments::getInstance();
  175. $iContentAuthorId = (int)$aContent[$CNF['FIELD_AUTHOR']];
  176. $sJsCodePay = $sJsMethodPay = '';
  177. $aJsPay = $oPayments->getAddToCartJs($iContentAuthorId, $this->MODULE, $aContent[$CNF['FIELD_ID']], $aOffer[$CNF['FIELD_OFR_QUANTITY']], true);
  178. if(!empty($aJsPay) && is_array($aJsPay))
  179. list($sJsCodePay, $sJsMethodPay) = $aJsPay;
  180. return $this->parseHtmlByName('entry-offer-accepted.html', array(
  181. 'js_object' => $this->_oConfig->getJsObject('entry'),
  182. 'id' => $aOffer['id'],
  183. 'amount' => _t_format_currency_ext($aOffer['amount'], [
  184. 'sign' => $oPayments->getCurrencySign($iContentAuthorId)
  185. ]),
  186. 'quantity' => _t('_bx_ads_txt_n_items', $aOffer['quantity']),
  187. 'bx_if:show_pay' => array(
  188. 'condition' => !empty($sJsMethodPay),
  189. 'content' => array(
  190. 'js_method_pay' => $sJsMethodPay,
  191. )
  192. ),
  193. 'js_code' => $sJsCodePay
  194. ));
  195. }
  196. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  197. {
  198. $CNF = &$this->_oConfig->CNF;
  199. $oPermalink = BxDolPermalinks::getInstance();
  200. $aTmplVarsItems = array();
  201. $this->_entryBreadcrumb($aContentInfo[$CNF['FIELD_CATEGORY']], $oPermalink, $aTmplVarsItems);
  202. $aTmplVarsItems = array_reverse($aTmplVarsItems);
  203. $aTmplVarsItems[] = array(
  204. 'url' => bx_absolute_url($oPermalink->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']])),
  205. 'title' => bx_process_output($aContentInfo[$CNF['FIELD_TITLE']])
  206. );
  207. return parent::entryBreadcrumb($aContentInfo, $aTmplVarsItems);
  208. }
  209. public function categoriesList($aParams = array())
  210. {
  211. $CNF = &$this->_oConfig->CNF;
  212. $sResult = $this->_categoriesList(0, array(
  213. 'url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array('category' => '')))
  214. ));
  215. if(empty($sResult) && isset($aParams['show_empty']) && $aParams['show_empty'] === true)
  216. $sResult = MsgBox(_t('_Empty'));
  217. return $sResult;
  218. }
  219. public function getEntryLink($aEntry)
  220. {
  221. $CNF = &$this->_oConfig->CNF;
  222. return $this->getLink('entry-link', array(
  223. 'href' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'], array(
  224. 'id' => $aEntry[$CNF['FIELD_ID']]
  225. ))),
  226. 'title' => bx_html_attribute($aEntry[$CNF['FIELD_TITLE']]),
  227. 'content' => $aEntry[$CNF['FIELD_TITLE']]
  228. ));
  229. }
  230. protected function getUnit($aData, $aParams = [])
  231. {
  232. $CNF = &$this->_oConfig->CNF;
  233. $aResult = parent::getUnit($aData, $aParams);
  234. if($this->_oConfig->isSources() && !empty($aData[$CNF['FIELD_URL']])) {
  235. $aResult = array_merge($aResult, [
  236. 'content_url' => $aData[$CNF['FIELD_URL']],
  237. ]);
  238. if($aResult['bx_if:thumb']['condition'])
  239. $aResult['bx_if:thumb']['content'] = array_merge($aResult['bx_if:thumb']['content'], [
  240. 'content_url' => $aData[$CNF['FIELD_URL']],
  241. ]);
  242. }
  243. if($this->_oConfig->isPromotion()) {
  244. $sJsObject = $this->_oConfig->getJsObject('main');
  245. $sContentOnclick = 'return ' . $sJsObject . '.registerClick(this, ' . $aData[$CNF['FIELD_ID']] . ')';
  246. $aResult = array_merge($aResult, [
  247. 'bx_if:show_onclick' => [
  248. 'condition' => true,
  249. 'content' => [
  250. 'content_onclick' => $sContentOnclick
  251. ]
  252. ],
  253. 'unit_content_after' => $this->_wrapInTagJsCode($sJsObject . '.registerTracker(' . $aData[$CNF['FIELD_ID']] . ');'),
  254. ]);
  255. if($aResult['bx_if:thumb']['condition'])
  256. $aResult['bx_if:thumb']['content'] = array_merge($aResult['bx_if:thumb']['content'], [
  257. 'bx_if:show_thumb_onclick' => [
  258. 'condition' => true,
  259. 'content' => [
  260. 'content_onclick' => $sContentOnclick
  261. ]
  262. ],
  263. ]);
  264. }
  265. return $aResult;
  266. }
  267. /**
  268. * Use Gallery image for both because currently there is no Unit types with small thumbnails.
  269. */
  270. protected function getUnitThumbAndGallery ($aData)
  271. {
  272. list($sPhotoThumb, $sPhotoGallery) = parent::getUnitThumbAndGallery($aData);
  273. return array($sPhotoGallery, $sPhotoGallery);
  274. }
  275. protected function _categoriesList($iParentId, $aParams = array())
  276. {
  277. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  278. $aTmplVars = array();
  279. foreach($aCategories as $aCategory) {
  280. $iItems = (int)$aCategory['items'];
  281. $sSibcategories = $this->_categoriesList($aCategory['id'], $aParams);
  282. if($iItems == 0 && empty($sSibcategories))
  283. continue;
  284. $aTmplVars[] = array(
  285. 'url' => $aParams['url'] . $aCategory['id'],
  286. 'title' => _t($aCategory['title']),
  287. 'bx_if:show_icon' => array(
  288. 'condition' => !empty($aCategory['icon']),
  289. 'content' => array(
  290. 'icon' => $aCategory['icon'],
  291. )
  292. ),
  293. 'bx_if:show_counter' => array(
  294. 'condition' => $iItems != 0,
  295. 'content' => array(
  296. 'items' => $iItems,
  297. )
  298. ),
  299. 'bx_if:show_subcategories' => array(
  300. 'condition' => !empty($sSibcategories),
  301. 'content' => array(
  302. 'subcategories' => $sSibcategories
  303. )
  304. )
  305. );
  306. }
  307. if(empty($aTmplVars))
  308. return '';
  309. return $this->parseHtmlByName('categories.html', array(
  310. 'bx_repeat:categories' => $aTmplVars
  311. ));
  312. }
  313. protected function _entryBreadcrumb($iCategory, &$oPermalink, &$aTmplVarsItems)
  314. {
  315. $CNF = &$this->_oConfig->CNF;
  316. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  317. if(empty($aCategory) || !is_array($aCategory))
  318. return;
  319. $aTmplVarsItems[] = array(
  320. 'url' => bx_absolute_url($oPermalink->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $aCategory['id']))),
  321. 'title' => bx_process_output(_t($aCategory['title']))
  322. );
  323. if(empty($aCategory['parent_id']))
  324. return;
  325. $this->_entryBreadcrumb((int)$aCategory['parent_id'], $oPermalink, $aTmplVarsItems);
  326. }
  327. }
  328. /** @} */