Skip to content

Commit 2899101

Browse files
author
Walther Lalk
committed
First commit for forum plug
0 parents  commit 2899101

28 files changed

+2425
-0
lines changed

controllers/forums_controller.php

+623
Large diffs are not rendered by default.

forums_app_controller.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
class ForumsAppController extends AppController {
3+
4+
}
5+
?>

forums_app_model.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
class ForumsAppModel extends AppModel {
3+
4+
}
5+
?>

models/forum_category.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
class ForumCategory extends ForumsAppModel
3+
{
4+
var $name = 'ForumCategory';
5+
var $hasMany = array('ForumForum' => array (
6+
'className' => 'Forums.ForumForum',
7+
'dependent' => true
8+
));
9+
var $actsAs = array('Sluggable', 'Orderable');
10+
var $order = "ForumCategory.order ASC";
11+
12+
function fetchCategories($slug, $userId)
13+
{
14+
$returnData = array();
15+
16+
$conditions = array();
17+
if ($slug != null)
18+
{
19+
$conditions['ForumCategory.slug'] = $slug;
20+
}
21+
22+
$allCategories = $this->ForumForum->find('list', array('contain' => false, 'conditions' => array('ForumForum.category' => 1)));
23+
$categories = $this->find('all', array('conditions' => $conditions, 'contain' => array('ForumForum' => array('order' => 'ForumForum.lft ASC', 'conditions'=>array('ForumForum.category' => 0, 'ForumForum.parent_id' => array_keys($allCategories))))));
24+
25+
foreach($categories as $category)
26+
{
27+
$returnCategory = array();
28+
$returnCategory['slug'] = $category['ForumCategory']['slug'];
29+
$returnCategory['title'] = $category['ForumCategory']['title'];
30+
$returnCategory['forums'] = array();
31+
foreach($category['ForumForum'] as $forum)
32+
{
33+
$returnForum = array();
34+
35+
$numberThreads = $this->ForumForum->ForumThread->find('count', array('contain' => false, 'conditions' => array('ForumThread.forum_forum_id' => $forum['id'])));
36+
$threadList = $this->ForumForum->ForumThread->find('list', array('contain' => false, 'conditions' => array('ForumThread.forum_forum_id' => $forum['id'])));
37+
$numberPosts = $this->ForumForum->ForumThread->ForumPost->find('count', array('contain' => false, 'conditions' => array('ForumPost.forum_thread_id' => array_keys($threadList))));
38+
$lastPost = $this->ForumForum->ForumThread->ForumPost->find('first', array('contain' => array('User', 'ForumThread'), 'conditions' => array('ForumPost.forum_thread_id' => array_keys($threadList)), 'order' => array('ForumPost.created DESC')));
39+
$hasUnread = $this->ForumForum->ForumThread->ForumUnreadPost->find('count', array('conditions' => array('ForumUnreadPost.user_id' => $userId, 'ForumUnreadPost.forum_thread_id' => array_keys($threadList))));
40+
41+
$returnForum['title'] = $forum['title'];
42+
$returnForum['slug'] = $forum['slug'];
43+
$returnForum['description'] = $forum['description'];
44+
$returnForum['number_threads'] = $numberThreads;
45+
$returnForum['number_posts'] = $numberPosts;
46+
$returnForum['lastPost'] = $lastPost;
47+
$returnForum['unreadPost'] = $hasUnread;
48+
$returnForum['ChildForum'] = $this->ForumForum->find('all', array('order' => 'ForumForum.lft ASC', 'contain' => false, 'conditions' => array('ForumForum.parent_id' => $forum['id'])));
49+
50+
$returnCategory['forums'][] = $returnForum;
51+
}
52+
53+
$returnData[] = $returnCategory;
54+
}
55+
56+
return $returnData;
57+
}
58+
}
59+
?>

