Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
TagConnectionController
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
2 / 2
16
100.00% covered (success)
100.00%
1 / 1
 add
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
7
 remove
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3class TagConnectionController extends AppController
4{
5    public function add($tsumegoID, $tagName)
6    {
7        if (!Auth::isLoggedIn())
8        {
9            $this->response->statusCode(403);
10            $this->response->body('Not logged in.');
11            return $this->response;
12        }
13        $tag = ClassRegistry::init('Tag')->findByName($tagName);
14        if (!$tag)
15        {
16            $this->response->statusCode(403);
17            $this->response->body('Tag "' . $tagName . '" doesn\'t exist.');
18            return $this->response;
19        }
20
21        $tsumego = ClassRegistry::init('Tsumego')->findById($tsumegoID);
22        if (!$tsumego)
23        {
24            $this->response->statusCode(403);
25            $this->response->body('Tsumego with id="' . $tsumegoID . '" wasn\'t found.');
26            return $this->response;
27        }
28
29        $tagConnection = ClassRegistry::init('TagConnection')->find('first', [
30            'conditions' => [
31                'tsumego_id' => $tsumegoID,
32                'tag_id' => $tag['Tag']['id']]]);
33        if ($tagConnection)
34        {
35            $this->response->statusCode(403);
36            $this->response->body('The tsumego already has tag ' . $tag['Tag']['name'] . '.');
37            return $this->response;
38        }
39
40        $tagConnection = [];
41        $tagConnection['tag_id'] = $tag['Tag']['id'];
42        $tagConnection['tsumego_id'] = $tsumegoID;
43        $tagConnection['user_id'] = Auth::getUserID();
44        $tagConnection['approved'] = Auth::isAdmin() ? 1 : 0;
45
46        if($tagConnection['approved'] == 1)
47            AdminActivityLogger::log(AdminActivityType::ADD_TAG, $tagConnection['tsumego_id'], null, ' ', $tagName);
48
49        ClassRegistry::init('TagConnection')->create();
50        ClassRegistry::init('TagConnection')->save($tagConnection);
51        $this->response->statusCode(200);
52        return $this->response;
53    }
54
55    public function remove($tsumegoID, $tagName)
56    {
57        if (!Auth::isLoggedIn())
58        {
59            $this->response->statusCode(403);
60            $this->response->body('Not logged in.');
61            return $this->response;
62        }
63
64        $tag = ClassRegistry::init('Tag')->findByName($tagName);
65        if (!$tag)
66        {
67            $this->response->statusCode(403);
68            $this->response->body('Tag "' . $tagName . '" doesn\'t exist.');
69            return $this->response;
70        }
71
72        $tsumego = ClassRegistry::init('Tsumego')->findById($tsumegoID);
73        if (!$tsumego)
74        {
75            $this->response->statusCode(403);
76            $this->response->body('Tsumego with id="' . $tsumegoID . '" wasn\'t found.');
77            return $this->response;
78        }
79
80        $tagConnection = ClassRegistry::init('TagConnection')->find('first', ['conditions' => [
81            'tag_id' => $tag['Tag']['id'],
82            'tsumego_id' => $tsumegoID]]);
83        if (!$tagConnection)
84        {
85            $this->response->statusCode(403);
86            $this->response->body('Tag to remove isn\'t assigned to this tsumego.');
87            return $this->response;
88        }
89
90        if ($tagConnection['TagConnection']['approved'] == 1 && !Auth::isAdmin())
91        {
92            $this->response->statusCode(403);
93            $this->response->body('Only admins can remove approved tags.');
94            return $this->response;
95        }
96
97        if ($tagConnection['TagConnection']['user_id'] != Auth::getUserID() && !Auth::isAdmin())
98        {
99            $this->response->statusCode(403);
100            $this->response->body('You can\'t remove tag proposed by someone else.');
101            return $this->response;
102        }
103
104        ClassRegistry::init('TagConnection')->delete($tagConnection['TagConnection']['id']);
105        $this->response->statusCode(200);
106        return $this->response;
107    }
108}