BxAdsModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. * Ads module
  13. */
  14. class BxAdsModule extends BxBaseModTextModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. $CNF = &$this->_oConfig->CNF;
  20. $this->_aSearchableNamesExcept = array_merge($this->_aSearchableNamesExcept, array(
  21. $CNF['FIELD_CATEGORY_VIEW'],
  22. $CNF['FIELD_CATEGORY_SELECT']
  23. ));
  24. }
  25. public function actionGetCategoryForm()
  26. {
  27. if(bx_get('category') === false)
  28. return echoJson(array());
  29. return echoJson(array(
  30. 'content' => $this->serviceGetCreatePostForm(array(
  31. 'absolute_action_url' => true,
  32. 'dynamic_mode' => true
  33. ))
  34. ));
  35. }
  36. public function actionInterested()
  37. {
  38. $CNF = &$this->_oConfig->CNF;
  39. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  40. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  41. if(empty($aContentInfo) || !is_array($aContentInfo))
  42. return echoJson(array());
  43. $iViewer = bx_get_logged_profile_id();
  44. $oViewer = BxDolProfile::getInstance($iViewer);
  45. if(!$oViewer)
  46. return echoJson(array());
  47. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  48. if($iContentAuthor == $iViewer)
  49. return echoJson(array('msg' => _t('_bx_ads_txt_err_your_own')));
  50. if($this->_oDb->isInterested($iContentId, $iViewer))
  51. return echoJson(array('msg' => _t('_bx_ads_txt_err_duplicate')));
  52. if(!$this->_oDb->insertInterested(array('entry_id' => $iContentId, 'profile_id' => $iViewer)))
  53. return echoJson(array('msg' => _t('_bx_ads_txt_err_cannot_perform_action')));
  54. sendMailTemplate($CNF['ETEMPLATE_INTERESTED'], 0, $iContentAuthor, array(
  55. 'viewer_name' => $oViewer->getDisplayName(),
  56. 'viewer_url' => $oViewer->getUrl(),
  57. 'ad_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  58. 'ad_url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php', array(
  59. 'i' => $CNF['URI_VIEW_ENTRY'],
  60. $CNF['FIELD_ID'] => $iContentId
  61. ))
  62. ));
  63. return echoJson(array('msg' => _t('_bx_ads_txt_msg_author_notified')));
  64. }
  65. public function serviceGetSafeServices()
  66. {
  67. $a = parent::serviceGetSafeServices();
  68. return array_merge($a, array (
  69. 'EntityReviews' => '',
  70. 'EntityReviewsRating' => '',
  71. 'CategoriesList' => '',
  72. 'BrowseCategory' => '',
  73. ));
  74. }
  75. public function serviceUpdateCategoriesStats()
  76. {
  77. $aStats = $this->_oDb->getCategories(array('type' => 'collect_stats'));
  78. if(empty($aStats) || !is_array($aStats))
  79. return true;
  80. $iUpdated = 0;
  81. foreach($aStats as $aStat)
  82. if($this->_oDb->updateCategory(array('items' => $aStat['count']), array('id' => $aStat['id'])))
  83. $iUpdated++;
  84. return count($aStats) == $iUpdated;
  85. }
  86. public function serviceGetCategoryOptions($iParentId, $bPleaseSelect = false)
  87. {
  88. $aValues = array();
  89. if($bPleaseSelect)
  90. $aValues[] = array('key' => '', 'value' => _t('_sys_please_select'));
  91. $this->_getCategoryOptions($iParentId, $aValues);
  92. return $aValues;
  93. }
  94. public function serviceGetSearchableFields($aInputsAdd = array())
  95. {
  96. $CNF = &$this->_oConfig->CNF;
  97. $aResult = parent::serviceGetSearchableFields(array_merge($aInputsAdd, $this->_getSearchableFields()));
  98. unset($aResult[$CNF['FIELD_CATEGORY_VIEW']], $aResult[$CNF['FIELD_CATEGORY_SELECT']]);
  99. return $aResult;
  100. }
  101. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  102. {
  103. $CNF = &$this->_oConfig->CNF;
  104. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  105. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  106. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  107. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  108. }
  109. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  110. }
  111. public function serviceEntityCreate ($sParams = false)
  112. {
  113. if(($sDisplay = $this->getCategoryDisplay('add')) !== false) {
  114. if(empty($sParams) || !is_array($sParams))
  115. $sParams = array();
  116. $sParams['display'] = $sDisplay;
  117. }
  118. return parent::serviceEntityCreate($sParams);
  119. }
  120. /**
  121. * @page service Service Calls
  122. * @section bx_ads Ads
  123. * @subsection bx_ads-page_blocks Page Blocks
  124. * @subsubsection bx_ads-entity_reviews entity_reviews
  125. *
  126. * @code bx_srv('bx_ads', 'entity_reviews', [...]); @endcode
  127. *
  128. * Get reviews for particular content
  129. * @param $iContentId content ID
  130. *
  131. * @see BxAdsModule::serviceEntityReviews
  132. */
  133. /**
  134. * @ref bx_ads-entity_reviews "entity_reviews"
  135. */
  136. public function serviceEntityReviews($iContentId = 0)
  137. {
  138. $CNF = &$this->_oConfig->CNF;
  139. if(empty($CNF['OBJECT_REVIEWS']))
  140. return false;
  141. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  142. }
  143. /**
  144. * @page service Service Calls
  145. * @section bx_ads Ads
  146. * @subsection bx_ads-page_blocks Page Blocks
  147. * @subsubsection bx_ads-entity_reviews_rating entity_reviews_rating
  148. *
  149. * @code bx_srv('bx_ads', 'entity_reviews_rating', [...]); @endcode
  150. *
  151. * Get reviews rating for particular content
  152. * @param $iContentId content ID
  153. *
  154. * @see BxAdsModule::serviceEntityReviewsRating
  155. */
  156. /**
  157. * @ref bx_ads-entity_reviews_rating "entity_reviews_rating"
  158. */
  159. public function serviceEntityReviewsRating($iContentId = 0)
  160. {
  161. $CNF = &$this->_oConfig->CNF;
  162. if(empty($CNF['OBJECT_REVIEWS']))
  163. return false;
  164. if(!$iContentId)
  165. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  166. if(!$iContentId)
  167. return false;
  168. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  169. if (!$oCmts || !$oCmts->isEnabled())
  170. return false;
  171. return $oCmts->getRatingBlock(array('in_designbox' => false));
  172. }
  173. /**
  174. * @page service Service Calls
  175. * @section bx_ads Ads
  176. * @subsection bx_ads-page_blocks Page Blocks
  177. * @subsubsection bx_ads-categories_list categories_list
  178. *
  179. * @code bx_srv('bx_ads', 'categories_list', [...]); @endcode
  180. *
  181. * Get reviews rating for particular content
  182. * @param $aParams additional params array, such as 'show_empty'
  183. *
  184. * @see BxAdsModule::serviceCategoriesList
  185. */
  186. /**
  187. * @ref bx_ads-categories_list "categories_list"
  188. */
  189. public function serviceCategoriesList($aParams = array())
  190. {
  191. if(!isset($aParams['show_empty']))
  192. $aParams['show_empty'] = true;
  193. return $this->_oTemplate->categoriesList($aParams);
  194. }
  195. /**
  196. * @page service Service Calls
  197. * @section bx_ads Ads
  198. * @subsection bx_ads-browse Browse
  199. * @subsubsection bx_ads-browse_category browse_category
  200. *
  201. * @code bx_srv('bx_ads', 'browse_category', [...]); @endcode
  202. *
  203. * Get reviews rating for particular content
  204. * @param $iCategoryId category ID
  205. * @param $aParams additional params array, such as empty_message, ajax_paginate, etc
  206. *
  207. * @see BxAdsModule::serviceBrowseCategory
  208. */
  209. /**
  210. * @ref bx_ads-browse_category "browse_category"
  211. */
  212. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  213. {
  214. $sParamName = $sParamGet = 'category';
  215. if(!$iCategoryId && bx_get($sParamGet) !== false)
  216. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  217. $bEmptyMessage = true;
  218. if(isset($aParams['empty_message'])) {
  219. $bEmptyMessage = (bool)$aParams['empty_message'];
  220. unset($aParams['empty_message']);
  221. }
  222. $bAjaxPaginate = true;
  223. if(isset($aParams['ajax_paginate'])) {
  224. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  225. unset($aParams['ajax_paginate']);
  226. }
  227. $aBlock = $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $iCategoryId), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  228. if(!empty($aBlock['content'])) {
  229. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  230. if(!empty($aCategory['title']))
  231. $aBlock['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  232. }
  233. return $aBlock;
  234. }
  235. /**
  236. * Common methods.
  237. */
  238. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  239. {
  240. if(empty($iCategory) && bx_get('category') !== false)
  241. $iCategory = (int)bx_get('category');
  242. if(empty($iCategory))
  243. return false;
  244. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  245. $sKey = 'type_display_' . $sDisplayType;
  246. if(empty($aCategory[$sKey]))
  247. return false;
  248. return $aCategory[$sKey];
  249. }
  250. /**
  251. * Internal methods.
  252. */
  253. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  254. {
  255. $CNF = &$this->_oConfig->CNF;
  256. $mixedContent = $this->_getContent($iContentId, true);
  257. if($mixedContent === false)
  258. return false;
  259. list($iContentId, $aContentInfo) = $mixedContent;
  260. $sDisplayType = false;
  261. switch($sFormMethod) {
  262. case 'editDataForm':
  263. $sDisplayType = 'edit';
  264. break;
  265. case 'viewDataForm':
  266. case 'viewDataEntry':
  267. $sDisplayType = 'view';
  268. break;
  269. }
  270. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  271. $sDisplay = $sDisplayNew;
  272. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  273. }
  274. protected function _getCategoryOptions($iParentId, &$aValues)
  275. {
  276. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  277. foreach($aCategories as $aCategory) {
  278. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  279. $this->_getCategoryOptions($aCategory['id'], $aValues);
  280. }
  281. }
  282. protected function _getSearchableFields($mixedDisplayType = '')
  283. {
  284. $CNF = &$this->_oConfig->CNF;
  285. if(empty($mixedDisplayType))
  286. $mixedDisplayType = array('add', 'edit');
  287. $aResult = array();
  288. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  289. foreach($aDisplays as $aDisplay) {
  290. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  291. continue;
  292. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  293. if(!$oForm)
  294. continue;
  295. $aResult = array_merge($aResult, $oForm->aInputs);
  296. }
  297. return $aResult;
  298. }
  299. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  300. {
  301. $CNF = &$this->_oConfig->CNF;
  302. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  303. $sCategory = '';
  304. if(!empty($CNF['FIELD_CATEGORY']) && !empty($aContentInfo[$CNF['FIELD_CATEGORY']])) {
  305. $iCategory = (int)$aContentInfo[$CNF['FIELD_CATEGORY']];
  306. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  307. $sCategory = _t($aCategory['title']);
  308. $sCategoryLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $iCategory));
  309. }
  310. $sPrice = _t('_bx_ads_txt_free');
  311. if(!empty($CNF['FIELD_PRICE']) && !empty($aContentInfo[$CNF['FIELD_PRICE']]))
  312. $sPrice = _t_format_currency((float)$aContentInfo[$CNF['FIELD_PRICE']]);
  313. $sInclude = $this->_oTemplate->addCss(array('timeline.css'), $bDynamic);
  314. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  315. $aResult['text'] = $this->_oTemplate->parseHtmlByName('timeline_post_text.html', array(
  316. 'category_link' => $sCategoryLink,
  317. 'category_title' => $sCategory,
  318. 'category_title_attr' => bx_html_attribute($sCategory),
  319. 'price' => $sPrice,
  320. 'text' => $aResult['text']
  321. )) . ($bDynamic ? $sInclude : '');
  322. return $aResult;
  323. }
  324. }
  325. /** @} */