Modify Uploads Module to Auto-Add Category

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Walkere
Forum Members
Forum Members
Posts: 12
Joined: Sat Oct 13, 2007 11:21 pm

Modify Uploads Module to Auto-Add Category

Post by Walkere »

Edit:  Made a slight change to the function declaration for autoAddCategory().  $grouplist now has a default of an empty array.  This should clear up any permission problems.

I'm using the Uploads module in conjunction with another module I'm writing. 

I wanted each FrontEndUser to have his or her own category in Uploads, conveniently named after the person's FEU loginname.  I couldn't find an option to auto-create a category if it didn't exist, so I modified the code a bit to get it to do what I wanted.

I thought about using a tag that was called by an event (i.e. a Front End User registering), but I didn't want to create a new category/folder unless someone actually tried to upload a file.  If there's an easier way to do it, let me know... but in case someone else wants the same functionality here's how I got it.

Around line 1700 in Uploads.module.php, you'll find the appropriate section of the AttemptUpload() method.

The original code was something like this...

Code: Select all

    $row = $dbresult->FetchRow ();
    if( !$row )
    {
	return array( FALSE, $this->Lang('error_invalidcategory') );
    }
The script just tried to pull the category information from the database.  If that fails ($row is empty), it kicks an error.  To auto-add the category, we add a little function here to do that.

I changed that snippet to...

Code: Select all

    $row = $dbresult->FetchRow ();
    if( !$row )
    {
	
			if (($params['category']) && ($params['category'] != 'Guest'))
  	  		{
				//  If the category doesn't already exist, let's try using our new method
				//  This should automatically create a category for us
	  			$this->autoAddCategory($params['category']);

				$query = "SELECT * FROM " . cms_db_prefix() . "module_uploads_categories WHERE upload_category_name = ?";
				$dbresult = $db->Execute($query, array($params['category']));

				$row = $dbresult->FetchRow();
			}
			
			if (!$row)
				return array( FALSE, $this->Lang('error_invalidcategory') );
				
    }
The function I added ($this->autoAddCategory()) adds the category.  Then, we query the DB to get the category info just like the module tried to do a few lines ago.  This time, $row should be full, but we kept the error check in there.

The only other thing to do is to write the autoAddCategory() function.  I just copied the do_addcategory action and made a few changes to it.  That action was intended to take parameters from a form, whereas our method will have its parameters passed to it when it is called.  I also cut out the last couple lines, which would have displayed a template - we want this method to execute without creating any output.

Here's that method.  I inserted it at the end of Uploads.module.php.

Code: Select all

  //  Adding this function so that we can add a category for each front end user
  //  when that user tries to upload a file for the first time
  
  //  Could potentially add a path and description, but for now they're simply based off the username
  function autoAddCategory($name, $grouplist = array(), $path = '', $desc = '')
  {
  		if ($path == '')
			$path = $name;
			
		if ($desc == '')
			$desc = "$name's uploaded files.";
			
		$listable = true;
		
		$db =& $this->GetDb();
     	$query = "SELECT * from ".cms_db_prefix ().
     	  		 "module_uploads_categories WHERE upload_category_name = ?";
     	$dbresult = $db->Execute ($query, array ($name));
     
	    // yep it does
     	if ($dbresult->FetchRow ())
       	{	
	 		$this->smarty->assign ('error', "1");
	 		$this->smarty->assign ('message', $this->Lang ("error_categoryexists"));
	 		$error = true;
       	}
		else
		{
			$query = "SELECT * from ".cms_db_prefix (). "module_uploads_categories WHERE upload_category_path = ?";
	 		$dbresult = $db->Execute ($query, array ($path));
	 
	 		if ($dbresult->FetchRow ())
	   		{
	    		$this->smarty->assign ('error', "1");
	     		$this->smarty->assign ('message', $this->Lang ("error_pathinuse"));
	     		
				$error = true;
		   	}
			else
			{
				// Create the directory if it doesn't exist
			    $do_scan = false;
	     		$dir = $this->_categoryPath($path);
				
				if( file_exists( $dir ) ) 
	       		{
		 			$do_scan = true;
	       		}
	     		else
	       		{
		 			// create the directory
		 			$result = $this->_mkdirr ($dir);
		 			if ($result == false) 
		   			{
		     			$this->smarty->assign ('error', "1");
		     			$this->smarty->assign ('message', $this->Lang ("error_categoryexists"));
		     			$error = true;
		   			}
		 
		 			// create an index.html file (empty)
		 			if( $this->GetPreference('create_dummy_index_html') )
		   			{
		     			touch( $dir.DIRECTORY_SEPARATOR."index.html" );
		   			}
	       		}
	    
		   		if( !$error )
	       		{
		 			$groups = null;
		 			if( $grouplist )
		   			{
		     			$groups = implode(',',$grouplist);
		   			}
		 
		 			// Add the record to the category table
		 			$catid = $db->GenID (cms_db_prefix ()."module_uploads_categories_seq");
		 			$category_id = $catid;
		 			$query = "INSERT INTO ".cms_db_prefix (). "module_uploads_categories VALUES (?,?,?,?,?,?)";
		 			
					$dbresult = $db->Execute ($query, array ($catid, $name, $desc, $path, $listable, $groups));
					
		 			if (!$dbresult)
		   			{
		   		  		$this->smarty->assign ('error', "1");
		     			$this->smarty->assign ('message', $this->Lang ("message_categoryadded"));
		     			$error = true;
		   			}
		 			else
		   			{
		     			if( $do_scan )
		       			{
			 				// category is added, and the directory already existed... so now
			 				// we're gonna scan the directory and add files to the database
			 				$this->ScanDirectory( $id, $params, $returnid, $dir );
		       			}
		     
		     			// send an event
		     			$parms = array();
		     			$parms['name'] = $name;
		     			$parms['description'] = $desc;
		     			$parms['path'] = $path;
		     			$parms['listable'] = $listable;
		     			$this->SendEvent( 'OnCreateCategory', $parms );
		   			}
	       		}
			}
		}
	}
