-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathView.php
More file actions
148 lines (131 loc) · 4.94 KB
/
Copy pathView.php
File metadata and controls
148 lines (131 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Controllers\User;
use \BNETDocs\Libraries\Comment;
use \BNETDocs\Libraries\Community\Credits;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Document;
use \BNETDocs\Libraries\Packet\Packet;
use \BNETDocs\Libraries\Server\Server;
use \BNETDocs\Libraries\User\User;
class View extends \BNETDocs\Controllers\Base
{
/**
* Constructs a Controller, typically to initialize properties.
*/
public function __construct()
{
$this->model = new \BNETDocs\Models\User\View();
}
/**
* Invoked by the Router class to handle the request.
*
* @param array|null $args The optional route arguments and any captured URI arguments.
* @return boolean Whether the Router should invoke the configured View.
*/
public function invoke(?array $args) : bool
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
$this->model->user_id = array_shift($args);
// Try to get the user
try { $this->model->user = new User($this->model->user_id); }
catch (\Throwable) { $this->model->user = null; return true; }
$this->model->_responseCode = HttpCode::HTTP_OK;
$this->model->user_id = $this->model->user->getId();
$this->model->user_profile = ($this->model->user ? $this->model->user->getUserProfile() : null);
// Summary of contributions
$this->model->sum_comments = Credits::getTotalCommentsByUserId($this->model->user_id);
$this->model->sum_documents = Credits::getTotalDocumentsByUserId($this->model->user_id);
$this->model->sum_news_posts = Credits::getTotalNewsPostsByUserId($this->model->user_id);
$this->model->sum_packets = Credits::getTotalPacketsByUserId($this->model->user_id);
$this->model->sum_servers = Credits::getTotalServersByUserId($this->model->user_id);
// Total number of contributions
$this->model->contributions = 0;
$this->model->contributions += $this->model->sum_comments;
$this->model->contributions += $this->model->sum_documents;
$this->model->contributions += $this->model->sum_news_posts;
$this->model->contributions += $this->model->sum_packets;
$this->model->contributions += $this->model->sum_servers;
// References to the contributions
$this->model->comments = ($this->model->sum_comments ?
Comment::getCommentsByUserId($this->model->user_id, true) : null
);
$this->model->documents = ($this->model->sum_documents ?
Document::getDocumentsByUserId($this->model->user_id) : null
);
$this->model->news_posts = ($this->model->sum_news_posts ?
\BNETDocs\Libraries\News\Post::getNewsPostsByUserId($this->model->user_id): null
);
$this->model->packets = ($this->model->sum_packets ?
Packet::getPacketsByUserId($this->model->user_id) : null
);
$this->model->servers = ($this->model->sum_servers ?
Server::getServersByUserId($this->model->user_id) : null
);
// Process documents
if ($this->model->documents)
{
// Alphabetically sort the documents
usort($this->model->documents, function($a, $b){
$a1 = $a->getTitle();
$b1 = $b->getTitle();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});
// Remove documents that are not published
$i = count($this->model->documents) - 1;
while ($i >= 0)
{
if (!($this->model->documents[$i]->isPublished())) unset($this->model->documents[$i]);
--$i;
}
}
// Process news posts
if ($this->model->news_posts)
{
// Alphabetically sort the news posts
usort($this->model->news_posts, function($a, $b){
$a1 = $a->getTitle();
$b1 = $b->getTitle();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});
// Remove news posts that are not published
$i = count($this->model->news_posts) - 1;
while ($i >= 0)
{
if (!($this->model->news_posts[$i]->isPublished())) unset($this->model->news_posts[$i]);
--$i;
}
}
// Process packets
if ($this->model->packets)
{
// Alphabetically sort the packets
usort($this->model->packets, function($a, $b){
$a1 = $a->getName();
$b1 = $b->getName();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});
// Remove packets that are not published
$i = count($this->model->packets) - 1;
while ($i >= 0)
{
if (!($this->model->packets[$i]->isPublished())) unset($this->model->packets[$i]);
--$i;
}
}
// Process servers
if ($this->model->servers) {
// Alphabetically sort the servers
usort($this->model->servers, function($a, $b){
$a1 = $a->getName();
$b1 = $b->getName();
if ($a1 == $b1) return 0;
return ($a1 < $b1 ? -1 : 1);
});
}
$this->model->_responseCode = ($this->model->user ? HttpCode::HTTP_OK : HttpCode::HTTP_NOT_FOUND);
return true;
}
}