Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.16% covered (warning)
58.16%
57 / 98
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SgfsController
57.73% covered (warning)
57.73%
56 / 97
0.00% covered (danger)
0.00%
0 / 2
67.50
0.00% covered (danger)
0.00%
0 / 1
 index
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 view
61.54% covered (warning)
61.54%
56 / 91
0.00% covered (danger)
0.00%
0 / 1
49.54
1<?php
2
3App::uses('NotFoundException', 'Routing/Error');
4
5class SgfsController extends AppController
6{
7    /**
8     * @return void
9     */
10    public function index()
11    {
12        $this->set('_title', 'Tsumego Hero');
13        $this->set('_page', 'play');
14        $sgfs = $this->Sgf->find('all');
15        if (!$sgfs)
16            $sgfs = [];
17
18        $this->set('sgfs', $sgfs);
19    }
20
21    /**
22     * @param string|int|null $id
23     * @return void
24     */
25    public function view($id = null)
26    {
27        $this->set('_page', 'play');
28        $this->loadModel('Tsumego');
29        $this->loadModel('Set');
30        $this->loadModel('User');
31        $this->loadModel('SetConnection');
32        $ux = '';
33        $type = 'tsumego';
34        $dId = [];
35        $dTitle = [];
36
37        if (isset($this->params['url']['delete']))
38        {
39            $sDel = $this->Sgf->findById($this->params['url']['delete']);
40            if ($sDel && Auth::getUserID() == $sDel['Sgf']['user_id'])
41                $this->Sgf->delete($sDel['Sgf']['id']);
42        }
43
44        if (isset($this->params['url']['duplicates']))
45        {
46            $newDuplicates = explode('-', $this->params['url']['duplicates']);
47            foreach ($newDuplicates as $duplicateId)
48            {
49                $dupl = $this->Tsumego->findById($duplicateId);
50                if (!$dupl)
51                    continue;
52                $scT = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $dupl['Tsumego']['id']]]);
53                if (!$scT)
54                    continue;
55                $dupl['Tsumego']['set_id'] = $scT['SetConnection']['set_id'];
56                $dSet = $this->Set->findById($dupl['Tsumego']['set_id']);
57                if (!$dSet)
58                    continue;
59                $dId[] = $dupl['Tsumego']['id'];
60                $dTitle[] = $dSet['Set']['title'] . ' - ' . $dupl['Tsumego']['num'];
61            }
62        }
63
64        $t = $this->Tsumego->findById($id);
65        if (!$t)
66            throw new NotFoundException('Tsumego not found');
67        $scT = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $t['Tsumego']['id']]]);
68        if (!$scT)
69            throw new NotFoundException('Tsumego not found in any set');
70        $t['Tsumego']['set_id'] = $scT['SetConnection']['set_id'];
71        $set = $this->Set->findById($t['Tsumego']['set_id']);
72        if (!$set)
73            throw new NotFoundException('Set not found');
74        $name = $set['Set']['title'] . ' ' . $set['Set']['title2'] . ' ' . $scT['SetConnection']['num'];
75        $this->set('_title', 'Upload History of ' . $name);
76
77        if (isset($this->params['url']['user']))
78        {
79            $s = $this->Sgf->find('all', [
80                'order' => 'id DESC',
81                'limit' => 100,
82                'conditions' => ['user_id' => $this->params['url']['user']],
83            ]);
84            if (!$s)
85                $s = [];
86            $type = 'user';
87        }
88        else
89        {
90            $s = $this->Sgf->find('all', [
91                'order' => 'id DESC',
92                'limit' => 100,
93                'conditions' => ['tsumego_id' => $id]]);
94            if (!$s)
95                $s = [];
96        }
97
98        //to display the correct version
99        $versionCounter = [];
100
101        $sCount = count($s);
102        for ($i = 0; $i < $sCount; $i++)
103        {
104
105
106            $u = $this->User->findById($s[$i]['Sgf']['user_id']);
107            $s[$i]['Sgf']['user'] = $u['User']['name'];
108            $ux = $u['User']['name'];
109            $t = $this->Tsumego->findById($s[$i]['Sgf']['tsumego_id']);
110            $scT = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $t['Tsumego']['id']]]);
111            $t['Tsumego']['set_id'] = $scT['SetConnection']['set_id'];
112            $set = $this->Set->findById($t['Tsumego']['set_id']);
113            $s[$i]['Sgf']['title'] = $set['Set']['title'] . ' ' . $set['Set']['title2'] . ' #' . $scT['SetConnection']['num'];
114            $s[$i]['Sgf']['num'] = $scT['SetConnection']['num'];
115
116            if(isset($versionCounter[$scT['SetConnection']['tsumego_id']]))
117                $versionCounter[$scT['SetConnection']['tsumego_id']]++;
118            else
119                $versionCounter[$scT['SetConnection']['tsumego_id']] = 0;
120            $s[$i]['Sgf']['version'] = count($this->Sgf->find('all', ['conditions' => ['tsumego_id' => $scT['SetConnection']['tsumego_id']]])) - $versionCounter[$scT['SetConnection']['tsumego_id']];
121
122            if ($s[$i]['Sgf']['user'] == 'noUser')
123                $s[$i]['Sgf']['user'] = 'automatically generated';
124            if (Auth::getUserID() == $s[$i]['Sgf']['user_id'])
125                $s[$i]['Sgf']['delete'] = true;
126            else
127                $s[$i]['Sgf']['delete'] = false;
128            if ($type == 'user')
129            {
130                $sDiff = $this->Sgf->find('all', ['order' => 'id DESC', 'limit' => 2, 'conditions' => ['tsumego_id' => $s[$i]['Sgf']['tsumego_id']]]);
131                if (!$sDiff)
132                    $sDiff = [];
133                $s[$i]['Sgf']['diff'] = $sDiff[1]['Sgf']['id'];
134            }
135            elseif ($i != count($s) - 1)
136                $s[$i]['Sgf']['diff'] = $s[$i + 1]['Sgf']['id'];
137        }
138
139        $this->set('ux', $ux);
140        $this->set('type', $type);
141        $this->set('name', $name);
142        $this->set('s', $s);
143        $this->set('id', $id);
144        $this->set('tNum', $t['Tsumego']['num']);
145        $this->set('dId', $dId);
146        $this->set('dTitle', $dTitle);
147    }
148
149}