1
0

BxBaseModTextDb.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxBaseModTextDb extends BxBaseModGeneralDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function searchByAuthorTerm($iAuthor, $sTerm, $iLimit)
  21. {
  22. $CNF = &$this->_oConfig->CNF;
  23. if (empty($CNF['FIELDS_QUICK_SEARCH']))
  24. return array();
  25. $aBindings = array(
  26. 'author' => $iAuthor
  27. );
  28. $sWhere = '';
  29. foreach ($CNF['FIELDS_QUICK_SEARCH'] as $sField) {
  30. $aBindings[$sField] = '%' . $sTerm . '%';
  31. $sWhere .= " OR `c`.`$sField` LIKE :" . $sField;
  32. }
  33. $sOrderBy = $this->prepareAsString(" ORDER BY `c`.`added` DESC LIMIT ?", (int)$iLimit);
  34. $sQuery = "SELECT `c`.* FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` AS `c` WHERE `c`.`" . $CNF['FIELD_AUTHOR'] . "`=:author AND (0 $sWhere)" . $sOrderBy;
  35. return $this->getAll($sQuery, $aBindings);
  36. }
  37. //--- Link attach related methods ---//
  38. public function getUnusedLinks($iUserId)
  39. {
  40. return $this->getLinksBy(array(
  41. 'type' => 'unused',
  42. 'profile_id' => $iUserId
  43. ));
  44. }
  45. public function deleteUnusedLinks($iUserId, $iLinkId = 0)
  46. {
  47. if(!$this->_oConfig->isAttachLinks())
  48. return false;
  49. $CNF = &$this->_oConfig->CNF;
  50. $aBindings = [
  51. 'profile_id' => $iUserId
  52. ];
  53. $sWhereAddon = '';
  54. if(!empty($iLinkId)) {
  55. $aBindings['id'] = $iLinkId;
  56. $sWhereAddon = " AND `id`=:id";
  57. }
  58. return $this->query("DELETE FROM `tl`, `tlc` USING `" . $CNF['TABLE_LINKS'] . "` AS `tl` LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id` WHERE `tl`.`profile_id`=:profile_id AND ISNULL(`tlc`.`content_id`)" . $sWhereAddon, $aBindings);
  59. }
  60. public function saveLink($iContentId, $iLinkId)
  61. {
  62. if(!$this->_oConfig->isAttachLinks())
  63. return false;
  64. $CNF = &$this->_oConfig->CNF;
  65. $aBindings = array(
  66. 'content_id' => $iContentId,
  67. 'link_id' => $iLinkId
  68. );
  69. $iId = $this->getOne("SELECT `id` FROM `" . $CNF['TABLE_LINKS2CONTENT'] . "` WHERE `content_id`=:content_id AND `link_id`=:link_id LIMIT 1", $aBindings);
  70. if(!empty($iId))
  71. return true;
  72. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_LINKS2CONTENT'] . "` SET `content_id`=:content_id, `link_id`=:link_id", $aBindings) > 0;
  73. }
  74. public function deleteLink($iId)
  75. {
  76. if(!$this->_oConfig->isAttachLinks())
  77. return false;
  78. $CNF = &$this->_oConfig->CNF;
  79. return (int)$this->query("DELETE FROM `tl`, `tlc` USING `" . $CNF['TABLE_LINKS'] . "` AS `tl` LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id` WHERE `tl`.`id` = :id", array(
  80. 'id' => $iId
  81. )) > 0;
  82. }
  83. public function deleteLinks($iContentId)
  84. {
  85. if(!$this->_oConfig->isAttachLinks())
  86. return false;
  87. $CNF = &$this->_oConfig->CNF;
  88. return (int)$this->query("DELETE FROM `tl`, `tlc` USING `" . $CNF['TABLE_LINKS'] . "` AS `tl` LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id` WHERE `tlc`.`content_id` = :content_id", array(
  89. 'content_id' => $iContentId
  90. )) > 0;
  91. }
  92. public function getLinks($iContentId)
  93. {
  94. return $this->getLinksBy(array('type' => 'content_id', 'content_id' => $iContentId));
  95. }
  96. public function getStatByProfile($iAuthorId)
  97. {
  98. $aBindings = array(
  99. 'author' => $iAuthorId
  100. );
  101. $CNF = &$this->_oConfig->CNF;
  102. return $this->getRow("SELECT COUNT(id) AS count, SUM(views) AS views, SUM(votes) AS votes, SUM(rvotes) AS rvotes FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `author`=:author", $aBindings);
  103. }
  104. public function getLinksBy($aParams = array())
  105. {
  106. if(!$this->_oConfig->isAttachLinks())
  107. return [];
  108. $CNF = &$this->_oConfig->CNF;
  109. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  110. $sSelectClause = "`tl`.*";
  111. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  112. switch($aParams['type']) {
  113. case 'id':
  114. $aMethod['name'] = 'getRow';
  115. $aMethod['params'][1] = array(
  116. 'id' => $aParams['id']
  117. );
  118. $sWhereClause = " AND `tl`.`id`=:id";
  119. if(!empty($aParams['profile_id'])) {
  120. $aMethod['params'][1]['profile_id'] = $aParams['profile_id'];
  121. $sWhereClause .= " AND `tl`.`profile_id`=:profile_id";
  122. }
  123. break;
  124. case 'content_id':
  125. $aMethod['params'][1] = array(
  126. 'content_id' => $aParams['content_id']
  127. );
  128. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id`";
  129. $sWhereClause = " AND `tlc`.`content_id`=:content_id";
  130. break;
  131. case 'unused':
  132. $aBindings = array(
  133. 'profile_id' => $aParams['profile_id']
  134. );
  135. if(isset($aParams['short']) && $aParams['short'] === true) {
  136. $aMethod['name'] = 'getPairs';
  137. $aMethod['params'][1] = 'url';
  138. $aMethod['params'][2] = 'id';
  139. $aMethod['params'][3] = $aBindings;
  140. }
  141. else
  142. $aMethod['params'][1] = $aBindings;
  143. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_LINKS2CONTENT'] . "` AS `tlc` ON `tl`.`id`=`tlc`.`link_id`";
  144. $sWhereClause = " AND `tl`.`profile_id`=:profile_id AND ISNULL(`tlc`.`content_id`)";
  145. $sOrderClause = "`tl`.`added` DESC";
  146. break;
  147. }
  148. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  149. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  150. $aMethod['params'][0] = "SELECT
  151. " . $sSelectClause . "
  152. FROM `" . $CNF['TABLE_LINKS'] . "` AS `tl` " . $sJoinClause . "
  153. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  154. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  155. }
  156. protected function _getEntriesBySearchIds($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
  157. {
  158. $CNF = &$this->_oConfig->CNF;
  159. if($CNF['FIELD_STATUS'])
  160. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_STATUS'] . "`='active'";
  161. if($CNF['FIELD_STATUS_ADMIN'])
  162. $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_STATUS_ADMIN'] . "`='active'";
  163. parent::_getEntriesBySearchIds($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  164. }
  165. }
  166. /** @} */