Bài đăng

Đang hiển thị bài đăng từ tháng 12 30, 2007

Utility function for writing a menu link in Joomla

&lt ?php /*** Utility function for writing a menu link */ function mosGetMenuLink( $mitem, $level=0, &$params, $open=null ) { global $Itemid, $mosConfig_live_site, $mainframe; $txt = ''; $image_link = 0; switch ($mitem->type) { case 'separator': case 'component_item_link': break; case 'url': if ( eregi( 'index.php\?', $mitem->link ) ) { if ( !eregi( 'Itemid=', $mitem->link ) ) { $mitem->link .= '&Itemid='. $mitem->id; } } break; case 'content_item_link': case 'content_typed': // load menu params $menuparams = new mosParameters( $mitem->params, $mainframe->getPath( 'menu_xml', $mitem->type ), 'men...

Model-View-Controller Component - Part 1

Model-View-Controller Component - Part 1 Introduction The new framework in Joomla! 1.5 unleashes a great deal of power for developers. The code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using this new framework. The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla! Requirements You need Joomla! 1.5 or greater for this tutorial. Introduction to Model-View-Controller While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized. Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that...

Model-View-Controller Component - Part 2 - Adding a Model

Model-View-Controller Component - Part 2 - Adding a Model Introduction In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated. In the first tutorial, the greeting was hardcoded into the view. This doesn’t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it. In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides. Creating the Model The concept of model gets its name because this class is intended to represent (or ‘model’) some entity. In our case, our first model will represent a ‘hello’, or a greeting. This is in line with our design thus far, because we have one view (’hello’), which is a view of our greeting. The naming convention for models in the Joomla! framework is that the class name starts with the...

Model-View-Controller Component - Part 3 - Using the Database

Model-View-Controller Component - Part 3 - Using the Database Introduction In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database. This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database. Retrieving the Data Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting. To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statem...

Model-View-Controller Component - Part 4 Creating an Administrator Interface

an Administrator Interface Introduction In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database. Creating the Basic Framework The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is admin.hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is...

How to use the database classes in your script

Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer. 1. Why should I use the Joomla database class? Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension. 2. Preparating the query Database usage // Get a database object $db = JFactory::getDBO(); $query = "SELECT * FROM #__example_table WHERE id = 999999"; $db->setQuery($query); First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-pre...

Calling different scripts in Joomla

Now we’ve changed a lot of our database and variable-retrieving code, but we still don’t know how to properly call our different script-files our former script consists of.If you have more than just one file in your script, you seem to have a problem calling those inside of Joomla. Joomla uses a case switching for this: Case switching for multiple scripts // no direct access defined( '_VALID_MOS' ) or die( 'Restricted access' ); require_once( $mainframe->getPath( 'front_html' ) ); $task = mosGetParam( $_REQUEST, 'task', 'view' ); switch ($task) { case 'edit': include('edit.php'); break; case 'save': include('save.php'); break; case 'view': default: include('view.php'); break; } ?> As you can see, we first read the value of the POST-variable “task” and then differentiate between the different tasks at hand. When we find our correct task, we link to the file with the corr...

How to determine if the user is viewing the front page

In Joomla! 1.0.x it was possible to determine if the user was viewing the front page by using code like this: Determine if user is viewing the front page in Joomla! 1.0.x &lt?php if ($option == 'com_frontpage' || $option == '') { echo 'This is the front page'; } ?> But in Joomla! 1.5.x the com_frontpage component is no longer present. This is how to achieve the same result in Joomla! 1.5.x Determine if user is viewing the front page in Joomla! 1.5.x &lt?php $menu = & JSite::getMenu(); if ($menu->getActive() == $menu->getDefault()) { echo 'This is the front page'; } ?> This works by checking to see if the current active menu item is the default one.

here are the steps to display in joomla 1.5

hello.php includes the controller.php and invokes its execute() method. This method will invoke the task handling method() depending on the task passed from the request (in the link). The default task is display(). The display() method is built into the JController class. The controller class name is HelloController. The view name is Hello. The display() method will take the Hello off the front of the controller class name, append View to it, and then add the view name, which is Hello, thus HelloViewHello. It will look for this in the directory views/{viewname}, so in this case, views/hello. It will look for a file called view.html.php. So, here are the steps: 1. hello.php 2. HelloController->execute() 3. HelloController->display() 4. JController->display() 5. HelloViewHello->display() Good to hear you've had some success, and hopefully I can help you out with things you're still working on. What you have hit is one of the limitations of doing a Hello World ex...