Skip to content

Commit

Permalink
refactor cover art management
Browse files Browse the repository at this point in the history
  • Loading branch information
Spine authored and itismadness committed Aug 22, 2021
1 parent 2ab4d3e commit 9920d44
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 113 deletions.
56 changes: 54 additions & 2 deletions app/TGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

class TGroup extends BaseObject {

const CACHE_KEY = 'tg_%d';
const CACHE_TLIST_KEY = 'tlist_%d';
const CACHE_KEY = 'tg_%d';
const CACHE_TLIST_KEY = 'tlist_%d';
const CACHE_COVERART_KEY = 'tg_cover_%d';

const ARTIST_DISPLAY_TEXT = 1;
const ARTIST_DISPLAY_HTML = 2;
Expand Down Expand Up @@ -242,6 +243,57 @@ public function torrentTagList(): array {
return $tag;
}

public function addCoverArt(string $image, string $summary, int $userId, \Gazelle\Log $logger): int {
$this->db->prepared_query("
INSERT IGNORE INTO cover_art
(GroupID, Image, Summary, UserID)
VALUES (?, ?, ?, ?)
", $this->id, $image, $summary, $userId
);
$n = $this->db->affected_rows();
if ($n) {
$logger->group($this->id, $userId, "Additional cover \"$summary - $image\" added to group");
$this->cache->delete_value(sprintf(self::CACHE_COVERART_KEY, $this->id));
}
return $n;
}

public function removeCoverArt(int $coverId, int $userId, \Gazelle\Log $logger): int {
[$image, $summary] = $this->db->row("
SELECT Image, Summary
FROM cover_art
WHERE ID = ?
", $coverId
);
$this->db->prepared_query("
DELETE FROM cover_art WHERE ID = ?
", $coverId
);
$n = $this->db->affected_rows();
if ($n) {
$logger->group($this->id, $userId, "Additional cover \"$summary - $image\" removed from group");
$this->cache->delete_value(sprintf(self::CACHE_COVERART_KEY, $this->id));
}
return $n;
}

public function coverArt(): array {
$key = sprintf(self::CACHE_COVERART_KEY, $this->id);
$list = $this->cache->get_value($key);
if ($list === false) {
$this->db->prepared_query("
SELECT ID, Image, Summary, UserID, Time
FROM cover_art
WHERE GroupID = ?
ORDER BY Time ASC
", $this->id
);
$list = $this->db->to_array(false, MYSQLI_ASSOC, false);
$this->cache->cache_value($key, $list, 0);
}
return $list;
}

/**
* Generate an HTML anchor or the name for an artist
*/
Expand Down
25 changes: 25 additions & 0 deletions db/migrations/20210726154658_cover_art_now.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CoverArtNow extends AbstractMigration
{
public function up(): void {
$this->table('cover_art')
->changeColumn('Image', 'string', ['length' => 255, 'null' => false])
->changeColumn('Summary', 'string', ['length' => 100, 'null' => false])
->changeColumn('UserID', 'integer', ['length' => 10, 'null' => false])
->changeColumn('Time', 'datetime', ['null' => false, 'default' => 'CURRENT_TIMESTAMP'])
->save();
}

public function down(): void {
$this->table('cover_art')
->changeColumn('Image', 'string', ['length' => 255, 'null' => false, 'default' => ''])
->changeColumn('Summary', 'string', ['length' => 100, 'null' => true])
->changeColumn('UserID', 'integer', ['length' => 10, 'null' => false, 'default' => 0])
->changeColumn('Time', 'datetime', ['null' => true])
->save();
}
}
3 changes: 3 additions & 0 deletions sections/ajax/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
case 'torrentgroupalbumart': // so the album art script can function without breaking the ratelimit
require('torrentgroupalbumart.php');
break;
case 'torrent_remove_cover_art':
require('torrent_remove_cover_art.php');
break;
case 'tcomments':
require('tcomments.php');
break;
Expand Down
17 changes: 17 additions & 0 deletions sections/ajax/torrent_remove_cover_art.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

authorize();
if (!$Viewer->permitted('site_edit_wiki')) {
json_die('failure', 'forbidden');
}
$tgroup = (new Gazelle\Manager\TGroup)->findById((int)$_GET['groupid']);
$coverId = (int)$_GET['id'];
if (!$coverId || is_null($tgroup)) {
json_die('failure', 'bad parameters');
}

if ($tgroup->removeCoverArt($coverId, $Viewer->id(), new Gazelle\Log)) {
json_print("success", ['id' => $coverId]);
} else {
json_die('failure', 'bad coverId');
}
45 changes: 14 additions & 31 deletions sections/torrents/add_cover_art.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
<?php
authorize();

if (!check_perms('site_edit_wiki')) {
if (!$Viewer->permitted('site_edit_wiki')) {
error(403);
}
authorize();

$GroupID = (int)$_POST['groupid'];
$Summaries = $_POST['summary'];
$Images = $_POST['image'];

if (!$GroupID) {
error(0);
$tgroup = (new Gazelle\Manager\TGroup)->findById((int)$_POST['groupid']);
if (is_null($tgroup)) {
error(404);
}

if (count($Images) != count($Summaries)) {
$summaryList = $_POST['summary'];
$imageList = $_POST['image'];
if (count($imageList) != count($summaryList)) {
error('Missing an image or a summary');
}

$Changed = false;
for ($i = 0; $i < count($Images); $i++) {
$Image = trim($Images[$i]);
if (ImageTools::blacklisted($Image, true) || !preg_match(IMAGE_REGEXP, $Image)) {
$logger = new Gazelle\Log;
for ($i = 0, $end = count($imageList); $i < $end; $i++) {
$image = trim($imageList[$i]);
if (ImageTools::blacklisted($image, true) || !preg_match(IMAGE_REGEXP, $image)) {
continue;
}
$Summary = trim($Summaries[$i]);

$DB->prepared_query("
INSERT IGNORE INTO cover_art
(GroupID, Image, Summary, UserID, Time)
VALUES (?, ?, ?, ?, now())
", $GroupID, $Image, $Summary, $Viewer->id()
);
if ($DB->affected_rows()) {
$Changed = true;
(new Gazelle\Log)->group($GroupID, $Viewer->id(), "Additional cover \"$Summary - $Image\" added to group");
}
}

if ($Changed) {
$Cache->delete_value("torrents_cover_art_$GroupID");
$tgroup->addCoverArt($image, trim($summaryList[$i]), $Viewer->id(), $logger);
}

header("Location: " . $_SERVER['HTTP_REFERER'] ?? "torrents.php?id={$GroupID}");
header("Location: " . redirectUrl("torrents.php?id=" . $tgroup->id()));
92 changes: 46 additions & 46 deletions sections/torrents/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}
$GroupID = $tgroup->id();
$RevisionID = (int)($_GET['revisionid'] ?? 0);
$tgroup = (new Gazelle\Manager\TGroup)->findById($GroupID);

[$TorrentDetails, $TorrentList] = get_group_info($GroupID, $RevisionID);

Expand Down Expand Up @@ -45,19 +46,25 @@
$AltName .= " [$name] ";
}

$CoverArt = $Cache->get_value("torrents_cover_art_$GroupID");
if (!$CoverArt) {
$DB->prepared_query('
SELECT ID, Image, Summary, UserID, Time
FROM cover_art
WHERE GroupID = ?
ORDER BY Time ASC', $GroupID);
$CoverArt = $DB->to_array();
if ($DB->has_results()) {
$Cache->cache_value("torrents_cover_art_$GroupID", $CoverArt, 0);
$Tags = [];
if ($TorrentTags != '') {
$TorrentTags = explode('|', $TorrentTags);
$TorrentTagIDs = explode('|', $TorrentTagIDs);
$TorrentTagUserIDs = explode('|', $TorrentTagUserIDs);
$TagPositiveVotes = explode('|', $TagPositiveVotes);
$TagNegativeVotes = explode('|', $TagNegativeVotes);

foreach ($TorrentTags as $TagKey => $TagName) {
$Tags[$TagKey]['name'] = $TagName;
$Tags[$TagKey]['score'] = ($TagPositiveVotes[$TagKey] - $TagNegativeVotes[$TagKey]);
$Tags[$TagKey]['id'] = $TorrentTagIDs[$TagKey];
$Tags[$TagKey]['userid'] = $TorrentTagUserIDs[$TagKey];
}
uasort($Tags, 'compare');
}

$CoverArt = $tgroup->coverArt();

// Comments (must be loaded before View::show_header so that subscriptions and quote notifications are handled properly)
$commentPage = new Gazelle\Comment\Torrent($GroupID);
if (isset($_GET['postid'])) {
Expand Down Expand Up @@ -112,39 +119,35 @@
<div class="box box_image box_image_albumart box_albumart"><!-- .box_albumart deprecated -->
<div class="head">
<strong><?=(count($CoverArt) > 0 ? 'Covers (' . (count($CoverArt) + 1) . ')' : 'Cover')?></strong>
<?php if (!$CoverArt) { ?>
<span>
<a class="brackets show_all_covers" href="#">Hide</a>
</span>
<?php
if (count($CoverArt) > 0) {
if (empty($LoggedUser['ShowExtraCovers'])) {
for ($Index = 0; $Index <= count($CoverArt); $Index++) {
} elseif ($Viewer->option('ShowExtraCovers')) {
for ($Index = 0, $last = count($CoverArt); $Index <= $last; $Index++) {
?>
<span id="cover_controls_<?=($Index)?>"<?=($Index > 0 ? ' style="display: none;"' : '')?>>
<?php if ($Index == count($CoverArt)) { ?>
<?php if ($Index == count($CoverArt)) { ?>
<a class="brackets prev_cover" data-gazelle-prev-cover="<?=($Index - 1)?>" href="#">Prev</a>
<a class="brackets show_all_covers" href="#">Show all</a>
<span class="brackets next_cover">Next</span>
<?php } elseif ($Index > 0) { ?>
<?php } elseif ($Index > 0) { ?>
<a class="brackets prev_cover" data-gazelle-prev-cover="<?=($Index - 1)?>" href="#">Prev</a>
<a class="brackets show_all_covers" href="#">Show all</a>
<a class="brackets next_cover" data-gazelle-next-cover="<?=($Index + 1)?>" href="#">Next</a>
<?php } elseif ($Index == 0 && count($CoverArt) > 0) { ?>
<?php } elseif ($Index == 0 && count($CoverArt) > 0) { ?>
<span class="brackets prev_cover">Prev</span>
<a class="brackets show_all_covers" href="#">Show all</a>
<a class="brackets next_cover" data-gazelle-next-cover="<?=($Index + 1)?>" href="#">Next</a>
<?php } ?>
</span>
<?php
}
} else {
?>
<span>
<a class="brackets show_all_covers" href="#">Hide</a>
<?php } ?>
</span>
<?php
}
}
}
$Index = 0;
?>
</div>
<?php $Index = 0; ?>
<div id="covers">
<div id="cover_div_<?=$Index?>" class="pad">
<?php if ($WikiImage != '') { ?>
Expand All @@ -156,36 +159,34 @@
$Index++;
?>
</div>
<?php
foreach ($CoverArt as $Cover) {
[$ImageID, $Image, $Summary, $AddedBy] = $Cover;
?>
<?php foreach ($CoverArt as $c) { ?>
<div id="cover_div_<?=$Index?>" class="pad"<?=(empty($LoggedUser['ShowExtraCovers']) ? ' style="display: none;"' : '')?>>
<p align="center">
<?php
if (empty($LoggedUser['ShowExtraCovers'])) {
$Src = 'src="" data-gazelle-temp-src="' . ImageTools::process($Image, true) . '"';
} else {
$Src = 'src="' . ImageTools::process($Image, true) . '"';
}
if (empty($LoggedUser['ShowExtraCovers'])) {
$Src = 'src="" data-gazelle-temp-src="' . ImageTools::process($c['Image'], true) . '"';
} else {
$Src = 'src="' . ImageTools::process($c['Image'], true) . '"';
}
?>
<img id="cover_<?=$Index?>" width="100%" <?=$Src?> alt="<?=$Summary?>" onclick="lightbox.init('<?=ImageTools::process($Image)?>', 220);" />
<img id="cover_<?=$Index?>" width="100%" <?=$Src?> alt="<?=$c['Summary']?>" onclick="lightbox.init('<?=ImageTools::process($c['Image'])?>', 220);" />
</p>
<ul class="stats nobullet">
<li>
<?=$Summary?>
<?=(check_perms('users_mod') ? ' added by ' . Users::format_username($AddedBy, false, false, false, false, false) : '')?>
<span class="remove remove_cover_art"><a href="#" onclick="if (confirm('Do not delete valid alternative cover art. Are you sure you want to delete this cover art?') == true) { ajax.get('torrents.php?action=remove_cover_art&amp;auth=<?=$Viewer->auth() ?>&amp;id=<?=$ImageID?>&amp;groupid=<?=$GroupID?>'); this.parentNode.parentNode.parentNode.style.display = 'none'; this.parentNode.parentNode.parentNode.previousElementSibling.style.display = 'none'; } else { return false; }" class="brackets tooltip" title="Remove image">X</a></span>
<li><?= $c['Summary'] ?>
<?php if ($Viewer->permitted('users_mod')) { ?>
added by <?= Users::format_username($c['UserID'], false, false, false, false, false) ?>
<?php } ?>
<span class="remove remove_cover_art"><a href="#" onclick="if (confirm('Do not delete valid alternative cover art. Are you sure you want to delete this cover art?') == true) { ajax.get('ajax.php?action=torrent_remove_cover_art&amp;auth=<?= $Viewer->auth() ?>&amp;id=<?= $c['ID'] ?>&amp;groupid=<?= $GroupID ?>'); this.parentNode.parentNode.parentNode.style.display = 'none'; this.parentNode.parentNode.parentNode.previousElementSibling.style.display = 'none'; } else { return false; }" class="brackets tooltip" title="Remove image">X</a></span>
</li>
</ul>
</div>
<?php
$Index++;
}
$Index++;
}
?>
</div>
</div>

<?php if (check_perms('site_edit_wiki') && $WikiImage != '') { ?>
<?php if ($Viewer->permitted('site_edit_wiki') && $WikiImage != '') { ?>
<div id="add_cover_div">
<div style="padding: 10px;">
<span style="float: right;" class="additional_add_artists">
Expand All @@ -202,8 +203,7 @@
</form>
</div>
</div>
<?php } ?>

<?php } ?>
</div>
<?php
if (CATEGORY[$GroupCategoryID - 1] == 'Music') {
Expand Down
3 changes: 0 additions & 3 deletions sections/torrents/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ function js_pages($Action, $TorrentID, $NumResults, $CurrentPage) {
case 'add_cover_art':
require_once('add_cover_art.php');
break;
case 'remove_cover_art':
require_once('remove_cover_art.php');
break;
case 'autocomplete_tags':
require_once('autocomplete_tags.php');
break;
Expand Down
31 changes: 0 additions & 31 deletions sections/torrents/remove_cover_art.php

This file was deleted.

0 comments on commit 9920d44

Please sign in to comment.