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