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

modules/video/controllers/MainController.php (2 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 / Video / 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\Security\Ban\Ban,
12
PH7\Framework\Navigation\Page,
13
PH7\Framework\Cache\Cache,
14
PH7\Framework\Url\Header,
15
PH7\Framework\Mvc\Router\Uri;
16
17
class MainController extends Controller
18
{
19
    const ALBUMS_PER_PAGE = 14;
20
    const VIDEOS_PER_PAGE = 10;
21
22
    private $oVideoModel, $oPage, $sUsername, $sUsernameLink, $iProfileId, $sTitle, $iTotalVideos;
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...
23
24
    public function __construct()
25
    {
26
        parent::__construct();
27
        $this->oVideoModel = new VideoModel;
28
        $this->oPage = new Page;
29
30
        $this->sUsername = $this->httpRequest->get('username');
31
32
        $oUser = new UserCore;
33
        $this->sUsernameLink = $oUser->getProfileLink($this->sUsername);
34
        $this->view->oUser = $oUser;
35
        unset($oUser);
36
37
        $this->view->member_id = $this->session->get('member_id');
38
        $this->iProfileId = (new UserCoreModel)->getId(null, $this->sUsername);
39
40
        // Predefined meta_keywords tags
41
        $this->view->meta_keywords = t('video,videos,free,free videos,music,online,watch,dating,video dating,social,community,social network,people video,flirt');
42
    }
43
44
    public function index()
45
    {
46
        $this->albums();
47
    }
48
49
    public function addAlbum()
50
    {
51
        $this->view->page_title = $this->view->h2_title = t('Add a new Album');
52
        $this->output();
53
    }
54
55
    public function addVideo()
56
    {
57
        // Add JS file to choose the type of video (regular | embed)
58
        $this->design->addJs(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, 'common.js');
59
60
        $this->view->page_title = $this->view->h2_title = t('Add a new Video');
61
        $this->output();
62
    }
63
64
    public function editAlbum()
65
    {
66
        $this->view->page_title = $this->view->h2_title = t('Edit Album');
67
        $this->output();
68
    }
69
70
    public function editVideo()
71
    {
72
        $this->view->page_title = $this->view->h2_title = t('Edit Video');
73
        $this->output();
74
    }
75
76
    public function albums()
77
    {
78
        $iProfileId = ($this->httpRequest->getExists('username')) ? $this->iProfileId : null;
79
        $this->view->total_pages = $this->oPage->getTotalPages(
80
            $this->oVideoModel->totalAlbums($iProfileId), self::ALBUMS_PER_PAGE
81
        );
82
        $this->view->current_page = $this->oPage->getCurrentPage();
83
        $oAlbums = $this->oVideoModel->album($iProfileId, null, 1, $this->oPage->getFirstItem(), $this->oPage->getNbItemsPerPage());
84
85
        if (empty($oAlbums))
86
        {
87
            $this->sTitle = t('No video albums found.');
88
            $this->_notFound(false); // Because the Ajax blocks profile, we cannot put HTTP error code 404, so the attribute is FALSE
89
        }
90
        else
91
        {
92
            // We can include HTML tags in the title since the template will erase them before displaying
93
            $this->sTitle = (!empty($iProfileId)) ? t("The <a href='%0%'>%1%</a>'s albums", $this->sUsernameLink, $this->str->upperFirst($this->sUsername)) : t('Video Gallery Community');
94
            $this->view->page_title = $this->view->h2_title = $this->sTitle;
95
            $this->view->meta_description = t("%0%'s Albums | Video Albums of the Dating Social Community - %site_name%", $this->str->upperFirst($this->sUsername));
96
            $this->view->albums = $oAlbums;
97
        }
98
        if (empty($iProfileId))
99
            $this->manualTplInclude('index.tpl');
100
101
        $this->output();
102
    }
103
104
    public function album()
105
    {
106
        // Adding the JS Video Player and "video_duration" CSS class
107
        $this->design->addJs(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, 'Video.js');
108
        $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');
109
110
        $this->view->total_pages = $this->oPage->getTotalPages(
111
            $this->oVideoModel->totalVideos($this->iProfileId), self::ALBUMS_PER_PAGE
112
        );
113
        $this->view->current_page = $this->oPage->getCurrentPage();
114
        $oAlbum = $this->oVideoModel->video($this->iProfileId, $this->httpRequest->get('album_id', 'int'), null, 1, $this->oPage->getFirstItem(), $this->oPage->getNbItemsPerPage());
115
116
        if (empty($oAlbum))
117
        {
118
            $this->sTitle = t('Album not found or is still in pending approval.');
119
            $this->_notFound();
120
        }
121
        else
122
        {
123
            $this->sTitle = t("<a href='%0%'>%1%</a>'s video album", $this->sUsernameLink, $this->str->upperFirst($this->sUsername));
124
            $this->view->page_title = $this->sTitle; // We can include HTML tags in the title since the template will erase them before displaying
125
            $this->view->h2_title = $this->sTitle;
126
            $this->view->meta_description = t('Browse Videos From %0% | Video Album Social Community - %site_name%', $this->str->upperFirst($this->sUsername));
127
            $this->view->album = $oAlbum;
128
129
            // Set Video Album Statistics since it needs the foreach loop and it is unnecessary to do both, we have placed in the file album.tpl
130
        }
131
132
        $this->output();
133
    }
134
135
    public function video()
136
    {
137
        // Adding the JS Video Player file.
138
        $this->design->addJs(PH7_LAYOUT . PH7_SYS . PH7_MOD . $this->registry->module . PH7_SH . PH7_TPL . PH7_TPL_MOD_NAME . PH7_SH . PH7_JS, 'Video.js');
139
140
        $oVideo = $this->oVideoModel->video($this->iProfileId, $this->httpRequest->get('album_id', 'int'), $this->httpRequest->get('video_id', 'int'), 1, 0, 1);
141
142
        if (empty($oVideo))
143
        {
144
            $this->sTitle = t('Video not found or is still in pending approval.');
145
            $this->_notFound();
146
        }
147
        else
148
        {
149
            $this->sTitle = t("Watch <a href='%0%'>%1%</a>'s video", $this->sUsernameLink, $this->str->upperFirst($this->sUsername));
150
151
            $sTitle = Ban::filterWord($oVideo->title, false);
152
            $this->view->page_title = t("%0%'s video, %1%", $oVideo->firstName, $sTitle);
153
            $this->view->meta_description = t("%0%'s video, %1%, %2%", $oVideo->firstName, $sTitle, substr(Ban::filterWord($oVideo->description, false), 0, 100));
154
            $this->view->meta_keywords = t('video,movie,videos,video sharing,music,gallery,%0%,%1%,%2%', str_replace(' ', ',', $sTitle), $oVideo->firstName, $oVideo->username);
155
            $this->view->h1_title = $this->sTitle;
156
            $this->view->video = $oVideo;
157
158
            //Set Video Statistics
159
            Framework\Analytics\Statistic::setView($oVideo->videoId, 'Videos');
160
        }
161
162
        $this->output();
163
    }
164
165
    public function deleteVideo()
166
    {
167
        $iVideoId = $this->httpRequest->post('video_id', 'int');
168
        CommentCoreModel::deleteRecipient($iVideoId, 'Video');
169
        $bVideo = $this->oVideoModel->deleteVideo($this->session->get('member_id'), $this->httpRequest->post('album_id', 'int'), $iVideoId);
170
        (new Video)->deleteVideo($this->httpRequest->post('album_id'), $this->session->get('member_username'), $this->httpRequest->post('video_link'));
171
172
        /* Clean VideoModel Cache */
173
        (new Cache)->start(VideoModel::CACHE_GROUP, null, null)->clear();
174
175
        Header::redirect(Uri::get('video', 'main', 'album', $this->session->get('member_username') . ',' . $this->httpRequest->post('album_title') . ',' . $this->httpRequest->post('album_id')), t('Your video has been deleted!'));
176
    }
177
178
    public function deleteAlbum()
179
    {
180
        $this->oVideoModel->deleteVideo($this->session->get('member_id'), $this->httpRequest->post('album_id', 'int'));
181
        $this->oVideoModel->deleteAlbum($this->session->get('member_id'), $this->httpRequest->post('album_id', 'int'));
182
        $sDir = PH7_PATH_PUBLIC_DATA_SYS_MOD . 'video/file/' . $this->session->get('member_username') . PH7_DS . $this->httpRequest->post('album_id') . PH7_DS;
183
        $this->file->deleteDir($sDir);
184
185
        /* Clean VideoModel Cache */
186
        (new Cache)->start(VideoModel::CACHE_GROUP, null, null)->clear();
187
        Header::redirect(Uri::get('video', 'main', 'albums'), t('Your album has been deleted!'));
188
    }
189
190
    public function search()
191
    {
192
        $this->view->page_title = $this->view->h2_title = t('Video Search - Looking for a video');
193
        $this->output();
194
    }
195
196
    public function result()
197
    {
198
        $this->iTotalVideos = $this->oVideoModel->search(
199
            $this->httpRequest->get('looking'),
200
            true,
201
            $this->httpRequest->get('order'),
202
            $this->httpRequest->get('sort'),
203
            null,
204
            null
205
        );
206
        $this->view->total_pages = $this->oPage->getTotalPages(
207
            $this->iTotalVideos, self::VIDEOS_PER_PAGE
208
        );
209
        $this->view->current_page = $this->oPage->getCurrentPage();
210
211
        $oSearch = $this->oVideoModel->search(
212
            $this->httpRequest->get('looking'),
213
            false,
214
            $this->httpRequest->get('order'),
215
            $this->httpRequest->get('sort'),
216
            $this->oPage->getFirstItem(),
217
            $this->oPage->getNbItemsPerPage()
218
        );
219
220
        if (empty($oSearch))
221
        {
222
            $this->sTitle = t('Sorry, Your search returned no results!');
223
            $this->_notFound();
224
        }
225
        else
226
        {
227
            $this->sTitle = t('Dating Social Video - Your search returned');
228
            $this->view->page_title = $this->view->h2_title = $this->sTitle;
229
            $this->view->h3_title = nt('%n% video found!', '%n% videos found!', $this->iTotalVideos);
230
            $this->view->meta_description = t('Search - %site_name% is a Dating Social Video Community!');
231
            $this->view->meta_keywords = t('search,video,dating,social network,community,music,movie,news,video sharing');
232
            $this->view->album = $oSearch;
233
        }
234
235
        $this->manualTplInclude('album.tpl');
236
        $this->output();
237
    }
238
239
    /**
240
     * Set a Not Found Error Message with HTTP 404 Code Status.
241
     *
242
     * @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
243
     * @return void
244
     */
245
    private function _notFound($b404Status = true)
246
    {
247
        if ($b404Status === true) {
248
            Framework\Http\Http::setHeadersByCode(404);
249
        }
250
251
        $sErrMsg = ($b404Status === true) ? '<br />' . t('Please return to <a href="%1%">the previous page</a> or <a href="%1%">add a new video</a> in this album.', 'javascript:history.back();', Uri::get('video', 'main', 'addvideo', $this->httpRequest->get('album_id'))) : '';
252
253
        $this->view->page_title = $this->view->h2_title = $this->sTitle;
254
        $this->view->error = $this->sTitle . $sErrMsg;
255
    }
256
}
257