BxAdsModule.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 serviceUpdateCategoriesStats()
  26. {
  27. $aStats = $this->_oDb->getCategories(array('type' => 'collect_stats'));
  28. if(empty($aStats) || !is_array($aStats))
  29. return true;
  30. $iUpdated = 0;
  31. foreach($aStats as $aStat)
  32. if($this->_oDb->updateCategory(array('items' => $aStat['count']), array('id' => $aStat['id'])))
  33. $iUpdated++;
  34. return count($aStats) == $iUpdated;
  35. }
  36. public function serviceGetCategoryOptions($iParentId, $bPleaseSelect = false)
  37. {
  38. $aValues = array();
  39. if($bPleaseSelect)
  40. $aValues[] = array('key' => '', 'value' => _t('_sys_please_select'));
  41. $this->_getCategoryOptions($iParentId, $aValues);
  42. return $aValues;
  43. }
  44. public function serviceGetSearchableFields($aInputsAdd = array())
  45. {
  46. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  47. return parent::serviceGetSearchableFields($aInputsAdd);
  48. }
  49. public function serviceGetSearchableFieldsExtended($aInputsAdd = array())
  50. {
  51. $CNF = &$this->_oConfig->CNF;
  52. $aInputsAdd = array_merge($aInputsAdd, $this->_getSearchableFields());
  53. if(isset($aInputsAdd[$CNF['FIELD_CATEGORY']])) {
  54. $aInputsAdd[$CNF['FIELD_CATEGORY']]['type'] = 'select';
  55. $aInputsAdd[$CNF['FIELD_CATEGORY']]['values_src'] = BxDolService::getSerializedService($this->_oConfig->getName(), 'get_category_options', array(0));
  56. }
  57. return parent::serviceGetSearchableFieldsExtended($aInputsAdd);
  58. }
  59. public function serviceEntityCreate ($sParams = false)
  60. {
  61. if(($sDisplay = $this->getCategoryDisplay('add')) !== false) {
  62. if(empty($sParams) || !is_array($sParams))
  63. $sParams = array();
  64. $sParams['display'] = $sDisplay;
  65. }
  66. return parent::serviceEntityCreate($sParams);
  67. }
  68. public function serviceEntityReviews($iContentId = 0)
  69. {
  70. $CNF = &$this->_oConfig->CNF;
  71. if(empty($CNF['OBJECT_REVIEWS']))
  72. return false;
  73. return $this->_entityComments($CNF['OBJECT_REVIEWS'], $iContentId);
  74. }
  75. public function serviceEntityReviewsRating($iContentId = 0)
  76. {
  77. $CNF = &$this->_oConfig->CNF;
  78. if(empty($CNF['OBJECT_REVIEWS']))
  79. return false;
  80. if(!$iContentId)
  81. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  82. if(!$iContentId)
  83. return false;
  84. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_REVIEWS'], $iContentId);
  85. if (!$oCmts || !$oCmts->isEnabled())
  86. return false;
  87. return $oCmts->getRatingBlock(array('in_designbox' => false));
  88. }
  89. public function serviceCategoriesList($aParams = array())
  90. {
  91. if(!isset($aParams['show_empty']))
  92. $aParams['show_empty'] = true;
  93. return $this->_oTemplate->categoriesList($aParams);
  94. }
  95. public function serviceBrowseCategory($iCategoryId = 0, $aParams = array())
  96. {
  97. $sParamGet = 'category';
  98. $bEmptyMessage = isset($aParams['empty_message']) ? (bool)$aParams['empty_message'] : true;
  99. if(!$iCategoryId)
  100. $iCategoryId = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  101. if(!$iCategoryId)
  102. return $bEmptyMessage ? MsgBox(_t('_Empty')) : '';
  103. $aBlock = $this->_serviceBrowseWithParam('category', $sParamGet, $iCategoryId, $aParams);
  104. if(!empty($aBlock['content'])) {
  105. $aCategory = $this->_oDb->getCategories(array('type' => 'id', 'id' => $iCategoryId));
  106. if(!empty($aCategory['title']))
  107. $aBlock['title'] = _t('_bx_ads_page_block_title_entries_by_category_mask', _t($aCategory['title']));
  108. }
  109. return $aBlock;
  110. }
  111. /**
  112. * Common methods.
  113. */
  114. public function getCategoryDisplay($sDisplayType, $iCategory = 0)
  115. {
  116. if(empty($iCategory) && bx_get('category') !== false)
  117. $iCategory = (int)bx_get('category');
  118. if(empty($iCategory))
  119. return false;
  120. $aCategory = $this->_oDb->getCategories(array('type' => 'id_full', 'id' => $iCategory));
  121. $sKey = 'type_display_' . $sDisplayType;
  122. if(empty($aCategory[$sKey]))
  123. return false;
  124. return $aCategory[$sKey];
  125. }
  126. /**
  127. * Internal methods.
  128. */
  129. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0, $sDisplay = false, $sCheckFunction = false, $bErrorMsg = true)
  130. {
  131. $CNF = &$this->_oConfig->CNF;
  132. $mixedContent = $this->_getContent($iContentId, true);
  133. if($mixedContent === false)
  134. return false;
  135. list($iContentId, $aContentInfo) = $mixedContent;
  136. $sDisplayType = false;
  137. switch($sFormMethod) {
  138. case 'editDataForm':
  139. $sDisplayType = 'edit';
  140. break;
  141. case 'viewDataForm':
  142. case 'viewDataEntry':
  143. $sDisplayType = 'view';
  144. break;
  145. }
  146. if($sDisplayType !== false && ($sDisplayNew = $this->getCategoryDisplay($sDisplayType, $aContentInfo[$CNF['FIELD_CATEGORY']])) !== false)
  147. $sDisplay = $sDisplayNew;
  148. return parent::_serviceEntityForm ($sFormMethod, $iContentId, $sDisplay, $sCheckFunction, $bErrorMsg);
  149. }
  150. protected function _getCategoryOptions($iParentId, &$aValues)
  151. {
  152. $aCategories = $this->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $iParentId));
  153. foreach($aCategories as $aCategory) {
  154. $aValues[] = array('key' => $aCategory['id'], 'value' => str_repeat('--', (int)$aCategory['level']) . ' ' . _t($aCategory['title']));
  155. $this->_getCategoryOptions($aCategory['id'], $aValues);
  156. }
  157. }
  158. protected function _getSearchableFields($mixedDisplayType = '')
  159. {
  160. $CNF = &$this->_oConfig->CNF;
  161. if(empty($mixedDisplayType))
  162. $mixedDisplayType = array('add', 'edit');
  163. $aResult = array();
  164. $aDisplays = $this->_oDb->getDisplays($this->_oConfig->getName() . '_entry', $mixedDisplayType);
  165. foreach($aDisplays as $aDisplay) {
  166. if($aDisplay['display_name'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  167. continue;
  168. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $aDisplay['display_name'], $this->_oTemplate);
  169. if(!$oForm)
  170. continue;
  171. $aResult = array_merge($aResult, $oForm->aInputs);
  172. }
  173. return $aResult;
  174. }
  175. }
  176. /** @} */