BxForumGrid.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 Forum Forum
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  12. class BxForumGrid extends BxTemplGrid
  13. {
  14. protected $_oModule;
  15. protected $_sDefaultSource;
  16. public function __construct ($aOptions, $oTemplate = false)
  17. {
  18. $this->_oModule = BxDolModule::getInstance('bx_forum');
  19. parent::__construct ($aOptions, $oTemplate);
  20. $this->_sDefaultSortingOrder = 'DESC';
  21. $this->_sDefaultSource = $this->_aOptions['source'];
  22. }
  23. public function setBrowseParams($aParams)
  24. {
  25. parent::setBrowseParams($aParams);
  26. $sField = 'added';
  27. if(!empty($this->_aBrowseParams['type']))
  28. switch($this->_aBrowseParams['type']) {
  29. case 'new':
  30. case 'index':
  31. case 'author':
  32. case 'favorite':
  33. case 'category':
  34. $sField = 'added';
  35. break;
  36. case 'featured':
  37. $sField = 'featured';
  38. break;
  39. case 'updated':
  40. $sField = 'changed';
  41. break;
  42. case 'latest':
  43. $sField = 'lr_timestamp';
  44. break;
  45. case 'top':
  46. $sField = 'comments';
  47. break;
  48. case 'popular':
  49. $sField = 'views';
  50. break;
  51. }
  52. $this->_aOptions['field_order'] = $sField;
  53. $this->_aOptions['paginate_per_page'] = !empty($this->_aBrowseParams['per_page']) ? (int)$this->_aBrowseParams['per_page'] : (int)$this->_oModule->_oDb->getParam('bx_forum_per_page_browse');
  54. }
  55. public function getCode ($isDisplayHeader = true)
  56. {
  57. $sCode = parent::getCode($isDisplayHeader);
  58. if(empty($sCode))
  59. return '';
  60. return $this->_oModule->_oTemplate->getJsCode('main') . $sCode;
  61. }
  62. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  63. {
  64. if($this->_oModule->checkAllowedAdd() !== CHECK_ACTION_RESULT_ALLOWED)
  65. return '';
  66. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oModule->_oConfig->CNF['URI_ADD_ENTRY']);
  67. unset($a['attr']['bx_grid_action_independent']);
  68. $a['attr'] = array_merge($a['attr'], array(
  69. "onclick" => "window.open('" . $sUrl . "','_self');"
  70. ));
  71. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  72. }
  73. protected function _getRowHead ()
  74. {
  75. return array();
  76. }
  77. protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow)
  78. {
  79. $mixedValue = $this->_oModule->_oTemplate->getEntryAuthor($aRow);
  80. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  81. }
  82. protected function _getCellLrTimestamp($mixedValue, $sKey, $aField, $aRow)
  83. {
  84. $mixedValue = $this->_oModule->_oTemplate->getEntryPreview($aRow);
  85. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  86. }
  87. protected function _getCellComments($mixedValue, $sKey, $aField, $aRow)
  88. {
  89. $mixedValue = $this->_oModule->_oTemplate->getEntryLabel($aRow, array('show_count' => 1));
  90. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  91. }
  92. protected function _addJsCss()
  93. {
  94. parent::_addJsCss();
  95. $this->_oModule->_oTemplate->addJs(array('main.js'));
  96. $this->_oModule->_oTemplate->addCss(array('main-media-tablet.css', 'main-media-desktop.css'));
  97. }
  98. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  99. {
  100. $sJoinClause = $sWhereClause = '';
  101. //--- Check status
  102. $sWhereClause .= " AND `" . $this->_oModule->_oConfig->CNF['FIELD_STATUS'] . "`='active'";
  103. $sWhereClause .= " AND `" . $this->_oModule->_oConfig->CNF['FIELD_STATUS_ADMIN'] . "`='active'";
  104. //--- Check privacy
  105. $iAuthorId = 0;
  106. if(!empty($this->_aBrowseParams['author'])) {
  107. $oProfileAuthor = BxDolProfile::getInstance((int)$this->_aBrowseParams['author']);
  108. if($oProfileAuthor)
  109. $iAuthorId = $oProfileAuthor->id();
  110. }
  111. $sPrivacy = $this->_oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW'];
  112. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacy);
  113. $aCondition = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart($iAuthorId) : array();
  114. if(isset($aCondition['join']))
  115. $sJoinClause .= $aCondition['join'];
  116. if(isset($aCondition['where']))
  117. $sWhereClause .= $aCondition['where'];
  118. //--- Check browse params
  119. if(!empty($this->_aBrowseParams['join']) && is_array($this->_aBrowseParams['join'])) {
  120. $sJoinClauseBrowse = $this->_getSqlJoinGroup($this->_aBrowseParams['join']);
  121. if(!empty($sJoinClauseBrowse))
  122. $sJoinClause .= " " . $sJoinClauseBrowse;
  123. }
  124. if(!empty($this->_aBrowseParams['where']) && is_array($this->_aBrowseParams['where'])) {
  125. $sWhereClauseBrowse = $this->_getSqlWhereFromGroup($this->_aBrowseParams['where']);
  126. if(!empty($sWhereClauseBrowse))
  127. $sWhereClause .= " AND " . $sWhereClauseBrowse;
  128. }
  129. $this->_aOptions['source'] = sprintf($this->_sDefaultSource, $sJoinClause, $sWhereClause);
  130. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  131. }
  132. protected function _getDataSqlOrderClause($sOrderByFilter, $sOrderField, $sOrderDir, $bFieldsOnly = false)
  133. {
  134. $sOrder = parent::_getDataSqlOrderClause($sOrderByFilter, $sOrderField, $sOrderDir, true);
  135. return " ORDER BY `" . $this->_oModule->_oConfig->CNF['FIELD_STICK'] . "` DESC, " . $sOrder;
  136. }
  137. protected function _getSqlJoinGroup($aGrp)
  138. {
  139. if(!isset($aGrp['grp']) || (bool)$aGrp['grp'] !== true)
  140. return $this->_getSqlJoinCondition($aGrp);
  141. $sResult = "";
  142. if(empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  143. return $sResult;
  144. $sOprGrp = " ";
  145. foreach($aGrp['cnds'] as $aCnd) {
  146. $sResultCnd = $this->_getSqlJoinCondition($aCnd);
  147. if(!empty($sResultCnd))
  148. $sResult .= $sOprGrp . $sResultCnd;
  149. }
  150. return trim($sResult, $sOprGrp);
  151. }
  152. protected function _getSqlJoinCondition($aCnd)
  153. {
  154. $sResult = "";
  155. if(!isset($aCnd['tp'], $aCnd['tbl1'], $aCnd['fld1'], $aCnd['fld2']))
  156. return $sResult;
  157. $sField1 = "`" . $aCnd['tbl1'] . "`.`" . $aCnd['fld1'] . "`";
  158. $sField2 = "`" . $aCnd['fld2'] . "`";
  159. if(!empty($aCnd['tbl2']))
  160. $sField2 = "`" . $aCnd['tbl2'] . "`." . $sField2;
  161. return $aCnd['tp'] . " JOIN `" . $aCnd['tbl1'] . "` ON (" . $sField1 . " = " . $sField2 . ")";
  162. }
  163. protected function _getSqlWhereFromGroup($aGrp)
  164. {
  165. if(!isset($aGrp['grp']) || (bool)$aGrp['grp'] !== true)
  166. return $this->_getSqlWhereFromCondition($aGrp);
  167. $sResult = "";
  168. if(!isset($aGrp['opr'], $aGrp['cnds']) || empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  169. return $sResult;
  170. $sOprGrp = " " . $aGrp['opr'] . " ";
  171. foreach($aGrp['cnds'] as $aCnd) {
  172. $sMethod = '_getSqlWhereFrom' . (isset($aGrp['grp']) && (bool)$aGrp['grp'] === true ? 'Group' : 'Condition');
  173. $sResultCnd = $this->$sMethod($aCnd);
  174. if(!empty($sResultCnd))
  175. $sResult .= $sOprGrp . $sResultCnd;
  176. }
  177. $sResult = trim($sResult, $sOprGrp);
  178. if(!empty($sResult))
  179. $sResult = "(" . $sResult . ")";
  180. return $sResult;
  181. }
  182. protected function _getSqlWhereFromCondition($aCnd)
  183. {
  184. $sResult = "";
  185. if(!isset($aCnd['fld'], $aCnd['val'], $aCnd['opr']))
  186. return $sResult;
  187. $sMethod = '_getSqlWhereFromCondition' . bx_gen_method_name($aCnd['fld']);
  188. if(method_exists($this, $sMethod)) {
  189. $mixedResult = $this->$sMethod($aCnd);
  190. if(is_array($mixedResult) && isset($mixedResult['fld'], $mixedResult['val'], $mixedResult['opr']))
  191. $aCnd = $mixedResult;
  192. else
  193. return $mixedResult;
  194. }
  195. $sField = "`" . $aCnd['fld'] . "`";
  196. if(!empty($aCnd['tbl']))
  197. $sField = "`" . $aCnd['tbl'] . "`." . $sField;
  198. switch($aCnd['opr']) {
  199. case 'IN':
  200. if(empty($aCnd['val']) || !is_array($aCnd['val']))
  201. break;
  202. $sResult .= $sField . " IN (" . $this->_oModule->_oDb->implode_escape($aCnd['val']) . ")";
  203. break;
  204. case 'LIKE':
  205. $sResult .= $sField . " LIKE " . $this->_oModule->_oDb->escape('%' . $aCnd['val'] . '%');
  206. break;
  207. default:
  208. $sResult .= $sField . " " . $aCnd['opr'] . " " . $this->_oModule->_oDb->escape($aCnd['val']);
  209. }
  210. return $sResult;
  211. }
  212. protected function _getSqlWhereFromConditionAuthorComment($aCnd)
  213. {
  214. $aEntriesIds = $this->_oModule->_oDb->getComments(array('type' => 'entries_author_search', 'author' => $aCnd['val']));
  215. if(!empty($aEntriesIds) && is_array($aEntriesIds))
  216. return array('fld' => 'id', 'val' => $aEntriesIds, 'opr' => 'IN');
  217. return '';
  218. }
  219. protected function _getSqlWhereFromConditionKeywordComment($aCnd)
  220. {
  221. $aEntriesIds = $this->_oModule->_oDb->getComments(array('type' => 'entries_keyword_search', 'keyword' => $aCnd['val']));
  222. if(!empty($aEntriesIds) && is_array($aEntriesIds))
  223. return array('fld' => 'id', 'val' => $aEntriesIds, 'opr' => 'IN');
  224. return '';
  225. }
  226. }
  227. /** @} */