e_MEDIA_FILE,
'multipart' => e_MEDIA_FILE,
'application' => e_MEDIA_FILE,
// 'audio' => e_MEDIA_AUDIO,
'image' => e_MEDIA_IMAGE,
'video' => e_MEDIA_VIDEO,
'other' => e_MEDIA_FILE
);
/**
* Import files from specified path into media database.
* @param string $cat Category nickname
* @param string $epath path to file.
* @param string $fmask [optional] filetypes eg. .jpg|.gif IMAGES is the default mask.
* @return e_media
*/
public function import($cat='',$epath,$fmask='',$options=array())
{
if(!vartrue($cat)){ return $this;}
if(is_string($options))
{
parse_str($options,$options);
}
if(!is_readable($epath))
{
return $this;
}
$fl = e107::getFile();
$tp = e107::getParser();
$sql = e107::getDb();
$mes = e107::getMessage();
$fl->setFileInfo('all');
if(!$fmask)
{
$fmask = '[a-zA-z0-9_-]+\.(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF)$';
}
$img_array = $fl->get_files($epath,$fmask,'',2);
if(!count($img_array))
{
e107::getMessage()->addDebug("Media-Import could not find any files in ".$epath." with fmask: ".$fmask);
return $this;
}
// print_a($img_array);
// return;
$count = 0;
foreach($img_array as $f)
{
if($f['fsize'] === 0) // prevent zero-byte files.
{
continue;
}
if(vartrue($options['min-width']) && ($f['img-width'] < $options['min-width']))
{
continue;
}
if(vartrue($options['min-size']) && ($f['fsize'] < $options['min-size']))
{
continue;
}
$fullpath = $tp->createConstants($f['path'].$f['fname'],1);
// echo "
cat=".$cat;
$insert = array(
'media_caption' => $f['fname'],
'media_description' => '',
'media_category' => $cat,
'media_datestamp' => $f['modified'],
'media_url' => $fullpath,
'media_userclass' => '0',
'media_name' => $f['fname'],
'media_author' => USERID,
'media_size' => $f['fsize'],
'media_dimensions' => vartrue($f['img-width']) ? $f['img-width']." x ".$f['img-height'] : "",
'media_usedby' => '',
'media_tags' => '',
'media_type' => $f['mime']
);
if(!$sql->db_Select('core_media','media_url',"media_url = '".$fullpath."' LIMIT 1"))
{
if($sql->db_Insert("core_media",$insert))
{
$count++;
$mes->addDebug("Imported Media: ".$f['fname']);
}
else
{
$mes->addError("Media not imported: ".$f['fname']);
}
}
}
if($count)
{
// $mes->addSuccess("Imported {$count} Media items.");
}
return $this;
}
/**
* Import icons into media-manager from specified path.
* @param string $path
* @return e_media
*/
public function importIcons($path)
{
$iconsrch = array(16,32,48,64);
foreach($iconsrch as $size)
{
$types = '[a-zA-z0-9_-]+'.$size.'\.(png|PNG)$';
$this->import('_icon_'.$size, $path, $types);
}
return $this;
}
/**
* Remove Media from media table
* @param string $cat [optional] remove a full category of media
* @return
*/
function removeCat($cat)
{
$tp = e107::getParser();
$sql = e107::getDb();
$mes = e107::getMessage();
if(vartrue($cat))
{
$status = ($sql->db_Delete('core_media',"media_cat = '".$cat."'")) ? TRUE : FALSE;
$mes->add("Removing Media in Category: ".$cat, E_MESSAGE_DEBUG);
return $status;
}
}
/**
* Remove Media from media table
* @param string $epath remove media in the specified path.
* @param string $type [optional] image|icon
* @return
*/
function removePath($epath, $type='image')
{
$tp = e107::getParser();
$sql = e107::getDb();
$mes = e107::getMessage();
$qry = ($type == 'icon') ? " AND media_category REGEXP '_icon_16|_icon_32|_icon_48|_icon_64' " : " AND NOT media_category REGEXP '_icon_16|_icon_32|_icon_48|_icon_64' ";
if(vartrue($epath))
{
$path = $tp->createConstants($epath, 'rel');
$status = ($sql->db_Delete('core_media',"media_url LIKE '".$path."%'".$qry)) ? TRUE : FALSE;
$message = ($type == 'image') ? "Removing Media with path: ".$path : "Removing Icons with path: ".$path;
$mes->add($message, E_MESSAGE_DEBUG);
return $status;
}
}
/**
* Return a list if icons in the specified path
* @param string $epath
* @return array
*/
function listIcons($epath)
{
if(!$epath) return;
$ret = array();
$sql = e107::getDb();
$tp = e107::getParser();
$path = $tp->createConstants($epath, 'rel');
$status = ($sql->db_Select_gen("SELECT * FROM `#core_media` WHERE `media_url` LIKE '".$path."%' AND media_category REGEXP '_icon_16|_icon_32|_icon_48|_icon_64' ")) ? TRUE : FALSE;
while ($row = $sql->db_Fetch())
{
$ret[] = $row['media_url'];
}
return $ret;
}
/**
* Create media category.
* 'class' data is optional, 'id' key is ignored
*
* @param array $data associative array, db keys should be passed without the leading 'media_cat_' e.g. 'class', 'type', etc.
* @return integer last inserted ID or false on error
*/
public function createCategory($datas)
{
foreach ($datas as $k => $v)
{
$data['media_cat_'.$k] = $v;
}
$data['media_cat_id'] = 0;
if(!isset($data['media_cat_class']) || '' === $data['media_cat_class'])
{
$data['media_cat_class'] = defset('e_UC_MEMBER', 253);
}
return e107::getDb()->db_Insert('core_media_cat', $data);
}
/**
* Create a user Media-Category.
* @param $type string image | file | video
* @param $userId int - leave empty for currently logged in user.
* @param $userName string - leave blank for currently logged in user
* @param $parms (optional) - for future use.
*/
public function createUserCategory($type='image', $userId = USERID, $userName = USERNAME, $parms=null)
{
if($type !='image' && $type='file' && $type !='video')
{
return false;
}
$cat = 'user_'.$type.'_'.intval($userId);
if(!e107::getDb()->gen('SELECT media_cat_id FROM #core_media_cat WHERE media_cat_category = "'.$cat.'" LIMIT 1'))
{
$insert = array(
'owner' => 'user',
'category' => $cat,
'title' => $userName,
'sef' => 'media-'.eHelper::title2sef($userName),
'diz' => '',
'class' => '',
'image' => '',
'order' => ''
);
return $this->createCategory($insert);
}
return false;
}
/**
* Create multiple media categories in once
* @param array $data
* @return integer number of successfully inserted records
*/
public function createCategories($multi_data)
{
$cnt = 0;
foreach ($multi_data as $cats)
{
if($this->createCategory($cats)) $cnt++;
}
return $cnt;
}
public function deleteCategory($id)
{
// TODO
}
public function deleteAllCategories($owner='')
{
if($owner == '')
{
return;
}
$sql = e107::getDb();
$sql->db_Select('core_media_cat',"media_cat_category", "media_cat_owner = '".$owner."' ");
while($row = $sql->db_Fetch())
{
$categories[] = "'".$row['media_cat_category']."'";
}
if($sql->db_Delete('core_media_cat', "media_cat_owner = '".$owner."' "))
{
//TODO retrieve all category names for owner, and reset all media categories to _common.
return TRUE;
// return $sql->db_Update('core_media', "media_category = '_common_image' WHERE media_category IN (".implode(",",$categories).")");
}
return FALSE;
}
/**
* Return an Array of Media Categories
*/
public function getCategories($owner='')
{
$ret = array();
$qry = "SELECT * FROM #core_media_cat ";
$qry .= ($owner) ? " WHERE media_cat_owner = '".$owner."' " : " (1) ";
$qry .= "AND media_cat_class IN (".USERCLASS_LIST.") ";
$qry .= "ORDER BY media_cat_order";
e107::getDb()->gen($qry);
while($row = e107::getDb()->db_Fetch(MYSQL_ASSOC))
{
$id = $row['media_cat_category'];
$ret[$id] = $row;
}
return $ret;
}
/**
* Return the total number of Images in a particular category
*
*/
public function countImages($cat,$search=null)
{
return $this->getImages($cat, 0, 'all',$search);
/*
$inc = array();
$searchinc = array();
if(strpos($cat,"+") || !$cat)
{
$cat = str_replace("+","",$cat);
$inc[] = "media_category = '_common_image' ";
}
if($cat)
{
$inc[] = "media_category REGEXP '(^|,)(".$cat.")(,|$)' "; // for multiple category field.
}
if($search)
{
$searchinc[] = "media_name LIKE '%".$search."%' ";
$searchinc[] = "media_description LIKE '%".$search."%' ";
$searchinc[] = "media_caption LIKE '%".$search."%' ";
$searchinc[] = "media_tags LIKE '%".$search."%' ";
}
$query = "SELECT * FROM #core_media WHERE media_userclass IN (".USERCLASS_LIST.") AND ( ".implode(" OR ",$inc)." )" ;
if($search)
{
$query .= " AND ( ".implode(" OR ",$searchinc)." ) " ;
}
return e107::getDb()->db_Select_gen($query);
*/
}
public function getFiles($from=0, $amount = null, $search = null)
{
return $this->getImages('_common_file', $from, $amount, $search);
}
/**
* Return an array of Images in a particular category
* @param string $cat : category name. use + to include _common eg. 'news+'
* @param $from
* @param $amount
* @param $search
*/
public function getImages($cat='', $from=0, $amount=null,$search=null)
{
$inc = array();
$searchinc = array();
if(strpos($cat,"+") || !$cat)
{
$cat = str_replace("+","",$cat);
// $inc[] = "media_category = '_common_image' ";
// $inc[] = "media_category REGEXP '(^|,)(_common_image)(,|$)' ";
// $inc[] = "media_category LIKE '%_common_image%' ";
$catArray[] = '_common_image';
}
if($cat)
{
if(strpos($cat, "|") && !strpos($cat,"+") )
{
$catArray = explode("|",$cat);
}
else
{
$catArray[] = $cat;
}
// $inc[] = "media_category LIKE '%".$cat."%' "; // for multiple category field.
// $inc[] = "media_category REGEXP '(^|,)(".$cat.")(,|$)' "; // for multiple category field.
}
// $inc[] = "media_category REGEXP '(^|,)_common_image|banner_image(,|$)' ";
// TODO check the category is valid.
if($search)
{
$searchinc[] = "media_name LIKE '%".$search."%' ";
$searchinc[] = "media_description LIKE '%".$search."%' ";
$searchinc[] = "media_caption LIKE '%".$search."%' ";
$searchinc[] = "media_tags LIKE '%".$search."%' ";
}
$ret = array();
$fields = ($amount == 'all') ? "media_id" : "*";
$query = "SELECT ".$fields." FROM #core_media WHERE `media_category` REGEXP '(^|,)".implode("|",$catArray)."(,|$)' AND `media_userclass` IN (".USERCLASS_LIST.") " ;
// $query = "SELECT ".$fields." FROM #core_media WHERE media_userclass IN (".USERCLASS_LIST.") AND ( ".implode(" OR ",$inc)." ) " ;
if($search)
{
$query .= " AND ( ".implode(" OR ",$searchinc)." ) " ;
}
$query .= " ORDER BY media_id DESC";
if($amount == 'all')
{
return e107::getDb()->gen($query);
}
if($amount)
{
$query .= " LIMIT ".$from." ,".$amount;
}
e107::getDb()->gen($query);
while($row = e107::getDb()->fetch(MYSQL_ASSOC))
{
$id = $row['media_id'];
$ret[$id] = $row;
}
return $ret;
}
/**
* Return an array of Images in a particular category
* @param string $type : 16 | 32 | 48 | 64
* @param integer $from
* @param integer $amount
*/
public function getIcons($type='', $from=0, $amount=null)
{
$inc = array();
if($type)
{
$inc[] = "media_category = '_icon_".$type."' ";
}
$ret = array();
$query = "SELECT * FROM #core_media WHERE media_userclass IN (".USERCLASS_LIST.") AND media_category LIKE '_icon%' ";
$query .= (count($inc)) ? " AND ( ".implode(" OR ",$inc)." )" : "";
$query .= " ORDER BY media_category, media_name";
if($amount)
{
$query .= " LIMIT ".$from." ,".$amount;
}
e107::getDb()->db_Select_gen($query);
while($row = e107::getDb()->db_Fetch(mySQL_ASSOC))
{
$id = $row['media_id'];
$ret[$id] = $row;
}
return $ret;
}
/**
* Generate Simple Thumbnail window for image -selection
*/
private function imageSelect($cat,$formid='imageSel')
{
$sql = e107::getDb();
$tp = e107::getParser();
$text .= "