Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
59.00% covered (warning)
59.00%
59 / 100
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SgfsController
58.59% covered (warning)
58.59%
58 / 99
0.00% covered (danger)
0.00%
0 / 2
64.91
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
62.37% covered (warning)
62.37%
58 / 93
0.00% covered (danger)
0.00%
0 / 1
47.80
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            $s[$i]['Sgf']['sgf'] = str_replace("\r", '', $s[$i]['Sgf']['sgf']);
105            $s[$i]['Sgf']['sgf'] = str_replace("\n", '"+"\n"+"', $s[$i]['Sgf']['sgf']);
106
107            $u = $this->User->findById($s[$i]['Sgf']['user_id']);
108            $s[$i]['Sgf']['user'] = $u['User']['name'];
109            $ux = $u['User']['name'];
110            $t = $this->Tsumego->findById($s[$i]['Sgf']['tsumego_id']);
111            $scT = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $t['Tsumego']['id']]]);
112            $t['Tsumego']['set_id'] = $scT['SetConnection']['set_id'];
113            $set = $this->Set->findById($t['Tsumego']['set_id']);
114            $s[$i]['Sgf']['title'] = $set['Set']['title'] . ' ' . $set['Set']['title2'] . ' #' . $scT['SetConnection']['num'];
115            $s[$i]['Sgf']['num'] = $scT['SetConnection']['num'];
116
117            if(isset($versionCounter[$scT['SetConnection']['tsumego_id']]))
118                $versionCounter[$scT['SetConnection']['tsumego_id']]++;
119            else
120                $versionCounter[$scT['SetConnection']['tsumego_id']] = 0;
121            $s[$i]['Sgf']['version'] = count($this->Sgf->find('all', ['conditions' => ['tsumego_id' => $scT['SetConnection']['tsumego_id']]])) - $versionCounter[$scT['SetConnection']['tsumego_id']];
122
123            if ($s[$i]['Sgf']['user'] == 'noUser')
124                $s[$i]['Sgf']['user'] = 'automatically generated';
125            if (Auth::getUserID() == $s[$i]['Sgf']['user_id'])
126                $s[$i]['Sgf']['delete'] = true;
127            else
128                $s[$i]['Sgf']['delete'] = false;
129            if ($type == 'user')
130            {
131                $sDiff = $this->Sgf->find('all', ['order' => 'id DESC', 'limit' => 2, 'conditions' => ['tsumego_id' => $s[$i]['Sgf']['tsumego_id']]]);
132                if (!$sDiff)
133                    $sDiff = [];
134                $s[$i]['Sgf']['diff'] = $sDiff[1]['Sgf']['id'];
135            }
136            elseif ($i != count($s) - 1)
137                $s[$i]['Sgf']['diff'] = $s[$i + 1]['Sgf']['id'];
138        }
139
140        $this->set('ux', $ux);
141        $this->set('type', $type);
142        $this->set('name', $name);
143        $this->set('s', $s);
144        $this->set('id', $id);
145        $this->set('tNum', $t['Tsumego']['num']);
146        $this->set('dId', $dId);
147        $this->set('dTitle', $dTitle);
148    }
149
150}