BxAdsModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. $CNF = &$this->_oConfig->CNF;
  76. $aResult = parent::serviceGetSearchableFields(array_merge($aInputsAdd, $this->_getSearchableFields()));
  77. unset($aResult[$CNF['FIELD_CATEGORY_VIEW']], $aResult[$CNF['FIELD_CATEGORY_SELECT']]);
  78. return $aResult;
  79. }
  80. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  81. {
  82. $CNF = &$this->_oConfig->CNF;
  83. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  84. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  85. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  86. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  87. }
  88. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  89. }
  90. public function serviceEntityCreate ($sParams = false)
  91. {
  92. if(($sDisplay = $this->getCategoryDisplay('add')) !== false) {
  93. if(empty($sParams) || !is_array($sParams))
  94. $sParams = array();
  95. $sParams['display'] = $sDisplay;
  96. }
  97. return parent::serviceEntityCreate($sParams);
  98. }
  99. public function serviceEntityReviews($iContentId = 0)
  100. {
  101. $CNF = &$this->_oConfig->CNF;
  102. if(empty($CNF['OBJECT_REVIEWS']))
  103. return false;
  104. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  105. }
  106. public function serviceEntityReviewsRating($iContentId = 0)
  107. {
  108. $CNF = &$this->_oConfig->CNF;
  109. if(empty($CNF['OBJECT_REVIEWS']))
  110. return false;
  111. if(!$iContentId)
  112. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  113. if(!$iContentId)
  114. return false;
  115. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  116. if (!$oCmts || !$oCmts->isEnabled())
  117. return false;
  118. return $oCmts->getRatingBlock(array('in_designbox' => false));
  119. }
  120. public function serviceCategoriesList($aParams = array())
  121. {
  122. if(!isset($aParams['show_empty']))
  123. $aParams['show_empty'] = true;
  124. return $this->_oTemplate->categoriesList($aParams);
  125. }
  126. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  127. {
  128. $sParamName = $sParamGet = 'category';
  129. if(!$iCategoryId && bx_get($sParamGet) !== false)
  130. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  131. $bEmptyMessage = true;
  132. if(isset($aParams['empty_message'])) {
  133. $bEmptyMessage = (bool)$aParams['empty_message'];
  134. unset($aParams['empty_message']);
  135. }
  136. $bAjaxPaginate = true;
  137. if(isset($aParams['ajax_paginate'])) {
  138. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  139. unset($aParams['ajax_paginate']);
  140. }
  141. $aBlock = $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $iCategoryId), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  142. if(!empty($aBlock['content'])) {
  143. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  144. if(!empty($aCategory['title']))
  145. $aBlock['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  146. }
  147. return $aBlock;
  148. }
  149. /**
  150. * Common methods.
  151. */
  152. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  153. {
  154. if(empty($iCategory) && bx_get('category') !== false)
  155. $iCategory = (int)bx_get('category');
  156. if(empty($iCategory))
  157. return false;
  158. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  159. $sKey = 'type_display_' . $sDisplayType;
  160. if(empty($aCategory[$sKey]))
  161. return false;
  162. return $aCategory[$sKey];
  163. }
  164. /**
  165. * Internal methods.
  166. */
  167. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  168. {
  169. $CNF = &$this->_oConfig->CNF;
  170. $mixedContent = $this->_getContent($iContentId, true);
  171. if($mixedContent === false)
  172. return false;
  173. list($iContentId, $aContentInfo) = $mixedContent;
  174. $sDisplayType = false;
  175. switch($sFormMethod) {
  176. case 'editDataForm':
  177. $sDisplayType = 'edit';
  178. break;
  179. case 'viewDataForm':
  180. case 'viewDataEntry':
  181. $sDisplayType = 'view';
  182. break;
  183. }
  184. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  185. $sDisplay = $sDisplayNew;
  186. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  187. }
  188. protected function _getCategoryOptions($iParentId, &$aValues)
  189. {
  190. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  191. foreach($aCategories as $aCategory) {
  192. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  193. $this->_getCategoryOptions($aCategory['id'], $aValues);
  194. }
  195. }
  196. protected function _getSearchableFields($mixedDisplayType = '')
  197. {
  198. $CNF = &$this->_oConfig->CNF;
  199. if(empty($mixedDisplayType))
  200. $mixedDisplayType = array('add', 'edit');
  201. $aResult = array();
  202. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  203. foreach($aDisplays as $aDisplay) {
  204. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  205. continue;
  206. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  207. if(!$oForm)
  208. continue;
  209. $aResult = array_merge($aResult, $oForm->aInputs);
  210. }
  211. return $aResult;
  212. }
  213. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  214. {
  215. $CNF = &$this->_oConfig->CNF;
  216. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  217. $sCategory = '';
  218. if(!empty($CNF['FIELD_CATEGORY']) && !empty($aContentInfo[$CNF['FIELD_CATEGORY']])) {
  219. $iCategory = (int)$aContentInfo[$CNF['FIELD_CATEGORY']];
  220. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  221. $sCategory = _t($aCategory['title']);
  222. $sCategoryLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $iCategory));
  223. }
  224. $sPrice = _t('_bx_ads_txt_free');
  225. if(!empty($CNF['FIELD_PRICE']) && !empty($aContentInfo[$CNF['FIELD_PRICE']]))
  226. $sPrice = _t_format_currency((float)$aContentInfo[$CNF['FIELD_PRICE']]);
  227. $sInclude = $this->_oTemplate->addCss(array('timeline.css'), $bDynamic);
  228. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  229. $aResult['text'] = $this->_oTemplate->parseHtmlByName('timeline_post_text.html', array(
  230. 'category_link' => $sCategoryLink,
  231. 'category_title' => $sCategory,
  232. 'category_title_attr' => bx_html_attribute($sCategory),
  233. 'price' => $sPrice,
  234. 'text' => $aResult['text']
  235. )) . ($bDynamic ? $sInclude : '');
  236. return $aResult;
  237. }
  238. }
  239. /** @} */