Completed
Branch master (078ac8)
by Pierre-Henry
35:09
created

modules/forum/controllers/ForumController.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author         Pierre-Henry Soria <[email protected]>
4
 * @copyright      (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
5
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
6
 * @package        PH7 / App / System / Module / Forum / Controller
7
 */
8
namespace PH7;
9
10
use
0 ignored issues
show
There must be a single space after the USE keyword
Loading history...
11
PH7\Framework\Navigation\Page,
12
PH7\Framework\Security\Ban\Ban,
13
PH7\Framework\Analytics\Statistic,
14
PH7\Framework\Mvc\Router\Uri,
15
PH7\Framework\Url\Header;
16
17
class ForumController extends Controller
18
{
19
    const TOPICS_PER_PAGE = 20;
20
    const FORUMS_PER_PAGE = 20;
21
    const POSTS_PER_PAGE = 10;
22
23
    private $oForumModel, $oPage, $sTitle, $sMsg, $iTotalTopics;
0 ignored issues
show
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
24
25
    public function __construct()
26
    {
27
        parent::__construct();
28
29
        $this->oForumModel = new ForumModel;
30
        $this->oPage = new Page;
31
        $this->view->avatarDesign = new AvatarDesignCore; // Avatar Design Class
32
        $this->view->member_id = $this->session->get('member_id');
33
34
        // Predefined meta_keywords tags
35
        $this->view->meta_keywords = t('forum,discussion,dating forum,social forum,people,meet people,forums,free dating forum,free forum,community forum,social forum');
36
37
        // Adding Css Style for the Layout Forum
38
        $this->design->addCss(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_CSS, 'common.css');
39
    }
40
41
    public function index()
42
    {
43
        $this->view->total_pages = $this->oPage->getTotalPages(
44
            $this->oForumModel->totalForums(), self::FORUMS_PER_PAGE
45
        );
46
        $this->view->current_page = $this->oPage->getCurrentPage();
47
48
        $oCategories = $this->oForumModel->getCategory();
49
        $oForums = $this->oForumModel->getForum(
50
            null,
51
            $this->oPage->getFirstItem(),
52
            $this->oPage->getNbItemsPerPage()
53
        );
54
55
        if (empty($oCategories) && empty($oForums))
56
        {
57
            $this->sTitle = t('No Forums found.');
58
            $this->_notFound();
59
        }
60
        else
61
        {
62
            $this->view->page_title = t('Discussion Forums - %site_name%');
63
            $this->view->meta_description = t('Discussion Forums, Social Network Site - %site_name%');
64
            $this->view->h1_title = t('Discussion Forums, Social Network Site');
65
66
            $this->view->categories = $oCategories;
67
            $this->view->forums = $oForums;
68
        }
69
70
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    public function topic()
74
    {
75
        $this->view->total_pages = $this->oPage->getTotalPages(
76
            $this->oForumModel->totalTopics(), self::TOPICS_PER_PAGE
77
        );
78
        $this->view->current_page = $this->oPage->getCurrentPage();
79
        $oTopics = $this->oForumModel->getTopic(
80
            strstr($this->httpRequest->get('forum_name'), '-', true),
81
            $this->httpRequest->get('forum_id', 'int'),
82
            null,
83
            null,
84
            null,
85
            1,
86
            $this->oPage->getFirstItem(),
87
            $this->oPage->getNbItemsPerPage()
88
        );
89
90
        $this->view->forum_name = $this->httpRequest->get('forum_name');
91
        $this->view->forum_id = $this->httpRequest->get('forum_id', 'int');
92
93
        if (empty($oTopics))
94
        {
95
            $this->sTitle = t('No Topics found.');
96
            $this->_notFound();
97
        }
98
        else
99
        {
100
            $this->view->page_title = t('%0% - Forums', $this->str->upperFirst($this->httpRequest->get('forum_name')));
101
            $this->view->meta_description = t('%0% - Topics - Discussion Forums', $this->httpRequest->get('forum_name'));
102
            $this->view->meta_keywords = t('%0%,forum,discussion,dating forum,social forum,people,meet people,forums,free dating forum', str_replace(' ', ',', $this->httpRequest->get('forum_name')));
103
            $this->view->h1_title = $this->str->upperFirst($this->httpRequest->get('forum_name'));
104
            $this->view->topics = $oTopics;
105
        }
106
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
    }
108
109
    public function post()
110
    {
111
        $oPost = $this->oForumModel->getTopic(
112
            strstr($this->httpRequest->get('forum_name'), '-', true),
113
            $this->httpRequest->get('forum_id', 'int'),
114
            strstr($this->httpRequest->get('topic_name'), '-', true),
115
            $this->httpRequest->get('topic_id', 'int'),
116
            null,
117
            1,
118
            0,
119
            1
120
        );
121
122
        $this->view->total_pages = $this->oPage->getTotalPages(
123
            $this->oForumModel->totalMessages(
124
                $this->httpRequest->get('topic_id', 'int')
125
            ),
126
            self::POSTS_PER_PAGE
127
        );
128
129
        $this->view->current_page = $this->oPage->getCurrentPage();
130
        $oMessages = $this->oForumModel->getMessage(
131
            $this->httpRequest->get('topic_id', 'int'),
132
            null,
133
            null,
134
            1,
135
            $this->oPage->getFirstItem(),
136
            $this->oPage->getNbItemsPerPage()
137
        );
138
139
        if (empty($oPost))
140
        {
141
            $this->sTitle = t('Topic Not Found!');
142
            $this->_notFound();
143
        }
144
        else
145
        {
146
            // Adding the RSS link
147
            $this->view->header = '<link rel="alternate" type="application/rss+xml" title="' . t('Latest Forum Posts') . '" href="' . Uri::get('xml', 'rss', 'xmlrouter', 'forum-post,' . $oPost->topicId) . '" />';
148
            $this->sTitle = t('%0% | %1% - Forum', $this->str->upperFirst($this->httpRequest->get('forum_name')), $this->str->escape(Ban::filterWord($oPost->title), true));
149
            $this->view->page_title = $this->sTitle;
150
            $this->view->meta_description = t('%0% Topics - Discussion Forums', substr($this->str->escape(Ban::filterWord($oPost->message), true), 0, 150));
151
152
            // Generates beautiful meta keywords for good SEO
153
            $this->view->meta_keywords = t('%0%,%1%,forum,discussion,dating forum,social forum', str_replace(' ', ',', $this->httpRequest->get('forum_name')), substr(str_replace(' ', ',', Ban::filterWord($oPost->title, false)), 0, 250));
154
            $this->view->h1_title = $this->sTitle;
155
156
            $this->view->dateTime = $this->dateTime;
157
            $this->view->post = $oPost;
158
            $this->view->messages = $oMessages;
159
160
            // Set Topics Views Statistics
161
            Statistic::setView($oPost->topicId, 'ForumsTopics');
162
        }
163
164
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
    }
166
167
    public function showPostByProfile()
168
    {
169
        $sUsername = $this->httpRequest->get('username');
170
        $this->view->username = $sUsername;
171
172
        $iId = (new UserCoreModel)->getId(null, $sUsername);
173
174
        $this->iTotalTopics = $this->oForumModel->totalTopics(null, $iId);
175
        $this->view->total_pages = $this->oPage->getTotalPages(
176
            $this->iTotalTopics, self::TOPICS_PER_PAGE
177
        );
178
        $this->view->current_page = $this->oPage->getCurrentPage();
179
180
        $this->view->topic_number = nt('%n% Topic:', '%n% Topics:', $this->iTotalTopics);
181
182
        $oTopics = $this->oForumModel->getPostByProfile(
183
            $iId,
184
            1,
185
            $this->oPage->getFirstItem(),
186
            $this->oPage->getNbItemsPerPage()
187
        );
188
        if (empty($oTopics))
189
        {
190
            $this->sTitle = t("%0% doesn't have any posts yet.", $sUsername);
191
            $this->_notFound(false); // Because the Ajax blocks profile, we can not put HTTP error code 404, so the attribute is "false"
192
        }
193
        else
194
        {
195
            $this->sTitle = t('%0%\'s Forum Posts', $sUsername);
196
            $this->view->page_title = $this->sTitle;
197
            $this->view->h2_title = $this->sTitle;
198
            $this->view->topics = $oTopics;
199
        }
200
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
201
    }
202
203
    public function search()
204
    {
205
        $this->sTitle = t('Forum Search - Looking for a Forum Post | %site_name%');
206
        $this->view->page_title = $this->sTitle;
207
        $this->view->meta_description = t('Topic Search - Discussion Forum - %site_name%');
208
        $this->view->h2_title = $this->sTitle;
209
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
210
    }
211
212
    public function result()
213
    {
214
        $this->iTotalTopics = $this->oForumModel->search(
215
            $this->httpRequest->get('looking'),
216
            true,
217
            $this->httpRequest->get('order'),
218
            $this->httpRequest->get('sort'),
219
            null,
220
            null
221
        );
222
        $this->view->total_pages = $this->oPage->getTotalPages(
223
            $this->iTotalTopics, self::POSTS_PER_PAGE
224
        );
225
226
        $this->view->current_page = $this->oPage->getCurrentPage();
227
228
        $oSearch = $this->oForumModel->search(
229
            $this->httpRequest->get('looking'),
230
            false,
231
            $this->httpRequest->get('order'),
232
            $this->httpRequest->get('sort'),
233
            $this->oPage->getFirstItem(),
234
            $this->oPage->getNbItemsPerPage()
235
        );
236
237
        if (empty($oSearch))
238
        {
239
            $this->sTitle = t('Sorry, Your search returned no results!');
240
            $this->_notFound();
241
        }
242
        else
243
        {
244
            $this->sTitle = t('Forums - Your search returned');
245
            $this->view->page_title = $this->sTitle;
246
            $this->view->h3_title = nt('%n% Result', '%n% Results', $this->iTotalTopics);
247
            $this->view->meta_description = t('Search - Discussion Forum');
248
            $this->view->meta_keywords = t('search,forum,forums,discussion forum');
249
            $this->view->h2_title = $this->sTitle;
250
            $this->view->topics = $oSearch;
251
        }
252
253
        $this->manualTplInclude('topic.tpl');
254
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
255
    }
256
257
    public function addTopic()
258
    {
259
        $this->sTitle = t('Add a new Topic');
260
        $this->view->page_title = $this->sTitle;
261
        $this->view->h2_title = $this->sTitle;
262
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
263
    }
264
265
    public function editTopic()
266
    {
267
        $this->sTitle = t('Edit Topic');
268
        $this->view->page_title = $this->sTitle;
269
        $this->view->h2_title = $this->sTitle;
270
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
271
    }
272
273
    public function editMessage()
274
    {
275
        $this->sTitle = t('Edit your Message');
276
        $this->view->page_title = $this->sTitle;
277
        $this->view->h2_title = $this->sTitle;
278
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
279
    }
280
281
    public function reply()
282
    {
283
        $this->sTitle = t('Reply Message');
284
        $this->view->page_title = $this->sTitle;
285
        $this->view->h2_title = $this->sTitle;
286
        $this->output();
0 ignored issues
show
The method output() does not seem to exist on object<PH7\ForumController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
287
    }
288
289
    public function deleteTopic()
290
    {
291
        $aData = explode('_', $this->httpRequest->post('id'));
292
        $iTopicId = (int) $aData[0];
293
        $iForumId = (int) $aData[1];
294
        $sForumName = (string) $aData[2];
295
296
        if ($this->oForumModel->deleteTopic($this->session->get('member_id'), $iTopicId))
297
            $this->sMsg = t('Your topic has been deleted!');
298
        else
299
            $this->sMsg = t('Oops! Your topic could not be deleted');
300
301
        Header::redirect(Uri::get('forum', 'forum', 'topic', $sForumName . ',' . $iForumId), $this->sMsg);
302
    }
303
304
    public function deleteMessage()
305
    {
306
        $aData = explode('_', $this->httpRequest->post('id'));
307
        $iMessageId = (int) $aData[0];
308
        $iTopicId = (int) $aData[1];
309
        $iForumId = (int) $aData[2];
310
        $sTopicTitle = (string) $aData[3];
311
        $sForumName = (string) $aData[4];
312
        unset($aData);
313
314
        if ($this->oForumModel->deleteMessage($this->session->get('member_id', 'int'), $iMessageId))
315
            $this->sMsg = t('Your message has been deleted!');
316
        else
317
            $this->sMsg = t('Oops! Your message could not be deleted');
318
319
        Header::redirect(Uri::get('forum', 'forum', 'post', $sForumName . ',' . $iForumId . ',' . $sTopicTitle . ',' . $iTopicId), $this->sMsg);
320
    }
321
322
    /**
323
     * Set a Not Found Error Message with HTTP 404 Code Status.
324
     *
325
     * @param boolean $b404Status For the Ajax blocks profile, we can not put HTTP error code 404, so the attribute must be set to "false". Default: TRUE
326
     * @return void
327
     */
328
    private function _notFound($b404Status = true)
329
    {
330
        if ($b404Status === true)
331
            Framework\Http\Http::setHeadersByCode(404);
332
333
        $sErrMsg = ($b404Status === true) ? '<br />' . t('Please return to the <a href="%0%">main forum page</a> or <a href="%1%">the previous page</a>.', Uri::get('forum', 'forum', 'index'), 'javascript:history.back();') : '';
334
335
        $this->view->page_title = $this->sTitle;
336
        $this->view->h2_title = $this->sTitle;
337
        $this->view->error = $this->sTitle . $sErrMsg;
338
    }
339
}
340