Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
62.07% |
18 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SgfController | |
59.26% |
16 / 27 |
|
0.00% |
0 / 2 |
16.76 | |
0.00% |
0 / 1 |
| fetch | |
47.06% |
8 / 17 |
|
0.00% |
0 / 1 |
8.71 | |||
| upload | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
| 1 | <?php |
| 2 | |
| 3 | App::uses('AdminActivityLogger', 'Utility'); |
| 4 | App::uses('AdminActivityType', 'Model'); |
| 5 | |
| 6 | class SgfController extends AppController |
| 7 | { |
| 8 | public function fetch(int $sgfID) |
| 9 | { |
| 10 | if (!Auth::isLoggedIn()) |
| 11 | { |
| 12 | $this->response->statusCode(403); |
| 13 | $this->response->body('User not logged in.'); |
| 14 | return $this->response; |
| 15 | } |
| 16 | |
| 17 | $sgf = ClassRegistry::init('Sgf')->find('first', ['conditions' => ['id' => $sgfID], 'order' => 'id DESC']); |
| 18 | if (!$sgf) |
| 19 | { |
| 20 | $this->response->statusCode(404); |
| 21 | $this->response->body('Sgf not found.'); |
| 22 | return $this->response; |
| 23 | } |
| 24 | |
| 25 | $status = ClassRegistry::init('TsumegoStatus')->find('first', ['conditions' => ['tsumego_id' => $sgf['Sgf']['tsumego_id'], 'user_id' => Auth::getUserID()]]); |
| 26 | if (!Auth::isAdmin() && !TsumegoUtil::isRecentlySolved($status['TsumegoStatus']['status'])) |
| 27 | { |
| 28 | $this->response->statusCode(403); |
| 29 | $this->response->body('Related tsumego is not in a solved state for the user ' . Auth::getUser()['name']); |
| 30 | return $this->response; |
| 31 | } |
| 32 | |
| 33 | $this->response->statusCode(200); |
| 34 | $this->response->body($sgf['Sgf']['sgf']); |
| 35 | return $this->response; |
| 36 | } |
| 37 | |
| 38 | public function upload($setConnectionID) |
| 39 | { |
| 40 | $setConnection = ClassRegistry::init('SetConnection')->findById($setConnectionID); |
| 41 | if (!$setConnection) |
| 42 | throw new AppException("Specified set connection does not exist."); |
| 43 | |
| 44 | // Use besogo textarea if provided, otherwise use file upload |
| 45 | $fileUpload = isset($_FILES['adminUpload']) && $_FILES['adminUpload']['error'] === UPLOAD_ERR_OK ? $_FILES['adminUpload'] : null; |
| 46 | $sgfDataOrFile = $this->data['sgfForBesogo'] ?? file_get_contents($fileUpload['tmp_name']); |
| 47 | |
| 48 | if (!$sgfDataOrFile) |
| 49 | throw new AppException('No SGF data provided.'); |
| 50 | |
| 51 | $this->set('sgf', $sgfDataOrFile); |
| 52 | $this->set('setConnectionID', $setConnectionID); |
| 53 | $this->render('/Tsumegos/setup_new_sgf'); |
| 54 | } |
| 55 | } |