Modify Uploads Module to Auto-Add Category
Posted: Fri Nov 09, 2007 8:36 pm
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...
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...
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.
Good luck,
- Walkere
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') );
}
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 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 );
}
}
}
}
}
- Walkere