1
0

BxDolTextDb.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // TODO: decide later what to do with text* classes and module, it looks like they will stay and text modules will be still based on it, but some refactoring is needed
  3. /***************************************************************************
  4. * Dolphin Smart Community Builder
  5. * -------------------
  6. * begin : Mon Mar 23 2006
  7. * copyright : (C) 2007 BoonEx Group
  8. * website : http://www.boonex.com
  9. * This file is part of Dolphin - Smart Community Builder
  10. *
  11. * Dolphin is free software; you can redistribute it and/or modify it under
  12. * the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the
  14. * License, or any later version.
  15. *
  16. * Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  17. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU General Public License for more details.
  19. * You should have received a copy of the GNU General Public License along with Dolphin,
  20. * see license.txt file; if not, write to marketing@boonex.com
  21. ***************************************************************************/
  22. bx_import('BxDolModuleDb');
  23. class BxDolTextDb extends BxDolModuleDb {
  24. var $_oConfig;
  25. function BxDolTextDb(&$oConfig) {
  26. parent::__construct($oConfig);
  27. $this->_oConfig = &$oConfig;
  28. }
  29. /**
  30. * Get entries.
  31. */
  32. function getEntries($aParams = array()) {
  33. $sMethod = 'getAll';
  34. $sSelectClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  35. switch($aParams['sample_type']) {
  36. case 'id':
  37. $sMethod = 'getRow';
  38. $sWhereClause = " AND `te`.`id`='" . $aParams['id'] . "'";
  39. $sOrderClause = "`te`.`when` DESC";
  40. $sLimitClause = "LIMIT 1";
  41. break;
  42. case 'uri':
  43. $sMethod = 'getRow';
  44. $sWhereClause = " AND `te`.`uri`='" . $aParams['uri'] . "'";
  45. $sOrderClause = "`te`.`when` DESC";
  46. $sLimitClause = "LIMIT 1";
  47. break;
  48. case 'view':
  49. $sWhereClause = " AND `te`.`uri`='" . $aParams['uri'] . "' AND `te`.`status`='" . BX_TD_STATUS_ACTIVE . "'";
  50. $sOrderClause = "`te`.`when` DESC";
  51. $sLimitClause = "LIMIT 1";
  52. break;
  53. case 'search_unit':
  54. $sWhereClause = " AND `te`.`uri`='" . $aParams['uri'] . "'";
  55. $sOrderClause = "`te`.`when` DESC";
  56. $sLimitClause = "LIMIT 1";
  57. break;
  58. case 'archive':
  59. $sWhereClause = " AND `te`.`status`='" . BX_TD_STATUS_ACTIVE . "'";
  60. $sOrderClause = "`te`.`when` DESC";
  61. $sLimitClause = "LIMIT " . (int)$aParams['start'] . ', ' . (int)$aParams['count'];
  62. break;
  63. case 'featured':
  64. $sWhereClause = " AND `te`.`status`='" . BX_TD_STATUS_ACTIVE . "' AND `te`.`featured`='1'";
  65. $sOrderClause = "`te`.`when` DESC";
  66. $sLimitClause = "LIMIT " . (int)$aParams['start'] . ', ' . (int)$aParams['count'];
  67. break;
  68. case 'top_rated':
  69. $sWhereClause = " AND `te`.`status`='" . BX_TD_STATUS_ACTIVE . "'";
  70. $sOrderClause = "`te`.`rate` DESC";
  71. $sLimitClause = "LIMIT " . (int)$aParams['start'] . ', ' . (int)$aParams['count'];
  72. break;
  73. case 'popular':
  74. $sWhereClause = " AND `te`.`status`='" . BX_TD_STATUS_ACTIVE . "'";
  75. $sOrderClause = "`te`.`view_count` DESC";
  76. $sLimitClause = "LIMIT " . (int)$aParams['start'] . ', ' . (int)$aParams['count'];
  77. break;
  78. case 'admin':
  79. $sWhereClause = !empty($aParams['filter_value']) ? " AND (`caption` LIKE '%" . $aParams['filter_value'] . "%' OR `content` LIKE '%" . $aParams['filter_value'] . "%' OR `tags` LIKE '%" . $aParams['filter_value'] . "%')" : "";
  80. $sOrderClause = "`te`.`when` DESC";
  81. $sLimitClause = "LIMIT " . $aParams['start'] . ', ' . $aParams['count'];
  82. break;
  83. case 'all':
  84. $sWhereClause = " AND `te`.`status`='" . BX_TD_STATUS_ACTIVE . "'";
  85. $sOrderClause = "`te`.`when` DESC";
  86. break;
  87. }
  88. $sSql = "SELECT
  89. " . $sSelectClause . "
  90. `te`.`id` AS `id`,
  91. `te`.`caption` AS `caption`,
  92. `te`.`snippet` AS `snippet`,
  93. `te`.`content` AS `content`,
  94. `te`.`when` AS `when_uts`,
  95. DATE_FORMAT(FROM_UNIXTIME(`te`.`when`), '%Y-%m-%d %H:%i') AS `when`,
  96. DATE_FORMAT(FROM_UNIXTIME(`te`.`when`), '" . $this->_oConfig->getDateFormat() . "') AS `when_uf`,
  97. UNIX_TIMESTAMP() - `te`.`when` AS `ago`,
  98. `te`.`uri` AS `uri`,
  99. `te`.`tags` AS `tags`,
  100. `te`.`categories` AS `categories`,
  101. `te`.`comment` AS `comment`,
  102. `te`.`vote` AS `vote`,
  103. `te`.`date` AS `date`,
  104. `te`.`status` AS `status`,
  105. `te`.`featured` AS `featured`,
  106. `te`.`cmts_count` AS `cmts_count`
  107. FROM `" . $this->_sPrefix . "entries` AS `te`
  108. WHERE 1 " . $sWhereClause . "
  109. ORDER BY " . $sOrderClause . " " . $sLimitClause;
  110. $aResult = $this->$sMethod($sSql);
  111. if(!in_array($aParams['sample_type'], array('id', 'uri', 'view'))) {
  112. $iSnippetLen = $this->_oConfig->getSnippetLength();
  113. for($i = 0; $i < count($aResult); $i++)
  114. $aResult[$i]['content'] = mb_substr(str_replace(array('&nbsp;', '&lt;', '&gt;'), array(' ', '', ''), strip_tags($aResult[$i]['snippet'])), 0, $iSnippetLen);
  115. }
  116. return $aResult;
  117. }
  118. /**
  119. * Delete entries.
  120. *
  121. * @param integer/array $mixed ID or an array of ID-s.
  122. * @return boolean result of operation.
  123. */
  124. function deleteEntries($mixed) {
  125. if(!is_array($mixed))
  126. $mixed = array($mixed);
  127. $sSql = "DELETE FROM `" . $this->_sPrefix . "entries` WHERE `id` IN ('" . implode("', '", $mixed) . "')";
  128. return $this->query($sSql) > 0;
  129. }
  130. /**
  131. * Update entries.
  132. *
  133. * @param integer/array $mixed ID or an array of ID-s.
  134. * @param array $aValues key/value pears to be saved in the DB.
  135. * @return boolean result of operation.
  136. */
  137. function updateEntry($mixed, $aValues) {
  138. if(!is_array($mixed))
  139. $mixed = array($mixed);
  140. $sSql = "";
  141. foreach($aValues as $sKey => $sValue)
  142. $sSql .= "`" . $sKey . "`='" . $sValue . "', ";
  143. $sSql = "UPDATE `" . $this->_sPrefix . "entries` SET " . substr($sSql, 0, -2) . " WHERE `id` IN ('" . implode("', '", $mixed) . "')";
  144. return $this->query($sSql) > 0;
  145. }
  146. function getCount($aParams = array()) {
  147. if(!isset($aParams['sample_type']))
  148. $aParams['sample_type'] = '';
  149. switch($aParams['sample_type']) {
  150. case 'featured':
  151. $sWhereClause = "`status`='" . BX_TD_STATUS_ACTIVE . "' AND `featured`='1'";
  152. break;
  153. case 'admin':
  154. $sWhereClause = !empty($aParams['filter_value']) ? "(`caption` LIKE '%" . $aParams['filter_value'] . "%' OR `content` LIKE '%" . $aParams['filter_value'] . "%' OR `tags` LIKE '%" . $aParams['filter_value'] . "%')" : "1";
  155. break;
  156. default:
  157. $sWhereClause = "`status`='" . BX_TD_STATUS_ACTIVE . "'";
  158. break;
  159. }
  160. $sSql = "SELECT COUNT(`id`) FROM `" . $this->_sPrefix . "entries` WHERE " . $sWhereClause . " LIMIT 1";
  161. return (int)$this->getOne($sSql);
  162. }
  163. function getByMonth($iYear, $iMonth, $iNextYear, $iNextMonth) {
  164. $sSql = "SELECT
  165. *,
  166. DAYOFMONTH(FROM_UNIXTIME(`when`)) AS `Day`
  167. FROM `" . $this->_sPrefix . "entries`
  168. WHERE `when` >= UNIX_TIMESTAMP('" . $iYear . "-" . $iMonth . "-1') AND `when` < UNIX_TIMESTAMP('" . $iNextYear . "-" . $iNextMonth . "-1') AND `status` = '0'";
  169. return $this->getAll($sSql);
  170. }
  171. function publish(&$aIds) {
  172. $aIds = $this->getColumn("SELECT
  173. `id`
  174. FROM `" . $this->_sPrefix . "entries`
  175. WHERE `status`='" . BX_TD_STATUS_PENDING . "' AND `when`<=UNIX_TIMESTAMP()");
  176. if(empty($aIds))
  177. return false;
  178. $iStatus = $this->_oConfig->isAutoapprove() ? BX_TD_STATUS_ACTIVE : BX_TD_STATUS_INACTIVE;
  179. return (int)$this->query("UPDATE `" . $this->_sPrefix . "entries`
  180. SET `status`='" . $iStatus . "'
  181. WHERE `id` IN ('" . implode("','", $aIds) . "')") > 0;
  182. }
  183. }
  184. ?>