BxForumGrid.php 8.0 KB

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