Improve search #40

Merged
marcdeb1 merged 4 commits from improve_search into master 2018-11-28 16:03:42 +01:00
6 changed files with 177 additions and 73 deletions

View file

@ -329,9 +329,6 @@ class MainController extends AppController {
public function find() { public function find() {
$criteria = $this->request->query('q'); $criteria = $this->request->query('q');
if ($criteria === null || strlen(trim($criteria)) == 0) {
return $this->redirect('/');
}
$this->loadModel('Blocks'); $this->loadModel('Blocks');
$this->loadModel('Claims'); $this->loadModel('Claims');
@ -354,7 +351,7 @@ class MainController extends AppController {
// Claim ID // Claim ID
$claim = $this->Claims->find()->select(['ClaimId'])->where(['ClaimId' => $criteria])->first(); $claim = $this->Claims->find()->select(['ClaimId'])->where(['ClaimId' => $criteria])->first();
if ($claim) { if ($claim) {
return $this->redirect('/claim/' . $claim->ClaimId); return $this->redirect('/claims/' . $claim->ClaimId);
} }
} else if (strlen(trim($criteria)) === 64) { // block or tx hash } else if (strlen(trim($criteria)) === 64) { // block or tx hash
// Try block hash first // Try block hash first
@ -369,16 +366,16 @@ class MainController extends AppController {
} }
} else { } else {
// finally, try exact claim name match // finally, try exact claim name match
$claim = $this->Claims->find()->select(['ClaimId'])->where(['Name' => $criteria])->first(); $claims = $this->Claims->find()->distinct(['Claims.ClaimId'])->where(['Name' => $criteria])->order(['Claims.Created' => 'DESC'])->limit(10)->toArray();
if ($claim) { if (count($claims) == 1) {
return $this->redirect('/claims/' . $claim->ClaimId); return $this->redirect('/claims/' . $claims[0]->ClaimId);
}
else {
$this->set('claims', $claims);
} }
} }
// Not found, redirect to index
return $this->redirect('/');
} }
public function blocks($height = null) { public function blocks($height = null) {
$this->loadModel('Blocks'); $this->loadModel('Blocks');

View file

@ -18,6 +18,33 @@ class Claim extends Entity {
$link = '/claims/' . $this->ClaimId; $link = '/claims/' . $this->ClaimId;
return $link; return $link;
} }
function getContentTag() {
$ctTag = null;
if (substr($this->ContentType, 0, 5) === 'audio') {
$ctTag = 'audio';
} else if (substr($this->ContentType, 0, 5) === 'video') {
$ctTag = 'video';
} else if (substr($this->ContentType, 0, 5) === 'image') {
$ctTag = 'image';
}
if (!$ctTag && $this->ClaimType == 1) {
$ctTag = 'identity';
}
return $ctTag;
}
function getAutoThumbText() {
$autoThumbText = '';
if ($this->ClaimType == 1) {
$autoThumbText = strtoupper(substr($this->Name, 1, min( strlen($this->Name), 3 )));
} else {
$str = (strlen(trim($this->Title)) > 0) ? $this->Title : $this->Name;
$autoThumbText = strtoupper(substr($str, 0, min (strlen($str), 2 )));
}
return $autoThumbText;
}
} }
?> ?>

View file

