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