album-add.php 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php

/* GNU FM -- a free network service for sharing your music listening habits

   Copyright (C) 2009 Free Software Foundation, Inc

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

require_once('database.php');
require_once('templating.php');
require_once('data/Album.php');

26
try {
27
	$artist = new Artist($_GET['artist']);
28
} catch (Exception $e) {
29 30
	displayError("Artist not found",
		"The artist {$_GET['artist']} was not found in the database.");
31
}
32

33
if (!isset($this_user) || !$this_user->manages($artist->name)) {
34 35
	displayError("Permission denied",
		"You don't have permission to edit this artist's details.");
36 37
}

38 39 40 41
$edit = false;
if (isset($_GET['album'])) {
	$edit = true;

Jonas Haraldsson's avatar
Jonas Haraldsson committed
42 43 44
	try {
		$album = new Album($_GET['album'], $artist->name);
	} catch (Exception $e) {
45 46
		displayError("Album not found",
			"The album {$_GET['album']} by artist {$artist->name} was not found in the database.");
Jonas Haraldsson's avatar
Jonas Haraldsson committed
47 48 49
	}
	
}
50

51
$smarty->assign('artist', $artist);
52 53 54 55 56 57 58 59 60
$smarty->assign('edit', $edit);
if ($edit) {
	$name = $album->name;
	$smarty->assign('name', $name);
	$smarty->assign('image', $album->image);
	$smarty->assign('pageheading', '<a href="' . $artist->getURL() . '">' . $artist->name . '</a> &mdash; Edit Album');
} else {
	$smarty->assign('pageheading', '<a href="' . $artist->getURL() . '">' . $artist->name . '</a> &mdash; Add Album');
}
61 62 63

if (isset($_POST['submit'])) {

64 65 66 67 68
	if(!$edit) {
		if (empty($_POST['name'])) {
			$errors[] = 'An album name must be specified.';
		}
		$name = $_POST['name'];
69 70 71 72
	}

	if (empty($_POST['image'])) {
		$image = '';
73
	} else if (!preg_match('/^[a-z0-9\+\.\-]+\:/i', $_POST['image'])) {
74
		$errors[] = 'Cover image must be a valid URL';
75
	} else if (preg_match('/\s/', $_POST['homepage'])) {
76 77 78 79 80
		$errors[] = 'Cover image must be a URL, as such it cannot contain whitespace.';
	} else {
		$image = $_POST['image'];
	}

81
	if ($errors) {
82 83 84 85
		$smarty->assign('errors', $errors);
		$smarty->assign('image', $image);
		$smarty->assign('name', $_POST['name']);
	} else {
86 87 88 89 90
		if($edit) {
			$album->setImage($image);
		} else {
			$album = Album::create($name, $artist->name, $image);
		}
91 92 93 94 95
		// If the creation was successful send the user back to the view page
		header('Location: ' . $album->getURL());
	}
}
$smarty->display('album-add.tpl');