BxForumGrid.php 8.2 KB

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