@ -39,12 +39,7 @@
<?php if (isset($claim)): <?php if (isset($claim)):
$a = ['purple', 'orange', 'blue', 'teal', 'green', 'yellow']; $a = ['purple', 'orange', 'blue', 'teal', 'green', 'yellow'];
$autoThumbText = ''; $autoThumbText = $claim->getAutoThumbText();
if ($claim->ClaimType == 1) { $autoThumbText = strtoupper(substr($claim->Name, 1, min( strlen($claim->Name), 10 ))); } else {
$str = str_replace(' ', '', (strlen(trim($claim->Title)) > 0) ? $claim->Title : $claim->Name);
$autoThumbText = strtoupper(mb_substr($str, 0, min( strlen($str), 10 )));
}
$cost = 'Free'; $cost = 'Free';
if (isset($claim->Price) && $claim->Price > 0) { if (isset($claim->Price) && $claim->Price > 0) {
$cost = $this->Amount->formatCurrency($claim->Price) . ' LBC'; $cost = $this->Amount->formatCurrency($claim->Price) . ' LBC';
@ -156,7 +151,7 @@ if (strlen(trim($desc)) == 0) {
$row++; $row++;
} }
$autoThumbText = ''; $autoThumbText = $claim->getAutoThumbText();
$cost = ''; $cost = '';
if (isset($claim->Price) && $claim->Price > 0) { if (isset($claim->Price) && $claim->Price > 0) {
$cost = $this->Amount->formatCurrency($claim->Price) . ' LBC'; $cost = $this->Amount->formatCurrency($claim->Price) . ' LBC';
@ -165,24 +160,7 @@ if (strlen(trim($desc)) == 0) {
} }
// content type // content type
$ctTag = null; $ctTag = $claim->getContentTag();
if (substr($claim->ContentType, 0, 5) === 'audio') {
$ctTag = 'audio';
} else if (substr($claim->ContentType, 0, 5) === 'video') {
$ctTag = 'video';
} else if (substr($claim->ContentType, 0, 5) === 'image') {
$ctTag = 'image';
}
if (!$ctTag && $claim->ClaimType == 1) {
$ctTag = 'identity';
}
if ($claim->ClaimType == 1) { $autoThumbText = strtoupper(substr($claim->Name, 1, min( strlen($claim->Name), 10 ))); } else {
$str = str_replace(' ', '', (strlen(trim($claim->Title)) > 0) ? $claim->Title : $claim->Name);
$autoThumbText = strtoupper(mb_substr($str, 0, min( strlen($str), 10 )));
}
?> ?>
<div data-id="<?php echo $claim->ClaimId ?>" class="claim-grid-item<?php if ($idx % 3 == 0): ?> last-item<?php endif; ?><?php if ($last_row): ?> last-row<?php endif; ?>"> <div data-id="<?php echo $claim->ClaimId ?>" class="claim-grid-item<?php if ($idx % 3 == 0): ?> last-item<?php endif; ?><?php if ($last_row): ?> last-row<?php endif; ?>">
<?php if (strlen(trim($cost)) > 0): ?> <?php if (strlen(trim($cost)) > 0): ?>
@ -274,7 +252,7 @@ if (strlen(trim($desc)) == 0) {
if ($idx % 3 == 0) { if ($idx % 3 == 0) {
$row++; $row++;
} }
$autoThumbText = ''; $autoThumbText = $claim->getAutoThumbText();
$cost = ''; $cost = '';
if (isset($claim->Price) && $claim->Price > 0) { if (isset($claim->Price) && $claim->Price > 0) {
$cost = $this->Amount->formatCurrency($claim->Price) . ' LBC'; $cost = $this->Amount->formatCurrency($claim->Price) . ' LBC';
@ -283,24 +261,7 @@ if (strlen(trim($desc)) == 0) {
} }
// content type // content type
$ctTag = null; $ctTag = $claim->getContentTag();
if (substr($claim->ContentType, 0, 5) === 'audio') {
$ctTag = 'audio';
} else if (substr($claim->ContentType, 0, 5) === 'video') {
$ctTag = 'video';
} else if (substr($claim->ContentType, 0, 5) === 'image') {
$ctTag = 'image';
}
if (!$ctTag && $claim->ClaimType == 1) {
$ctTag = 'identity';
}
if ($claim->ClaimType == 1) { $autoThumbText = strtoupper(substr($claim->Name, 1, min( strlen($claim->Name), 10 ))); } else {
$str = str_replace(' ', '', (strlen(trim($claim->Title)) > 0) ? $claim->Title : $claim->Name);
$autoThumbText = strtoupper(mb_substr($str, 0, min( strlen($str), 10 )));
}
?> ?>
<div data-id="<?php echo $claim->ClaimId ?>" class="claim-grid-item<?php if ($idx % 3 == 0): ?> last-item<?php endif; ?><?php if ($last_row): ?> last-row<?php endif; ?>"> <div data-id="<?php echo $claim->ClaimId ?>" class="claim-grid-item<?php if ($idx % 3 == 0): ?> last-item<?php endif; ?><?php if ($last_row): ?> last-row<?php endif; ?>">
<?php if (strlen(trim($cost)) > 0): ?> <?php if (strlen(trim($cost)) > 0): ?>

134
src/Template/Main/find.ctp Normal file
View file

@ -0,0 +1,134 @@
<?php $this->start('script'); ?>
<script type="text/javascript">
var resizeCards = function() {
var claimInfo = $('.claim-info');
var claimMtdt = $('.claim-metadata');
if (claimMtdt.outerHeight() < claimInfo.outerHeight()) {
claimMtdt.outerHeight(claimInfo.outerHeight());
} else if (claimInfo.outerHeight() < claimMtdt.outerHeight()) {
claimInfo.outerHeight(claimMtdt.outerHeight());
}
};
window.onload = function() {
resizeCards();
};
$(document).ready(function() {
resizeCards();
$('.claim-grid-item img,.claim-info img').on('error', function() {
var img = $(this);
var parent = img.parent();
var text = parent.attr('data-autothumb');
img.remove();
parent.append(
$('<div></div>').attr({'class': 'autothumb' }).text(text)
);
});
$(document).on('click', '.claim-grid-item', function() {
var id = $(this).attr('data-id');
location.href = '/claims/' + id;
});
});
</script>
<?php $this->end(); ?>
<?php echo $this->element('header') ?>
<?php $this->assign('title', 'Search Results') ?>
<div class="claims-head">
<h3>Search results</h3>
</div>
<div class="claims-grid">
<?php if (isset($claims) && count($claims) > 0): ?>
<?php
$idx = 1;
$row = 1;
$rowCount = ceil(count($claims) / 3);
$a = ['purple', 'orange', 'blue', 'teal', 'green', 'yellow'];
foreach ($claims as $claim):
$last_row = ($row == $rowCount);
if ($idx % 3 == 0) {
$row++;
}
$autoThumbText = $claim->getAutoThumbText();
$cost = '';
if (isset($claim->Price) && $claim->Price > 0) {
$cost = $this->Amount->formatCurrency($claim->Price) . ' LBC';
} else if (isset($claim->Fee) && strtolower($claim->FeeCurrency) === 'lbc') {
$cost = $this->Amount->formatCurrency($claim->Fee) . ' LBC';
}
// content type
$ctTag = $claim->getContentTag();
?>
<div data-id="<?php echo $claim->ClaimId ?>" class="claim-grid-item<?php if ($idx % 3 == 0): ?> last-item<?php endif; ?><?php if ($last_row): ?> last-row<?php endif; ?>">
<?php if (strlen(trim($cost)) > 0): ?>
<div class="price-tag"><?php echo $cost ?></div>
<?php endif; ?>
<div class="tags">
<?php if ($ctTag): ?>
<div class="content-type"><?php echo strtoupper($ctTag) ?></div>
<?php endif; ?>
<?php if ($claim->IsNSFW): ?>
<div class="nsfw">NSFW</div>
<?php endif; ?>
</div>
<div data-autothumb="<?php echo $autoThumbText ?>" class="thumbnail <?php echo $a[mt_rand(0, count($a) - 1)] ?>">
<?php if (!$claim->IsNSFW && strlen(trim($claim->ThumbnailUrl)) > 0): ?>
<img src="<?php echo htmlspecialchars($claim->ThumbnailUrl) ?>" alt="" />
<?php else: ?>
<div class="autothumb"><?php echo $autoThumbText ?></div>
<?php endif; ?>
</div>
<div class="metadata">
<div class="title" title="<?php echo $claim->ClaimType == 1 ? $claim->Name : ((strlen(trim($claim->Title)) > 0) ? $claim->Title : '') ?>"><?php echo $claim->ClaimType == 1 ? $claim->Name : ((strlen(trim($claim->Title)) > 0) ? $claim->Title : '<em>No Title</em>') ?></div>
<div class="link" title="<?php echo $claim->getLbryLink() ?>"><a href="<?php echo $claim->getLbryLink() ?>" rel="nofollow"><?php echo $claim->getLbryLink() ?></a></div>
<div class="desc"><?php echo strlen(trim($claim->Description)) > 0 ? $claim->Description : '<em>No description available</em>' ?></div>
<div class="label half-width">Transaction</div>
<div class="label half-width">Created</div>
<div class="value half-width"><a href="/tx/<?php echo $claim->TransactionHash ?>#output-<?php echo $claim->Vout ?>" title="<?php echo $claim->TransactionHash ?>"><?php echo $claim->TransactionHash ?></a></div>
<div class="value half-width" title="<?php echo $claim->Created->format('j M Y H:i:s') ?> UTC">
<?php echo \Carbon\Carbon::createFromTimestamp($claim->Created->format('U'))->diffForHumans(); ?>
</div>
<div class="clear spacer"></div>
<?php if ($claim->ClaimType == 2): ?>
<div class="label half-width">Content Type</div>
<div class="label half-width">Language</div>
<div class="value half-width" title="<?php echo $claim->ContentType ?>"><?php echo $claim->ContentType ?></div>
<div class="value half-width" title="<?php echo $claim->Language == 'en' ? 'English' : $claim->Language ?>"><?php echo $claim->Language == 'en' ? 'English' : $claim->Language ?></div>
<div class="clear spacer"></div>
<div class="label half-width">Author</div>
<div class="label half-width">License</div>
<div class="value half-width" title="<?php echo strlen(trim($claim->Author)) > 0 ? $claim->Author : '<em>Unspecified</em>' ?>"><?php echo strlen(trim($claim->Author)) > 0 ? $claim->Author : '<em>Unspecified</em>' ?></div>
<div class="value half-width" title="<?php echo strlen(trim($claim->License)) > 0 ? $claim->License : '' ?>">
<?php if (strlen(trim($claim->LicenseUrl)) > 0): ?><a href="<?php echo $claim->LicenseUrl ?>" rel="nofollow" target="_blank"><?php endif; ?>
<?php echo strlen(trim($claim->License)) > 0 ? $claim->License : '<em>Unspecified</em>' ?>
<?php if (strlen(trim($claim->LicenseUrl))): ?></a><?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php $idx++; endforeach; ?>
<?php else: ?>
<div class="no-results">No results were found.</div>
<?php endif; ?>
<div class="clear"></div>
</div>
<?php echo $this->element('pagination') ?>

View file

@ -185,7 +185,7 @@
<a class="claim-explorer-link" href="/claims">Claims Explorer</a> <a class="claim-explorer-link" href="/claims">Claims Explorer</a>
<?php $idx = 0; $a = ['purple', 'orange', 'blue', 'teal', 'green', 'yellow']; foreach ($recentClaims as $claim): <?php $idx = 0; $a = ['purple', 'orange', 'blue', 'teal', 'green', 'yellow']; foreach ($recentClaims as $claim):
$idx++; $idx++;
$autoThumbText = ''; $autoThumbText = $claim->getAutoThumbText();
$link = $claim->Name; $link = $claim->Name;
$rawLink = $claim->Name; $rawLink = $claim->Name;
if (isset($claim->Publisher->Name)) { if (isset($claim->Publisher->Name)) {
@ -196,23 +196,7 @@
$rawLink = 'lbry://' . $rawLink; $rawLink = 'lbry://' . $rawLink;
// content type // content type
$ctTag = null; $ctTag = $claim->getContentTag();
if (substr($claim->ContentType, 0, 5) === 'audio') {
$ctTag = 'audio';
} else if (substr($claim->ContentType, 0, 5) === 'video') {
$ctTag = 'video';
} else if (substr($claim->ContentType, 0, 5) === 'image') {
$ctTag = 'image';
}
if (!$ctTag && $claim->ClaimType == 1) {
$ctTag = 'identity';
}
if ($claim->ClaimType == 1) { $autoThumbText = strtoupper(substr($claim->Name, 1, min( strlen($claim->Name), 3 ))); } else {
$str = (strlen(trim($claim->Title)) > 0) ? $claim->Title : $claim->Name;
$autoThumbText = strtoupper(substr($str, 0, min (strlen($str), 2 )));
}
?> ?>
<div data-id="<?php echo $claim->ClaimId ?>" class="claim-box<?php if ($idx == 5): ?> last<?php endif; ?>"> <div data-id="<?php echo $claim->ClaimId ?>" class="claim-box<?php if ($idx == 5): ?> last<?php endif; ?>">
<div class="tags"> <div class="tags">

View file

@ -88,6 +88,7 @@ border-radius: 0 8px 8px 0 }
.claims-grid .claim-grid-item .link { font-size: 95%; font-weight: 300; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; line-height: 20px; height: 20px; margin-top: 6px } .claims-grid .claim-grid-item .link { font-size: 95%; font-weight: 300; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; line-height: 20px; height: 20px; margin-top: 6px }
.claims-grid .claim-grid-item .label { font-size: 80%; color: #1e88e5 } .claims-grid .claim-grid-item .label { font-size: 80%; color: #1e88e5 }
.claims-grid .no-results {font-style: italic; width: 100%; height: 300px; text-align: center; font-size: 90%; color: grey; }
.claims-grid .claim-grid-item .value { font-weight: 300; word-break: break-word; word-wrap: break-word; font-size: 95%; line-height: 24px; height: 24px } .claims-grid .claim-grid-item .value { font-weight: 300; word-break: break-word; word-wrap: break-word; font-size: 95%; line-height: 24px; height: 24px }
.claims-grid .claim-grid-item .half-width { width: 155px; float: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 12px } .claims-grid .claim-grid-item .half-width { width: 155px; float: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 12px }
.claims-grid .claim-grid-item .spacer { height: 16px } .claims-grid .claim-grid-item .spacer { height: 16px }