BxBaseModGeneralDb.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxBaseModGeneralDb extends BxDolModuleDb
  15. {
  16. protected $_oConfig;
  17. public function __construct(&$oConfig)
  18. {
  19. parent::__construct($oConfig);
  20. $this->_oConfig = $oConfig;
  21. }
  22. public function getContentInfoById ($iContentId)
  23. {
  24. $CNF = &$this->_oConfig->CNF;
  25. if(empty($CNF['TABLE_ENTRIES']) || empty($CNF['FIELD_ID']))
  26. return array();
  27. $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_ID'] . "` = ?", $iContentId);
  28. return $this->getRow($sQuery);
  29. }
  30. function getEntriesBy($aParams = array())
  31. {
  32. $CNF = &$this->_oConfig->CNF;
  33. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query', 1 => array()));
  34. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  35. $sSelectClause = "`{$CNF['TABLE_ENTRIES']}`.*";
  36. switch($aParams['type']) {
  37. case 'id':
  38. $aMethod['name'] = 'getRow';
  39. $aMethod['params'][1]['id'] = (int)$aParams['id'];
  40. $sWhereClause .= " AND `{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_ID'] . "` = :id";
  41. break;
  42. case 'author':
  43. $aMethod['name'] = 'getRow';
  44. $aMethod['params'][1]['author'] = (int)$aParams['author'];
  45. $sWhereClause .= " AND `{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_AUTHOR'] . "` = :author";
  46. break;
  47. case 'search_ids':
  48. $aMethod['name'] = 'getColumn';
  49. $sSelectClause = "`{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_ID'] . "`";
  50. $sWhereConditions = "1";
  51. foreach($aParams['search_params'] as $sSearchParam => $aSearchParam) {
  52. $sSearchValue = "";
  53. switch ($aSearchParam['operator']) {
  54. case 'like':
  55. $sSearchValue = " LIKE " . $this->escape("%" . $aSearchParam['value'] . "%");
  56. break;
  57. case 'in':
  58. $sSearchValue = " IN (" . $this->implode_escape($aSearchParam['value']) . ")";
  59. break;
  60. case 'and':
  61. $iResult = 0;
  62. if (is_array($aSearchParam['value']))
  63. foreach ($aSearchParam['value'] as $iValue)
  64. $iResult |= pow (2, $iValue - 1);
  65. else
  66. $iResult = (int)$aSearchParam['value'];
  67. $sSearchValue = " & " . $iResult . "";
  68. break;
  69. case 'locate':
  70. if(!isset($CNF['OBJECT_METATAGS']))
  71. break;
  72. list($fLatitude, $fLongitude, $sCountry, $sState, $sCity, $sZip) = $aSearchParam['value'];
  73. $aSql = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])->locationsGetAsSQLPart($CNF['TABLE_ENTRIES'], $CNF['FIELD_ID'], $sCountry, $sState, $sCity, $sZip);
  74. if(!empty($aSql['join']))
  75. $sJoinClause .= $aSql['join'];
  76. if(!empty($aSql['where']))
  77. $sWhereConditions .= $aSql['where'];
  78. break;
  79. default:
  80. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  81. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  82. }
  83. if(!empty($sSearchValue))
  84. $sWhereConditions .= " AND `{$CNF['TABLE_ENTRIES']}`.`" . $sSearchParam . "`" . $sSearchValue;
  85. }
  86. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  87. $sOrderClause .= "`{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  88. break;
  89. case 'all_ids':
  90. $aMethod['name'] = 'getColumn';
  91. $sSelectClause = "`{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_ID'] . "`";
  92. $sOrderClause .= "`{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  93. break;
  94. case 'all':
  95. $sOrderClause .= "`{$CNF['TABLE_ENTRIES']}`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  96. break;
  97. }
  98. if(!empty($sOrderClause))
  99. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  100. if(!empty($sLimitClause))
  101. $sLimitClause = 'LIMIT ' . $sLimitClause;
  102. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `{$CNF['TABLE_ENTRIES']}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  103. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  104. }
  105. public function getEntriesByAuthor ($iProfileId)
  106. {
  107. $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
  108. return $this->getAll($sQuery);
  109. }
  110. public function getEntriesNumByAuthor ($iProfileId)
  111. {
  112. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
  113. return $this->getOne($sQuery);
  114. }
  115. public function alterFulltextIndex ()
  116. {
  117. $CNF = $this->_oConfig->CNF;
  118. $bFulltextIndex = false;
  119. $aIndexes = $this->getAll("SHOW INDEXES FROM `" . $CNF['TABLE_ENTRIES'] . "`");
  120. foreach ($aIndexes as $r) {
  121. if ($CNF['TABLE_ENTRIES_FULLTEXT'] == $r['Key_name']) {
  122. $bFulltextIndex = true;
  123. break;
  124. }
  125. }
  126. if ($bFulltextIndex)
  127. $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` DROP INDEX `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "`");
  128. if (!($aFields = explode(',', getParam($CNF['PARAM_SEARCHABLE_FIELDS']))))
  129. return true;
  130. $sFields = '';
  131. foreach ($aFields as $s)
  132. $sFields .= "`$s`,";
  133. return $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` ADD FULLTEXT `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "` (" . trim($sFields, ', ') . ")");
  134. }
  135. }
  136. /** @} */