get_catalog_files(); /* Foreach through each file and find it a home! */ foreach ($songs as $song) { /* Find this poor song a home */ $song->format_song(); $directory = sort_find_home($song,$catalog->sort_pattern,$catalog->path); $filename = sort_find_filename($song,$catalog->rename_pattern); echo $directory . "/" . $filename . "\n"; flush(); } // end foreach song } // end foreach catalogs /************** FUNCTIONS *****************/ /** * sort_find_filename * This gets the filename that this file should have, it takes the rename pattern of the catalog * along with the song object. Nothing Special Here */ function sort_find_filename($song,$rename_pattern) { /* Create the filename that this file should have */ $album = $song->f_album_full; $artist = $song->f_artist_full; $genre = $song->f_genre; $track = $song->track; $title = $song->title; $year = $song->year; /* Start replacing stuff */ $replace_array = array('%a','%A','%t','%T','%y','%g'); $content_array = array($artist,$album,$title,$track,$year,$genre); $rename_pattern = str_replace($replace_array,$content_array,$rename_pattern); $rename_pattern = preg_replace("/[^A-Za-z0-9\-\_\ \'\,]/","_",$rename_pattern); return $rename_pattern; } // sort_find_filename /** * sort_find_home * Get the directory for this file from the catalog and the song info using the sort_pattern * takes into account various artists and the alphabet_prefix */ function sort_find_home($song,$sort_pattern,$base) { $home = rtrim($base,"\/"); $home = rtrim($home,"\\"); /* Create the filename that this file should have */ $album = $song->f_album_full; $artist = $song->f_artist_full; $genre = $song->f_genre; $track = $song->track; $title = $song->title; $year = $song->year; /* Do the various check */ $album_object = new Album($song->album); if ($album_object->artist_count != '1') { $artist = "Various"; } /* IF we want a,b,c,d we need to know the first element */ if ($GLOBALS['alphabet_prefix']) { $sort_pattern = preg_replace("/\/?%o\//","",$sort_pattern); $first_element = substr($sort_pattern,0,2); $element = sort_element_name($first_element); $alphabet = strtoupper(substr(${$element},0,1)); $alphabet = preg_replace("/[^A-Za-z0-9]/","ZZ",$alphabet); $home .= "/$alphabet"; } /* Replace everything we can find */ $replace_array = array('%a','%A','%t','%T','%y','%g'); $content_array = array($artist,$album,$title,$track,$year,$genre); $sort_pattern = str_replace($replace_array,$content_array,$sort_pattern); /* Remove non A-Z0-9 chars */ $sort_pattern = preg_replace("/[^A-Za-z0-9\-\_\ \'\,]/","_",$sort_pattern); $home .= "/$sort_pattern"; return $home; } // sort_find_home /** * sort_element_name * gets the name of the %? in a yea.. too much beer */ function sort_element_name($key) { switch ($key) { case '%t': return 'title'; break; case '%T': return 'track'; break; case '%a': return 'artist'; break; case '%A': return 'album'; break; case '%y': return 'year'; break; case '%g': return 'genre'; break; default: break; } // switch on key return false; } // sort_element_name ?>