propel
*/
private $_dbMD = array (
"track_title" => "DbTrackTitle",
"artist_name" => "DbArtistName",
"album_title" => "DbAlbumTitle",
"genre" => "DbGenre",
"mood" => "DbMood",
"track_number" => "DbTrackNumber",
"bpm" => "DbBpm",
"label" => "DbLabel",
"composer" => "DbComposer",
"encoded_by" => "DbEncodedBy",
"conductor" => "DbConductor",
"year" => "DbYear",
"info_url" => "DbInfoUrl",
"isrc_number" => "DbIsrcNumber",
"copyright" => "DbCopyright",
"length" => "DbLength",
"bit_rate" => "DbBitRate",
"sample_rate" => "DbSampleRate",
"mime" => "DbMime",
"md5" => "DbMd5",
"ftype" => "DbFtype",
"language" => "DbLanguage"
);
public function __construct()
{
}
public function getId()
{
return $this->_file->getDbId();
}
public function getGunId() {
return $this->_file->getDbGunid();
}
public function getFormat()
{
return $this->_file->getDbFtype();
}
public function getPropelOrm(){
return $this->_file;
}
public function setFormat($p_format)
{
$this->_file->setDbFtype($p_format);
}
/**
* Set multiple metadata values using defined metadata constants.
*
* @param array $p_md
* example: $p_md['MDATA_KEY_URL'] = 'http://www.fake.com'
*/
public function setMetadata($p_md=null)
{
if (is_null($p_md)) {
$this->setDbColMetadata();
}
else {
$dbMd = array();
foreach ($p_md as $mdConst => $mdValue) {
$dbMd[constant($mdConst)] = $mdValue;
}
$this->setDbColMetadata($dbMd);
}
$this->_file->setDbMtime(new DateTime("now"), new DateTimeZone("UTC"));
$this->_file->save();
}
/**
* Set multiple metadata values using database columns as indexes.
*
* @param array $p_md
* example: $p_md['url'] = 'http://www.fake.com'
*/
public function setDbColMetadata($p_md=null)
{
if (is_null($p_md)) {
foreach ($this->_dbMD as $dbColumn => $propelColumn) {
$method = "set$propelColumn";
$this->_file->$method(null);
}
}
else {
foreach ($p_md as $dbColumn => $mdValue) {
//don't blank out name, defaults to original filename on first insertion to database.
if($dbColumn == "track_title" && (is_null($mdValue) || $mdValue == "")) {
continue;
}
if (isset($this->_dbMD[$dbColumn])) {
$propelColumn = $this->_dbMD[$dbColumn];
$method = "set$propelColumn";
$this->_file->$method($mdValue);
}
}
}
$this->_file->setDbMtime(new DateTime("now"), new DateTimeZone("UTC"));
$this->_file->save();
}
/**
* Set metadata element value
*
* @param string $category
* Metadata element by metadata constant
* @param string $value
* value to store, if NULL then delete record
*/
public function setMetadataValue($p_category, $p_value)
{
// constant() was used because it gets quoted constant name value from
// api_client.py. This is the wrapper funtion
$this->setDbColMetadataValue(constant($p_category), $p_value);
}
/**
* Set metadata element value
*
* @param string $category
* Metadata element by db column
* @param string $value
* value to store, if NULL then delete record
*/
public function setDbColMetadataValue($p_category, $p_value)
{
//don't blank out name, defaults to original filename on first insertion to database.
if($p_category == "track_title" && (is_null($p_value) || $p_value == "")) {
return;
}
if (isset($this->_dbMD[$p_category])) {
$propelColumn = $this->_dbMD[$p_category];
$method = "set$propelColumn";
$this->_file->$method($p_value);
$this->_file->save();
}
}
/**
* Get one metadata value.
*
* @param string $p_category (MDATA_KEY_URL)
* @return string
*/
public function getMetadataValue($p_category)
{
// constant() was used because it gets quoted constant name value from
// api_client.py. This is the wrapper funtion
return $this->getDbColMetadataValue(constant($p_category));
}
/**
* Get one metadata value.
*
* @param string $p_category (url)
* @return string
*/
public function getDbColMetadataValue($p_category)
{
$propelColumn = $this->_dbMD[$p_category];
$method = "get$propelColumn";
return $this->_file->$method();
}
/**
* Get metadata as array, indexed by the column names in the database.
*
* @return array
*/
public function getDbColMetadata()
{
$md = array();
foreach ($this->_dbMD as $dbColumn => $propelColumn) {
$method = "get$propelColumn";
$md[$dbColumn] = $this->_file->$method();
}
return $md;
}
/**
* Get metadata as array, indexed by the constant names.
*
* @return array
*/
public function getMetadata()
{
$c = get_defined_constants(true);
$md = array();
foreach ($c['user'] as $constant => $value) {
if (preg_match('/^MDATA_KEY/', $constant)) {
if (isset($this->_dbMD[$value])) {
$md[$constant] = $this->getDbColMetadataValue($value);
}
}
}
return $md;
}
/**
* Set state of virtual file
*
* @param string $p_state
* 'empty'|'incomplete'|'ready'|'edited'
* @param int $p_editedby
* user id | 'NULL' for clear editedBy field
* @return TRUE|PEAR_Error
*/
public function setState($p_state, $p_editedby=NULL)
{
global $CC_CONFIG, $CC_DBC;
$escapedState = pg_escape_string($p_state);
$eb = (!is_null($p_editedby) ? ", editedBy=$p_editedby" : '');
$sql = "UPDATE ".$CC_CONFIG['filesTable']
." SET state='$escapedState'$eb, mtime=now()"
." WHERE gunid='{$this->gunid}'";
$res = $CC_DBC->query($sql);
if (PEAR::isError($res)) {
return $res;
}
$this->state = $p_state;
$this->editedby = $p_editedby;
return TRUE;
}
/**
* Returns an array of playlist objects that this file is a part of.
* @return array
*/
public function getPlaylists() {
global $CC_CONFIG, $CC_DBC;
$sql = "SELECT playlist_id "
." FROM ".$CC_CONFIG['playistTable']
." WHERE file_id='{$this->id}'";
$ids = $CC_DBC->getAll($sql);
$playlists = array();
if (is_array($ids) && count($ids) > 0) {
foreach ($ids as $id) {
$playlists[] = Application_Model_Playlist::Recall($id);
}
}
return $playlists;
}
/**
* Delete stored virtual file
*
* @param boolean $p_deleteFile
*
*/
public function delete($deleteFromPlaylist=false)
{
$filepath = $this->getFilePath();
// Check if the file is scheduled to be played in the future
if (Application_Model_Schedule::IsFileScheduledInTheFuture($this->getId())) {
throw new DeleteScheduledFileException();
}
if (file_exists($filepath)) {
$data = array("filepath" => $filepath, "delete" => 1);
Application_Model_RabbitMq::SendMessageToMediaMonitor("file_delete", $data);
}
if ($deleteFromPlaylist){
Application_Model_Playlist::DeleteFileFromAllPlaylists($this->getId());
}
// set file_exists falg to false
$this->_file->setDbFileExists(false);
$this->_file->save();
}
/**
* Return suitable extension.
*
* @return string
* file extension without a dot
*/
public function getFileExtension()
{
$mime = $this->_file->getDbMime();
if ($mime == "audio/vorbis" || $mime == "application/ogg") {
return "ogg";
}
else if ($mime == "audio/mp3" || $mime == "audio/mpeg") {
return "mp3";
}
}
/**
* Get real filename of raw media data
*
* @return string
*/
public function getFilePath()
{
$music_dir = Application_Model_MusicDir::getDirByPK($this->_file->getDbDirectory());
$directory = $music_dir->getDirectory();
$filepath = $this->_file->getDbFilepath();
return $directory.$filepath;
}
/**
* Set real filename of raw media data
*
* @return string
*/
public function setFilePath($p_filepath)
{
$path_info = Application_Model_MusicDir::splitFilePath($p_filepath);
if (is_null($path_info)) {
return -1;
}
$musicDir = Application_Model_MusicDir::getDirByPath($path_info[0]);
$this->_file->setDbDirectory($musicDir->getId());
$this->_file->setDbFilepath($path_info[1]);
$this->_file->save();
}
/**
* Get the URL to access this file using the server name/address that
* this PHP script was invoked through.
*/
public function getFileUrl()
{
$serverName = $_SERVER['SERVER_NAME'];
$serverPort = $_SERVER['SERVER_PORT'];
return $this->constructGetFileUrl($serverName, $serverPort);
}
/**
* Get the URL to access this file using the server name/address that
* is specified in the airtime.conf config file. If either of these is
* not specified, then use values provided by the $_SERVER global variable.
*/
public function getFileUrlUsingConfigAddress(){
global $CC_CONFIG;
if (isset($CC_CONFIG['baseUrl'])){
$serverName = $CC_CONFIG['baseUrl'];
} else {
$serverName = $_SERVER['SERVER_NAME'];
}
if (isset($CC_CONFIG['basePort'])){
$serverPort = $CC_CONFIG['basePort'];
} else {
$serverPort = $_SERVER['SERVER_PORT'];
}
return $this->constructGetFileUrl($serverName, $serverPort);
}
private function constructGetFileUrl($p_serverName, $p_serverPort){
Logging::log("getting media! - 2");
return "http://$p_serverName:$p_serverPort/api/get-media/file/".$this->getGunId().".".$this->getFileExtension();
}
/**
* Sometimes we want a relative URL and not a full URL. See bug
* http://dev.sourcefabric.org/browse/CC-2403
*/
public function getRelativeFileUrl($baseUrl)
{
Logging::log("getting media!");
return $baseUrl."/api/get-media/file/".$this->getGunId().".".$this->getFileExtension();
}
public static function Insert($md=null)
{
$file = new CcFiles();
$file->setDbGunid(md5(uniqid("", true)));
$file->setDbUtime(new DateTime("now"), new DateTimeZone("UTC"));
$file->setDbMtime(new DateTime("now"), new DateTimeZone("UTC"));
$storedFile = new Application_Model_StoredFile();
$storedFile->_file = $file;
if(isset($md['MDATA_KEY_FILEPATH'])) {
// removed "//" in the path. Always use '/' for path separator
$filepath = str_replace("//", "/", $md['MDATA_KEY_FILEPATH']);
$res = $storedFile->setFilePath($filepath);
if ($res === -1) {
return null;
}
}
else {
return null;
}
if(isset($md)) {
$storedFile->setMetadata($md);
}
return $storedFile;
}
/**
* Fetch instance of StoreFile object.
* Should be supplied with only ONE parameter, all the rest should
* be NULL.
*
* @param int $p_id
* local id
* @param string $p_gunid
* global unique id of file
* @param string $p_md5sum
* MD5 sum of the file
* @return Application_Model_StoredFile|NULL
* Return NULL if the object doesnt exist in the DB.
*/
public static function Recall($p_id=null, $p_gunid=null, $p_md5sum=null, $p_filepath=null)
{
if (isset($p_id)) {
$file = CcFilesQuery::create()->findPK(intval($p_id));
}
else if (isset($p_gunid)) {
$file = CcFilesQuery::create()
->filterByDbGunid($p_gunid)
->findOne();
}
else if (isset($p_md5sum)) {
$file = CcFilesQuery::create()
->filterByDbMd5($p_md5sum)
->findOne();
}
else if (isset($p_filepath)) {
$path_info = Application_Model_MusicDir::splitFilePath($p_filepath);
if (is_null($path_info)) {
return null;
}
$music_dir = Application_Model_MusicDir::getDirByPath($path_info[0]);
$file = CcFilesQuery::create()
->filterByDbDirectory($music_dir->getId())
->filterByDbFilepath($path_info[1])
->findOne();
}
else {
return null;
}
if (isset($file)) {
$storedFile = new Application_Model_StoredFile();
$storedFile->_file = $file;
return $storedFile;
}
else {
return null;
}
}
public function getName(){
$info = pathinfo($this->getFilePath());
return $info['filename'];
}
/**
* Create instance of StoreFile object and recall existing file
* by gunid.
*
* @param string $p_gunid
* global unique id of file
* @return Application_Model_StoredFile|NULL
*/
public static function RecallByGunid($p_gunid)
{
return Application_Model_StoredFile::Recall(null, $p_gunid);
}
/**
* Fetch the Application_Model_StoredFile by looking up the MD5 value.
*
* @param string $p_md5sum
* @return Application_Model_StoredFile|NULL
*/
public static function RecallByMd5($p_md5sum)
{
return Application_Model_StoredFile::Recall(null, null, $p_md5sum);
}
/**
* Fetch the Application_Model_StoredFile by looking up its filepath.
*
* @param string $p_filepath path of file stored in Airtime.
* @return Application_Model_StoredFile|NULL
*/
public static function RecallByFilepath($p_filepath)
{
return Application_Model_StoredFile::Recall(null, null, null, $p_filepath);
}
public static function RecallByPartialFilepath($partial_path){
$path_info = Application_Model_MusicDir::splitFilePath($partial_path);
if (is_null($path_info)) {
return null;
}
$music_dir = Application_Model_MusicDir::getDirByPath($path_info[0]);
$files = CcFilesQuery::create()
->filterByDbDirectory($music_dir->getId())
->filterByDbFilepath("$path_info[1]%")
->find();
$res = array();
foreach ($files as $file){
$storedFile = new Application_Model_StoredFile();
$storedFile->_file = $file;
$res[] = $storedFile;
}
return $res;
}
public static function searchFilesForPlaylistBuilder($datatables) {
$displayColumns = array("id", "track_title", "artist_name", "album_title", "genre", "length",
"year", "utime", "mtime", "ftype", "track_number", "mood", "bpm", "composer", "info_url",
"bit_rate", "sample_rate", "isrc_number", "encoded_by", "label", "copyright", "mime",
"language", "gunid", "filepath"
);
$plSelect = array();
$fileSelect = array();
foreach ($displayColumns as $key) {
if ($key === "id") {
$plSelect[] = "PL.id AS ".$key;
$fileSelect[] = $key;
}
else if ($key === "track_title") {
$plSelect[] = "name AS ".$key;
$fileSelect[] = $key;
}
else if ($key === "ftype") {
$plSelect[] = "'playlist'::varchar AS ".$key;
$fileSelect[] = $key;
}
else if ($key === "artist_name") {
$plSelect[] = "login AS ".$key;
$fileSelect[] = $key;
}
//same columns in each table.
else if(in_array($key, array("length", "utime", "mtime"))) {
$plSelect[] = $key;
$fileSelect[] = $key;
}
else if ($key === "year") {
$plSelect[] = "EXTRACT(YEAR FROM utime)::varchar AS ".$key;
$fileSelect[] = "EXTRACT(YEAR FROM to_date(year, 'YYYY-MM-DD'))::varchar AS ".$key;
}
//need to cast certain data as ints for the union to search on.
else if (in_array($key, array("track_number", "bit_rate", "sample_rate"))){
$plSelect[] = "NULL::int AS ".$key;
$fileSelect[] = $key;
}
else {
$plSelect[] = "NULL::text AS ".$key;
$fileSelect[] = $key;
}
}
$plSelect = "SELECT ". join(",", $plSelect);
$fileSelect = "SELECT ". join(",", $fileSelect);
$type = intval($datatables["type"]);
$plTable = "({$plSelect} FROM cc_playlist AS PL LEFT JOIN cc_subjs AS sub ON (sub.id = PL.creator_id))";
$fileTable = "({$fileSelect} FROM cc_files AS FILES WHERE file_exists = 'TRUE')";
$unionTable = "({$plTable} UNION {$fileTable} ) AS RESULTS";
//choose which table we need to select data from.
switch ($type) {
case 0:
$fromTable = $unionTable;
break;
case 1:
$fromTable = $fileTable." AS File"; //need an alias for the table if it's standalone.
break;
case 2:
$fromTable = $plTable." AS Playlist"; //need an alias for the table if it's standalone.
break;
default:
$fromTable = $unionTable;
}
$results = Application_Model_StoredFile::searchFiles($displayColumns, $fromTable, $datatables);
//Used by the audio preview functionality in the library.
foreach ($results['aaData'] as &$row) {
$row['id'] = intval($row['id']);
$formatter = new LengthFormatter($row['length']);
$row['length'] = $formatter->format();
if ($row['ftype'] === "audioclip") {
$formatter = new SamplerateFormatter($row['sample_rate']);
$row['sample_rate'] = $formatter->format();
$formatter = new BitrateFormatter($row['bit_rate']);
$row['bit_rate'] = $formatter->format();
}
// add checkbox row
$row['checkbox'] = "";
$type = substr($row['ftype'], 0, 2);
$row['tr_id'] = "{$type}_{$row['id']}";
//TODO url like this to work on both playlist/showbuilder screens.
//datatable stuff really needs to be pulled out and generalized within the project
//access to zend view methods to access url helpers is needed.
if($type == "au"){//&& isset( $audioResults )) {
$row['audioFile'] = $row['gunid'].".".pathinfo($row['filepath'], PATHINFO_EXTENSION);
$row['image'] = '