Data Management Made Easy Using Nennius: Advanced Data Handling - File Attachments
(Page 8 of 8 )
Now that our Comments component is finished, let's re-visit our News component. Begin by opening the news component's descriptor file, '/descriptors/news.php'. Once again our file contains only the required information, but by making a few simple modifications our component can gain a great deal of added functionality.
For instance: what if we wanted to add functionality for admin users to attach images to news releases, so that the our front-end system can display a photo gallery? What if we wanted the system to keep records of when each release was posted and modified, and by whom? We could do that very simply by adding a few lines to the end of our existing defaults file:
# set file attachment associations for component
# NOTE: these fields allows for zero to an infinite number of file attachments
# to be associated with component via a seperate indexing table.
$GLOBALS['g_optional_attachments_db_table'] = 'news_attachments';
$GLOBALS['g_optional_attachments_db_key'] = 'id';
$GLOBALS['g_optional_attachments_db_index'] = 'news_id';
$GLOBALS['g_optional_attachments_db_filename'] = 'filename';
# set record history table info (to record record creation & modification)
$GLOBALS['g_optional_history_db_table'] = 'news_history';
$GLOBALS['g_optional_history_db_key'] = 'id';
$GLOBALS['g_optional_history_db_index'] = 'news_id';
$GLOBALS['g_optional_history_db_user_id'] = 'user_id';
$GLOBALS['g_optional_history_db_datetime'] = 'datetime';
$GLOBALS['g_optional_history_db_description'] = 'description';
Using Hook Functions At this point our example 'news' application is complete, but our learning exercise is not. We still have not discussed another powerful feature available to Nennius developers: hook functions.
Nennius supports a fairly robust set of p-defined hook functions, allowing developers to enhance or override default behavior without modifying the Nennius engine directly. A hook function isn't needed for this example application, but we'll create one anyway in order to familiarize ourselves with how to do so. Our hook function will simply prevent the modification or deletion of any news article older than one week.
Please begin by opening the '/components/news.php' file and adding the following information:
#-----------------------------------------------------------------
# returns TRUE / FALSE, indicating if record is locked for edit / delete
#-----------------------------------------------------------------
function is_edit_lock() {
$f_component_details = NULL;
# retrieve necessary API objects from control class
$db_api =& $this->nennius_control->get_db_api();
# retrieve component's primary key
$f_component_details = $this->nennius_control->get_component_details();
$news_id = $f_component_details['id'];
# run query to determine the date of record creation
$db_query = " SELECT datetime
FROM `news`
WHERE id = $news_id
AND datetime < NOW() - 5184000";
$db_api->query( $db_query );
# if a result has been found, then record is too old to edit
if ( $db_api->get_num_rows() > 0 )
return TRUE;
# if no result has been found, record is editable
else return FALSE;
} # END is_edit_lock()
As you can see, the above function retrieves the current component's details from the Nennius Control class, runs a SQL query to determine if the record in question is older than one week, and then returns TRUE or FALSE, indicating whether the record is locked for deletion. This is a rather simple example, but it suggests the power available to Nennius developers through the use of hook functions.
Conclusion Although this article has taken a brief look at several of the advanced features available to Nennius developers, it is in no way a comprehensive list. The main purpose of the example application we have constructed was to demonstrate a variety of common ways Nennius may be configured to handle data. You are encouraged to take the finished product, and modify it to gain a better understanding of the configuration options we have discussed. You may also want to refer to the Nennius developer manual (http://nennius.sourceforge.net/dev_manual.php) for a more detailed list of hook functions and configuration options.
A completed version of the application we have just created is available online (http://nennius.sourceforge.net/nennius/webapps/news_mod/). You may also download the source code for the application (http://nennius.sourceforge.net/nennius/webapps/news_mod.zip). If you have any questions or would like to learn more about the Nennius application engine, please visit the Nennius website (http://nennius.sourceforge.net/) or check out the Nennius forums (http://nennius.sourceforge.net/phpbb/).
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |