Paypal Donate
| File |
|---|
In Development
Extension: Attachment Module
Writing a Joomla Module
There is a lovely extension out there for adding attachments to an article. It lets you upload/download files from an article. You install the component and a plugin, and the plugin displays the list of attachments at the base of the article content. You can find that extension here.
There are quite a few tutorials on how to write a Joomla! extension of the HelloWorld variety. I thought it would be amusing to write one with a practical application, for a change. I want the list of downloadable attachments to appear in a module location rather than in the article content.
If you are logged in to this site, you can download the module we will be writing in this article.
First, we need a name for this module. Since the attachments component is com_attachments, we will call this module mod_attachments.
We create a directory called mod_attachments, and create two files (to begin): mod_attachments.xml and mod_attachments.php.
The XML file is a definition file that tells the Joomla installer, among other things, what files are required and other metadata about the module.
mod_attachments.xml
<?@xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
<name>Attachments List</name>
<author>jennifer nodwell</author>
<creationDate>March 2009</creationDate>
<copyright>Copyright (C) 2008 - 2010 Jennifer Nodwell. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
<authorEmail>
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
</authorEmail>
<authorUrl>www.nodwell.net</authorUrl>
<version>1.5.0</version>
<description>This module shows the attachments in a module position</description>
<files>
<filename>mod_attachments.xml</filename>
<filename module="mod_attachments">mod_attachments.php</filename>
<filename>index.html</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
<filename>styles/attachments.css</filename>
</files>
<params>
<param name="cache" type="list" default="1" label="Caching" description="Select whether to cache the content of this module">
<option value="1">Use global</option>
<option value="0">No caching</option>
</param>
</params>
</install>
The <name> tag is the name of the module as you will see it when you add a new module from the back-end under Extensions->Module Manager->New.
The PHP file will communicate with the component and generate the list of attachments for the given article being displayed. It will call on the tmpl/default.php file to actually write the information to the browser. If there were no component in play here, it would be typical to write a helper file for the module. However, we're going to use the helper file in the com_attachments component.
mod_attachments.php
<?@php // no direct access
defined('_JEXEC') or die('Restricted access');
// get the current article id and call the
// component helper to build the list of
// attachments for the current article
require_once(JPATH_SITE.DS.'components'.DS.'com_attachments'.DS.'helper.php');
// Generate the HTML for the attachments for the specified article
$article_id = (int) JRequest::getVar('id', false);
$attachments = "";
$db =& JFactory::getDBO();
$query = "SELECT count(*) FROM #__attachments WHERE article_id='" . $article_id . "' AND published='1'";
$db->setQuery($query);
$total = $db->loadResult();
if ( $total > 0 ) {
// Get the component parameters
jimport('joomla.application.component.helper');
$params = JComponentHelper::getParams('com_attachments');
// Check the security status
$attach_dir = JPATH_SITE.DS.$params->get('upload_dir', 'attachments');
$secure = $params->get('secure', false);
$hta_filename = $attach_dir.DS.'.htaccess';
if ( ($secure && !file_exists($hta_filename)) ||
(!$secure && file_exists($hta_filename)) ) {
AttachmentsHelper::setup_upload_directory($attach_dir, $secure);
}
$attachments = AttachmentsHelper::attachmentsTableHTML($article_id, false, true, true, $from);
}
//display the list of attachments
require_once(JPATH_SITE.DS.'modules'.DS.'mod_attachments'.DS.'tmpl'.DS.'default.php');
?>
This line
defined('_JEXEC') or die('Restricted access');
ensures that this file only runs under Joomla! and isn't accessed directly.
This line
require_once(JPATH_SITE.DS.'components'.DS.'com_attachments'.DS.'helper.php');
gets the helper file (in our case from the component).
This line
$article_id = (int) JRequest::getVar('id', false);
gets the article id so we know what attachments should be displayed
These lines
$db =& JFactory::getDBO();
$query = "SELECT count(*) FROM #__attachments WHERE article_id='" . $article_id . "' AND published='1'";
$db->setQuery($query);
$total = $db->loadResult();
connect to the Joomla! database and get a list of attachments associated to the article.
These lines
jimport('joomla.application.component.helper');
$params = JComponentHelper::getParams('com_attachments');
connect to the component's helper and get the component's parameters.
This line
$attachments = AttachmentsHelper::attachmentsTableHTML($article_id, false, true, true, $from);
calls the appropriate function in the helper class to get the information we want.
This line
require_once(JPATH_SITE.DS.'modules'.DS.'mod_attachments'.DS.'tmpl'.DS.'default.php');
passes control over to the tmpl/default.php file to display the information.
tmpl/default.php
<?@php // no direct access
defined('_JEXEC') or die('Restricted access');
$document = & JFactory::getDocument();
$document->addStyleSheet( JURI::base() . 'modules/mod_attachments/styles/attachments.css', 'text/css', null, array() );
echo $attachments;
?>
Now all we need is a style sheet and some dummy index.html files, and we can install the module. Zip all the files listed in the XML file into an archive, and upload it through the back end.
| File |
|---|
Trackback(0)
TrackBack URI for this entryComments (5)
Subscribe to this comment's feedinstallation problem with XML
So what happens if I do everything as outlined above, and I get the following error message on installation:
Error! Could not find a Joomla! XML setup file in the package.
mod_attachments.xml is definitely included in the installation package, and I copied and pasted your code directly into the three new documents and placed them in the correct directories before zipping them up, so I'm not sure what that is about. Any help you could provide would be appreciated.
Thanks
...
Also, it could be your compression. I've seen some zip file types that Joomla! cannot read. Although, it's not likely this is the case.
See if you can install from my zip file here, or some other extension or template. If that fails, it's definitely a permission issue somewhere.
installation problem with XML
What happened was that I get a radio button with a blank title in the list, and an error message at the top which says: "XML Parsing Error at 1:3. Error 46: Processing Instruction not started".
mod_attachments
If you're getting an xml error on install, I'm not quite sure what would cause that. I've tested the installation of the file here several times on several different installs of Joomla! It maybe a permissions issue. Do you have that problem installing other extensions/templates? You may have some issue peculiar to your server.
I have seen cases where your /tmp directory has appropriate permissions, but your webserver user doesn't have permissions to write in the server cache temporary directory and so it can store the zip file to /tmp, but it cannot process it to open it up in temporary memory.
Try this. FTP the zip file to your server's /tmp folder. Make sure that is the only file in there. Then go to the installer in the back end and instead of browsing for the file, drop down in the box below, and try install from directory. Just put /tmp in there - no file name. It will look for and install any zip it finds in /tmp. Sometimes, you get more useful error messages that way.
installation problem with XML
We have had some permissions issues on this server which have hampered the installation of other components/modules/plugins which I never run into on other servers. I'm going to try installing the component and your module on another site of mine which is on a different server and see if I run into the same issue. I'll let you know how that turns out.
Write comment

Last Updated (Wednesday, 11 March 2009 23:19)





