virtuemart_media_id = $id;
$this->theme_url = VmConfig::get('vm_themeurl',0);
if(empty($this->theme_url)){
$this->theme_url = 'components/com_virtuemart/';
}
}
/**
* The type of the media determines the used path for storing them
*
* @author Max Milbers
* @param string $type type of the media, allowed values product, category, shop, vendor, manufacturer, forSale
*/
public function getMediaUrlByView($type){
//the problem is here, that we use for autocreatoin the name of the model, here products
//But for storing we use the product to build automatically the table out of it (product_medias)
$choosed = false;
if($type == 'product' || $type == 'products'){
$relUrl = VmConfig::get('media_product_path');
$choosed = true;
}
else if($type == 'category' || $type == 'categories'){
$relUrl = VmConfig::get('media_category_path');
$choosed = true;
}
else if($type == 'shop'){
$relUrl = VmConfig::get('media_path');
$choosed = true;
}
else if($type == 'vendor' || $type == 'vendors'){
$relUrl = VmConfig::get('media_vendor_path');
$choosed = true;
}
else if($type == 'manufacturer' || $type == 'manufacturers'){
$relUrl = VmConfig::get('media_manufacturer_path');
$choosed = true;
}
else if($type == 'forSale' || $type== 'file_is_forSale'){
if (!class_exists ('shopFunctionsF'))
require(VMPATH_SITE . DS . 'helpers' . DS . 'shopfunctionsf.php');
$relUrl = shopFunctions::checkSafePath();
if($relUrl){
$choosed = true;
$this->file_is_forSale=1;
}
}
if($choosed && empty($relUrl)){
$link =JURI::root() . 'administrator/index.php?option=com_virtuemart&view=config';
vmInfo('COM_VIRTUEMART_MEDIA_NO_PATH_TYPE',$type,$link );
//Todo add general media_path to config
//$relUrl = VmConfig::get('media_path');
$relUrl = self::getStoriesFb().'/';
$this->setRole=true;
// } else if(!$choosed and empty($relUrl) and $this->file_is_forSale==0){
} else if(!$choosed and empty($relUrl) ){
if(empty($this->file_type) and !empty($this->file_url)){
vmAdminInfo('COM_VIRTUEMART_MEDIA_CHOOSE_TYPE',$this->file_title );
// vmError('Ignore this message, when it appears while the media synchronisation process, else report to http://forum.virtuemart.net/index.php?board=127.0 : cant create media of unknown type, a programmers error, used type ',$type);
}
$relUrl = self::getStoriesFb('typeless').'/';
$this->setRole=true;
} else if(!$choosed and $this->file_is_forSale==1){
$relUrl = '';
$this->setRole=false;
}
return $relUrl;
}
static function getStoriesFb($suffix = ''){
if(!class_exists('JFolder')){
require(VMPATH_LIBS.DS.'joomla'.DS.'filesystem'.DS.'folder.php');
}
$url = 'images/virtuemart/'. $suffix ;
if(JFolder::exists(VMPATH_ROOT .'/'.$url)) {
return $url;
} else {
$urlOld = 'images/stories/virtuemart/'. $suffix;
if(JFolder::exists(VMPATH_ROOT .'/'.$urlOld)){
return $urlOld;
}
}
if(JFolder::create(VMPATH_ROOT .'/'.$url)) {
return $url;
} else {
return false;
}
}
/**
* This function determines the type of a media and creates it.
* When you want to write a child class of the mediahandler, you need to manipulate this function.
* We may use later here a hook for plugins or simular
*
* @author Max Milbers
* @param object $table
* @param string $type vendor,product,category,...
* @param string $file_mimetype such as image/jpeg
*/
static public function createMedia($table,$type='',$file_mimetype=''){
if(!class_exists('JFile')) require(VMPATH_LIBS.DS.'joomla'.DS.'filesystem'.DS.'file.php');
$extension = strtolower(JFile::getExt($table->file_url));
$isImage = self::isImage($extension);
if($isImage){
if (!class_exists('VmImage')) require(VMPATH_ADMIN.DS.'helpers'.DS.'image.php');
$media = new VmImage();
} else {
$media = new VmMediaHandler();
}
$attribsImage = $table->getProperties();
foreach($attribsImage as $k=>$v){
$media->$k = $v;
}
if(empty($type)){
$type = $media->file_type;
} else {
$media->file_type = $type;
}
$media->setFileInfo($type);
return $media;
}
/**
* This prepares the object for storing the data. This means it does the action
* and returns the data for storing in the table
*
* @author Max Milbers
* @param object $table
* @param array $data
* @param string $type
*/
static public function prepareStoreMedia($table,$data,$type){
$media = VmMediaHandler::createMedia($table,$type);
$data = $media->processAttributes($data);
$data = $media->processAction($data);
if($data===false) return false;
$attribsImage = get_object_vars($media);
foreach($attribsImage as $k=>$v){
$data[$k] = $v;
}
return $data;
}
/**
* Sets the file information and paths/urls and so on.
*
* @author Max Milbers
* @param unknown_type $filename
* @param unknown_type $url
* @param unknown_type $path
*/
function setFileInfo($type=0){
$this->file_url_folder = '';
$this->file_path_folder = '';
$this->file_url_folder_thumb = '';
if($this->file_is_forSale==0 and $type!='forSale'){
$this->file_url_folder = $this->getMediaUrlByView($type);
$this->file_url_folder_thumb = $this->file_url_folder.'resized/';
$this->file_path_folder = str_replace('/',DS,$this->file_url_folder);
} else {
if (!class_exists ('shopFunctions'))
require(VMPATH_SITE . DS . 'helpers' . DS . 'shopfunctions.php');
$safePath = shopFunctions::checkSafePath();
if(!$safePath){
return FALSE;
}
$this->file_path_folder = $safePath;
$this->file_url_folder = $this->file_path_folder;//str_replace(DS,'/',$this->file_path_folder);
$this->file_url_folder_thumb = VmConfig::get('forSale_path_thumb');
}
//Clean from possible injection
while(strpos($this->file_path_folder,'..')!==false){
$this->file_path_folder = str_replace('..', '', $this->file_path_folder);
};
$this->file_path_folder = preg_replace('#[/\\\\]+#', DS, $this->file_path_folder);
if(empty($this->file_url)){
$this->file_url = $this->file_url_folder;
$this->file_name = '';
$this->file_extension = '';
} else {
if(!class_exists('JFile')) require(VMPATH_LIBS.DS.'joomla'.DS.'filesystem'.DS.'file.php');
if($this->file_is_forSale==1){
$rdspos = strrpos($this->file_url,DS);
if($rdspos!==false){
$name = substr($this->file_url,$rdspos+1);
}else {
vmdebug('$name',$this->file_url,$rdspos);
}
} else {
//This construction is only valid for the images, it is for own structuring using folders
$name = str_replace($this->file_url_folder,'',$this->file_url);
}
if(!empty($name) && $name !=='/'){
$this->file_name = JFile::stripExt($name);
//$this->file_extension = strtolower(JFile::getExt($name));
$this->file_extension = strtolower(JFile::getExt($name));
//Ensure using right directory
$file_url = $this->getMediaUrlByView($type).$name;
if($this->file_is_forSale==1){
if(JFile::exists($file_url)){
$this->file_url = $file_url;
} else {
// vmdebug('MediaHandler, file does not exist in safepath '.$file_url);
}
} else {
$pathToTest = VMPATH_ROOT.DS.str_replace('/',DS,$file_url);
if(JFile::exists($pathToTest)){
$this->file_url = $file_url;
} else {
// vmdebug('MediaHandler, file does not exist in '.$pathToTest);
}
}
}
}
if($this->file_is_downloadable) $this->media_role = 'file_is_downloadable';
if($this->file_is_forSale) $this->media_role = 'file_is_forSale';
if(empty($this->media_role)) $this->media_role = 'file_is_displayable';
$this->determineFoldersToTest();
//Do we need this?
/*if(!empty($this->file_url) && empty($this->file_url_thumb)){
$this->displayMediaThumb('',true,'',false);
}*/
}
public function getUrl(){
return $this->file_url_folder.$this->file_name.'.'.$this->file_extension;
}
public function getThumbUrl(){
return $this->file_url_folder_thumb.$this->file_name.'.'.$this->file_extension;
}
public function getFullPath(){
$rel_path = str_replace('/',DS,$this->file_url_folder);
return VMPATH_ROOT.DS.$rel_path.$this->file_name.'.'.$this->file_extension;
}
public function getThumbPath(){
$rel_path = str_replace('/',DS,$this->file_url_folder);
return VMPATH_ROOT.DS.$rel_path.$this->file_name_thumb.'.'.$this->file_extension;
}
/**
* Tests if a function is an image by mime or extension
*
* @author Max Milbers
* @param string $file_mimetype
* @param string $file_extension
*/
static private function isImage($file_extension=0){
if($file_extension == 'jpg' || $file_extension == 'jpeg' || $file_extension == 'png' || $file_extension == 'gif'){
$isImage = TRUE;
} else {
$isImage = FALSE;
}
return $isImage;
}
private $_foldersToTest = array();
/**
* This functions adds the folders to test for each media, you can add more folders to test with
* addFoldersToTest
* @author Max Milbers
*/
public function determineFoldersToTest(){
if(VmAccess::manager('core')){
$r = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
$this->addFoldersToTest($r);
}
$file_path = str_replace('/',DS,$this->file_url_folder);
if($this->file_is_forSale){
$this->addFoldersToTest($file_path);
} else {
$this->addFoldersToTest(VMPATH_ROOT.DS.$file_path);
}
$file_path_thumb = str_replace('/',DS,$this->file_url_folder_thumb);
$this->addFoldersToTest(VMPATH_ROOT.DS.$file_path_thumb);
}
/**
* Add complete paths here to test/display if their are writable
*
* @author Max Milbers
* @param absolutepPath $folders
*/
public function addFoldersToTest($folders){
if(!is_array($folders)) $folders = (array) $folders;
$this->_foldersToTest = array_merge($this->_foldersToTest, $folders);
}
/**
* Displays for paths if they are writeable
* You set the folders to test with the function addFoldersToTest
* @author Max Milbers
*/
public function displayFoldersWriteAble(){
$style = 'text-align:left;margin-left:20px;';
$result = '
';
$Images[$key ]['value'] = $image->file_title.' :: '.$image->virtuemart_media_id;
}
//$list['htmlImages'] = $htmlImages;
return $Images;
}
/**
* Retrieve a list of layouts from the default and chosen templates directory.
*
* We may use here the getFiles function of the media model or write something simular
* @author Max Milbers
* @param name of the view
* @return object List of flypage objects
*/
static function getImagesList($type = '',$limit=0, $max=16) {
$db = JFactory::getDBO();
$list = array();
$vendorId = vmAccess::isSuperVendor();
$q='SELECT SQL_CALC_FOUND_ROWS `virtuemart_media_id` FROM `#__virtuemart_medias` WHERE `published`=1
AND (`virtuemart_vendor_id`= "'.(int)$vendorId.'" OR `shared` = "1")';
if(!empty($type)){
$q .= ' AND `file_type` = "'.$type.'" ';
}
$search = trim(vRequest::getString('term', false));
if (!empty($search)){
$search = '"%' . $db->escape( $search, true ) . '%"' ;
$q .= ' AND (`file_title` LIKE '.$search.' OR `file_description` LIKE '.$search.' OR `file_meta` LIKE '.$search.') ';
}
$q .= ' LIMIT '.(int)$limit.', '.(int)$max;
$db->setQuery($q);
if ($virtuemart_media_ids = $db->loadColumn()) {
$model = VmModel::getModel('Media');
$db->setQuery('SELECT FOUND_ROWS()');
$list['total'] = $db->loadResult();
$list['images'] = $model->createMediaByIds($virtuemart_media_ids, $type);
return $list;
}
else return array();
}
/**
* This displays a media handler. It displays the full and the thumb (icon) of the media.
* It also gives a possibility to upload/change/thumbnail media
*
* @param string $imageArgs html atttributes, Just for displaying the fullsized image
*/
public function displayFileHandler($vendorId = 0){
vmLanguage::loadJLang('com_virtuemart_media');
$this->addHiddenByType();
$html = '';
$this->addMediaActionByType();
$html .= '';
$html .= $this->displayFoldersWriteAble();
$html .= $this->displayHidden();
// $html .= '';
return $html;
}
/**
* child classes can add their own options and you can get them with this function
*
* @param array $optionsarray Allowed values are $this->_actions and $this->_attributes
*/
private function getOptions($optionsarray){
$options=array();
foreach($optionsarray as $optionName=>$langkey){
$options[] = JHtml::_('select.option', $optionName, vmText::_( $langkey ) );
}
return $options;
}
/**
* Just for creating simpel rows
*
* @author Max Milbers
* @param string $descr
* @param string $name
*/
private function displayRow($descr, $name,$readonly='',$value = null){
$v = (isset($value))? $value: $this->$name;
$html = '
'.vmText::_($descr).'
';
return $html;
}
/**
* renders the hiddenfields added in the layout before (used to make the displayFileHandle reusable)
* @author Max Milbers
*/
private function displayHidden(){
$html='';
foreach($this->_hidden as $k=>$v){
$html .= '';
}
return $html;
}
}