models/forum_forum.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
class ForumForum extends ForumsAppModel
3+
{
4+
var $name = 'ForumForum';
5+
var $actsAs = array('Acl'=>'controlled', 'Sluggable', 'Tree' => array('scope' => 'ForumCategory'));
6+
var $hasMany = array('ForumThread' => array (
7+
'className' => 'Forums.ForumThread',
8+
'dependent' => true
9+
)/*,
10+
'ChildForum' => array('className' => 'Forums.ForumForum',
11+
'foreignKey' => 'parent_id',
12+
'dependent' => true,
13+
'conditions' => '',
14+
'fields' => '',
15+
'order' => 'ChildForum.`order` ASC',
16+
'limit' => '',
17+
'offset' => '',
18+
'exclusive' => '',
19+
'finderQuery' => '',
20+
'counterQuery' => ''
21+
)*/);
22+
23+
var $belongsTo = array('ForumCategory'/*,
24+
'ParentForum' => array('className' => 'Forums.ForumForum',
25+
'foreignKey' => 'parent_id',
26+
'conditions' => '',
27+
'fields' => '',
28+
'order' => 'ParentForum.`order` ASC'
29+
)*/);
30+
var $order = "ForumForum.lft ASC";
31+
var $recursive = 0;
32+
33+
function fetchSubForums($slug, $userId)
34+
{
35+
$returnData = array();
36+
37+
$this->recursive = -1;
38+
$forum = $this->findBySlug($slug);
39+
40+
$childForums = $this->find('all', array('conditions' => array('ForumForum.parent_id' => $forum['ForumForum']['id'])));
41+
42+
foreach($childForums as $childForum)
43+
{
44+
$returnForum = array();
45+
46+
$numberThreads = $this->ForumThread->find('count', array('contain' => false, 'conditions' => array('ForumThread.forum_forum_id' => $childForum['ForumForum']['id'])));
47+
$threadList = $this->ForumThread->find('list', array('contain' => false, 'conditions' => array('ForumThread.forum_forum_id' => $childForum['ForumForum']['id'])));
48+
$numberPosts = $this->ForumThread->ForumPost->find('count', array('contain' => false, 'conditions' => array('ForumPost.forum_thread_id' => array_keys($threadList))));
49+
$lastPost = $this->ForumThread->ForumPost->find('first', array('contain' => array('User', 'ForumThread'), 'conditions' => array('ForumPost.forum_thread_id' => array_keys($threadList)), 'order' => array('ForumPost.created DESC')));
50+
$hasUnread = $this->ForumThread->ForumUnreadPost->find('count', array('conditions' => array('ForumUnreadPost.user_id' => $userId, 'ForumUnreadPost.forum_thread_id' => array_keys($threadList))));
51+
52+
$returnForum['title'] = $childForum['ForumForum']['title'];
53+
$returnForum['slug'] = $childForum['ForumForum']['slug'];
54+
$returnForum['description'] = $childForum['ForumForum']['description'];
55+
$returnForum['number_threads'] = $numberThreads;
56+
$returnForum['number_posts'] = $numberPosts;
57+
$returnForum['unreadPost'] = $hasUnread;
58+
$returnForum['lastPost'] = $lastPost;
59+
$returnForum['ChildForum'] = $this->find('all', array('contain' => false, 'conditions' => array('ForumForum.parent_id' => $childForum['ForumForum']['id'])));
60+
61+
$returnData[] = $returnForum;
62+
}
63+
64+
return $returnData;
65+
}
66+
67+
function fetchBreadcrumbs($slug)
68+
{
69+
$returnData = array();
70+
71+
$this->recursive = 0;
72+
$forum = $this->findBySlug($slug);
73+
74+
$returnData[0]['title'] = $forum['ForumCategory']['title'];
75+
$returnData[0]['slug'] = $forum['ForumCategory']['slug'];
76+
77+
$parentForums = $this->getpath($forum['ForumForum']['id']);
78+
unset($parentForums[count($parentForums)-1]);
79+
$i = 1;
80+
foreach($parentForums as $parentForum)
81+
{
82+
if ($parentForum['ForumForum']['category'] != 1)
83+
{
84+
$returnData[$i]['title'] = $parentForum['ForumForum']['title'];
85+
$returnData[$i++]['slug'] = $parentForum['ForumForum']['slug'];
86+
}
87+
}
88+
89+
ksort($returnData);
90+
91+
return $returnData;
92+
}
93+
94+
function parentNode()
95+
{
96+
return "CMScout Forums";
97+
}
98+
}
99+
?>

models/forum_post.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
class ForumPost extends ForumsAppModel
3+
{
4+
var $name = 'ForumPost';
5+
var $belongsTo = array('ForumThread' => array('className' => 'Forums.ForumThread'),
6+
'User' => array('fields' => array("id", "username", "avatar", "signature")),
7+
'EditUser' => array('fields' => array("id", "username"),
8+
'className' => 'User',
9+
'foreignKey' => 'edit_user'));
10+
var $actsAs = array('Tag'=>array('table_label'=>'tags', 'tags_label'=>'tag', 'separator'=>','), 'Sluggable');
11+
12+
var $hasAndBelongsToMany = "Tag";
13+
14+
function getPageNumber($pageId, $perPage=25)
15+
{
16+
$viewPost = $this->find('first', array('conditions' => array('ForumPost.id' => $pageId), 'fields' => array('id', 'forum_thread_id','created'),
17+
'contain' => false));
18+
$numberOfPost = $this->find('count', array("conditions" => array ('ForumPost.forum_thread_id' => $viewPost['ForumPost']['forum_thread_id'], 'ForumPost.id <=' => $pageId)));
19+
return ceil($numberOfPost / $perPage);
20+
}
21+
}
22+
?>

