Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

File Upload - Dymanic Folder Paths? 05 Feb 2013 13:22 #6641

  • BTB300
  • BTB300's Avatar Topic Author
  • Away
  • Moderator
  • Moderator
  • Posts: 415
  • Thank you received: 132
Hey there everyone

Tonight i have a mental blank (probably because i have not been cooking for a while)

I want to dynamically create upload folder behind the default upload folder set in config such as

com_componentname/files/default_uploadfolder/another_folder

the folder name is defined in a field that is available in the current model

I know that i need to change 3 files in order to do this
- com_mycomponentname/administrator/classes/upload.php
- com_mycomponentname/administrator/classes/file.php
- com_mycomponentname/config.xml

I know that in the past (Cook 1.5) i created and called a helper function that returned the current item field values but can't i just pass or call the current model data?

com_mycomponentname/administrator/classes/upload.php
function setUploadFolder($uploadFolder)
	{
		// I want to be able to do something like this...
		$uploadFolder = $this->parsePath($uploadFolder).DS.JText::_($this->model->another_folder);	
		
		$app = JFactory::getApplication();

in
com_mycomponentname/administrator/classes/file.php
function getMarkers()
	{

		$configMedias = JComponentHelper::getParams('com_media');
		$config = JComponentHelper::getParams('com_mycomponent');


		return array(
			'DIR_MYIMAGE_IMAGE' => $config->get("upload_dir_myimage_image", "[COM_SITE]" .DS. "files" .DS. "myimage_image") .DS."[ANOTHER_FOLDER]".DS,
			
		...
		...
		// Add the new marker something like this
		'ANOTHER_FOLDER' => $this->model->another_folder
		...
		...
	}

in
com_mycomponentname/config.xml
<config>
	...
	...
	// Add the marker to the default upload directory
	<field name="upload_dir_myimage_image" type="text" default="[COM_SITE]/files/myimage_image/[ANOTHER_FOLDER]" label="MYCOMPONENT...

Thanks in advance any help appreciated

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 07 Feb 2013 08:46 #6664

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
Hey @BTB300

Just wondering if you had found a solution to this yet?

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 07 Feb 2013 11:24 #6665

  • BTB300
  • BTB300's Avatar Topic Author
  • Away
  • Moderator
  • Moderator
  • Posts: 415
  • Thank you received: 132
Thanks Gez
Sorry no haven't been able to find solution - cant even find where i did it on one of the old 1.5 components

But i know if you use Jfactory::getUser() you can do what i want such as
.../uploadfolder/username

or if you hard code it with the foldername it works by modifying the files as i described in my original post

I know its something simple that i am missing but cant remember :S
- import a class file perhaps?

If i can get this working i can then override the default rename method the upload files as well

for instance it would be nice to be able to rename a file such as

{SUPPLIER}_{INVOICEDATE}_{INVOICE_NUMBER}.{MIMEXT}

Thanks for checking

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 07 Feb 2013 12:14 #6668

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
Hi @BTB300,

I've not done what you're wishing to do so this might not be as easy as I propose...

Could you extend (or duplicate and rename) the file class to take in a new parameter/property that can then be instanced in your JDom call? Because then, you're in a view with direct access to the model data and can therefore just pass the $dir in which can be appended to the defined base path...

Won't that work?

Hope it helps in any case!

Best of luck,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 07 Feb 2013 14:06 #6677

  • BTB300
  • BTB300's Avatar Topic Author
  • Away
  • Moderator
  • Moderator
  • Posts: 415
  • Thank you received: 132
Thanks Gez
Will Try... its a bit late here so i guess maybe tomorrow i will have 5 minutes to check if it works

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 07 Feb 2013 14:28 #6679

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
No worries buddy!

I hope that it is a viable option as (and I may very well be wrong) but it seems the easiest way to go to me!

Get some sleep!!!

Take care,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 18 Feb 2013 16:05 #6812

  • BTB300
  • BTB300's Avatar Topic Author
  • Away
  • Moderator
  • Moderator
  • Posts: 415
  • Thank you received: 132
Yeah some progress tonight! :cheer:

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
The following user(s) said Thank You: JoomGuy

Please Log in or Create an account to join the conversation.

Last edit: by BTB300.

Re: File Upload - Dymanic Folder Paths? 21 Feb 2013 14:14 #6826

  • BTB300
  • BTB300's Avatar Topic Author
  • Away
  • Moderator
  • Moderator
  • Posts: 415
  • Thank you received: 132
Um... was working :angry:
it uploads files amd images to the $custom_dir but once again the images are not found/displayed in both list views and item views

so it looks like my work around to pass $path to the get markers function was only a part of the solution... now where else did i alter the code to make the images display correctly :S

Unless i parsed the custom_dir through options the same as the ID...?

Please Log in or Create an account to join the conversation.

Last edit: by BTB300.

Re: File Upload - Dymanic Folder Paths? 27 Mar 2013 09:40 #7013

This is how I create custom dynamic upload folders in jcook v2.0:

add the following function in the helper file located at YOUR_COMPONENT/helpers/helper.php to create the custom/dynamic folder path :
	function getUploadFolder($model, $fieldName){
		
		// the formula to create the custom folder path
		$item = $model->getItem();
		$dynamicFolder = 'just_a_folder' .DS. $item->id ;
		
		$dir = '[COM_SITE]' .DS. $dynamicFolder .DS. $model->view_list . '_' . $fieldName;
		
		return $dir;
	}

then in the jmodel.item.php located at YOUR_COMPONENT/classes/jmodel.item.php :

line 123:
		if (!$dir)
			$dir = $config->get('upload_dir_' . $this->view_list . '_' . $fieldName, '[COM_SITE]' .DS. 'files' .DS. $this->view_list . '_' . $fieldName);

become:
		if (!$dir)
			$dir = COMPONENTHelper::getUploadFolder($this, $fieldName ); /* hack */


line 143:
		// Previous value parameter
		$previousVar = "__$fieldName";
		$previous = (isset($post[$previousVar])?$post[$previousVar]:null);

become:
		// Previous value parameter
		$previousVar = "__$fieldName";
		$previous = basename(isset($post[$previousVar])?$post[$previousVar]:null); /* hack */

line 199
		if ($changed)
		{
			//Store the image file name with path pattern
			if (!$this->save(array($fieldName => $fileName)))
				return false;
		}

become:
		if ($changed)
		{
			/* hack */
			if($fileName != ''){
				$fileName = $dir .DS. $fileName;
			}
			/* hack */
			
			//Store the image file name with path pattern
			if (!$this->save(array($fieldName => $fileName)))
				return false;
		}


line 506:
		foreach($fileFields as $fieldName => $op)
		{
			$dir = $config->get('upload_dir_' . $this->view_list . '_' . $fieldName, '[COM_SITE]' .DS. 'files' .DS. $this->view_list . '_' . $fieldName);

become:
		foreach($fileFields as $fieldName => $op)
		{
			$dir = JshopHelper::getUploadFolder($this, $fieldName ); /* hack */

I hope will save some time to someone.
The following user(s) said Thank You: admin, BTB300

Please Log in or Create an account to join the conversation.

Re: File Upload - Dymanic Folder Paths? 03 Apr 2013 09:48 #7022

  • BTB300
  • BTB300's Avatar Topic Author
  • Away
  • Moderator
  • Moderator
  • Posts: 415
  • Thank you received: 132
Tomaselli
many thanks for taking the time to post your solution
greatly appreciated
K+1

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.107 seconds

Amazing Tool !
And only tool I know that allows complete Scaffolding in terms of component creation. It really saves loads of time (actually much more than that). Simple architecture enables quick and painless changes at any time. Code is clean and consintent. It's just... what's the word... neat! :) Great work! You should definetely try this one!

pi_wo (JED)
         

Get Started