BxAdsTemplate.php 15 KB

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