BxAdsModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. if(isset($aInputsAdd[$CNF['FIELD_PRICE']])) {
  110. $aInputsAdd[$CNF['FIELD_PRICE']]['search_type'] = 'text_range';
  111. $aInputsAdd[$CNF['FIELD_PRICE']]['search_operator'] = 'between';
  112. }
  113. if(isset($aInputsAdd[$CNF['FIELD_YEAR']])) {
  114. $aInputsAdd[$CNF['FIELD_YEAR']]['search_type'] = 'text_range';
  115. $aInputsAdd[$CNF['FIELD_YEAR']]['search_operator'] = 'between';
  116. }
  117. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  118. }
  119. public function serviceEntityCreate ($sParams = false)
  120. {
  121. if(($sDisplay = $this->getCategoryDisplay('add')) !== false) {
  122. if(empty($sParams) || !is_array($sParams))
  123. $sParams = array();
  124. $sParams['display'] = $sDisplay;
  125. }
  126. return parent::serviceEntityCreate($sParams);
  127. }
  128. /**
  129. * @page service Service Calls
  130. * @section bx_ads Ads
  131. * @subsection bx_ads-page_blocks Page Blocks
  132. * @subsubsection bx_ads-entity_reviews entity_reviews
  133. *
  134. * @code bx_srv('bx_ads', 'entity_reviews', [...]); @endcode
  135. *
  136. * Get reviews for particular content
  137. * @param $iContentId content ID
  138. *
  139. * @see BxAdsModule::serviceEntityReviews
  140. */
  141. /**
  142. * @ref bx_ads-entity_reviews "entity_reviews"
  143. */
  144. public function serviceEntityReviews($iContentId = 0)
  145. {
  146. $CNF = &$this->_oConfig->CNF;
  147. if(empty($CNF['OBJECT_REVIEWS']))
  148. return false;
  149. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  150. }
  151. /**
  152. * @page service Service Calls
  153. * @section bx_ads Ads
  154. * @subsection bx_ads-page_blocks Page Blocks
  155. * @subsubsection bx_ads-entity_reviews_rating entity_reviews_rating
  156. *
  157. * @code bx_srv('bx_ads', 'entity_reviews_rating', [...]); @endcode
  158. *
  159. * Get reviews rating for particular content
  160. * @param $iContentId content ID
  161. *
  162. * @see BxAdsModule::serviceEntityReviewsRating
  163. */
  164. /**
  165. * @ref bx_ads-entity_reviews_rating "entity_reviews_rating"
  166. */
  167. public function serviceEntityReviewsRating($iContentId = 0)
  168. {
  169. $CNF = &$this->_oConfig->CNF;
  170. if(empty($CNF['OBJECT_REVIEWS']))
  171. return false;
  172. if(!$iContentId)
  173. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  174. if(!$iContentId)
  175. return false;
  176. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  177. if (!$oCmts || !$oCmts->isEnabled())
  178. return false;
  179. return $oCmts->getRatingBlock(array('in_designbox' => false));
  180. }
  181. /**
  182. * @page service Service Calls
  183. * @section bx_ads Ads
  184. * @subsection bx_ads-page_blocks Page Blocks
  185. * @subsubsection bx_ads-categories_list categories_list
  186. *
  187. * @code bx_srv('bx_ads', 'categories_list', [...]); @endcode
  188. *
  189. * Get reviews rating for particular content
  190. * @param $aParams additional params array, such as 'show_empty'
  191. *
  192. * @see BxAdsModule::serviceCategoriesList
  193. */
  194. /**
  195. * @ref bx_ads-categories_list "categories_list"
  196. */
  197. public function serviceCategoriesList($aParams = array())
  198. {
  199. if(!isset($aParams['show_empty']))
  200. $aParams['show_empty'] = true;
  201. return $this->_oTemplate->categoriesList($aParams);
  202. }
  203. /**
  204. * @page service Service Calls
  205. * @section bx_ads Ads
  206. * @subsection bx_ads-browse Browse
  207. * @subsubsection bx_ads-browse_category browse_category
  208. *
  209. * @code bx_srv('bx_ads', 'browse_category', [...]); @endcode
  210. *
  211. * Get reviews rating for particular content
  212. * @param $iCategoryId category ID
  213. * @param $aParams additional params array, such as empty_message, ajax_paginate, etc
  214. *
  215. * @see BxAdsModule::serviceBrowseCategory
  216. */
  217. /**
  218. * @ref bx_ads-browse_category "browse_category"
  219. */
  220. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  221. {
  222. $sParamName = $sParamGet = 'category';
  223. if(!$iCategoryId && bx_get($sParamGet) !== false)
  224. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  225. $bEmptyMessage = true;
  226. if(isset($aParams['empty_message'])) {
  227. $bEmptyMessage = (bool)$aParams['empty_message'];
  228. unset($aParams['empty_message']);
  229. }
  230. $bAjaxPaginate = true;
  231. if(isset($aParams['ajax_paginate'])) {
  232. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  233. unset($aParams['ajax_paginate']);
  234. }
  235. $aBlock = $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $iCategoryId), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  236. if(!empty($aBlock['content'])) {
  237. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  238. if(!empty($aCategory['title']))
  239. $aBlock['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  240. }
  241. return $aBlock;
  242. }
  243. /**
  244. * Common methods.
  245. */
  246. public function processMetasAdd($iContentId)
  247. {
  248. if(!parent::processMetasAdd($iContentId))
  249. return false;
  250. $CNF = &$this->_oConfig->CNF;
  251. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  252. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  253. $aCategoryTypes = $this->_oDb->getCategoryTypes(array('type' => 'all'));
  254. foreach($aCategoryTypes as $aCategoryType)
  255. $oMetatags->metaAddAuto($iContentId, $aContentInfo, $CNF, $aCategoryType['display_add']);
  256. return true;
  257. }
  258. public function processMetasEdit($iContentId, $oForm)
  259. {
  260. if(!parent::processMetasEdit($iContentId, $oForm))
  261. return false;
  262. $CNF = &$this->_oConfig->CNF;
  263. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  264. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  265. $aCategoryTypes = $this->_oDb->getCategoryTypes(array('type' => 'all'));
  266. foreach($aCategoryTypes as $aCategoryType)
  267. $oMetatags->metaAddAuto($iContentId, $aContentInfo, $CNF, $aCategoryType['display_edit']);
  268. return true;
  269. }
  270. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  271. {
  272. if(empty($iCategory) && bx_get('category') !== false)
  273. $iCategory = (int)bx_get('category');
  274. if(empty($iCategory))
  275. return false;
  276. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  277. $sKey = 'type_display_' . $sDisplayType;
  278. if(empty($aCategory[$sKey]))
  279. return false;
  280. return $aCategory[$sKey];
  281. }
  282. /**
  283. * Internal methods.
  284. */
  285. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  286. {
  287. $CNF = &$this->_oConfig->CNF;
  288. $mixedContent = $this->_getContent($iContentId, true);
  289. if($mixedContent === false)
  290. return false;
  291. list($iContentId, $aContentInfo) = $mixedContent;
  292. $sDisplayType = false;
  293. switch($sFormMethod) {
  294. case 'editDataForm':
  295. $sDisplayType = 'edit';
  296. break;
  297. case 'viewDataForm':
  298. case 'viewDataEntry':
  299. $sDisplayType = 'view';
  300. break;
  301. }
  302. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  303. $sDisplay = $sDisplayNew;
  304. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  305. }
  306. protected function _getCategoryOptions($iParentId, &$aValues)
  307. {
  308. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  309. foreach($aCategories as $aCategory) {
  310. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  311. $this->_getCategoryOptions($aCategory['id'], $aValues);
  312. }
  313. }
  314. protected function _getSearchableFields($mixedDisplayType = '')
  315. {
  316. $CNF = &$this->_oConfig->CNF;
  317. if(empty($mixedDisplayType))
  318. $mixedDisplayType = array('add', 'edit');
  319. $aResult = array();
  320. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  321. foreach($aDisplays as $aDisplay) {
  322. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  323. continue;
  324. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  325. if(!$oForm)
  326. continue;
  327. $aResult = array_merge($aResult, $oForm->aInputs);
  328. }
  329. return $aResult;
  330. }
  331. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  332. {
  333. $CNF = &$this->_oConfig->CNF;
  334. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  335. $sCategory = '';
  336. if(!empty($CNF['FIELD_CATEGORY']) && !empty($aContentInfo[$CNF['FIELD_CATEGORY']])) {
  337. $iCategory = (int)$aContentInfo[$CNF['FIELD_CATEGORY']];
  338. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  339. $sCategory = _t($aCategory['title']);
  340. $sCategoryLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $iCategory));
  341. }
  342. $sPrice = _t('_bx_ads_txt_free');
  343. if(!empty($CNF['FIELD_PRICE']) && !empty($aContentInfo[$CNF['FIELD_PRICE']]))
  344. $sPrice = _t_format_currency((float)$aContentInfo[$CNF['FIELD_PRICE']]);
  345. $sInclude = $this->_oTemplate->addCss(array('timeline.css'), $bDynamic);
  346. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  347. $aResult['text'] = $this->_oTemplate->parseHtmlByName('timeline_post_text.html', array(
  348. 'category_link' => $sCategoryLink,
  349. 'category_title' => $sCategory,
  350. 'category_title_attr' => bx_html_attribute($sCategory),
  351. 'price' => $sPrice,
  352. 'text' => $aResult['text']
  353. )) . ($bDynamic ? $sInclude : '');
  354. return $aResult;
  355. }
  356. }
  357. /** @} */