Gazelle/sections/collages/add_artist.php

165 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2013-05-25 08:01:03 +00:00
<?
//NumTorrents is actually the number of things in the collage, the name just isn't generic.
authorize();
2013-05-27 08:00:58 +00:00
include(SERVER_ROOT.'/classes/validate.class.php');
2013-05-25 08:01:03 +00:00
$Val = new VALIDATE;
function add_artist($CollageID, $ArtistID) {
global $Cache, $LoggedUser, $DB;
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT MAX(Sort)
FROM collages_artists
WHERE CollageID = '$CollageID'");
2013-05-25 08:01:03 +00:00
list($Sort) = $DB->next_record();
2013-06-28 08:01:04 +00:00
$Sort += 10;
2013-05-25 08:01:03 +00:00
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT ArtistID
FROM collages_artists
WHERE CollageID = '$CollageID'
AND ArtistID = '$ArtistID'");
2013-07-10 00:08:53 +00:00
if (!$DB->has_results()) {
2013-06-28 08:01:04 +00:00
$DB->query("
INSERT IGNORE INTO collages_artists
(CollageID, ArtistID, UserID, Sort, AddedOn)
2013-05-25 08:01:03 +00:00
VALUES
2013-06-28 08:01:04 +00:00
('$CollageID', '$ArtistID', '$LoggedUser[ID]', '$Sort', '" . sqltime() . "')");
2013-05-25 08:01:03 +00:00
2013-06-28 08:01:04 +00:00
$DB->query("
UPDATE collages
SET NumTorrents = NumTorrents + 1, Updated = '" . sqltime() . "'
WHERE ID = '$CollageID'");
2013-05-25 08:01:03 +00:00
2013-06-28 08:01:04 +00:00
$Cache->delete_value("collage_$CollageID");
$Cache->delete_value("artists_collages_$ArtistID");
$Cache->delete_value("artists_collages_personal_$ArtistID");
2013-05-25 08:01:03 +00:00
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT UserID
FROM users_collage_subs
WHERE CollageID = $CollageID");
2013-05-25 08:01:03 +00:00
while (list($CacheUserID) = $DB->next_record()) {
2013-06-28 08:01:04 +00:00
$Cache->delete_value("collage_subs_user_new_$CacheUserID");
2013-05-25 08:01:03 +00:00
}
}
}
$CollageID = $_POST['collageid'];
if (!is_number($CollageID)) {
error(404);
}
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT UserID, CategoryID, Locked, NumTorrents, MaxGroups, MaxGroupsPerUser
FROM collages
WHERE ID = '$CollageID'");
2013-05-25 08:01:03 +00:00
list($UserID, $CategoryID, $Locked, $NumTorrents, $MaxGroups, $MaxGroupsPerUser) = $DB->next_record();
if (!check_perms('site_collages_delete')) {
if ($Locked) {
2013-06-28 08:01:04 +00:00
$Err = 'This collage is locked';
2013-05-25 08:01:03 +00:00
}
if ($CategoryID == 0 && $UserID != $LoggedUser['ID']) {
2013-06-28 08:01:04 +00:00
$Err = 'You cannot edit someone else\'s personal collage.';
2013-05-25 08:01:03 +00:00
}
if ($MaxGroups > 0 && $NumTorrents >= $MaxGroups) {
2013-06-28 08:01:04 +00:00
$Err = 'This collage already holds its maximum allowed number of artists.';
2013-05-25 08:01:03 +00:00
}
if (isset($Err)) {
error($Err);
}
}
if ($MaxGroupsPerUser > 0) {
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT COUNT(*)
FROM collages_artists
WHERE CollageID = '$CollageID'
AND UserID = '$LoggedUser[ID]'");
2013-05-25 08:01:03 +00:00
list($GroupsForUser) = $DB->next_record();
if (!check_perms('site_collages_delete') && $GroupsForUser >= $MaxGroupsPerUser) {
error(403);
}
}
if ($_REQUEST['action'] == 'add_artist') {
2013-06-28 08:01:04 +00:00
$Val->SetFields('url', '1', 'regex', 'The URL must be a link to a artist on the site.', array('regex' => '/^'.ARTIST_REGEX.'/i'));
2013-05-25 08:01:03 +00:00
$Err = $Val->ValidateForm($_POST);
if ($Err) {
error($Err);
}
$URL = $_POST['url'];
// Get artist ID
2013-06-11 08:01:24 +00:00
preg_match('/^'.ARTIST_REGEX.'/i', $URL, $Matches);
$ArtistID = $Matches[4];
2013-10-30 08:01:19 +00:00
if (!$ArtistID || (int)$ArtistID === 0) {
2013-05-25 08:01:03 +00:00
error(404);
}
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT ArtistID
FROM artists_group
WHERE ArtistID = '$ArtistID'");
2013-05-25 08:01:03 +00:00
list($ArtistID) = $DB->next_record();
if (!$ArtistID) {
error('The artist was not found in the database.');
}
add_artist($CollageID, $ArtistID);
} else {
2013-06-28 08:01:04 +00:00
$URLs = explode("\n", $_REQUEST['urls']);
2013-05-25 08:01:03 +00:00
$ArtistIDs = array();
$Err = '';
foreach ($URLs as $Key => &$URL) {
$URL = trim($URL);
if ($URL == '') {
unset($URLs[$Key]);
}
}
unset($URL);
if (!check_perms('site_collages_delete')) {
if ($MaxGroups > 0 && ($NumTorrents + count($URLs) > $MaxGroups)) {
$Err = "This collage can only hold $MaxGroups artists.";
}
if ($MaxGroupsPerUser > 0 && ($GroupsForUser + count($URLs) > $MaxGroupsPerUser)) {
$Err = "You may only have $MaxGroupsPerUser artists in this collage.";
}
}
foreach ($URLs as $URL) {
$Matches = array();
2013-06-11 08:01:24 +00:00
if (preg_match('/^'.ARTIST_REGEX.'/i', $URL, $Matches)) {
$ArtistIDs[] = $Matches[4];
$ArtistID = $Matches[4];
2013-05-25 08:01:03 +00:00
} else {
$Err = "One of the entered URLs ($URL) does not correspond to an artist on the site.";
break;
}
2013-06-28 08:01:04 +00:00
$DB->query("
SELECT ArtistID
FROM artists_group
WHERE ArtistID = '$ArtistID'");
2013-07-10 00:08:53 +00:00
if (!$DB->has_results()) {
2013-05-25 08:01:03 +00:00
$Err = "One of the entered URLs ($URL) does not correspond to an artist on the site.";
break;
}
}
if ($Err) {
error($Err);
}
foreach ($ArtistIDs as $ArtistID) {
add_artist($CollageID, $ArtistID);
}
}
2013-10-30 08:01:19 +00:00
header("Location: collages.php?id=$CollageID");