models/forum_thread.php

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
function __compareCreated($a, $b)
3+
{
4+
$a = strtotime($a['lastPost']['created']);
5+
$b = strtotime($b['lastPost']['created']);
6+
7+
if ($a == $b)
8+
return 0;
9+
10+
return ($a > $b) ? 1 : -1 ;
11+
}
12+
13+
function __compareNumber($a, $b)
14+
{
15+
$a = $a['number_posts'];
16+
$b = $b['number_posts'];
17+
18+
if ($a == $b)
19+
return 0;
20+
21+
return ($a > $b) ? 1 : -1 ;
22+
}
23+
24+
class ForumThread extends ForumsAppModel
25+
{
26+
var $name = 'ForumThread';
27+
var $belongsTo = array('ForumForum' => array('className' => 'Forums.ForumForum'), 'User' => array('fields' => array("id", "username")));
28+
var $hasMany = array('ForumPost' => array (
29+
'className' => 'Forums.ForumPost',
30+
'dependent' => true
31+
),
32+
'ForumUnreadPost' => array (
33+
'className' => 'Forums.ForumUnreadPost',
34+
'dependent' => true
35+
),
36+
'ForumSubscriber' => array (
37+
'className' => 'Forums.ForumSubscriber',
38+
'dependent' => true
39+
));
40+
var $actsAs = array('Sluggable');
41+
42+
function __configureThreads($threads)
43+
{
44+
$returnData = array();
45+
foreach($threads as $thread)
46+
{
47+
$returnThread = array();
48+
49+
$returnThread['title'] = $thread['ForumThread']['title'];
50+
$returnThread['slug'] = $thread['ForumThread']['slug'];
51+
$returnThread['description'] = $thread['ForumThread']['description'];
52+
$returnThread['views'] = $thread['ForumThread']['views'];
53+
$returnThread['number_posts'] = $this->ForumPost->find('count', array('contain' => false, 'conditions' => array('ForumPost.forum_thread_id' => $thread['ForumThread']['id'])));
54+
$returnThread['unreadPost'] = isset($thread['ForumUnreadPost'][0]['forum_thread_id']) ? 1 : 0;
55+
$returnThread['type'] = $thread['ForumThread']['thread_type'];
56+
$returnThread['locked'] = $thread['ForumThread']['locked'];
57+
$returnThread['userPost'] = $thread['User'];
58+
$returnThread['lastPost'] = $thread['ForumPost'][0];
59+
60+
$returnData[] = $returnThread;
61+
}
62+
63+
return $returnData;
64+
}
65+
66+
function findThreads($forumId, $userId, $type = 'ANNOUNCEMENT')
67+
{
68+
$returnData = array();
69+
70+
$this->recursive = -1;
71+
72+
$threads = $this->find('all', array('conditions' => array('ForumThread.thread_type' => $type, 'ForumThread.forum_forum_id' => $forumId),
73+
'contain' => array('User',
74+
'ForumPost' => array('User', 'order' => 'ForumPost.created DESC', 'limit' => 1),
75+
'ForumUnreadPost' => array('conditions' => array('ForumUnreadPost.user_id' => $userId)))));
76+
77+
78+
$returnData = $this->__configureThreads($threads);
79+
usort($returnData, '__compareCreated');
80+
81+
return $returnData;
82+
}
83+
84+
function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array())
85+
{
86+
if (is_array($order) && in_array(key($order), array('number_posts', 'lastPost')))
87+
{
88+
$useOrder['field'] = key($order);
89+
$useOrder['direction'] = $order[$useOrder['field']];
90+
$order = array();
91+
}
92+
elseif (!is_array($order))
93+
{
94+
$useOrder['field'] = 'lastPost';
95+
$useOrder['direction'] = 'desc';
96+
$order = array();
97+
}
98+
else
99+
{
100+
$useOrder = '';
101+
}
102+
103+
$this->recursive = -1;
104+
105+
$threads = $this->find('all', compact('conditions', 'fields', 'limit', 'page', 'group', 'order') + $extra);
106+
107+
$returnData = $this->__configureThreads($threads);
108+
109+
if (isset($useOrder['field']) && $useOrder['field'] == 'lastPost')
110+
usort($returnData, '__compareCreated');
111+
elseif (isset($useOrder['field']) && $useOrder['field'] == 'number_posts')
112+
usort($returnData, '__compareNumber');
113+
114+
if (isset($useOrder['direction']) && $useOrder['direction'] == 'desc')
115+
$returnData = array_reverse($returnData);
116+
117+
return $returnData;
118+
}
119+
120+
function fetchBreadcrumbs($slug)
121+
{
122+
$returnData = array();
123+
124+
$this->recursive = -1;
125+
$thread = $this->findBySlug($slug);
126+
127+
128+
$returnData = array();
129+
130+
$this->recursive = 0;
131+
$forum = $this->ForumForum->findById($thread['ForumThread']['forum_forum_id']);
132+
133+
$returnData[0]['title'] = $forum['ForumCategory']['title'];
134+
$returnData[0]['slug'] = $forum['ForumCategory']['slug'];
135+
136+
$parentForums = $this->ForumForum->getpath($forum['ForumForum']['id']);
137+
$i = 1;
138+
foreach($parentForums as $parentForum)
139+
{
140+
if ($parentForum['ForumForum']['category'] != 1)
141+
{
142+
$returnData[$i]['title'] = $parentForum['ForumForum']['title'];
143+
$returnData[$i++]['slug'] = $parentForum['ForumForum']['slug'];
144+
}
145+
}
146+
147+
ksort($returnData);
148+
149+
return $returnData;
150+
}
151+
}
152+
?>

0 commit comments

Comments
 (0)