From 78f51e89b48a4bf47093b2baed14440e819b2140 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Thu, 15 Jun 2017 23:18:47 +0100 Subject: [PATCH] claim explorer implementation --- config/routes.php | 2 +- src/Controller/MainController.php | 80 ++++++- src/Model/Table/ClaimsTable.php | 8 + src/Template/Element/header.ctp | 2 +- src/Template/Main/claims.ctp | 344 ++++++++++++++++++++++++++++++ src/Template/Main/index.ctp | 7 +- webroot/css/main.css | 89 +++++++- 7 files changed, 517 insertions(+), 15 deletions(-) create mode 100644 src/Template/Main/claims.ctp diff --git a/config/routes.php b/config/routes.php index 3019465..6d4870d 100644 --- a/config/routes.php +++ b/config/routes.php @@ -47,6 +47,7 @@ Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/', ['controller' => 'Main', 'action' => 'index']); $routes->connect('/address/*', ['controller' => 'Main', 'action' => 'address']); $routes->connect('/blocks/*', ['controller' => 'Main', 'action' => 'blocks']); + $routes->connect('/claims/*', ['controller' => 'Main', 'action' => 'claims']); $routes->connect('/find', ['controller' => 'Main', 'action' => 'find']); $routes->connect('/realtime', ['controller' => 'Main', 'action' => 'realtime']); $routes->connect('/tx/*', ['controller' => 'Main', 'action' => 'tx']); @@ -69,4 +70,3 @@ Router::scope('/', function (RouteBuilder $routes) { * how to customize the loading of plugin routes. */ Plugin::routes(); - diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index ec9b0c6..d779347 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -56,13 +56,6 @@ class MainController extends AppController { if ($diffMinutes >= 15) { // 15 minutes $shouldRefreshPrice = true; } - - /* - $diffHours = $diff->h; - $diffHours = $diffHours + ($diff->days * 24); - if ($diffHours >= 3) { - $shouldRefreshPrice = true; - }*/ } } @@ -114,6 +107,62 @@ class MainController extends AppController { $this->set('hashRate', $hashRate); } + public function claims($id = null) { + $this->loadModel('Claims'); + $this->loadModel('Transactions'); + + if (!$id) { + $claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->order(['Claims.Created' => 'DESC'])->limit(96)->toArray(); + for ($i = 0; $i < count($claims); $i++) { + if (isset($claims[$i]->Stream)) { + $json = json_decode($claims[$i]->Stream->Stream); + if (isset($json->metadata->license)) { + $claims[$i]->License = $json->metadata->license; + } + if (isset($json->metadata->licenseUrl)) { + $claims[$i]->LicenseUrl = $json->metadata->licenseUrl; + } + } + } + $this->set('claims', $claims); + } else { + $claim = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['ClaimId', 'Name']]])->where(['Claims.ClaimId' => $id])->first(); + if (!$claim) { + return $this->redirect('/'); + } + + $json = json_decode($claim->Stream->Stream); + if (isset($json->metadata->license)) { + $claim->License = $json->metadata->license; + } + if (isset($json->metadata->licenseUrl)) { + $claim->LicenseUrl = $json->metadata->licenseUrl; + } + + $moreClaims = []; + if (isset($claim->Publisher)) { + // find more claims for the publisher + $moreClaims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])-> + where(['Claims.ClaimType' => 2, 'Claims.Id <>' => $claim->Id, 'Claims.PublisherId' => $claim->Publisher->ClaimId])-> + limit(9)->order(['RAND()' => 'DESC'])->toArray(); + for ($i = 0; $i < count($moreClaims); $i++) { + if (isset($moreClaims[$i]->Stream)) { + $json = json_decode($moreClaims[$i]->Stream->Stream); + if (isset($json->metadata->license)) { + $moreClaims[$i]->License = $json->metadata->license; + } + if (isset($json->metadata->licenseUrl)) { + $moreClaims[$i]->LicenseUrl = $json->metadata->licenseUrl; + } + } + } + } + + $this->set('claim', $claim); + $this->set('moreClaims', $moreClaims); + } + } + public function realtime() { $this->loadModel('Blocks'); $this->loadModel('Transactions'); @@ -187,6 +236,7 @@ class MainController extends AppController { } $this->loadModel('Blocks'); + $this->loadModel('Claims'); $this->loadModel('Addresses'); $this->loadModel('Transactions'); @@ -196,13 +246,19 @@ class MainController extends AppController { if ($block) { return $this->redirect('/blocks/' . $height); } - } else if (strlen(trim($criteria)) <= 40) { + } else if (strlen(trim($criteria)) === 34) { // Address $address = $this->Addresses->find()->select(['Id', 'Address'])->where(['Address' => $criteria])->first(); if ($address) { return $this->redirect('/address/' . $address->Address); } - } else { + } else if (strlen(trim($criteria)) === 40) { + // Claim ID + $claim = $this->Claims->find()->select(['ClaimId'])->where(['ClaimId' => $criteria])->first(); + if ($claim) { + return $this->redirect('/claim/' . $claim->ClaimId); + } + } else if (strlen(trim($criteria)) === 64) { // block or tx hash // Try block hash first $block = $this->Blocks->find()->select(['Height'])->where(['Hash' => $criteria])->first(); if ($block) { @@ -213,6 +269,12 @@ class MainController extends AppController { return $this->redirect('/tx/' . $tx->Hash); } } + } else { + // finally, try exact claim name match + $claim = $this->Claims->find()->select(['ClaimId'])->where(['Name' => $criteria])->first(); + if ($claim) { + return $this->redirect('/claims/' . $claim->ClaimId); + } } // Not found, redirect to index diff --git a/src/Model/Table/ClaimsTable.php b/src/Model/Table/ClaimsTable.php index da80139..4177faa 100644 --- a/src/Model/Table/ClaimsTable.php +++ b/src/Model/Table/ClaimsTable.php @@ -20,6 +20,14 @@ class ClaimsTable extends Table { 'bindingKey' => 'ClaimId', 'propertyName' => 'Publisher' ] + ], + 'hasOne' => [ + 'Stream' => [ + 'className' => 'App\Model\Table\ClaimStreamsTable', + 'foreignKey' => 'Id', + 'bindingKey' => 'Id', + 'propertyName' => 'Stream' + ] ] ]); } diff --git a/src/Template/Element/header.ctp b/src/Template/Element/header.ctp index 861478e..1a561f1 100644 --- a/src/Template/Element/header.ctp +++ b/src/Template/Element/header.ctp @@ -5,7 +5,7 @@