Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
55.19% covered (warning)
55.19%
133 / 241
22.22% covered (danger)
22.22%
2 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
TagsController
55.19% covered (warning)
55.19%
133 / 241
22.22% covered (danger)
22.22%
2 / 9
209.40
0.00% covered (danger)
0.00%
0 / 1
 add
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addAction
78.95% covered (warning)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 1
4.15
 view
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 user
49.68% covered (danger)
49.68%
77 / 155
0.00% covered (danger)
0.00%
0 / 1
65.00
 edit
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
2.09
 editAction
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
2.03
 delete
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 index
n/a
0 / 0
n/a
0 / 0
1
 acceptTagProposal
68.75% covered (warning)
68.75%
11 / 16
0.00% covered (danger)
0.00%
0 / 1
4.49
 rejectTagProposal
64.29% covered (warning)
64.29%
9 / 14
0.00% covered (danger)
0.00%
0 / 1
4.73
1<?php
2
3class TagsController extends AppController
4{
5    public function add()
6    {
7        $allTags = $this->getAllTags();
8        $this->set('allTags', $allTags);
9    }
10
11    public function addAction(): CakeResponse
12    {
13        $tagName = $this->data['tag_name'];
14        if (empty($tagName))
15        {
16            CookieFlash::set('Tag name not provided', 'error');
17            return $this->redirect('/tags/add');
18        }
19
20        $existingTag = ClassRegistry::init('Tag')->find('first', ['conditions' => ['name' => $tagName]]);
21        if ($existingTag)
22        {
23            CookieFlash::set('Tag "' . $tagName . '" already exists.', 'error');
24            return $this->redirect('/tags/add');
25        }
26
27        $tag = [];
28        $tag['name'] = $tagName;
29        $tag['description'] = $this->data['tag_description'];
30        $tag['hint'] = $this->data['tag_hint'];
31        $tag['link'] = $this->data['tag_reference'];
32        $tag['user_id'] = Auth::getUserID();
33        $tag['approved'] = Auth::isAdmin() ? 1 : 0;
34        ClassRegistry::init('Tag')->save($tag);
35        $saved = ClassRegistry::init('Tag')->find('first', ['conditions' => ['name' => $tagName]])['Tag'];
36
37        CookieFlash::set('Tag "' . $tagName . '" has been added.', 'success');
38        return $this->redirect('/tags/view/' . $saved['id']);
39    }
40
41    /**
42     * @param string|int|null $id
43     * @return void
44     */
45    public function view($id = null)
46    {
47        $tn = $this->Tag->findById($id);
48        $allTags = $this->getAllTags();
49        $user = $this->User->findById($tn['Tag']['user_id']);
50        $tn['Tag']['user'] = $user['User']['name'];
51        $this->set('allTags', $allTags);
52        $this->set('tn', $tn);
53    }
54
55    /**
56     * @param string|int|null $id User ID
57     * @return void
58     */
59    public function user($id)
60    {
61        $this->loadModel('Sgf');
62        $this->loadModel('Reject');
63
64        $list = [];
65        $listCreated = [];
66        $listType = [];
67        $listTid = [];
68        $listTsumego = [];
69        $listUser = [];
70        $listStatus = [];
71        $listTag = [];
72
73        $u = $this->User->findById($id);
74        $tagNames = $this->Tag->find('all', ['limit' => 50, 'order' => 'created DESC', 'conditions' => ['user_id' => $id, 'approved' => 1]]);
75        if (!$tagNames)
76            $tagNames = [];
77        $tags = ClassRegistry::init('TagConnection')->find('all', ['limit' => 50, 'order' => 'created DESC', 'conditions' => ['user_id' => $id, 'approved' => 1]]) ?: [];
78        $proposals = $this->Sgf->find('all', [
79            'limit' => 50,
80            'order' => 'created DESC',
81            'conditions' => ['user_id' => $id],
82        ]) ?: [];
83        $rejectedProposals = $this->Reject->find('all', ['limit' => 50, 'order' => 'created DESC', 'conditions' => ['user_id' => $id, 'type' => 'proposal']]) ?: [];
84        $rejectedTags = $this->Reject->find('all', ['limit' => 50, 'order' => 'created DESC', 'conditions' => ['user_id' => $id, 'type' => 'tag']]) ?: [];
85        $rejectedTagNames = $this->Reject->find('all', ['limit' => 50, 'order' => 'created DESC', 'conditions' => ['user_id' => $id, 'type' => 'tag name']]) ?: [];
86        $tagNamesCount = count($tagNames);
87        for ($i = 0; $i < $tagNamesCount; $i++)
88        {
89            $ux = $this->User->findById($tagNames[$i]['Tag']['user_id']);
90            $tagNames[$i]['Tag']['status'] = '<b style="color:#047804">accepted</b>';
91            $tagNames[$i]['Tag']['type'] = 'tag name';
92            $tagNames[$i]['Tag']['user'] = $ux['User']['name'];
93
94            $listCreated[] = $tagNames[$i]['Tag']['created'];
95            $listType[] = 'tag name';
96            $listTid[] = '';
97            $listTsumego[] = '';
98            $listUser[] = $tagNames[$i]['Tag']['user'];
99            $listStatus[] = $tagNames[$i]['Tag']['status'];
100            $listTag[] = $tagNames[$i]['Tag']['name'];
101        }
102        $rejectedTagNamesCount = count($rejectedTagNames);
103        for ($i = 0; $i < $rejectedTagNamesCount; $i++)
104        {
105            $ux = $this->User->findById($rejectedTagNames[$i]['Reject']['user_id']);
106            $r = [];
107            $r['Tag']['name'] = $rejectedTagNames[$i]['Reject']['text'];
108            $r['Tag']['type'] = $rejectedTagNames[$i]['Reject']['type'];
109            $r['Tag']['status'] = '<b style="color:#ce3a47">rejected</b>';
110            $r['Tag']['created'] = $rejectedTagNames[$i]['Reject']['created'];
111            $r['Tag']['user'] = $ux['User']['name'];
112            $tagNames[] = $r;
113
114            $listCreated[] = $r['Tag']['created'];
115            $listType[] = 'tag name';
116            $listTid[] = '';
117            $listTsumego[] = '';
118            $listUser[] = $r['Tag']['user'];
119            $listStatus[] = $r['Tag']['status'];
120            $listTag[] = $r['Tag']['name'];
121        }
122
123        $tagsCount = count($tags);
124        for ($i = 0; $i < $tagsCount; $i++)
125        {
126            $tnx = $this->Tag->findById($tags[$i]['TagConnection']['tag_id']);
127            $tx = $this->Tsumego->findById($tags[$i]['TagConnection']['tsumego_id']);
128            $scx = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $tx['Tsumego']['id']]]);
129            if (!$scx)
130                continue;
131            $sx = $this->Set->findById($scx['SetConnection']['set_id']);
132            $ux = $this->User->findById($tags[$i]['TagConnection']['user_id']);
133            if ($tnx['Tag']['name'] == '')
134                $tags[$i]['TagConnection']['tag_name'] = '<i>[not found]</i>';
135            else
136                $tags[$i]['TagConnection']['tag_name'] = $tnx['Tag']['name'];
137            $tags[$i]['TagConnection']['tsumego'] = $sx['Set']['title'] . ' - ' . $scx['SetConnection']['num'];
138            $tags[$i]['TagConnection']['user'] = $ux['User']['name'];
139            $tags[$i]['TagConnection']['type'] = 'tag';
140            $tags[$i]['TagConnection']['status'] = '<b style="color:#047804">accepted</b>';
141
142            $listCreated[] = $tags[$i]['TagConnection']['created'];
143            $listType[] = 'tag';
144            $listTid[] = $tags[$i]['TagConnection']['tsumego_id'];
145            $listTsumego[] = $tags[$i]['TagConnection']['tsumego'];
146            $listUser[] = $tags[$i]['TagConnection']['user'];
147            $listStatus[] = $tags[$i]['TagConnection']['status'];
148            $listTag[] = $tags[$i]['TagConnection']['tag_name'];
149        }
150        $rejectedTagsCount = count($rejectedTags);
151        for ($i = 0; $i < $rejectedTagsCount; $i++)
152        {
153            $tx = $this->Tsumego->findById($rejectedTags[$i]['Reject']['tsumego_id']);
154            $scx = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $tx['Tsumego']['id']]]);
155            if (!$scx)
156                continue;
157            $sx = $this->Set->findById($scx['SetConnection']['set_id']);
158            $ux = $this->User->findById($rejectedTags[$i]['Reject']['user_id']);
159            $r = [];
160            $r['TagConnection']['tsumego_id'] = $rejectedTags[$i]['Reject']['tsumego_id'];
161            $r['TagConnection']['tag_name'] = $rejectedTags[$i]['Reject']['text'];
162            $r['TagConnection']['tsumego'] = $sx['Set']['title'] . ' - ' . $scx['SetConnection']['num'];
163            $r['TagConnection']['user'] = $ux['User']['name'];
164            $r['TagConnection']['type'] = $rejectedTags[$i]['Reject']['type'];
165            $r['TagConnection']['status'] = '<b style="color:#ce3a47">rejected</b>';
166            $r['TagConnection']['created'] = $rejectedTags[$i]['Reject']['created'];
167            $tags[] = $r;
168
169            $listCreated[] = $r['TagConnection']['created'];
170            $listType[] = 'tag';
171            $listTid[] = $r['TagConnection']['tsumego_id'];
172            $listTsumego[] = $r['TagConnection']['tsumego'];
173            $listUser[] = $r['TagConnection']['user'];
174            $listStatus[] = $r['TagConnection']['status'];
175            $listTag[] = $r['TagConnection']['tag_name'];
176        }
177
178        $proposalsCount = count($proposals);
179        for ($i = 0; $i < $proposalsCount; $i++)
180        {
181            $tx = $this->Tsumego->findById($proposals[$i]['Sgf']['tsumego_id']);
182            $scx = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $tx['Tsumego']['id']]]);
183            if (!$scx)
184                continue;
185            $sx = $this->Set->findById($scx['SetConnection']['set_id']);
186            $ux = $this->User->findById($proposals[$i]['Sgf']['user_id']);
187            $proposals[$i]['Sgf']['tsumego'] = $sx['Set']['title'] . ' - ' . $scx['SetConnection']['num'];
188            $proposals[$i]['Sgf']['status'] = '<b style="color:#047804">accepted</b>';
189            $proposals[$i]['Sgf']['user'] = $ux['User']['name'];
190            $proposals[$i]['Sgf']['type'] = 'proposal';
191
192            $listCreated[] = $proposals[$i]['Sgf']['created'];
193            $listType[] = 'proposal';
194            $listTid[] = $proposals[$i]['Sgf']['tsumego_id'];
195            $listTsumego[] = $proposals[$i]['Sgf']['tsumego'];
196            $listUser[] = $proposals[$i]['Sgf']['user'];
197            $listStatus[] = $proposals[$i]['Sgf']['status'];
198            $listTag[] = '';
199        }
200        $rejectedProposalsCount = count($rejectedProposals);
201        for ($i = 0; $i < $rejectedProposalsCount; $i++)
202        {
203            $tx = $this->Tsumego->findById($rejectedProposals[$i]['Reject']['tsumego_id']);
204            $scx = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $tx['Tsumego']['id']]]);
205            if (!$scx)
206                continue;
207            $sx = $this->Set->findById($scx['SetConnection']['set_id']);
208            $ux = $this->User->findById($rejectedProposals[$i]['Reject']['user_id']);
209            $r = [];
210            $r['Sgf']['tsumego_id'] = $rejectedProposals[$i]['Reject']['tsumego_id'];
211            $r['Sgf']['tsumego'] = $sx['Set']['title'] . ' - ' . $scx['SetConnection']['num'];
212            $r['Sgf']['status'] = '<b style="color:#ce3a47">rejected</b>';
213            $r['Sgf']['type'] = $rejectedProposals[$i]['Reject']['type'];
214            $r['Sgf']['user'] = $ux['User']['name'];
215            $r['Sgf']['created'] = $rejectedProposals[$i]['Reject']['created'];
216            $proposals[] = $r;
217
218            $listCreated[] = $r['Sgf']['created'];
219            $listType[] = 'proposal';
220            $listTid[] = $r['Sgf']['tsumego_id'];
221            $listTsumego[] = $r['Sgf']['tsumego'];
222            $listUser[] = $r['Sgf']['user'];
223            $listStatus[] = $r['Sgf']['status'];
224            $listTag[] = '';
225        }
226
227        array_multisort($listCreated, $listType, $listTid, $listTsumego, $listUser, $listStatus, $listTag);
228        $index = 0;
229        $listCreatedCount = count($listCreated);
230        for ($i = $listCreatedCount - 1; $i >= 0; $i--)
231        {
232            $list[$index]['created'] = $listCreated[$i];
233            $list[$index]['type'] = $listType[$i];
234            $list[$index]['tsumego_id'] = $listTid[$i];
235            $list[$index]['tsumego'] = $listTsumego[$i];
236            $list[$index]['user'] = $listUser[$i];
237            $list[$index]['status'] = $listStatus[$i];
238            $list[$index]['tag'] = $listTag[$i];
239            $index++;
240        }
241
242        $this->set('list', $list);
243    }
244
245    public function edit($tagID): ?CakeResponse
246    {
247        $tag = ClassRegistry::init('Tag')->findById($tagID);
248        if (!$tag)
249        {
250            CookieFlash::set('Tag to edit not found.', 'error');
251            return $this->redirect('/users/adminstats');
252        }
253
254        $this->set('allTags', $this->getAllTags());
255        $this->set('tag', $tag['Tag']);
256        return null;
257    }
258
259    public function editAction($tagID)
260    {
261        $tag = ClassRegistry::init('Tag')->findById($tagID);
262        if (!$tag)
263        {
264            CookieFlash::set('Tag to edit not found.');
265            $this->redirect('/users/adminstats');
266        }
267
268        $tag = $tag['Tag'];
269
270        $tag['description'] = $this->data['tag_description'];
271        $tag['hint'] = $this->data['tag_hint'];
272        $tag['link'] = $this->data['tag_link'];
273        ClassRegistry::init('Tag')->save($tag);
274        return $this->redirect('/tags/view/' . $tagID);
275    }
276
277    /**
278     * @param string|int $id Tag name ID
279     * @return void
280     */
281    public function delete($id)
282    {
283        $this->loadModel('Tag');
284        $tn = $this->Tag->findById($id);
285
286        if (isset($this->data['Tag']))
287            if ($this->data['Tag']['delete'] == $id)
288            {
289                $tags = $this->TagConnection->find('all', ['conditions' => ['tag_id' => $id]]);
290                if (!$tags)
291                    $tags = [];
292                foreach ($tags as $tag)
293                    $this->TagConnection->delete($tag['TagConnection']['id']);
294                $this->Tag->delete($id);
295                $this->set('del', 'del');
296            }
297
298        $this->set('tn', $tn);
299    }
300
301    public function index() {}
302
303    public function acceptTagProposal($tagID): CakeResponse
304    {
305        if (!Auth::isAdmin())
306            return $this->redirect('/');
307
308        $tagToApprove = ClassRegistry::init('Tag')->findById($tagID);
309        if (!$tagToApprove)
310        {
311            CookieFlash::set('Tag to approve not found', 'error');
312            return $this->redirect('/users/adminstats');
313        }
314
315        $tagToApprove = $tagToApprove['Tag'];
316
317        if ($tagToApprove['Tag']['approved'] == 1)
318        {
319            CookieFlash::set('Tag to approve was already approved', 'error');
320            return $this->redirect('/users/adminstats');
321        }
322
323        AppController::handleContribution(Auth::getUserID(), 'reviewed');
324        $tagToApprove['approved'] = '1';
325        ClassRegistry::init('Tag')->save($tagToApprove);
326        AppController::handleContribution($tagToApprove['user_id'], 'created_tag');
327        CookieFlash::set('Tag ' . $tagToApprove['name'] . ' was approved', 'success');
328        return $this->redirect('/users/adminstats');
329    }
330
331    public function rejectTagProposal($tagID): CakeResponse
332    {
333        if (!Auth::isAdmin())
334            return $this->redirect('/');
335
336        $tagToReject = ClassRegistry::init('Tag')->findById($tagID);
337        if (!$tagToReject)
338        {
339            CookieFlash::set('Tag to approve not found', 'error');
340            return $this->redirect('/users/adminstats');
341        }
342
343        $tagToReject = $tagToReject['Tag'];
344
345        if ($tagToReject['Tag']['approved'] == 1)
346        {
347            CookieFlash::set('Tag to reject was already approved', 'error');
348            return $this->redirect('/users/adminstats');
349        }
350
351        AppController::handleContribution(Auth::getUserID(), 'reviewed');
352
353        ClassRegistry::init('Tag')->delete($tagToReject);
354
355        CookieFlash::set('Tag ' . $tagToReject['name'] . ' was rejected', 'success');
356        return $this->redirect('/users/adminstats');
357    }
358}