Example #1
0
function set_attribute($ttid, $attribute, $value)
{
    // NOTE:
    // This will set value for an EXISTING attribute to 0, but it will NOT create a NEW attribute
    // when $value is 0. This is because 0 is meant to represent 'no attribute'.
    // This keeps the table size down and ALSO means import functions
    // can cause new tracks to be added just by tring to set Rating to 0.
    global $album_created, $artist_created, $returninfo;
    // We're setting an attribute.
    // If we're setting it on a hidden track we have to:
    // 1. Work out if this will cause a new artist and/or album to appear in the collection
    // 2. Unhide the track
    $unhidden = false;
    if (track_is_hidden($ttid)) {
        $unhidden = true;
        debuglog("Setting attribute on a hidden track", "MYSQL", 6);
        if ($artist_created == false && $prefs['sortcollectionby'] == 'artist') {
            // See if this means we're revealing a new artist
            if ($result = generic_sql_query("SELECT COUNT(AlbumArtistindex) AS num FROM Albumtable LEFT JOIN Tracktable USING\n\t\t\t\t(Albumindex) WHERE AlbumArtistindex IN\n\t\t\t\t(SELECT AlbumArtistindex FROM Albumtable JOIN Tracktable USING (Albumindex)\n\t\t\t\tWHERE TTindex = " . $ttid . ") AND Hidden = 0 AND Uri IS NOT NULL")) {
                $obj = $result->fetch(PDO::FETCH_OBJ);
                if ($obj->num == 0) {
                    if ($result = generic_sql_query("SELECT AlbumArtistindex FROM Tracktable LEFT JOIN\n\t\t\t\t\t\tAlbumtable USING (Albumindex) WHERE TTindex = " . $ttid)) {
                        while ($obj = $result->fetch(PDO::FETCH_OBJ)) {
                            $artist_created = $obj->AlbumArtistindex;
                            debuglog("Revealing Artist Index " . $artist_created, "MYSQL", 6);
                        }
                    }
                }
            }
        }
        if ($artist_created == false && $album_created == false) {
            // See if this means we're revealing a new album
            if ($result = generic_sql_query("SELECT COUNT(TTindex) AS num FROM Tracktable WHERE Albumindex = (SELECT Albumindex " . "FROM Tracktable WHERE TTindex = " . $ttid . ") AND Hidden = 0 AND Uri IS NOT NULL")) {
                $obj = $result->fetch(PDO::FETCH_OBJ);
                if ($obj->num == 0) {
                    if ($result = generic_sql_query("SELECT Albumindex FROM Tracktable WHERE TTindex = " . $ttid)) {
                        while ($obj = $result->fetch(PDO::FETCH_OBJ)) {
                            $album_created = $obj->Albumindex;
                            debuglog("Revealing Album Index " . $album_created, "MYSQL", 6);
                        }
                    }
                }
            }
        }
        generic_sql_query("UPDATE Tracktable SET Hidden=0 WHERE TTindex=" . $ttid);
    }
    // Similarly, if it's a search result of type 2, it needs to become a type 1
    if (track_is_searchresult($ttid)) {
        $unhidden = true;
        debuglog("Setting attribute on a search result track", "MYSQL", 6);
        if ($artist_created == false && $album_created == false && $prefs['sortcollectionby'] == 'artist') {
            // See if this means we're revealing a new artist
            if ($result = generic_sql_query("SELECT COUNT(AlbumArtistindex) AS num FROM Albumtable\n\t\t\t\tLEFT JOIN Tracktable USING (Albumindex) WHERE AlbumArtistindex IN\n\t\t\t\t(SELECT AlbumArtistindex FROM Albumtable JOIN Tracktable USING (Albumindex) WHERE\n\t\t\t\tTTindex = " . $ttid . ") AND Hidden = 0 AND Uri IS NOT NULL AND isSearchResult < 2")) {
                $obj = $result->fetch(PDO::FETCH_OBJ);
                if ($obj->num == 0) {
                    if ($result = generic_sql_query("SELECT AlbumArtistindex FROM Tracktable LEFT JOIN\n\t\t\t\t\t\tAlbumtable USING (Albumindex) WHERE TTindex = " . $ttid)) {
                        while ($obj = $result->fetch(PDO::FETCH_OBJ)) {
                            $artist_created = $obj->AlbumArtistindex;
                            debuglog("Revealing Artist Index " . $artist_created, "MYSQL", 6);
                        }
                    }
                }
            }
        }
        if ($artist_created == false && $album_created == false) {
            // See if this means we're revealing a new album
            if ($result = generic_sql_query("SELECT COUNT(TTindex) AS num FROM Tracktable WHERE\n\t\t\t\tAlbumindex = (SELECT Albumindex FROM Tracktable WHERE TTindex = " . $ttid . ") AND\n\t\t\t\tHidden = 0 AND Uri IS NOT NULL AND isSearchResult < 2")) {
                $obj = $result->fetch(PDO::FETCH_OBJ);
                if ($obj->num == 0) {
                    if ($result = generic_sql_query("SELECT Albumindex FROM Tracktable WHERE TTindex = " . $ttid)) {
                        while ($obj = $result->fetch(PDO::FETCH_OBJ)) {
                            $album_created = $obj->Albumindex;
                            debuglog("Revealing Album Index " . $album_created, "MYSQL", 6);
                        }
                    }
                }
            }
        }
        // NOTE we must set LastModified to NULL if it's a search result, otherwise
        // we don't get the cross next to it and a collection update will remove it.
        generic_sql_query("UPDATE Tracktable SET isSearchResult = 1,\n\t\t\tLastModified = NULL WHERE TTindex=" . $ttid);
    }
    if ($unhidden) {
        if ($result = generic_sql_query("SELECT Uri, Albumindex, AlbumArtistindex FROM\n\t\t\tTracktable JOIN Albumtable USING (Albumindex) WHERE Tracktable.TTindex = " . $ttid)) {
            $obj = $result->fetch(PDO::FETCH_OBJ);
            $returninfo['displaynewtrack'] = array('artistindex' => $obj->AlbumArtistindex, 'albumindex' => $obj->Albumindex, 'trackuri' => rawurlencode($obj->Uri));
        }
    }
    if ($attribute == 'Tags') {
        return addTags($ttid, $value);
    } else {
        debuglog("Setting " . $attribute . " to " . $value . " on " . $ttid, "MYSQL", 8);
        if ($stmt = sql_prepare_query("UPDATE " . $attribute . "table SET " . $attribute . "=? WHERE TTindex=?", $value, $ttid)) {
            if ($stmt->rowCount() == 0 && $value !== 0) {
                debuglog("  Update affected 0 rows, creating new value", "MYSQL", 8);
                if ($stmt = sql_prepare_query("INSERT INTO " . $attribute . "table (TTindex, " . $attribute . ") VALUES (?, ?)", $ttid, $value)) {
                    debuglog("    New Value Created", "MYSQL", 8);
                } else {
                    // NOTE - we could get here if the attribute we are setting already exists
                    // (eg setting Rating to 5 on a track that already has rating set to 5).
                    // We don't check that because the database is set up such that this
                    // can't happen twice - because the rating table uses TWO indices to keep things unique.
                    // Hence an error here is probably not a problem, so we ignore them.
                    // debuglog("  Error Executing mySql", "MYSQL");
                }
            }
        } else {
            return false;
        }
        return true;
    }
}
Example #2
0
 // We don't simply call into this using 'set' with urionly set to true
 // because that might result in the rating being changed
 $ttids = find_item($uri, $title, $artist, $album, $albumartist, true);
 // As we check by URI we can only have one result.
 $ttid = null;
 if (count($ttids) > 0) {
     $ttid = array_shift($ttids);
 }
 if ($ttid != null) {
     // If we found it, just make sure it's not hidden.
     // This is slightly trickier than it sounds because if it is it might cause a new album
     // and/or artist to appear in the collection when we unhide it.
     // The code to do this already exists in set_attribute.
     // If it's hidden it will have no attributes (except a playcount) so we can check if
     // it's hidden and if it is, set its rating to 0.
     if (track_is_hidden($ttid) || track_is_searchresult($ttid)) {
         set_attribute($ttid, "Rating", "0");
         update_track_stats();
         send_list_updates($artist_created, $album_created, $ttid);
     } else {
         if ($uri && $album) {
             if (check_wishlist_doodads(array($ttid))) {
                 update_track_stats();
                 send_list_updates($artist_created, $album_created, $ttid);
             }
         }
     }
 } else {
     check_album_image();
     $ttid = create_new_track($title, $artist, $trackno, $duration, $albumartist, $albumuri, $image, $album, $date, $uri, null, null, null, null, md5($albumartist . " " . $album), null, $disc, null, $uri === null ? "local" : getDomain($uri), 0, $trackimage, 0);
     update_track_stats();