BxForumGrid.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. define('BX_FORUM_FILTER_STATUS_RESOLVED', 1);
  13. define('BX_FORUM_FILTER_STATUS_UNRESOLVED', 0);
  14. define('BX_FORUM_FILTER_ORDER_RECENT', 'latest');
  15. define('BX_FORUM_FILTER_ORDER_NEW', 'new');
  16. define('BX_FORUM_FILTER_ORDER_TOP', 'top');
  17. define('BX_FORUM_FILTER_ORDER_UPDATED', 'updated');
  18. define('BX_FORUM_FILTER_ORDER_POPULAR', 'popular');
  19. define('BX_FORUM_FILTER_ORDER_FEATURED', 'featured');
  20. define('BX_FORUM_FILTER_ORDER_FAVORITE', 'favorite');
  21. define('BX_FORUM_FILTER_ORDER_PARTAKEN', 'partaken');
  22. class BxForumGrid extends BxTemplGrid
  23. {
  24. protected $_oModule;
  25. protected $_sDefaultSource;
  26. protected $_sFilter1Name;
  27. protected $_sFilter1Value;
  28. protected $_aFilter1Values;
  29. protected $_sFilter2Name;
  30. protected $_sFilter2Value;
  31. protected $_aFilter2Values;
  32. protected $_sFilter3Name;
  33. protected $_sFilter3Value;
  34. protected $_aFilter3Values;
  35. protected $_sParamsDivider;
  36. public function __construct ($aOptions, $oTemplate = false)
  37. {
  38. $this->_oModule = BxDolModule::getInstance('bx_forum');
  39. parent::__construct ($aOptions, $oTemplate);
  40. $this->_sFilter1Name = 'filter1';
  41. $this->_aFilter1Values = array(
  42. '' => _t('_bx_forum_grid_filter_resolved_all'),
  43. BX_FORUM_FILTER_STATUS_RESOLVED => _t('_bx_forum_grid_filter_resolved_resolved'),
  44. BX_FORUM_FILTER_STATUS_UNRESOLVED => _t('_bx_forum_grid_filter_resolved_unresolved'),
  45. );
  46. $sFilter1 = bx_get($this->_sFilter1Name);
  47. if(!empty($sFilter1)) {
  48. $this->_sFilter1Value = bx_process_input($sFilter1);
  49. $this->_aQueryAppend['filter1'] = $this->_sFilter1Value;
  50. }
  51. $this->_sFilter2Name = 'filter2';
  52. $aBadges = BxDolBadges::getInstance()->getData(['type' => 'by_module', 'module' => $this->_oModule->_aModule['name']]);
  53. $this->_aFilter2Values = array(
  54. '' => _t('_bx_forum_grid_filter_badges_all'),
  55. );
  56. foreach($aBadges as $aBadge) {
  57. $this->_aFilter2Values[$aBadge['id']] = $aBadge['text'];
  58. }
  59. $sFilter2 = bx_get($this->_sFilter1Name);
  60. if(!empty($sFilter2)) {
  61. $this->_sFilter2Value = bx_process_input($sFilter2);
  62. $this->_aQueryAppend['filter2'] = $this->_sFilter2Value;
  63. }
  64. $this->_sFilter3Name = 'filter3';
  65. $this->_aFilter3Values = array(
  66. BX_FORUM_FILTER_ORDER_RECENT => _t('_bx_forum_grid_filter_order_recent'),
  67. BX_FORUM_FILTER_ORDER_NEW => _t('_bx_forum_grid_filter_order_new'),
  68. BX_FORUM_FILTER_ORDER_TOP => _t('_bx_forum_grid_filter_order_top'),
  69. BX_FORUM_FILTER_ORDER_UPDATED => _t('_bx_forum_grid_filter_order_updated'),
  70. BX_FORUM_FILTER_ORDER_POPULAR => _t('_bx_forum_grid_filter_order_popular'),
  71. BX_FORUM_FILTER_ORDER_FEATURED => _t('_bx_forum_grid_filter_order_featured'),
  72. BX_FORUM_FILTER_ORDER_FAVORITE => _t('_bx_forum_grid_filter_order_favorite'),
  73. BX_FORUM_FILTER_ORDER_PARTAKEN => _t('_bx_forum_grid_filter_order_partaken'),
  74. );
  75. $sFilter3 = bx_get($this->_sFilter3Name);
  76. if(!empty($sFilter3)) {
  77. $this->_sFilter1Value = bx_process_input($sFilter3);
  78. $this->_aQueryAppend['filter3'] = $this->_sFilter1Value;
  79. }
  80. $this->_sParamsDivider = '#-#';
  81. $this->_sDefaultSortingOrder = 'DESC';
  82. $this->_sDefaultSource = $this->_aOptions['source'];
  83. }
  84. public function setBrowseParams($aParams)
  85. {
  86. if(isset($aParams['ajax_paginate'])) {
  87. if(!$aParams['ajax_paginate']) {
  88. list($sPageLink, $aPageParams) = bx_get_base_url_inline();
  89. $aPageParams[$this->_aOptions['paginate_get_start']] = '{start}';
  90. $this->_aOptions['paginate_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams));
  91. }
  92. unset($aParams['ajax_paginate']);
  93. }
  94. if ($aParams['type'])
  95. $this->_sFilter3Value = $aParams['type'];
  96. parent::setBrowseParams($aParams);
  97. $sField = 'added';
  98. if(!empty($this->_aBrowseParams['type']))
  99. switch($this->_aBrowseParams['type']) {
  100. case 'new':
  101. case 'index':
  102. case 'author':
  103. case 'favorite':
  104. case 'category':
  105. $sField = 'added';
  106. break;
  107. case 'featured':
  108. $sField = 'featured';
  109. break;
  110. case 'updated':
  111. $sField = 'changed';
  112. break;
  113. case 'latest':
  114. $sField = 'lr_timestamp';
  115. break;
  116. case 'popular':
  117. $sField = 'views';
  118. break;
  119. }
  120. $this->_aOptions['field_order'] = $sField;
  121. $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');
  122. }
  123. public function getCode ($isDisplayHeader = true)
  124. {
  125. $sCode = parent::getCode($isDisplayHeader);
  126. if(empty($sCode))
  127. return '';
  128. return $this->_oModule->_oTemplate->parseHtmlByName('units.html', array(
  129. 'code' => $sCode,
  130. 'js_code' => $this->_oModule->_oTemplate->getJsCode('main')
  131. ));
  132. }
  133. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  134. {
  135. if($this->_oModule->checkAllowedAdd() !== CHECK_ACTION_RESULT_ALLOWED)
  136. return '';
  137. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oModule->_oConfig->CNF['URI_ADD_ENTRY']);
  138. unset($a['attr']['bx_grid_action_independent']);
  139. $a['attr'] = array_merge($a['attr'], array(
  140. "onclick" => "window.open('" . $sUrl . "','_self');"
  141. ));
  142. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  143. }
  144. protected function _getRowHead ()
  145. {
  146. return array();
  147. }
  148. protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow)
  149. {
  150. $aField['attr_cell']['class'] = 'bx-grid-table-cell-' . $sKey;
  151. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  152. }
  153. protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow)
  154. {
  155. $mixedValue = $this->_oModule->_oTemplate->getEntryAuthor($aRow);
  156. return self::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  157. }
  158. protected function _getCellCategory($mixedValue, $sKey, $aField, $aRow)
  159. {
  160. $sIcon = '';
  161. $o = BxDolCategory::getObjectInstance('bx_forum_cats');
  162. $aCategoryData = $this->_oModule->_oDb->getCategories(array('type' => 'by_category', 'category' => $aRow['cat']));
  163. if(isset($aCategoryData['icon']))
  164. $sIcon = $this->_oTemplate->getImage($aCategoryData['icon'], array('class' => 'sys-icon'));
  165. $mixedValue = $this->_oModule->_oTemplate->parseHtmlByName('thumb.html', ['icon' => $sIcon, 'title' => $o->getCategoryTitle($aRow['cat'])]);
  166. return self::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  167. }
  168. protected function _getCellRating($mixedValue, $sKey, $aField, $aRow)
  169. {
  170. $CNF = &$this->_oModule->_oConfig->CNF;
  171. $oObject = isset($CNF['OBJECT_SCORES']) ? BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $aRow[$CNF['FIELD_ID']]) : null;
  172. $mixedValue = $oObject ? $oObject->getElementInline() : '';
  173. return self::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  174. }
  175. protected function _getCellParticipants($mixedValue, $sKey, $aField, $aRow)
  176. {
  177. $CNF = &$this->_oModule->_oConfig->CNF;
  178. $oObject = isset($CNF['OBJECT_COMMENTS']) ? BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $aRow[$CNF['FIELD_ID']]) : null;
  179. $mixedValue = $oObject ? $oObject->getCounter(['show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => true, 'caption' => '', 'show_icon' => false, 'caption_empty' => '']) : '';
  180. return self::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  181. }
  182. protected function _getCellText($mixedValue, $sKey, $aField, $aRow)
  183. {
  184. $mixedValue = $this->_oModule->_oTemplate->getEntryPreview($aRow, ['show_meta_counters' => false, 'show_meta_reply' => false]);
  185. return self::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  186. }
  187. protected function _addJsCss()
  188. {
  189. parent::_addJsCss();
  190. $this->_oModule->_oTemplate->addJs(array('main.js'));
  191. $this->_oModule->_oTemplate->addCss(array('main.css', 'main-media-phone.css', 'main-media-tablet.css', 'main-media-desktop.css'));
  192. }
  193. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  194. {
  195. $CNF = &$this->_oModule->_oConfig->CNF;
  196. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  197. list($this->_sFilter1Value, $this->_sFilter2Value, $this->_sFilter3Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  198. // featured
  199. if ($this->_sFilter3Value == BX_FORUM_FILTER_ORDER_FEATURED){
  200. $this->_aBrowseParams['where'] = ['fld' => 'featured', 'val' => 0, 'opr' => '<>'];
  201. }
  202. // partaken
  203. if ($this->_sFilter3Value == BX_FORUM_FILTER_ORDER_PARTAKEN){
  204. $this->_aBrowseParams['select'] = array(
  205. 'tbla' => 'tco',
  206. 'fld' => 'cmt_author_id',
  207. );
  208. $this->_aBrowseParams['join'] = array(
  209. 'tp' => 'INNER',
  210. 'tbl1' => 'bx_forum_cmts',
  211. 'tbl1a' => 'tco',
  212. 'fld1' => 'cmt_object_id',
  213. 'tbl2' => 'bx_forum_discussions',
  214. 'fld2' => 'id'
  215. );
  216. $this->_aBrowseParams['where'] = array(
  217. 'tbl' => 'tco',
  218. 'fld' => 'cmt_author_id',
  219. 'val' => bx_get_logged_profile_id(),
  220. 'opr' => '='
  221. );
  222. $this->_aBrowseParams['group_by'] = array(
  223. 'tbl' => 'bx_forum_discussions',
  224. 'fld' => 'id',
  225. );
  226. }
  227. //FAVORITE
  228. if ($this->_sFilter3Value == BX_FORUM_FILTER_ORDER_FAVORITE){
  229. $oProfile = BxDolProfile:: getInstance(bx_process_input(bx_get('profile_id'), BX_DATA_INT));
  230. if(!$oProfile)
  231. $oProfile = BxDolProfile::getInstance();
  232. if(!$oProfile)
  233. return '';
  234. $iProfileId = $oProfile->id();
  235. $iProfileAuthor = $oProfile->id();
  236. $oFavorite = $this->_oModule->getObjectFavorite();
  237. if(!$oFavorite->isPublic() && $iProfileAuthor != bx_get_logged_profile_id())
  238. return '';
  239. $aConditions = $oFavorite->getConditionsTrack($CNF['TABLE_ENTRIES'], 'id', $iProfileAuthor);
  240. if(empty($aConditions) || !is_array($aConditions))
  241. return '';
  242. $aJoinGroup = array('grp' => true, 'cnds' => array());
  243. if(!empty($aConditions['join']))
  244. foreach($aConditions['join'] as $aCondition)
  245. $aJoinGroup['cnds'][] = array(
  246. 'tp' => $aCondition['type'],
  247. 'tbl1' => $aCondition['table'],
  248. 'fld1' => $aCondition['onField'],
  249. 'tbl2' => $aCondition['mainTable'],
  250. 'fld2' => $aCondition['mainField']
  251. );
  252. $aWhereGroup = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
  253. if(!empty($aConditions['restriction']))
  254. foreach($aConditions['restriction'] as $aCondition)
  255. $aWhereGroup['cnds'][] = array(
  256. 'tbl' => (!empty($aCondition['table']) ? $aCondition['table'] : ''),
  257. 'fld' => $aCondition['field'],
  258. 'val' => $aCondition['value'],
  259. 'opr' => $aCondition['operator']
  260. );
  261. $this->_aBrowseParams['author'] = $iProfileId;
  262. $this->_aBrowseParams['join'] = $aJoinGroup;
  263. $this->_aBrowseParams['where'] = $aWhereGroup;
  264. }
  265. $CNF = $this->_oModule->_oConfig->CNF;
  266. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupByClause = '';
  267. //--- Check status
  268. $sWhereClause .= " AND `" . $this->_oModule->_oConfig->CNF['FIELD_STATUS'] . "`='active'";
  269. $sWhereClause .= " AND `" . $this->_oModule->_oConfig->CNF['FIELD_STATUS_ADMIN'] . "`='active'";
  270. //--- Check privacy
  271. $iAuthorId = 0;
  272. if(!empty($this->_aBrowseParams['author'])) {
  273. $oProfileAuthor = BxDolProfile::getInstance((int)$this->_aBrowseParams['author']);
  274. if($oProfileAuthor)
  275. $iAuthorId = $oProfileAuthor->id();
  276. }
  277. $sPrivacy = $this->_oModule->_oConfig->CNF['OBJECT_PRIVACY_VIEW'];
  278. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacy);
  279. $aCondition = $oPrivacy ? $oPrivacy->getContentPublicAsSQLPart($iAuthorId) : array();
  280. if(isset($aCondition['join']))
  281. $sJoinClause .= $aCondition['join'];
  282. if(isset($aCondition['where']))
  283. $sWhereClause .= $aCondition['where'];
  284. //--- Check browse params
  285. if(!empty($this->_aBrowseParams['select']) && is_array($this->_aBrowseParams['select'])) {
  286. $sSelectClauseBrowse = $this->_getSqlSelectFromGroup($this->_aBrowseParams['select']);
  287. if(!empty($sSelectClauseBrowse))
  288. $sSelectClause .= ", " . $sSelectClauseBrowse;
  289. }
  290. if(!empty($this->_aBrowseParams['join']) && is_array($this->_aBrowseParams['join'])) {
  291. $sJoinClauseBrowse = $this->_getSqlJoinFromGroup($this->_aBrowseParams['join']);
  292. if(!empty($sJoinClauseBrowse))
  293. $sJoinClause .= " " . $sJoinClauseBrowse;
  294. }
  295. if(!empty($this->_aBrowseParams['where']) && is_array($this->_aBrowseParams['where'])) {
  296. $sWhereClauseBrowse = $this->_getSqlWhereFromGroup($this->_aBrowseParams['where']);
  297. if(!empty($sWhereClauseBrowse))
  298. $sWhereClause .= " AND " . $sWhereClauseBrowse;
  299. }
  300. if(!empty($this->_aBrowseParams['group_by']) && is_array($this->_aBrowseParams['group_by'])) {
  301. $sGroupByClauseBrowse = $this->_getSqlGroupByFromGroup($this->_aBrowseParams['group_by']);
  302. if(!empty($sGroupByClauseBrowse))
  303. $sGroupByClause .= " GROUP BY " . $sGroupByClauseBrowse;
  304. }
  305. $sFilterSql = "";
  306. // filter by resolved status
  307. if(isset($this->_sFilter1Value) && $this->_sFilter1Value != ''){
  308. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_RESOLVABLE'] . "` = 1 AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_RESOLVE'] . "` = " . $this->_sFilter1Value;
  309. }
  310. // filter by badges
  311. if(isset($this->_sFilter2Value) && $this->_sFilter2Value != ''){
  312. $aObjects = BxDolBadges::getInstance()->getData(['type' => 'by_module&badge', 'badge_id' => $this->_sFilter2Value, 'module' => $this->_oModule->_aModule['name']]);
  313. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "` IN (" . implode(',', $aObjects ) . ")";
  314. }
  315. $this->_aOptions['source'] = sprintf($this->_sDefaultSource, $sSelectClause, $sJoinClause, $sWhereClause, $sGroupByClause);
  316. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  317. }
  318. protected function _getDataSqlOrderClause($sOrderByFilter, $sOrderField, $sOrderDir, $bFieldsOnly = false)
  319. {
  320. $CNF = &$this->_oModule->_oConfig->CNF;
  321. if(isset($this->_sFilter3Value) && $this->_sFilter3Value != ''){
  322. $sOrderField = $this->_aBrowseParams['type'] = $this->_sFilter3Value;
  323. if (!empty($this->_aBrowseParams['type']) && $this->_aBrowseParams['type'] != 'partaken'){
  324. $this->_aBrowseParams['type'] = $this->_aBrowseParams['type'] = $this->_sFilter3Value;
  325. }
  326. }
  327. $sOrder = parent::_getDataSqlOrderClause($sOrderByFilter, $sOrderField, $sOrderDir, true);
  328. if(!empty($this->_aBrowseParams['type']))
  329. switch($this->_aBrowseParams['type']) {
  330. case 'top':
  331. $aPartsUp = $aPartsDown = array(0);
  332. if(($oVote = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], 0, false)) !== false && $oVote->isEnabled()) {
  333. $aVote = $oVote->getSystemInfo();
  334. if(!empty($aVote['trigger_table']) && !empty($aVote['trigger_field_count']))
  335. $aPartsUp[] = '`' . $aVote['trigger_table'] . '`.`' . $aVote['trigger_field_count'] . '`';
  336. }
  337. if(($oReaction = BxDolVote::getObjectInstance($CNF['OBJECT_REACTIONS'], 0, false)) !== false && $oReaction->isEnabled()) {
  338. $aReaction = $oReaction->getSystemInfo();
  339. if(!empty($aReaction['trigger_table']) && !empty($aReaction['trigger_field_count']))
  340. $aPartsUp[] = '`' . $aReaction['trigger_table'] . '`.`' . $aReaction['trigger_field_count'] . '`';
  341. }
  342. if(($oScore = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], 0, false)) !== false && $oScore->isEnabled()) {
  343. $aScore = $oScore->getSystemInfo();
  344. if(!empty($aScore['trigger_table']) && !empty($aScore['trigger_field_cup']) && !empty($aScore['trigger_field_cdown'])) {
  345. $aPartsUp[] = '`' . $aScore['trigger_table'] . '`.`' . $aScore['trigger_field_cup'] . '`';
  346. $aPartsDown[] = '`' . $aScore['trigger_table'] . '`.`' . $aScore['trigger_field_cdown'] . '`';
  347. }
  348. }
  349. $sOrder = pow(10, 8) . ' * ((' . implode(' + ', $aPartsUp) . ') - (' . implode(' + ', $aPartsDown) . ')) / (UNIX_TIMESTAMP() - `' . $this->_aOptions['table'] . '`.`' . $CNF['FIELD_ADDED'] . '`) ' . $this->_sDefaultSortingOrder;
  350. break;
  351. case 'partaken':
  352. $sOrder = 'MAX(`tco`.`cmt_time`) ' . $this->_sDefaultSortingOrder;
  353. break;
  354. }
  355. return " ORDER BY `" . $CNF['FIELD_STICK'] . "` DESC, " . $sOrder;
  356. }
  357. protected function _getSqlSelectFromGroup($aGrp)
  358. {
  359. if(!isset($aGrp['grp']) || (bool)$aGrp['grp'] !== true)
  360. return $this->_getSqlSelectFromCondition($aGrp);
  361. $sResult = "";
  362. if(empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  363. return $sResult;
  364. $sOprGrp = ", ";
  365. foreach($aGrp['cnds'] as $aCnd) {
  366. $sResultCnd = $this->_getSqlSelectFromCondition($aCnd);
  367. if(!empty($sResultCnd))
  368. $sResult .= $sOprGrp . $sResultCnd;
  369. }
  370. return trim($sResult, $sOprGrp);
  371. }
  372. protected function _getSqlSelectFromCondition($aCnd)
  373. {
  374. $sResult = "";
  375. if(!isset($aCnd['fld']))
  376. return $sResult;
  377. $sResult = "`" . $aCnd['fld'] . "`";
  378. if(!empty($aCnd['tbla']))
  379. $sResult = "`" . $aCnd['tbla'] . "`." . $sResult;
  380. else if(!empty($aCnd['tbl']))
  381. $sResult = "`" . $aCnd['tbl'] . "`." . $sResult;
  382. return $sResult;
  383. }
  384. protected function _getSqlJoinFromGroup($aGrp)
  385. {
  386. if(!isset($aGrp['grp']) || (bool)$aGrp['grp'] !== true)
  387. return $this->_getSqlJoinFromCondition($aGrp);
  388. $sResult = "";
  389. if(empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  390. return $sResult;
  391. $sOprGrp = " ";
  392. foreach($aGrp['cnds'] as $aCnd) {
  393. $sResultCnd = $this->_getSqlJoinFromCondition($aCnd);
  394. if(!empty($sResultCnd))
  395. $sResult .= $sOprGrp . $sResultCnd;
  396. }
  397. return trim($sResult, $sOprGrp);
  398. }
  399. protected function _getSqlJoinFromCondition($aCnd)
  400. {
  401. $sResult = "";
  402. if(!isset($aCnd['tp'], $aCnd['tbl1'], $aCnd['fld1'], $aCnd['fld2']))
  403. return $sResult;
  404. $sTbl1A = $aCnd['tbl1'];
  405. if(!empty($aCnd['tbl1a']))
  406. $sTbl1A = $aCnd['tbl1a'];
  407. $sField1 = "`" . $sTbl1A . "`.`" . $aCnd['fld1'] . "`";
  408. $sField2 = "`" . $aCnd['fld2'] . "`";
  409. if(!empty($aCnd['tbl2a']))
  410. $sField2 = "`" . $aCnd['tbl2a'] . "`." . $sField2;
  411. else if(!empty($aCnd['tbl2']))
  412. $sField2 = "`" . $aCnd['tbl2'] . "`." . $sField2;
  413. return $aCnd['tp'] . " JOIN `" . $aCnd['tbl1'] . "` AS `" . $sTbl1A . "` ON (" . $sField1 . " = " . $sField2 . ")";
  414. }
  415. protected function _getSqlWhereFromGroup($aGrp)
  416. {
  417. if(!isset($aGrp['grp']) || (bool)$aGrp['grp'] !== true)
  418. return $this->_getSqlWhereFromCondition($aGrp);
  419. $sResult = "";
  420. if(!isset($aGrp['opr'], $aGrp['cnds']) || empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  421. return $sResult;
  422. $sOprGrp = " " . $aGrp['opr'] . " ";
  423. foreach($aGrp['cnds'] as $aCnd) {
  424. $sMethod = '_getSqlWhereFrom' . (isset($aGrp['grp']) && (bool)$aGrp['grp'] === true ? 'Group' : 'Condition');
  425. $sResultCnd = $this->$sMethod($aCnd);
  426. if(!empty($sResultCnd))
  427. $sResult .= $sOprGrp . $sResultCnd;
  428. }
  429. $sResult = trim($sResult, $sOprGrp);
  430. if(!empty($sResult))
  431. $sResult = "(" . $sResult . ")";
  432. return $sResult;
  433. }
  434. protected function _getSqlWhereFromCondition($aCnd)
  435. {
  436. $sResult = "";
  437. if(!isset($aCnd['fld'], $aCnd['val'], $aCnd['opr']))
  438. return $sResult;
  439. $sMethod = '_getSqlWhereFromCondition' . bx_gen_method_name($aCnd['fld']);
  440. if(method_exists($this, $sMethod)) {
  441. $mixedResult = $this->$sMethod($aCnd);
  442. if(is_array($mixedResult) && isset($mixedResult['fld'], $mixedResult['val'], $mixedResult['opr']))
  443. $aCnd = $mixedResult;
  444. else
  445. return $mixedResult;
  446. }
  447. $sField = "`" . $aCnd['fld'] . "`";
  448. if(!empty($aCnd['tbl']))
  449. $sField = "`" . $aCnd['tbl'] . "`." . $sField;
  450. switch($aCnd['opr']) {
  451. case 'IN':
  452. if(empty($aCnd['val']) || !is_array($aCnd['val']))
  453. break;
  454. $sResult .= $sField . " IN (" . $this->_oModule->_oDb->implode_escape($aCnd['val']) . ")";
  455. break;
  456. case 'LIKE':
  457. $sResult .= $sField . " LIKE " . $this->_oModule->_oDb->escape('%' . $aCnd['val'] . '%');
  458. break;
  459. default:
  460. $sResult .= $sField . " " . $aCnd['opr'] . " " . $this->_oModule->_oDb->escape($aCnd['val']);
  461. }
  462. return $sResult;
  463. }
  464. protected function _getSqlGroupByFromGroup($aGrp)
  465. {
  466. if(!isset($aGrp['grp']) || (bool)$aGrp['grp'] !== true)
  467. return $this->_getSqlGroupByFromCondition($aGrp);
  468. $sResult = "";
  469. if(empty($aGrp['cnds']) || !is_array($aGrp['cnds']))
  470. return $sResult;
  471. $sOprGrp = ", ";
  472. foreach($aGrp['cnds'] as $aCnd) {
  473. $sResultCnd = $this->_getSqlGroupByFromCondition($aCnd);
  474. if(!empty($sResultCnd))
  475. $sResult .= $sOprGrp . $sResultCnd;
  476. }
  477. return trim($sResult, $sOprGrp);
  478. }
  479. protected function _getSqlGroupByFromCondition($aCnd)
  480. {
  481. $sResult = "";
  482. if(!isset($aCnd['fld']))
  483. return $sResult;
  484. $sResult = "`" . $aCnd['fld'] . "`";
  485. if(!empty($aCnd['tbla']))
  486. $sResult = "`" . $aCnd['tbla'] . "`." . $sResult;
  487. else if(!empty($aCnd['tbl']))
  488. $sResult = "`" . $aCnd['tbl'] . "`." . $sResult;
  489. return $sResult;
  490. }
  491. protected function _getSqlWhereFromConditionAuthorComment($aCnd)
  492. {
  493. $aEntriesIds = $this->_oModule->_oDb->getComments(array('type' => 'entries_author_search', 'author' => $aCnd['val']));
  494. if(!empty($aEntriesIds) && is_array($aEntriesIds))
  495. return array('fld' => 'id', 'val' => $aEntriesIds, 'opr' => 'IN');
  496. return '';
  497. }
  498. protected function _getSqlWhereFromConditionKeywordComment($aCnd)
  499. {
  500. $aEntriesIds = $this->_oModule->_oDb->getComments(array('type' => 'entries_keyword_search', 'keyword' => $aCnd['val']));
  501. if(!empty($aEntriesIds) && is_array($aEntriesIds))
  502. return array('fld' => 'id', 'val' => $aEntriesIds, 'opr' => 'IN');
  503. return '';
  504. }
  505. protected function _getFilterControls ()
  506. {
  507. parent::_getFilterControls();
  508. return $this->_getFilterSelectOne($this->_sFilter3Name, $this->_sFilter3Value, $this->_aFilter3Values) . $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values) . $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values) . $this->_getSearchInput();
  509. }
  510. protected function _getSearchInput()
  511. {
  512. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  513. $aInputSearch = array(
  514. 'type' => 'text',
  515. 'name' => 'search',
  516. 'attrs' => array(
  517. 'id' => 'bx-grid-search-' . $this->_sObject,
  518. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sJsObject . '.onChangeFilter(this)',
  519. 'onBlur' => 'javascript:' . $sJsObject . '.onChangeFilter(this)',
  520. )
  521. );
  522. $oForm = new BxTemplFormView(array());
  523. return $oForm->genRow($aInputSearch);
  524. }
  525. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues)
  526. {
  527. if(empty($sFilterName) || empty($aFilterValues))
  528. return '';
  529. $CNF = &$this->_oModule->_oConfig->CNF;
  530. $sJsObject = $this->_oModule->_oConfig->getJsObject('main');
  531. foreach($aFilterValues as $sKey => $sValue)
  532. $aFilterValues[$sKey] = _t($sValue);
  533. $aInputModules = array(
  534. 'type' => 'select',
  535. 'name' => $sFilterName,
  536. 'attrs' => array(
  537. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  538. 'onChange' => 'javascript:' . $sJsObject . '.onChangeFilter(this)'
  539. ),
  540. 'value' => $sFilterValue,
  541. 'values' => $aFilterValues
  542. );
  543. $oForm = new BxTemplFormView(array());
  544. return $oForm->genRow($aInputModules);
  545. }
  546. }
  547. /** @} */