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