rendered paste body<?php
//include album class
include_once(LIBDIR .'../Artist/Artist.php.class');
class ArtistAdmin {
/**
* Class constructor
*
* @param $db - an instance of the Db class
*
* @return an instance of the AlbumAdmin class
*
**/
function ArtistAdmin($db_obj) {
// ensure we have logger access
global $logger;
if(strtolower(get_class($logger)) != 'logger') {
die(__FILE__. "FATAL ERROR: no valid global logger object is instantiated - you must have a global \$logger object to continue");
}
// verify our db object
if(!is_object($db_obj) or get_class($db_obj) != 'db') {
$logger->fatal("DB OBJECT PASSED TO " . __FILE__. " CONSTRUCTOR IS INVALID! - CLASS PASSED WAS A " . get_class($db_obj));
}
$this->_db = $db_obj;
return $this;
}
/**
* Class constructor
*
* @param $new_artist_details - array of data to add a new artist
*
* @return an instance of the ArtistAdmin class
*
**/
function createArtist($new_artist_details) {
//Logger
global $logger;
//Error
(bool)$error = false;
// sanity check
(array)$required_param = array( 'name', 'website', 'bio' );
if(!is_array($new_artist_details)) {
$logger->warn("Not a valid array");
return null;
}
foreach($required_param as $param) {
if(empty($new_artist_details[$param])) {
$logger->warn("Required parameter is not set ($new_artist_details[$param]) -- cannot create artist");
return NULL;
}
}
if($error==false) {
// Insert the artist
(object)$res = $this->_db->query("INSERT INTO artists (name, website, bio)
VALUES ('%s', '%s', '%s')",
array ( $new_artist_details['name'],
$new_artist_details['website'],
$new_artist_details['bio']));
if(!$res) {
$logger->warn("Artist insert query failed - " . mysql_error());
print_r("Artist insert query failed - " . mysql_error() ."\n\n");
}
global $new_artist_id;
(integer)$new_artist_id = mysql_insert_id();
// use class artist
$artist_obj = new Artist($this->_db, $new_artist_id);
return $artist_obj ;
}
}
function updateArtist($artist_id, $revised_artist_details) {
//Logger
global $logger;
//Error
(bool)$error = false;
// sanity check
(array)$required_param = array( 'name', 'website', 'bio' );
if(!is_array($revised_artist_details)) {
$logger->warn("Not a valid array");
return null;
}
foreach($required_param as $param) {
if(empty($new_artist_details[$param])) {
$erorrs = true;
$logger->warn("Required parameter is not set ($revised_artist_details[$param]) -- cannot create artist");
return NULL;
}
}
//checks valid artist_id and sets default array value
(object)$sql = $this->_db->query("SELECT
artistid,
name,
website,
bio
FROM artists
WHERE artistid = $artist_id ") or die (mysql_error());
if(!$sql) {
$logger->warn("Artist update query failed - " . mysql_error());
print_r("Artist update query failed - " . mysql_error() ."\n\n");
}
if($sql) {
while (list ($key, $val) = each ($revised_artist_details)){
if ($i > 0){
$fields .= ", ";
$values .= ", ";
$updateList .= ", ";
}
$fields .= $key;
$updateList .= "$key='$val'";
$values .= "'$val'";
$i++;
}
(object)$query = $this->_db->query("update artists set $updateList WHERE artistid = '$artist_id'") or die ("UPDATE ERROR : " . mysql_error());
if(mysql_affected_rows() > 0 )
return 1;
if(mysql_affected_rows() < 1 )
return 0;
}
}
}
?>