Good luck,
- Walkere
Last edited by Walkere on Sat Nov 10, 2007 6:25 pm, edited 1 time in total.
Image
liudaz

Re: Modify Uploads Module to Auto-Add Category

Post by liudaz »

I have just installed theese modules and thought about such opportunity. Thank you Walkere very much.
Several thing i would like to ask.. How do you call this module in the template? Do you need to specify the category?
{cms_module module="Uploads" category="something" mode="upload"} ?I have changed the code as you have written above, but no changes happened. Files get created as with a basic uploads module.
Though, if i specify the category, that has not been created via admin panel, it gets automatically created by your script. I suppose you have to send an author name from the front end as a category name in order for it to be created.

I'd really appreasiate your help.
Last edited by liudaz on Sat Nov 10, 2007 1:37 pm, edited 1 time in total.
Walkere
Forum Members
Forum Members
Posts: 12
Joined: Sat Oct 13, 2007 11:21 pm

Re: Modify Uploads Module to Auto-Add Category

Post by Walkere »

You shouldn't notice any differences, except that a category is automatically created if it doesn't already exist.  It sounds like that is working for you.

In order to customize the category based on the user, call the module just like you did.  Then, in place of "something" use a FrontEndUser or CustomContent variable for the Category.

At the moment, I use CustomContent to get the FEU login name and use that for the directory.  So the basic call you would put into a page is...

Code: Select all

{cms_module module='Uploads' category=$customcontent_loginname mode='upload'}
- Walkere
Image
liudaz

Re: Modify Uploads Module to Auto-Add Category

Post by liudaz »

Thanks for the quick reply.
Allright, i did all what you said. I have wrapped the uploads module tag with the custom content tags. But anyways, when i click on "submit", i get an error:
Error!

Error: Invalid (or empty) category.

Here is the call:

Code: Select all

{cms_module module=FrontEndUsers} 
{if $customcontent_loggedin}
{cms_module module='Uploads' category=$customcontent_loginname mode='upload'} 
{else}
No good
{/if}
Am i doing something wrong? :)
Walkere
Forum Members
Forum Members
Posts: 12
Joined: Sat Oct 13, 2007 11:21 pm

Re: Modify Uploads Module to Auto-Add Category

Post by Walkere »

Hmm...  not sure exactly what could be going wrong there.

One thing to check.  Load the page with the form on it.  View the source and look for the "hidden" field "category."  Check and make sure that the field has a value.  If that field is blank for some reason, nothing will happen and you should get that error.

- Walkere
Image
liudaz

Re: Modify Uploads Module to Auto-Add Category

Post by liudaz »

My mistake. I must have copied not the whole source from the one that you have given in the forum. Everything works great now, just like i wanted. Thanks a lot, man!

Maybe developers of this module could put this as a part of the official release of the uploads module? :)
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Modify Uploads Module to Auto-Add Category

Post by calguy1000 »

I wouldn't auto create categories like this, I would have simple created a UDT that handled the FrontEndUsers onUserAdded event and then called the uploads module and told it to create a category for that user.  It would have been less code.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Walkere
Forum Members
Forum Members
Posts: 12
Joined: Sat Oct 13, 2007 11:21 pm

Re: Modify Uploads Module to Auto-Add Category

Post by Walkere »

As I mentioned in my first post, I thought about that... but I didn't necessarily want a category for every user that signs up.

Let's say only 40% of users actually use the upload feature and add files.  If every user gets a category, that's a lot of redundancy.

Given the requirement that a category is only created when a user actually uploads a file, I don't think an event would do exactly what I wanted.

I agree with liudaz that it'd be a nice addition to the module functionality.  Although it shouldn't be automatic - there should be a parameter (like autocreate='true') that you put in the module call.  If that's set, then the module would auto-create the category for you...

But it's not that important.  Just thought I'd post the hack in case anyone wanted similar functionality.

- Walkere
Image
Bl1nk

Re: Modify Uploads Module to Auto-Add Category

Post by Bl1nk »

i have a problem, i did all what you said Walkere and there is a error:

Fatal error: Call to undefined method Uploads::autoAddCategory() in /usr/home/users/akaragfx.com/www/modules/Uploads/Uploads.module.php on line 1729

any solution?
Last edited by Bl1nk on Thu Oct 09, 2008 5:12 pm, edited 1 time in total.
gap_tooth_clan

Re: Modify Uploads Module to Auto-Add Category

Post by gap_tooth_clan »

Dont know if this is too late for you but I had the same problem, I was being a little stupid and did not put the function inside the class!

Put the function within the last } !!
Post Reply

Return to “Tips and Tricks”