BxBaseModGeneralDb.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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['where'])) {
  75. $sWhereConditions .= $aSql['where'];
  76. if(!empty($aSql['join']))
  77. $sJoinClause .= $aSql['join'];
  78. }
  79. break;
  80. case 'between':
  81. if(!is_array($aSearchParam['value']) || count($aSearchParam['value']) != 2)
  82. break;
  83. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` >= :" . $sSearchParam . "_from";
  84. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "` <= :" . $sSearchParam . "_to";
  85. $aMethod['params'][1][$sSearchParam . "_from"] = $aSearchParam['value'][0];
  86. $aMethod['params'][1][$sSearchParam . "_to"] = $aSearchParam['value'][1];
  87. break;
  88. default:
  89. if(empty($aSearchParam['operator']))
  90. break;
  91. $sSearchValue = " " . $aSearchParam['operator'] . " :" . $sSearchParam;
  92. $aMethod['params'][1][$sSearchParam] = $aSearchParam['value'];
  93. }
  94. if(!empty($sSearchValue))
  95. $sWhereConditions .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $sSearchParam . "`" . $sSearchValue;
  96. }
  97. $sWhereClause .= " AND (" . $sWhereConditions . ")";
  98. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  99. break;
  100. case 'all_ids':
  101. $aMethod['name'] = 'getColumn';
  102. $sSelectClause = "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ID'] . "`";
  103. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  104. break;
  105. case 'all':
  106. $sOrderClause .= "`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_ADDED'] . "` ASC";
  107. break;
  108. }
  109. if(!empty($sOrderClause))
  110. $sOrderClause = 'ORDER BY ' . $sOrderClause;
  111. if(!empty($sLimitClause))
  112. $sLimitClause = 'LIMIT ' . $sLimitClause;
  113. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  114. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  115. }
  116. public function getEntriesByAuthor ($iProfileId)
  117. {
  118. $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ? ORDER BY `" . $this->_oConfig->CNF['FIELD_ADDED'] . "` DESC", $iProfileId);
  119. return $this->getAll($sQuery);
  120. }
  121. public function getEntriesNumByAuthor ($iProfileId)
  122. {
  123. $sQuery = $this->prepare ("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_AUTHOR'] . "` = ?", $iProfileId);
  124. return $this->getOne($sQuery);
  125. }
  126. public function alterFulltextIndex ()
  127. {
  128. $CNF = $this->_oConfig->CNF;
  129. $bFulltextIndex = false;
  130. $aIndexes = $this->getAll("SHOW INDEXES FROM `" . $CNF['TABLE_ENTRIES'] . "`");
  131. foreach ($aIndexes as $r) {
  132. if ($CNF['TABLE_ENTRIES_FULLTEXT'] == $r['Key_name']) {
  133. $bFulltextIndex = true;
  134. break;
  135. }
  136. }
  137. if ($bFulltextIndex)
  138. $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` DROP INDEX `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "`");
  139. $sFields = getParam($CNF['PARAM_SEARCHABLE_FIELDS']);
  140. if (!$sFields || !($aFields = explode(',', $sFields)))
  141. return true;
  142. $sFields = '';
  143. foreach ($aFields as $s)
  144. $sFields .= "`$s`,";
  145. return $this->query("ALTER TABLE `" . $CNF['TABLE_ENTRIES'] . "` ADD FULLTEXT `" . $CNF['TABLE_ENTRIES_FULLTEXT'] . "` (" . trim($sFields, ', ') . ")");
  146. }
  147. }
  148. /** @} */