Yeah some progress tonight!
I knew it wasn't that hard...
I completly forgot the _upload function in
com_mycomponent/administrator/jmodel.item.php
public function _upload($fieldName, $extensions = null, $options = array(), $dir = null)
{
//Send the id for eventual name or path parsing in upload
$options['id'] = $this->getId();
$custom_dir=$this->getItem();
cimport('classes.upload');
$config = JComponentHelper::getParams( 'com_mycomponent' );
if (!$dir)
$dir = $config->get('upload_dir_' . $this->view_list . '_' . $fieldName, '[COM_SITE]' .DS. 'files' .DS. $this->view_list . '_' . $fieldName
.DS.JApplication::stringURLSafe($custom_dir->supplier)); // <== ADDED THIS LINE
And Here in the same file
com_mycomponent/administrator/jmodel.item.php
//Get all indexes for all fields
$query = "SELECT " . $db->quoteName(implode($db->quoteName(', '), array_keys($fileFields)))
. " FROM " . $db->quoteName($table->getTableName())
. ' WHERE id IN ( '.implode(', ', $pks) .' )';
$db->setQuery($query);
$files = $db->loadObjectList();
cimport("classes.file");
$config = JComponentHelper::getParams( 'com_mycomponent' );
foreach($fileFields as $fieldName => $op)
{
$dir = $config->get('upload_dir_' . $this->view_list . '_' . $fieldName, '[COM_SITE]' .DS. 'files' .DS. $this->view_list . '_' . $fieldName
.DS.JApplication::stringURLSafe($custom_dir->supplier)); <== ADDED THIS
With the above code changes dynamic folder uploads is possible
it will create a directory behind the default directory such as com_mycomponent/files/default_file/custom_dir
- if $custom_dir-> supplier is null (not specified) will upload to the default directory specified in the Config XML file
BUT there is some issues
- the thumbnails are NOT found in item or list views (work around detailed below)
- if the custom_dir is changed after a record is saved it does not create a new directory and upload or move the the file
As a temporary "WORKAROUND" for thumbnails not being shown - modified the getMarkers() function
com_mycomponent/administrator/file.php
function getMarkers($path) //added path
{
$configMedias = JComponentHelper::getParams('com_media');
$config = JComponentHelper::getParams('com_mycomponent');
return array(
// WORK AROUND
// REMOVED THE FOLLOWING LINE
//'DIR_NEWTABLE_MY_FILE' => $config->get("upload_dir_newtable_my_file", "[COM_SITE]" .DS. "files" .DS. "newtable_my_file") .DS,
// REPLACED WITH
'DIR_NEWTABLE_MY_FILE' => $path .DS,
And just below that Parsed path from here (com_mycomponent/administrator/file.php)
function parsePath($path)
{
// Protect against back dir (../)
if (DS == "\\")
$ds = "\\\\";
else
$ds = "\/";
$path = preg_replace("/\.\." .$ds. "/", "", $path);
$markers = self::getMarkers($path); // parsed path to getMarkers() function)
Still needs alot more work but its a start and a least it gives me an idea in what needs to be done to create a custom class or class extension - (didn't have great success before) Did try to pull the data into the file.php using something like getModel() and getInstance(...) but that's another story
TODO
- construct switch statement to detect custom_dir to remove the workaround
- multiple custom directory not possible with above because it aliases the file name
- create index.html file in each sub directory when using multiple sub directories
- allow custom directory uploads (override the default directory)
- make safe custom directory names rather than alias them
- if file is the same but custom_dir is different move file to the new custom directory (leave / delete old file??)
....
....
I Guess Something like this would be a great addition to the file up loader especially for sorting pictures, but would be great for anyone using categories as well such as invoices, document filing, inventory..........
Anyhow as i said its a start