-
-
Save kochen/425247bc02f395c6f3ff to your computer and use it in GitHub Desktop.
<? | |
$catid = 2; // the number of the specific category to assign the document to. | |
$fileinfo = array( /* an array of file info, including BASENAME and FILENAME */ ); | |
$datetime = date("Y-m-d H:i:s"); // current datetime | |
$user = KObjectMAnager::getInstance()->getObject('user'); // the Joomla user object | |
$controller = KObjectMAnager::getInstance()->getObject('com://admin/docman.controller.document'); | |
$controller->add(array( | |
docman_category_id => $catid, | |
storage_path => $fileinfo['filename'], | |
title => $fileinfo['basename'], | |
created_on => $datetime, | |
modified_on => $datetime, | |
modified_bycr => $user->id, | |
created_by => $user->id, | |
)); |
johanjanssens
commented
Sep 4, 2014
- created_on and modified_on are not required. When left empty they will be automatically set using the current datetime.
- created_by and modified_by are not required. When left empty they will be automatically set using the active user (KObjectMAnager::getInstance()->getObject('user')) To set a different user pass in the user id.
What kind of errors will this call return? Does it check the file from storage_path exists? How about whether the title already exists or not? How about if I send in an unknown or unauthorized catid?
If something goes wrong with this call an exception will be thrown at all times. Depending on what went wrong different exceptions are thrown and can be catched. We do not have a list of exceptions per method.
A. During development the adviced approach is to install our Joomla Vagrant Box which offers support for xdebug and makes full use of the build in framework debugger. This gives you complete stack tracing and exception output if something goes wrong.
To do so :
- Install the Joomla Vagrant box : http://developer.joomlatools.com/tools/vagrant/introduction.html and http://www.slideshare.net/joomlatools/joomla-in-a-box
- Turn on debug in Joomla
- Turn on xdebug on the command line (vagrant ssh > xdebug enable)
The last step is not required by useful for debugging fatal errors intead of exceptions. You get a complete stacktrace this way, which php doesn't provide out of the box.
B. During production it makes sense to catch exceptions that could occur. For example you can wrap the call in
try
{
//Code goes here
} catch (Exception $exception) {
//Exception handling goes here
}