1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class Agora_Driver_Sql extends Agora_Driver {
17:
18: 19: 20: 21: 22: 23: 24:
25: public function getForumId($forum_name)
26: {
27: static $ids = array();
28:
29: if (!isset($ids[$forum_name])) {
30: $sql = 'SELECT forum_id FROM ' . $this->_forums_table . ' WHERE scope = ? AND forum_name = ? ';
31: $params = array($this->_scope, $forum_name);
32: $ids[$forum_name] = $this->_db->selectValue($sql, $params);
33: }
34:
35: return $ids[$forum_name];
36: }
37:
38: 39: 40: 41: 42:
43: public function getBareForums()
44: {
45: if ($this->_scope == 'agora') {
46: $sql = 'SELECT forum_id, forum_name FROM ' . $this->_forums_table . ' WHERE scope = ?';
47: } else {
48: $sql = 'SELECT forum_id, forum_description FROM ' . $this->_forums_table . ' WHERE scope = ?';
49: }
50:
51: return $this->_db->selectAssoc($sql, array($this->_scope));
52: }
53:
54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
71: protected function _getForums($root_forum = 0, $formatted = true,
72: $sort_by = 'forum_name', $sort_dir = 0,
73: $add_scope = false, $from = 0, $count = 0)
74: {
75: $key = $this->_scope . ':' . $root_forum . ':' . $formatted . ':'
76: . $sort_by . ':' . $sort_dir . ':' . $add_scope . ':' . $from
77: . ':' . $count;
78: $forums = $this->_getCache($key);
79: if ($forums) {
80: return unserialize($forums);
81: }
82:
83: $sql = 'SELECT forum_id, forum_name';
84:
85: if ($formatted) {
86: $sql .= ', scope, active, forum_description, forum_parent_id, '
87: . 'forum_moderated, forum_attachments, message_count, thread_count, '
88: . 'last_message_id, last_message_author, last_message_timestamp';
89: }
90:
91: $sql .= ' FROM ' . $this->_forums_table . ' WHERE active = ? ';
92: $params = array(1);
93:
94: if ($root_forum != 0) {
95: $sql .= ' AND forum_parent_id = ? ';
96: $params[] = $root_forum;
97: }
98:
99: if ($add_scope) {
100: $sql .= ' AND scope = ? ';
101: $params[] = $this->_scope;
102: }
103:
104:
105: $sql .= ' ORDER BY ';
106: if ($sort_by == 'forum_name' || $sort_by == 'message_count') {
107: $sql .= $sort_by;
108: } else {
109: $sql .= 'forum_id';
110: }
111: $sql .= ' ' . ($sort_dir ? 'DESC' : 'ASC');
112:
113:
114: if ($count) {
115: $sql = $this->_db->addLimitOffset($sql, array('limit' => $count, 'offset' => $from));
116: }
117:
118: try {
119: $forums = $this->_db->selectAll($sql, $params);
120: } catch (Horde_Db_Exception $e) {
121: throw new Agora_Exception($e->getMessage());
122: }
123: if (empty($forums)) {
124: throw new Horde_Exception_NotFound(_("There are no forums."));
125: }
126:
127: if ($formatted) {
128: $forums = $this->_formatForums($forums);
129: }
130:
131: $this->_setCache($key, serialize($forums));
132:
133: return $forums;
134: }
135:
136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151:
152: protected function _getThreads($thread_root = 0,
153: $all_levels = false,
154: $sort_by = 'message_modifystamp',
155: $sort_dir = 0,
156: $message_view = false,
157: $from = 0,
158: $count = 0)
159: {
160:
161: $key = $this->_scope . ':' . $this->_forum_id . ':' . $thread_root . ':' . intval($all_levels) . ':'
162: . $sort_by . ':' . $sort_dir . ':' . intval($message_view) . ':' . intval($from) . ':' . intval($count);
163: $messages = $this->_getCache($key, $thread_root);
164: if ($messages) {
165: return unserialize($messages);
166: }
167:
168: $bind = $this->_buildThreadsQuery(null, $thread_root, $all_levels, $sort_by,
169: $sort_dir, $message_view, $from, $count);
170:
171:
172: if ($sort_by != 'message_thread' && $count) {
173: $bind[0] = $this->_db->addLimitOffset($bind[0], array('limit' => $count, 'offset' => $from));
174: }
175:
176: try {
177: $messages = $this->_db->selectAll($bind[0], $bind[1]);
178: } catch (Horde_Db_Exception $e) {
179: throw new Agora_Exception($e->getMessage());
180: }
181:
182: $messages = $this->_formatThreads($messages, $sort_by, $message_view, $thread_root);
183:
184: $this->_setCache($key, serialize($messages), $thread_root);
185:
186: return $messages;
187: }
188:
189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205:
206: public function getThreadsByForumOwner($forum_owner,
207: $thread_root = 0,
208: $all_levels = false,
209: $sort_by = 'message_modifystamp',
210: $sort_dir = 0,
211: $message_view = false,
212: $from = 0,
213: $count = 0)
214: {
215: $bind = $this->_buildThreadsQuery($forum_owner, $thread_root, $all_levels,
216: $sort_by, $sort_dir, $message_view, $from, $count);
217:
218: if ($sort_by != 'message_thread' && $count) {
219: $bind[0] = $this->_db->addLimitOffset($bind[0], array('limit' => $count, 'offset' => $from));
220: }
221:
222: try {
223: $messages = $this->_db->selectAll($bind[0], $bind[1]);
224: } catch (Horde_Db_Exception $e) {
225: throw new Agora_Exception($e->getMessage());
226: }
227:
228: return $this->_formatThreads($messages, $sort_by, $message_view, $thread_root);
229: }
230:
231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245:
246: private function _buildThreadsQuery($forum_owner = null,
247: $thread_root = 0,
248: $all_levels = false,
249: $sort_by = 'message_modifystamp',
250: $sort_dir = 0,
251: $message_view = false,
252: $from = 0,
253: $count = 0)
254: {
255:
256: $sql = 'SELECT m.message_id AS message_id, m.forum_id AS forum_id, m.message_thread AS message_thread, m.parents AS parents, m.message_author AS message_author, '
257: . 'm.message_subject AS message_subject, m.message_timestamp AS message_timestamp, m.locked AS locked, m.view_count AS view_count, '
258: . 'm.message_seq AS message_seq , m.attachments AS attachments';
259:
260: if ($message_view) {
261: $sql .= ', m.body AS body';
262: }
263:
264: if ($thread_root == 0) {
265: $sql .= ', m.last_message_id AS last_message_id, m.last_message_author AS last_message_author'.
266: ', m.message_modifystamp AS last_message_timestamp';
267: }
268:
269: $sql .= ' FROM ' . $this->_threads_table . ' m, ' . $this->_forums_table . ' AS f ';
270:
271: $params = array();
272: $sql .= ' WHERE f.forum_id = m.forum_id ';
273:
274:
275: if ($forum_owner !== null) {
276: $sql .= ' AND f.author = ? AND f.scope = ?';
277: $params[] = $forum_owner;
278: $params[] = $this->_scope;
279: } elseif ($this->_forum_id) {
280: $sql .= ' AND m.forum_id = ?';
281: $params[] = $this->_forum_id;
282: } else {
283: $sql .= ' AND f.scope = ?';
284: $params[] = $this->_scope;
285: }
286:
287:
288: if (!$all_levels) {
289: $sql .= ' AND m.parents = ?';
290: $params[] = '';
291: }
292:
293:
294: if ($this->_forum['forum_moderated']) {
295: $sql .= ' AND m.approved = ?';
296: $params[] = 1;
297: }
298:
299: if ($thread_root) {
300: $sql .= ' AND (m.message_id = ? OR m.message_thread = ?)';
301: $params[] = $thread_root;
302: $params[] = $thread_root;
303: }
304:
305:
306: $sql .= ' ORDER BY m.' . $sort_by . ' ' . ($sort_dir ? 'DESC' : 'ASC');
307:
308: return array($sql, $params);
309: }
310: }
311: