BxAdsModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 actionInterested()
  26. {
  27. $CNF = &$this->_oConfig->CNF;
  28. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  29. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  30. if(empty($aContentInfo) || !is_array($aContentInfo))
  31. return echoJson(array());
  32. $iViewer = bx_get_logged_profile_id();
  33. $oViewer = BxDolProfile::getInstance($iViewer);
  34. if(!$oViewer)
  35. return echoJson(array());
  36. $iContentAuthor = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  37. if($iContentAuthor == $iViewer)
  38. return echoJson(array('msg' => _t('_bx_ads_txt_err_your_own')));
  39. if($this->_oDb->isInterested($iContentId, $iViewer))
  40. return echoJson(array('msg' => _t('_bx_ads_txt_err_duplicate')));
  41. if(!$this->_oDb->insertInterested(array('entry_id' => $iContentId, 'profile_id' => $iViewer)))
  42. return echoJson(array('msg' => _t('_bx_ads_txt_err_cannot_perform_action')));
  43. sendMailTemplate($CNF['ETEMPLATE_INTERESTED'], 0, $iContentAuthor, array(
  44. 'viewer_name' => $oViewer->getDisplayName(),
  45. 'viewer_url' => $oViewer->getUrl(),
  46. 'ad_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  47. 'ad_url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php', array(
  48. 'i' => $CNF['URI_VIEW_ENTRY'],
  49. $CNF['FIELD_ID'] => $iContentId
  50. ))
  51. ));
  52. return echoJson(array('msg' => _t('_bx_ads_txt_msg_author_notified')));
  53. }
  54. public function serviceUpdateCategoriesStats()
  55. {
  56. $aStats = $this->_oDb->getCategories(array('type' => 'collect_stats'));
  57. if(empty($aStats) || !is_array($aStats))
  58. return true;
  59. $iUpdated = 0;
  60. foreach($aStats as $aStat)
  61. if($this->_oDb->updateCategory(array('items' => $aStat['count']), array('id' => $aStat['id'])))
  62. $iUpdated++;
  63. return count($aStats) == $iUpdated;
  64. }
  65. public function serviceGetCategoryOptions($iParentId, $bPleaseSelect = false)
  66. {
  67. $aValues = array();
  68. if($bPleaseSelect)
  69. $aValues[] = array('key' => '', 'value' => _t('_sys_please_select'));
  70. $this->_getCategoryOptions($iParentId, $aValues);
  71. return $aValues;
  72. }
  73. public function serviceGetSearchableFields($aInputsAdd = array())
  74. {
  75. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  76. return parent::serviceGetSearchableFields($aInputsAdd);
  77. }
  78. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  79. {
  80. $CNF = &$this->_oConfig->CNF;
  81. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  82. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  83. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  84. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  85. }
  86. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  87. }
  88. public function serviceEntityCreate ($sParams = false)
  89. {
  90. if(($sDisplay = $this->getCategoryDisplay('add')) !== false) {
  91. if(empty($sParams) || !is_array($sParams))
  92. $sParams = array();
  93. $sParams['display'] = $sDisplay;
  94. }
  95. return parent::serviceEntityCreate($sParams);
  96. }
  97. public function serviceEntityReviews($iContentId = 0)
  98. {
  99. $CNF = &$this->_oConfig->CNF;
  100. if(empty($CNF['OBJECT_REVIEWS']))
  101. return false;
  102. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  103. }
  104. public function serviceEntityReviewsRating($iContentId = 0)
  105. {
  106. $CNF = &$this->_oConfig->CNF;
  107. if(empty($CNF['OBJECT_REVIEWS']))
  108. return false;
  109. if(!$iContentId)
  110. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  111. if(!$iContentId)
  112. return false;
  113. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  114. if (!$oCmts || !$oCmts->isEnabled())
  115. return false;
  116. return $oCmts->getRatingBlock(array('in_designbox' => false));
  117. }
  118. public function serviceCategoriesList($aParams = array())
  119. {
  120. if(!isset($aParams['show_empty']))
  121. $aParams['show_empty'] = true;
  122. return $this->_oTemplate->categoriesList($aParams);
  123. }
  124. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  125. {
  126. $sParamName = $sParamGet = 'category';
  127. if(!$iCategoryId && bx_get($sParamGet) !== false)
  128. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  129. $bEmptyMessage = true;
  130. if(isset($aParams['empty_message'])) {
  131. $bEmptyMessage = (bool)$aParams['empty_message'];
  132. unset($aParams['empty_message']);
  133. }
  134. $bAjaxPaginate = true;
  135. if(isset($aParams['ajax_paginate'])) {
  136. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  137. unset($aParams['ajax_paginate']);
  138. }
  139. $aBlock = $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $iCategoryId), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  140. if(!empty($aBlock['content'])) {
  141. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  142. if(!empty($aCategory['title']))
  143. $aBlock['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  144. }
  145. return $aBlock;
  146. }
  147. /**
  148. * Common methods.
  149. */
  150. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  151. {
  152. if(empty($iCategory) && bx_get('category') !== false)
  153. $iCategory = (int)bx_get('category');
  154. if(empty($iCategory))
  155. return false;
  156. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  157. $sKey = 'type_display_' . $sDisplayType;
  158. if(empty($aCategory[$sKey]))
  159. return false;
  160. return $aCategory[$sKey];
  161. }
  162. /**
  163. * Internal methods.
  164. */
  165. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  166. {
  167. $CNF = &$this->_oConfig->CNF;
  168. $mixedContent = $this->_getContent($iContentId, true);
  169. if($mixedContent === false)
  170. return false;
  171. list($iContentId, $aContentInfo) = $mixedContent;
  172. $sDisplayType = false;
  173. switch($sFormMethod) {
  174. case 'editDataForm':
  175. $sDisplayType = 'edit';
  176. break;
  177. case 'viewDataForm':
  178. case 'viewDataEntry':
  179. $sDisplayType = 'view';
  180. break;
  181. }
  182. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  183. $sDisplay = $sDisplayNew;
  184. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  185. }
  186. protected function _getCategoryOptions($iParentId, &$aValues)
  187. {
  188. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  189. foreach($aCategories as $aCategory) {
  190. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  191. $this->_getCategoryOptions($aCategory['id'], $aValues);
  192. }
  193. }
  194. protected function _getSearchableFields($mixedDisplayType = '')
  195. {
  196. $CNF = &$this->_oConfig->CNF;
  197. if(empty($mixedDisplayType))
  198. $mixedDisplayType = array('add', 'edit');
  199. $aResult = array();
  200. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  201. foreach($aDisplays as $aDisplay) {
  202. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  203. continue;
  204. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  205. if(!$oForm)
  206. continue;
  207. $aResult = array_merge($aResult, $oForm->aInputs);
  208. }
  209. return $aResult;
  210. }
  211. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  212. {
  213. $CNF = &$this->_oConfig->CNF;
  214. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  215. $sCategory = '';
  216. if(!empty($CNF['FIELD_CATEGORY']) && !empty($aContentInfo[$CNF['FIELD_CATEGORY']])) {
  217. $iCategory = (int)$aContentInfo[$CNF['FIELD_CATEGORY']];
  218. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  219. $sCategory = _t($aCategory['title']);
  220. $sCategoryLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $iCategory));
  221. }
  222. $sPrice = _t('_bx_ads_txt_free');
  223. if(!empty($CNF['FIELD_PRICE']) && !empty($aContentInfo[$CNF['FIELD_PRICE']]))
  224. $sPrice = _t_format_currency((float)$aContentInfo[$CNF['FIELD_PRICE']]);
  225. $sInclude = $this->_oTemplate->addCss(array('timeline.css'), $bDynamic);
  226. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  227. $aResult['text'] = $this->_oTemplate->parseHtmlByName('timeline_post_text.html', array(
  228. 'category_link' => $sCategoryLink,
  229. 'category_title' => $sCategory,
  230. 'category_title_attr' => bx_html_attribute($sCategory),
  231. 'price' => $sPrice,
  232. 'text' => $aResult['text']
  233. )) . ($bDynamic ? $sInclude : '');
  234. return $aResult;
  235. }
  236. }
  237. /** @} */