Cheetah
DbAdmin.php
Go to the documentation of this file.
1 <?php
2 
8 // admin operations with database
9 
10 if (!defined('TF_FORUM')) {
11  define('TF_FORUM', '`' . $gConf['db']['prefix'] . 'forum`');
12 }
13 if (!defined('TF_FORUM_CAT')) {
14  define('TF_FORUM_CAT', '`' . $gConf['db']['prefix'] . 'forum_cat`');
15 }
16 if (!defined('TF_FORUM_POST')) {
17  define('TF_FORUM_POST', '`' . $gConf['db']['prefix'] . 'forum_post`');
18 }
19 if (!defined('TF_FORUM_VOTE')) {
20  define('TF_FORUM_VOTE', '`' . $gConf['db']['prefix'] . 'forum_vote`');
21 }
22 if (!defined('TF_FORUM_REPORT')) {
23  define('TF_FORUM_REPORT', '`' . $gConf['db']['prefix'] . 'forum_report`');
24 }
25 if (!defined('TF_FORUM_TOPIC')) {
26  define('TF_FORUM_TOPIC', '`' . $gConf['db']['prefix'] . 'forum_topic`');
27 }
28 if (!defined('TF_FORUM_FLAG')) {
29  define('TF_FORUM_FLAG', '`' . $gConf['db']['prefix'] . 'forum_flag`');
30 }
31 if (!defined('TF_FORUM_USER_STAT')) {
32  define('TF_FORUM_USER_STAT', '`' . $gConf['db']['prefix'] . 'forum_user_stat`');
33 }
34 
35 define('CAT_ORDER_STEP', 128);
36 
37 class DbAdmin extends ChDb
38 {
39  function deleteCategoryAll($cat_id)
40  {
41  $sql = "SELECT `forum_id` FROM " . TF_FORUM . " WHERE `cat_id` = '$cat_id'";
42  $a = $this->getAll($sql);
43  foreach ($a as $r) {
44  $this->deleteForumPosts($r['forum_id']);
45  $this->deleteForumTopics($r['forum_id']);
46  $this->deleteForum($r['forum_id']);
47  }
48 
49  return $this->deleteCategory($cat_id);
50  }
51 
52  function getCatByForumId($forum_id)
53  {
54  $sql = "SELECT `cat_id`, `cat_uri` FROM " . TF_FORUM . " INNER JOIN " . TF_FORUM_CAT . " USING (`cat_id`) WHERE `forum_id` = ? LIMIT 1";
55 
56  return $this->getRow($sql, [$forum_id]);
57  }
58 
59  function getCatOrder($cat_id)
60  {
61  if (!$cat_id) {
62  return false;
63  }
64  $sql = "SELECT `cat_order` FROM " . TF_FORUM_CAT . " WHERE `cat_id` = ? LIMIT 1";
65 
66  return $this->getOne($sql, [$cat_id]);
67  }
68 
69  function getCatsInOrder($cat_order, $dir, $num)
70  {
71  $sql = "SELECT `cat_id`,`cat_order` FROM " . TF_FORUM_CAT . " WHERE `cat_order` " . ($dir == 'up' ? '<' : '>') . " $cat_order ORDER BY `cat_order` " . ($dir == 'up' ? 'DESC' : 'ASC') . " LIMIT $num";
72 
73  return $this->getAll($sql);
74  }
75 
76  function deleteForumAll($forum_id)
77  {
78  $this->deleteForumPosts($forum_id);
79  $this->deleteForumTopics($forum_id);
80 
81  return $this->deleteForum($forum_id);
82  }
83 
84  function deleteCategory($cat_id)
85  {
86  $sql = "DELETE FROM " . TF_FORUM_CAT . " WHERE `cat_id` = ?";
87 
88  return $this->query($sql, [$cat_id]);
89  }
90 
91  function deleteForumPosts($forum_id)
92  {
93  $a = $this->getAll("SELECT `post_id`, `user` FROM " . TF_FORUM_POST . " WHERE `forum_id` = '$forum_id'");
94  foreach ($a as $r) {
95  $this->query("DELETE FROM " . TF_FORUM_VOTE . " WHERE `post_id` = ?", [$r['post_id']]);
96  $this->query("DELETE FROM " . TF_FORUM_REPORT . " WHERE `post_id` = ?", [$r['post_id']]);
97  $this->query("UPDATE " . TF_FORUM_USER_STAT . " SET `posts` = `posts` - 1 WHERE `user` = ? AND `posts` > 0", [$r['user']]);
98  }
99 
100  return $this->query("DELETE FROM " . TF_FORUM_POST . " WHERE `forum_id` = ?", [$forum_id]);
101  }
102 
103  function deleteForumTopics($forum_id)
104  {
105  $a = $this->getAll("SELECT `topic_id` FROM " . TF_FORUM_TOPIC . " WHERE `forum_id` = ?", [$forum_id]);
106  foreach ($a as $r) {
107  $this->query("DELETE FROM " . TF_FORUM_FLAG . " WHERE `topic_id` = ?", [$r['topic_id']]);
108  }
109 
110  return $this->query("DELETE FROM " . TF_FORUM_TOPIC . " WHERE `forum_id` = ?", [$forum_id]);
111  }
112 
113  function deleteForum($forum_id)
114  {
115  $sql = "DELETE FROM " . TF_FORUM . " WHERE `forum_id` = ?";
116 
117  return $this->query($sql, [$forum_id]);
118  }
119 
120  function getCatName($cat_id)
121  {
122  $sql = "SELECT `cat_name` FROM " . TF_FORUM_CAT . " WHERE `cat_id` = ? LIMIT 1";
123 
124  return $this->getOne($sql, [$cat_id]);
125  }
126 
127  function editCategory($cat_id, $cat_name, $cat_order, $cat_expanded)
128  {
129  $sql = "UPDATE " . TF_FORUM_CAT . " SET `cat_name` = ?, `cat_order` = ?, `cat_expanded` = ? WHERE `cat_id` = ?";
130 
131  return $this->query($sql, [$cat_name, $cat_order, $cat_expanded, $cat_id]);
132  }
133 
134  function insertCategory($cat_name, $uri, $cat_order, $cat_expanded)
135  {
136  $sql = "INSERT INTO " . TF_FORUM_CAT . " SET `cat_name` = ?, `cat_uri` = ?, `cat_order` = ?, `cat_expanded` = ?";
137 
138  return $this->query($sql, [$cat_name, $uri, $cat_order, $cat_expanded]);
139  }
140 
141  function getForum($forum_id)
142  {
143  $sql = "SELECT `cat_id`, `forum_title`, `forum_desc`, `forum_order`, `forum_type` FROM " . TF_FORUM . " WHERE `forum_id` = ? LIMIT 1";
144 
145  return $this->getRow($sql, [$forum_id]);
146  }
147 
148  function editForum($forum_id, $title, $desc, $type, $order)
149  {
150  $sql = "UPDATE " . TF_FORUM . " SET `forum_title` = ?, `forum_desc` = ?, `forum_type` = ?, `forum_order` = ? WHERE `forum_id` = ?";
151 
152  return $this->query($sql, [$title, $desc, $type, $order, $forum_id]);
153  }
154 
155  function insertForum($cat_id, $title, $desc, $type, $uri, $order)
156  {
157  $sql = "INSERT INTO " . TF_FORUM . " SET `cat_id` = ?, `forum_title` = ?, `forum_desc` = ?, `forum_type` = ?, `forum_uri` = ?, `forum_order` = ?";
158 
159  return $this->query($sql, [$cat_id, $title, $desc, $type, $uri, $order]);
160  }
161 
162  function getReportedPosts($u)
163  {
164  return $this->getXxxPosts($u, ' AND `reports` != 0');
165  }
166 
167  function getHiddenPosts($u)
168  {
169  return $this->getXxxPosts($u, ' AND `hidden` != 0');
170  }
171 
172  function getXxxPosts($u, $sWhere = '')
173  {
174  global $gConf;
175 
176  $sql_add1 = ", '-1' AS `voted`, 0 as `vote_user_point` ";
177  $sql_add2 = '';
178 
179  if ($u) {
180  $sql_add1 = ", (1 - ISNULL(t2.`post_id`)) AS `voted`, t2.`vote_point` as `vote_user_point` ";
181  $sql_add2 = " LEFT JOIN " . TF_FORUM_VOTE . " AS t2 ON ( t2.`user_name` = '$u' AND t1.`post_id` = t2.`post_id`) ";
182  }
183 
184  $sql = "SELECT `forum_id`, `topic_id`, t1.`post_id`, `user`, `post_text`, `votes`, `hidden`, t1.`when` $sql_add1 FROM " . TF_FORUM_POST . " AS t1 $sql_add2 WHERE 1 $sWhere ORDER BY t1.`when` DESC";
185 
186  return $this->getAll($sql);
187  }
188 
189  function clearReport($post_id)
190  {
191  return $this->query("UPDATE " . TF_FORUM_POST . " SET `reports` = 0 WHERE `post_id` = ? LIMIT 1", [$post_id]);
192  }
193 
194  // private functions
195 
196 }
DbAdmin\getCatByForumId
getCatByForumId($forum_id)
Definition: DbAdmin.php:52
ChDb\getAll
getAll($query, $bindings=[], $arr_type=PDO::FETCH_ASSOC)
Definition: ChDb.php:108
TF_FORUM_POST
const TF_FORUM_POST
Definition: DbForum.php:10
DbAdmin\deleteForumAll
deleteForumAll($forum_id)
Definition: DbAdmin.php:76
TF_FORUM_VOTE
const TF_FORUM_VOTE
Definition: DbForum.php:12
ChDb\getOne
getOne($query, $bindings=[])
Definition: ChDb.php:32
DbAdmin\insertForum
insertForum($cat_id, $title, $desc, $type, $uri, $order)
Definition: DbAdmin.php:155
DbAdmin\getCatOrder
getCatOrder($cat_id)
Definition: DbAdmin.php:59
DbAdmin\insertCategory
insertCategory($cat_name, $uri, $cat_order, $cat_expanded)
Definition: DbAdmin.php:134
php
TF_FORUM_USER_STAT
const TF_FORUM_USER_STAT
Definition: DbForum.php:15
DbAdmin
Definition: DbAdmin.php:38
DbAdmin\deleteCategoryAll
deleteCategoryAll($cat_id)
Definition: DbAdmin.php:39
DbAdmin\getReportedPosts
getReportedPosts($u)
Definition: DbAdmin.php:162
DbAdmin\getCatsInOrder
getCatsInOrder($cat_order, $dir, $num)
Definition: DbAdmin.php:69
DbAdmin\deleteForumTopics
deleteForumTopics($forum_id)
Definition: DbAdmin.php:103
DbAdmin\deleteForumPosts
deleteForumPosts($forum_id)
Definition: DbAdmin.php:91
DbAdmin\editCategory
editCategory($cat_id, $cat_name, $cat_order, $cat_expanded)
Definition: DbAdmin.php:127
DbAdmin\getHiddenPosts
getHiddenPosts($u)
Definition: DbAdmin.php:167
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
TF_FORUM_FLAG
const TF_FORUM_FLAG
Definition: DbForum.php:13
DbAdmin\getXxxPosts
getXxxPosts($u, $sWhere='')
Definition: DbAdmin.php:172
ChDb\getRow
getRow($query, $bindings=[], $arr_type=PDO::FETCH_ASSOC)
Definition: ChDb.php:20
ChDb\query
query($query, $bindings=[])
Definition: ChDb.php:95
ChDb
Definition: ChDb.php:11
TF_FORUM
const TF_FORUM
Definition: DbForum.php:8
TF_FORUM_CAT
const TF_FORUM_CAT
Definition: DbForum.php:9
DbAdmin\deleteForum
deleteForum($forum_id)
Definition: DbAdmin.php:113
DbAdmin\getCatName
getCatName($cat_id)
Definition: DbAdmin.php:120
DbAdmin\clearReport
clearReport($post_id)
Definition: DbAdmin.php:189
$gConf
global $gConf
Definition: header.inc.php:8
TF_FORUM_TOPIC
const TF_FORUM_TOPIC
Definition: DbForum.php:11
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
DbAdmin\editForum
editForum($forum_id, $title, $desc, $type, $order)
Definition: DbAdmin.php:148
DbAdmin\deleteCategory
deleteCategory($cat_id)
Definition: DbAdmin.php:84
DbAdmin\getForum
getForum($forum_id)
Definition: DbAdmin.php:141
$dir
$dir
Definition: config.php:10