BxAdsModule.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. $iInterestId = $this->_oDb->insertInterested(array('entry_id' => $iContentId, 'profile_id' => $iViewer));
  53. if(!$iInterestId)
  54. return echoJson(array('msg' => _t('_bx_ads_txt_err_cannot_perform_action')));
  55. bx_alert($this->getName(), 'doInterest', $iContentId, $iViewer, array(
  56. 'subobject_id' => $iInterestId,
  57. 'subobject_author_id' => $iViewer,
  58. 'object_author_id' => $iContentAuthor
  59. ));
  60. if(getParam($CNF['PARAM_USE_IIN']) == 'on')
  61. sendMailTemplate($CNF['ETEMPLATE_INTERESTED'], 0, $iContentAuthor, array(
  62. 'viewer_name' => $oViewer->getDisplayName(),
  63. 'viewer_url' => $oViewer->getUrl(),
  64. 'ad_name' => $aContentInfo[$CNF['FIELD_TITLE']],
  65. 'ad_url' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php', array(
  66. 'i' => $CNF['URI_VIEW_ENTRY'],
  67. $CNF['FIELD_ID'] => $iContentId
  68. ))
  69. ));
  70. return echoJson(array('msg' => _t('_bx_ads_txt_msg_author_notified')));
  71. }
  72. public function serviceGetSafeServices()
  73. {
  74. $a = parent::serviceGetSafeServices();
  75. return array_merge($a, array (
  76. 'EntityReviews' => '',
  77. 'EntityReviewsRating' => '',
  78. 'CategoriesList' => '',
  79. 'BrowseCategory' => '',
  80. ));
  81. }
  82. public function serviceUpdateCategoriesStats()
  83. {
  84. $aStats = $this->_oDb->getCategories(array('type' => 'collect_stats'));
  85. if(empty($aStats) || !is_array($aStats))
  86. return true;
  87. $iUpdated = 0;
  88. foreach($aStats as $aStat)
  89. if($this->_oDb->updateCategory(array('items' => $aStat['count']), array('id' => $aStat['id'])))
  90. $iUpdated++;
  91. return count($aStats) == $iUpdated;
  92. }
  93. public function serviceGetCategoryOptions($iParentId, $bPleaseSelect = false)
  94. {
  95. $aValues = array();
  96. if($bPleaseSelect)
  97. $aValues[] = array('key' => '', 'value' => _t('_sys_please_select'));
  98. $this->_getCategoryOptions($iParentId, $aValues);
  99. return $aValues;
  100. }
  101. public function serviceGetSearchableFields($aInputsAdd = array())
  102. {
  103. $CNF = &$this->_oConfig->CNF;
  104. $aResult = parent::serviceGetSearchableFields(array_merge($aInputsAdd, $this->_getSearchableFields()));
  105. unset($aResult[$CNF['FIELD_CATEGORY_VIEW']], $aResult[$CNF['FIELD_CATEGORY_SELECT']]);
  106. unset($aResult[$CNF['FIELD_PRICE']], $aResult[$CNF['FIELD_YEAR']]);
  107. return $aResult;
  108. }
  109. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  110. {
  111. $CNF = &$this->_oConfig->CNF;
  112. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  113. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  114. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  115. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  116. }
  117. if(isset($aInputsAdd[$CNF['FIELD_PRICE']])) {
  118. $aInputsAdd[$CNF['FIELD_PRICE']]['search_type'] = 'text_range';
  119. $aInputsAdd[$CNF['FIELD_PRICE']]['search_operator'] = 'between';
  120. }
  121. if(isset($aInputsAdd[$CNF['FIELD_YEAR']])) {
  122. $aInputsAdd[$CNF['FIELD_YEAR']]['search_type'] = 'text_range';
  123. $aInputsAdd[$CNF['FIELD_YEAR']]['search_operator'] = 'between';
  124. }
  125. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  126. }
  127. public function serviceEntityCreate ($sParams = false)
  128. {
  129. if(($sDisplay = $this->getCategoryDisplay('add')) !== false) {
  130. if(empty($sParams) || !is_array($sParams))
  131. $sParams = array();
  132. $sParams['display'] = $sDisplay;
  133. }
  134. return parent::serviceEntityCreate($sParams);
  135. }
  136. /**
  137. * @page service Service Calls
  138. * @section bx_ads Ads
  139. * @subsection bx_ads-page_blocks Page Blocks
  140. * @subsubsection bx_ads-entity_reviews entity_reviews
  141. *
  142. * @code bx_srv('bx_ads', 'entity_reviews', [...]); @endcode
  143. *
  144. * Get reviews for particular content
  145. * @param $iContentId content ID
  146. *
  147. * @see BxAdsModule::serviceEntityReviews
  148. */
  149. /**
  150. * @ref bx_ads-entity_reviews "entity_reviews"
  151. */
  152. public function serviceEntityReviews($iContentId = 0)
  153. {
  154. $CNF = &$this->_oConfig->CNF;
  155. if(empty($CNF['OBJECT_REVIEWS']))
  156. return false;
  157. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  158. }
  159. /**
  160. * @page service Service Calls
  161. * @section bx_ads Ads
  162. * @subsection bx_ads-page_blocks Page Blocks
  163. * @subsubsection bx_ads-entity_reviews_rating entity_reviews_rating
  164. *
  165. * @code bx_srv('bx_ads', 'entity_reviews_rating', [...]); @endcode
  166. *
  167. * Get reviews rating for particular content
  168. * @param $iContentId content ID
  169. *
  170. * @see BxAdsModule::serviceEntityReviewsRating
  171. */
  172. /**
  173. * @ref bx_ads-entity_reviews_rating "entity_reviews_rating"
  174. */
  175. public function serviceEntityReviewsRating($iContentId = 0)
  176. {
  177. $CNF = &$this->_oConfig->CNF;
  178. if(empty($CNF['OBJECT_REVIEWS']))
  179. return false;
  180. if(!$iContentId)
  181. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  182. if(!$iContentId)
  183. return false;
  184. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  185. if (!$oCmts || !$oCmts->isEnabled())
  186. return false;
  187. return $oCmts->getRatingBlock(array('in_designbox' => false));
  188. }
  189. /**
  190. * @page service Service Calls
  191. * @section bx_ads Ads
  192. * @subsection bx_ads-page_blocks Page Blocks
  193. * @subsubsection bx_ads-categories_list categories_list
  194. *
  195. * @code bx_srv('bx_ads', 'categories_list', [...]); @endcode
  196. *
  197. * Get reviews rating for particular content
  198. * @param $aParams additional params array, such as 'show_empty'
  199. *
  200. * @see BxAdsModule::serviceCategoriesList
  201. */
  202. /**
  203. * @ref bx_ads-categories_list "categories_list"
  204. */
  205. public function serviceCategoriesList($aParams = array())
  206. {
  207. if(!isset($aParams['show_empty']))
  208. $aParams['show_empty'] = true;
  209. return $this->_oTemplate->categoriesList($aParams);
  210. }
  211. /**
  212. * @page service Service Calls
  213. * @section bx_ads Ads
  214. * @subsection bx_ads-browse Browse
  215. * @subsubsection bx_ads-browse_category browse_category
  216. *
  217. * @code bx_srv('bx_ads', 'browse_category', [...]); @endcode
  218. *
  219. * Get reviews rating for particular content
  220. * @param $iCategoryId category ID
  221. * @param $aParams additional params array, such as empty_message, ajax_paginate, etc
  222. *
  223. * @see BxAdsModule::serviceBrowseCategory
  224. */
  225. /**
  226. * @ref bx_ads-browse_category "browse_category"
  227. */
  228. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  229. {
  230. $sParamName = $sParamGet = 'category';
  231. if(!$iCategoryId && bx_get($sParamGet) !== false)
  232. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  233. $bEmptyMessage = true;
  234. if(isset($aParams['empty_message'])) {
  235. $bEmptyMessage = (bool)$aParams['empty_message'];
  236. unset($aParams['empty_message']);
  237. }
  238. $bAjaxPaginate = true;
  239. if(isset($aParams['ajax_paginate'])) {
  240. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  241. unset($aParams['ajax_paginate']);
  242. }
  243. $aBlock = $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $iCategoryId), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  244. if(!empty($aBlock['content'])) {
  245. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  246. if(!empty($aCategory['title']))
  247. $aBlock['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  248. }
  249. return $aBlock;
  250. }
  251. public function serviceGetNotificationsData()
  252. {
  253. $sModule = $this->_aModule['name'];
  254. $sEventPrivacy = $sModule . '_allow_view_event_to';
  255. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  256. $sEventPrivacy = '';
  257. $aResult = parent::serviceGetNotificationsData();
  258. $aResult['handlers'] = array_merge($aResult['handlers'], array(
  259. array('group' => $sModule . '_interest', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doInterest', 'module_name' => $sModule, 'module_method' => 'get_notifications_interest', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy)
  260. ));
  261. $aResult['settings'] = array_merge($aResult['settings'], array(
  262. array('group' => 'interest', 'unit' => $sModule, 'action' => 'doInterest', 'types' => array('personal'))
  263. ));
  264. $aResult['alerts'] = array_merge($aResult['alerts'], array(
  265. array('unit' => $sModule, 'action' => 'doInterest')
  266. ));
  267. return $aResult;
  268. }
  269. public function serviceGetNotificationsInterest($aEvent)
  270. {
  271. $CNF = &$this->_oConfig->CNF;
  272. $iContentId = (int)$aEvent['object_id'];
  273. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  274. if(empty($aContentInfo) || !is_array($aContentInfo))
  275. return array();
  276. $iInterestedId = (int)$aEvent['subobject_id'];
  277. $aInterestedInfo = $this->_oDb->getInterested(array('type' => 'id', 'id' => $iInterestedId));
  278. if(empty($aInterestedInfo) || !is_array($aInterestedInfo))
  279. return array();
  280. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId);
  281. return array(
  282. 'entry_sample' => $CNF['T']['txt_sample_single'],
  283. 'entry_url' => $sEntryUrl,
  284. 'entry_caption' => $aContentInfo[$CNF['FIELD_TITLE']],
  285. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  286. 'subentry_sample' => $CNF['T']['txt_sample_interest_single'],
  287. 'subentry_url' => '',
  288. 'lang_key' => '_bx_ads_txt_ntfs_subobject_interested', //may be empty or not specified. In this case the default one from Notification module will be used.
  289. );
  290. }
  291. /**
  292. * Common methods.
  293. */
  294. public function processMetasAdd($iContentId)
  295. {
  296. if(!parent::processMetasAdd($iContentId))
  297. return false;
  298. $CNF = &$this->_oConfig->CNF;
  299. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  300. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  301. $aCategoryTypes = $this->_oDb->getCategoryTypes(array('type' => 'all'));
  302. foreach($aCategoryTypes as $aCategoryType)
  303. $oMetatags->metaAddAuto($iContentId, $aContentInfo, $CNF, $aCategoryType['display_add']);
  304. return true;
  305. }
  306. public function processMetasEdit($iContentId, $oForm)
  307. {
  308. if(!parent::processMetasEdit($iContentId, $oForm))
  309. return false;
  310. $CNF = &$this->_oConfig->CNF;
  311. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  312. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  313. $aCategoryTypes = $this->_oDb->getCategoryTypes(array('type' => 'all'));
  314. foreach($aCategoryTypes as $aCategoryType)
  315. $oMetatags->metaAddAuto($iContentId, $aContentInfo, $CNF, $aCategoryType['display_edit']);
  316. return true;
  317. }
  318. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  319. {
  320. if(empty($iCategory) && bx_get('category') !== false)
  321. $iCategory = (int)bx_get('category');
  322. if(empty($iCategory))
  323. return false;
  324. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  325. $sKey = 'type_display_' . $sDisplayType;
  326. if(empty($aCategory[$sKey]))
  327. return false;
  328. return $aCategory[$sKey];
  329. }
  330. /**
  331. * Internal methods.
  332. */
  333. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  334. {
  335. $CNF = &$this->_oConfig->CNF;
  336. $mixedContent = $this->_getContent($iContentId, true);
  337. if($mixedContent === false)
  338. return false;
  339. list($iContentId, $aContentInfo) = $mixedContent;
  340. $sDisplayType = false;
  341. switch($sFormMethod) {
  342. case 'editDataForm':
  343. $sDisplayType = 'edit';
  344. break;
  345. case 'viewDataForm':
  346. case 'viewDataEntry':
  347. $sDisplayType = 'view';
  348. break;
  349. }
  350. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  351. $sDisplay = $sDisplayNew;
  352. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  353. }
  354. protected function _getCategoryOptions($iParentId, &$aValues)
  355. {
  356. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  357. foreach($aCategories as $aCategory) {
  358. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  359. $this->_getCategoryOptions($aCategory['id'], $aValues);
  360. }
  361. }
  362. protected function _getSearchableFields($mixedDisplayType = '')
  363. {
  364. $CNF = &$this->_oConfig->CNF;
  365. if(empty($mixedDisplayType))
  366. $mixedDisplayType = array('add', 'edit');
  367. $aResult = array();
  368. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  369. foreach($aDisplays as $aDisplay) {
  370. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  371. continue;
  372. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  373. if(!$oForm)
  374. continue;
  375. $aResult = array_merge($aResult, $oForm->aInputs);
  376. }
  377. return $aResult;
  378. }
  379. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  380. {
  381. $CNF = &$this->_oConfig->CNF;
  382. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  383. $sCategory = '';
  384. if(!empty($CNF['FIELD_CATEGORY']) && !empty($aContentInfo[$CNF['FIELD_CATEGORY']])) {
  385. $iCategory = (int)$aContentInfo[$CNF['FIELD_CATEGORY']];
  386. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategory));
  387. $sCategory = _t($aCategory['title']);
  388. $sCategoryLink = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORIES'], array($CNF['GET_PARAM_CATEGORY'] => $iCategory));
  389. }
  390. $sPrice = _t('_bx_ads_txt_free');
  391. if(!empty($CNF['FIELD_PRICE']) && !empty($aContentInfo[$CNF['FIELD_PRICE']]))
  392. $sPrice = _t_format_currency((float)$aContentInfo[$CNF['FIELD_PRICE']]);
  393. $sInclude = $this->_oTemplate->addCss(array('timeline.css'), $bDynamic);
  394. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  395. $aResult['text'] = $this->_oTemplate->parseHtmlByName('timeline_post_text.html', array(
  396. 'category_link' => $sCategoryLink,
  397. 'category_title' => $sCategory,
  398. 'category_title_attr' => bx_html_attribute($sCategory),
  399. 'price' => $sPrice,
  400. 'text' => $aResult['text']
  401. )) . ($bDynamic ? $sInclude : '');
  402. return $aResult;
  403. }
  404. }
  405. /** @} */