BxDolTwigModuleDb.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?
  2. // TODO: decide later what to do with twig* classes and module, it looks like they will stay and 'complex' 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. /**
  24. * Base database class for modules like events/groups/store
  25. */
  26. class BxDolTwigModuleDb extends BxDolModuleDb {
  27. var $_sTableMain = 'main';
  28. var $_sTableMediaPrefix = '';
  29. var $_sFieldId = 'id';
  30. var $_sFieldAuthorId = 'author_id';
  31. var $_sFieldUri = 'uri';
  32. var $_sFieldTitle = 'title';
  33. var $_sFieldDescription = 'desc';
  34. var $_sFieldTags = 'tags';
  35. var $_sFieldThumb = 'thumb';
  36. var $_sFieldStatus = 'status';
  37. var $_sFieldFeatured = 'featured';
  38. var $_sFieldCreated = 'created';
  39. var $_sFieldDesc = 'desc';
  40. var $_sFieldFansCount = 'fans_count';
  41. var $_sTableFans = 'fans';
  42. var $_sTableAdmins = 'admins';
  43. var $_sFieldAllowViewTo = 'allow_view_to';
  44. var $_sFieldCommentCount = 'comments_count';
  45. /**
  46. * Constructor.
  47. */
  48. function BxDolTwigModuleDb(&$oConfig) {
  49. parent::__construct($oConfig);
  50. }
  51. // entry functions
  52. function isAnyPublicContent() {
  53. return $this->getOne ("SELECT `{$this->_sFieldId}` FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldStatus}` = 'approved' AND `{$this->_sFieldAllowViewTo}` = '" . BX_DOL_PG_ALL . "' LIMIT 1");
  54. }
  55. function getEntryByIdAndOwner ($iId, $iOwner, $isAdmin) {
  56. $sWhere = '';
  57. if (!$isAdmin)
  58. $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
  59. return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldId}` = $iId $sWhere LIMIT 1");
  60. }
  61. function getEntryById ($iId) {
  62. return $this->getEntryByIdAndOwner ($iId, 0, true);
  63. }
  64. function getEntriesByAuthor($iProfileId) {
  65. return $this->getPairs ("SELECT `{$this->_sFieldId}` FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldAuthorId}` = '$iProfileId'", $this->_sFieldId, $this->_sFieldId);
  66. }
  67. function getCountByAuthorAndStatus($iProfileId, $sStatus) {
  68. return $this->getOne ("SELECT COUNT(*) FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldStatus}` = '$sStatus' AND `{$this->_sFieldAuthorId}` = '$iProfileId'");
  69. }
  70. function getEntryByUri ($sUri) {
  71. return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldUri}` = '$sUri' LIMIT 1");
  72. }
  73. function getLatestFeaturedItem () {
  74. $sWhere = " AND `{$this->_sFieldFeatured}` = '1' ";
  75. return $this->getRow ("SELECT * FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldStatus}` = 'approved' AND `{$this->_sFieldAllowViewTo}` = '" . BX_DOL_PG_ALL . "' $sWhere ORDER BY `{$this->_sFieldCreated}` DESC LIMIT 1");
  76. }
  77. function getEntriesByMonth ($iYear, $iMonth, $iNextYear, $iNextMonth) {
  78. return $this->getAll ("SELECT *, DAYOFMONTH(FROM_UNIXTIME(`{$this->_sFieldCreated}`)) AS `Day`
  79. FROM `" . $this->_sPrefix . $this->_sTableMain . "`
  80. WHERE `{$this->_sFieldCreated}` >= UNIX_TIMESTAMP('$iYear-$iMonth-1') AND `{$this->_sFieldCreated}` < UNIX_TIMESTAMP('$iNextYear-$iNextMonth-1') AND `{$this->_sFieldStatus}` = 'approved'");
  81. }
  82. function deleteEntryByIdAndOwner ($iId, $iOwner, $isAdmin) {
  83. $sWhere = '';
  84. if (!$isAdmin)
  85. $sWhere = " AND `{$this->_sFieldAuthorId}` = '$iOwner' ";
  86. if (!($iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldId}` = $iId $sWhere LIMIT 1")))
  87. return false;
  88. $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "images` WHERE `entry_id` = $iId");
  89. return true;
  90. }
  91. function markAsFeatured ($iId) {
  92. return $this->query ("UPDATE `" . $this->_sPrefix . $this->_sTableMain . "` SET `{$this->_sFieldFeatured}` = (`{$this->_sFieldFeatured}` - 1)*(`{$this->_sFieldFeatured}` - 1) WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
  93. }
  94. function activateEntry ($iId) {
  95. return $this->query ("UPDATE `" . $this->_sPrefix . $this->_sTableMain . "` SET `{$this->_sFieldStatus}` = 'approved' WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
  96. }
  97. // media functions
  98. function updateMedia ($iEntryId, $aMediaAdd, $aMediaDelete, $sMediaType) {
  99. $this->deleteMedia ($iEntryId, $aMediaDelete, $sMediaType);
  100. return $this->insertMedia ($iEntryId, $aMediaAdd, $sMediaType);
  101. }
  102. function insertMedia ($iEntryId, $aMedia, $sMediaType) {
  103. if (!$aMedia)
  104. return false;
  105. if (is_array($aMedia))
  106. $sValues = implode ("), ($iEntryId, ", $aMedia);
  107. else
  108. $sValues = (int)$aMedia;
  109. return $this->query ("INSERT IGNORE INTO `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` VALUES ($iEntryId, $sValues)");
  110. }
  111. function deleteMedia ($iEntryId, $aMedia, $sMediaType) {
  112. if (!$aMedia)
  113. return false;
  114. if (is_array($aMedia))
  115. $sValues = implode ("') OR (`entry_id` = $iEntryId AND `media_id` = '", $aMedia);
  116. else
  117. $sValues = (int)$aMedia;
  118. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` WHERE (`entry_id` = '$iEntryId' AND `media_id` = '$sValues')");
  119. }
  120. function deleteEntryMediaAll ($iEntryId, $sMediaType) {
  121. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` WHERE `entry_id` = '$iEntryId'");
  122. }
  123. function deleteMediaFile ($iMediaId, $sMediaType) {
  124. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` WHERE `media_id` = '$iMediaId'");
  125. }
  126. function getMediaIds ($iEntryId, $sMediaType) {
  127. return $this->getPairs ("SELECT `media_id` FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` WHERE `entry_id` = '$iEntryId'", 'media_id', 'media_id');
  128. }
  129. function isMediaInUse ($iMediaId, $sMediaType) {
  130. return $this->getOne ("SELECT `entry_id` FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` WHERE `media_id` = '$iEntryId' LIMIT 1");
  131. }
  132. function getMedia ($iEntryId, $iMediaId, $sMediaType) {
  133. return $this->getRow ("SELECT `entry_id`, `media_id` FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix . "{$sMediaType}` WHERE `entry_id` = '$iEntryId' AND `media_id` = '$iMediaId'");
  134. }
  135. function setThumbnail ($iEntryId, $iImageId) {
  136. if (!$iImageId) {
  137. $iOldThumbId = $this->getOne ("SELECT `{$this->_sFieldThumb}` FROM `" . $this->_sPrefix . $this->_sTableMain . "` WHERE `{$this->_sFieldId}` = '$iEntryId' LIMIT 1");
  138. if ($iOldThumbId > 0 && $this->getOne("SELECT `entry_id` FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix. "images` WHERE `media_id` = '$iOldThumbId' LIMIT 1") > 0)
  139. return false;
  140. $iImageId = $this->getOne("SELECT `media_id` FROM `" . $this->_sPrefix . $this->_sTableMediaPrefix. "images` WHERE `entry_id` = '$iEntryId' LIMIT 1");
  141. }
  142. if (!$iImageId)
  143. return false;
  144. return $this->query ("UPDATE `" . $this->_sPrefix . $this->_sTableMain . "` SET `{$this->_sFieldThumb}` = '$iImageId' WHERE `{$this->_sFieldId}` = '$iEntryId' LIMIT 1");
  145. }
  146. // profile functions
  147. function getProfileNickNameById ($iId) {
  148. return 'Undefined'; // TODO:
  149. }
  150. function getProfileIdByNickName ($sNick, $isProcessDbInput = true) {
  151. if ($isProcessDbInput)
  152. $sNick = process_db_input ($sNick, BX_TAGS_NO_ACTION, BX_SLASHES_NO_ACTION);
  153. return $this->getOne ("SELECT `ID` FROM `Profiles` WHERE `NickName` = '$sNick' LIMIT 1");
  154. }
  155. // settings functions
  156. function getSettingsCategory($sName) {
  157. return $this->getOne("SELECT `ID` FROM `sys_options_cats` WHERE `name` = '{$sName}' LIMIT 1");
  158. }
  159. function getPotentialVisitors ($iProfileId) {
  160. $a = $this->getAllWithKey ("SELECT `p`.`NickName`, `p`.`ID` FROM `sys_friend_list` AS `o` INNER JOIN `Profiles` AS `p` ON (`p`.`ID` = `o`.`ID` AND `o`.`Profile` = '$iProfileId')", 'NickName');
  161. $a = array_merge($a, $this->getAllWithKey ("SELECT `p`.`NickName`, `p`.`ID` FROM `sys_friend_list` AS `o` INNER JOIN `Profiles` AS `p` ON (`p`.`ID` = `o`.`Profile` AND `o`.`ID` = '$iProfileId')", 'NickName'));
  162. $a = array_merge($a, $this->getAllWithKey ("SELECT `p`.`NickName`, `p`.`ID` FROM `sys_fave_list` AS `o` INNER JOIN `Profiles` AS `p` ON (`p`.`ID` = `o`.`ID` AND `o`.`Profile` = '$iProfileId')", 'NickName'));
  163. $a = array_merge($a, $this->getAllWithKey ("SELECT `p`.`NickName`, `p`.`ID` FROM `sys_fave_list` AS `o` INNER JOIN `Profiles` AS `p` ON (`p`.`ID` = `o`.`Profile` AND `o`.`ID` = '$iProfileId')", 'NickName'));
  164. $a = array_merge($a, $this->getAllWithKey ("SELECT `p`.`NickName`, `p`.`ID` FROM `sys_messages` AS `o` INNER JOIN `Profiles` AS `p` ON (`p`.`ID` = `o`.`Recipient` AND `o`.`Sender` = '$iProfileId')", 'NickName'));
  165. foreach ($a as $k => $r) {
  166. if ($iProfileId == $r['ID']) {
  167. unset($a[$k]);
  168. break;
  169. }
  170. }
  171. asort ($a);
  172. return $a;
  173. }
  174. // fans and admins functions
  175. function getBroadcastRecipients ($iEntryId) {
  176. return $this->getAll ("SELECT DISTINCT `p`.`ID`, `p`.`Email` FROM `" . $this->_sPrefix . $this->_sTableFans . "` AS `f` INNER JOIN `Profiles` as `p` ON (`f`.`id_entry` = '$iEntryId' AND `f`.`id_profile` = `p`.`ID` AND `f`.`confirmed` = 1 AND `p`.`Status` = 'Active')");
  177. }
  178. function joinEntry($iEntryId, $iProfileId, $isConfirmed) {
  179. $isConfirmed = $isConfirmed ? 1 : 0;
  180. $iRet = $this->query ("INSERT IGNORE INTO `" . $this->_sPrefix . $this->_sTableFans . "` SET `id_entry` = '$iEntryId', `id_profile` = '$iProfileId', `confirmed` = '$isConfirmed', `when` = '" . time() . "'");
  181. if ($iRet && $isConfirmed)
  182. $this->query ("UPDATE `" . $this->_sPrefix . "main` SET `" . $this->_sFieldFansCount . "` = `" . $this->_sFieldFansCount . "` + 1 WHERE `id` = '$iEntryId'");
  183. return $iRet;
  184. }
  185. function leaveEntry ($iEntryId, $iProfileId) {
  186. $isConfirmed = $this->getOne ("SELECT `confirmed` FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `id_entry` = '$iEntryId' AND `id_profile` = '$iProfileId' LIMIT 1");
  187. $iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `id_entry` = '$iEntryId' AND `id_profile` = '$iProfileId'");
  188. if ($iRet && $isConfirmed)
  189. $this->query ("UPDATE `" . $this->_sPrefix . "main` SET `" . $this->_sFieldFansCount . "` = `" . $this->_sFieldFansCount . "` - 1 WHERE `id` = '$iEntryId'");
  190. return $iRet;
  191. }
  192. function isFan($iEntryId, $iProfileId, $isConfirmed) {
  193. $isConfirmed = $isConfirmed ? 1 : 0;
  194. return $this->getOne ("SELECT `when` FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `id_entry` = '$iEntryId' AND `id_profile` = '$iProfileId' AND `confirmed` = '$isConfirmed' LIMIT 1");
  195. }
  196. function getFansBrowse(&$aProfiles, $iEntryId, $iStart, $iMaxNum) {
  197. return $this->getFans($aProfiles, $iEntryId, true, $iStart, $iMaxNum);
  198. }
  199. function getFans(&$aProfiles, $iEntryId, $isConfirmed, $iStart, $iMaxNum, $aFilter = array()) {
  200. $isConfirmed = $isConfirmed ? 1 : 0;
  201. $sFilter = '';
  202. if ($aFilter) {
  203. $s = implode (' OR `f`.`id_profile` = ', $aFilter);
  204. $sFilter = ' AND (`f`.`id_profile` = ' . $s . ') ';
  205. }
  206. $aProfiles = $this->getAll ("SELECT SQL_CALC_FOUND_ROWS `p`.* FROM `Profiles` AS `p` INNER JOIN `" . $this->_sPrefix . $this->_sTableFans . "` AS `f` ON (`f`.`id_entry` = '$iEntryId' AND `f`.`id_profile` = `p`.`ID` AND `f`.`confirmed` = $isConfirmed AND `p`.`Status` = 'Active' $sFilter) ORDER BY `f`.`when` DESC LIMIT $iStart, $iMaxNum");
  207. return $this->getOne("SELECT FOUND_ROWS()");
  208. }
  209. function confirmFans ($iEntryId, $aProfileIds) {
  210. if (!$aProfileIds)
  211. return false;
  212. $s = implode (' OR `id_profile` = ', $aProfileIds);
  213. $iRet = $this->query ("UPDATE `" . $this->_sPrefix . $this->_sTableFans . "` SET `confirmed` = 1 WHERE `id_entry` = '$iEntryId' AND `confirmed` = 0 AND (`id_profile` = $s)");
  214. if ($iRet)
  215. $this->query ("UPDATE `" . $this->_sPrefix . "main` SET `" . $this->_sFieldFansCount . "` = `" . $this->_sFieldFansCount . "` + $iRet WHERE `id` = '$iEntryId'");
  216. return $iRet;
  217. }
  218. function removeFans ($iEntryId, $aProfileIds) {
  219. if (!$aProfileIds)
  220. return false;
  221. $s = implode (' OR `id_profile` = ', $aProfileIds);
  222. $iRet = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `id_entry` = '$iEntryId' AND `confirmed` = 1 AND (`id_profile` = $s)");
  223. if ($iRet)
  224. $this->query ("UPDATE `" . $this->_sPrefix . "main` SET `" . $this->_sFieldFansCount . "` = `" . $this->_sFieldFansCount . "` - $iRet WHERE `id` = '$iEntryId'");
  225. if ($iRet && $this->_sTableAdmins)
  226. $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableAdmins . "` WHERE `id_entry` = '$iEntryId' AND `id_profile` = $s");
  227. return $iRet;
  228. }
  229. function removeFanFromAllEntries ($iProfileId) {
  230. $iProfileId = (int)$iProfileId;
  231. if (!$iProfileId || !$this->_sTableFans)
  232. return false;
  233. // delete unconfirmed fans
  234. $iDeleted = $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `confirmed` = 0 AND `id_profile` = " . $iProfileId);
  235. // delete confirmed fans
  236. $aEntries = $this->getColumn("SELECT DISTINCT `id_entry` FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `id_profile` = " . $iProfileId);
  237. foreach ($aEntries as $iEntryId) {
  238. $iDeleted += $this->leaveEntry ($iEntryId, $iProfileId) ? 1 : 0;
  239. }
  240. return $iDeleted;
  241. }
  242. function removeAdminFromAllEntries ($iProfileId) {
  243. $iProfileId = (int)$iProfileId;
  244. if (!$iProfileId || !$this->_sTableAdmins)
  245. return false;
  246. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableAdmins . "` WHERE `id_profile` = " . $iProfileId);
  247. }
  248. function rejectFans ($iEntryId, $aProfileIds) {
  249. if (!$aProfileIds)
  250. return false;
  251. $s = implode (' OR `id_profile` = ', $aProfileIds);
  252. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableFans . "` WHERE `id_entry` = '$iEntryId' AND `confirmed` = 0 AND (`id_profile` = $s)");
  253. }
  254. function getAdmins(&$aProfiles, $iEntryId, $iStart, $iMaxNum, $aFilter = array()) {
  255. $sFilter = '';
  256. if ($aFilter) {
  257. $s = implode (' OR `f`.`id_profile` = ', $aFilter);
  258. $sFilter = ' AND (`f`.`id_profile` = ' . $s . ') ';
  259. }
  260. $aProfiles = $this->getAll ("SELECT SQL_CALC_FOUND_ROWS `p`.* FROM `Profiles` AS `p` INNER JOIN `" . $this->_sPrefix . $this->_sTableAdmins . "` AS `f` ON (`f`.`id_entry` = '$iEntryId' AND `f`.`id_profile` = `p`.`ID` AND `p`.`Status` = 'Active' $sFilter) ORDER BY `f`.`when` DESC LIMIT $iStart, $iMaxNum");
  261. return $this->getOne("SELECT FOUND_ROWS()");
  262. }
  263. function isGroupAdmin($iEntryId, $iProfileId) {
  264. return $this->getOne ("SELECT `when` FROM `" . $this->_sPrefix . $this->_sTableAdmins . "` WHERE `id_entry` = '$iEntryId' AND `id_profile` = '$iProfileId' LIMIT 1");
  265. }
  266. function addGroupAdmin($iEntryId, $aProfileIds) {
  267. if (is_array($aProfileIds)) {
  268. $iRet = 0;
  269. foreach ($aProfileIds AS $iProfileId)
  270. $iRet += $this->query ("INSERT IGNORE INTO `" . $this->_sPrefix . $this->_sTableAdmins . "` SET `id_entry` = '$iEntryId', `id_profile` = '$iProfileId', `when` = '" . time() . "'");
  271. return $iRet;
  272. } else {
  273. return $this->query ("INSERT IGNORE INTO `" . $this->_sPrefix . $this->_sTableAdmins . "` SET `id_entry` = '$iEntryId', `id_profile` = '$aProfileIds', `when` = '" . time() . "'");
  274. }
  275. }
  276. function removeGroupAdmin($iEntryId, $aProfileIds) {
  277. if (!$aProfileIds)
  278. return false;
  279. if (is_array($aProfileIds)) {
  280. $s = implode (' OR `id_profile` = ', $aProfileIds);
  281. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableAdmins . "` WHERE `id_entry` = '$iEntryId' AND (`id_profile` = $s)");
  282. } else {
  283. return $this->query ("DELETE FROM `" . $this->_sPrefix . $this->_sTableAdmins . "` WHERE `id_entry` = '$iEntryId' AND `id_profile` = '$aProfileIds'");
  284. }
  285. }
  286. }
  287. ?>