Showing posts with label joomla. Show all posts
Showing posts with label joomla. Show all posts

Monday, September 13, 2010

Joomla 1.5 - Created date to mySql

Joomla! allows you to set date values through the Joomla! framework whenever you want to save a date to the database.

To use a the current date/time in your JModel entended class, do the following.

In the store() method after the $row->bind() method, add:

//Get a JDate object
$createdate = &JFactory::getDate();
//Create a date in MySQL's datetime format
$row->created = $createdate->toMySQL();
//PS: make sure your table have a created field :)



Joomla!'s JDate also allows you to set custom Unix dates simply by passing in the date to JFactory::getDate() method. E.g.

$createdate =& JFactory::getDate(time());

Note that you wil override the date everytime you save to the database. See if you can modify the model to set the create date only on new saves. :)

See Joomla!'s API documentation for more on what JDate can do.

http://api.joomla.org/Joomla-Framework/Utilities/JDate.html

Monday, September 6, 2010

Joomla 1.5 Additional select tag values

This continues on my blog post on how to add a select tag through the Joomla! framework. The code that follows will show you how to add to the ObjectList for a Select tag. This is especially useful where you have to use a select tag in a search form.

I normally use a value of 0 to indicate a search of "All Values".

The first thing you do is create a class at the top of your page as follows:

<?php
class MySelectObject{
var $id;
var $name;
}
?>



Then to use it do the following:

<?php
$db = &JFactory::getDBO();
$query = "SELECT `id`,`name` FROM
#__town";

$db->setQuery($query);
$townlist = $db->loadObjectList();

/*Create an instance of MySelectObject*/
$mso = new MySelectObject();

/*Set values*/
$mso->id = 0;
$mso->name = 'Any Town';

/*Create an array for insertion*/
$tmpArr = array(0 => $mso);

/*Merge with array result from query. Add to the front of list*/
$selectList =
array_merge($tmpArr,$townlist);

/*Make MySelectObject the default selected*/
echo
JHTML::_('Select.genericlist',$selectList,'town',"","id","name",0);


?>

Wednesday, October 14, 2009

Joomla 1.5 - Creating RAW output for the front end

If you want your component to create RAW output on the front end, like xml data to be returned for an AJAX POST/GET, then this is what you have to do.

1. In your component's views/view/ folder, add a view.raw.php file. E.g. if your component has a view called smile your will add view.raw.php to your views/smile/ folder . i.e. com_mycomponent/views/smile/view.raw.php

2. Whenever you call your component via a url simply append format=raw to the url. E.g. index.php?option=com_mycomponent&format=raw

Notes: For the backend administrator side, you can just specify format=raw without having to add the view.raw.php file to your views folder.

Monday, August 10, 2009

Joomla 1.5 Raising Errors, Notices and Warnings through code

Sometimes it is neccessary to inform an user that incorrect information was entered, like with a login or form validation or sometimes the users needs to be notified what fields needs to be filled in when the form gets loaded.


Making use of Joomla!'s JError class it is possible to display this kind of messages theough the Joomla! framework. To use simply call the JError::raiseError, JError::raiseNotice or JError::raiseWarning methods.


<?php
$msg = 'Error Message';
JError::raiseError(0,$msg);

$msg = 'Notice Message';
JError::raiseNotice(0,$msg);

$msg = 'Warning Message';
JError::raiseWarning(0,$msg);
?>


[update - 11 Feb 2010]
For more go to the following url:
http://docs.joomla.org/Display_error_messages_and_notices

Monday, May 25, 2009

Joomla! 1.5 Fixing the Tabs for IE

The basic tabs style does not really display properly in Internet Explorer. See my tutorial on how to create tabs through the Joomla! framework.

In Internet Explorer the dd and dt tags does not stack properly while it display properly in Firefox, Chrome, Opera and Safari.

To fix this issue, an IE only style is created and included in the site template's  index.php file, or the existing ieonly.css file in the template/css directory can be used, if there is such a file.
E.g.
<!--[if IE]>
<link href="<?=$this->baseurl?>/templates/<?=$this->template?>/css/ieonly.css" rel="stylesheet" type="text/css" />
<![endif]-->

In this css file the following code is added.
div.current { overflow:auto; }
div.current dd  { float:left; }

And the display issue is fixed!

Credits: I would like to credit alexwai for figuring out a solution for this. See thie forum thread.

Thursday, May 21, 2009

Joomla! 1.5 Adding to the Breadcrumb/PathWay through the Joomla Framework

Joomla's JPathway class allows you to add a new item to the pathway to be displayed in the Breadcrumb.

The first thing to do is to access the global JApplication object, then get a reference to the JPathway object.

<?php 
global $mainframe;
$breadcrumbs = &$mainframe->getPathWay();
?>
Adding a breadcrumb is now as simple as calling the JPathway::addItem() method. The addItem method takes a name as the first parameter and a link as the second parameter.
<?php 
$breadcrumbs->addItem( 'My Crumb!', JRoute::_('index.php?option=com_mycrumb') );
?>

If your Item is the last item on the breadcrumb the link will be ignored.

Tuesday, January 27, 2009

Joomla! 1.5 How to create Tabs through the Joomla Framework

It is quite easy to create tabs through the Joomla's Framwork.

According to the Joomla API documentation the class to use is the JPane class, specifically an instance of a JPaneTabs class.

The folloing code get the instance of a JPaneTabs class
<?php
//Inform Joomla we want to make use of panes
jimport('joomla.html.pane');
   //Get JPaneTabs instance
$myTabs = & JPane::getInstance('tabs', array('startOffset'=>0)); 
?>
The first parameter of JPane::getInstance() specify the apearance of the pane, in this case tabs. Another option is sliders. What the function basically do is append the first parameter to the JPane name and create an instanc of that class. E.g. 'JPane' + 'Tabs' give you JPaneTabs. Ths same is true for the Sliders.

The Second parameter specify the starting, or default, tab.

Once there is an instance of the JPaneTabs, Joomla need to know that it should start with a Tab pane. The JPaneTabs ::startPane($id) will be used with the first parameter a unique id. All panes needs to be accessed by Mootools which will use the unique id in order to draw the tabs correctly.

What is a pane without some tabs? The  JPaneTabs ::startPanel($title,$id) function will inform Joomla that a new tab should be created. IT takes a tab name or title as a first parameter and a unique id as the second parameter. (id is for Mootools).

An example implimentation will look as follows:

<?php
//Inform Joomla we want to make use of panes
jimport('joomla.html.pane');
//Get JPaneTabs instance
$myTabs = & JPane::getInstance('tabs', array('startOffset'=>0));
$output = '';
//Create Pane
$output .= $myTabs->startPane( 'pane' );
//Create 1st Tab
$output .= $myTabs->startPanel( '1st', 'tab1' );
$output  .= '<p>This is the first tab</p>';
$output .= $myTabs->endPanel();
//Create 2nd Tab
$output .= $myTabs->startPanel( '2nd', 'tab2' );
$output  .= '<p>This is the secondt tab</p>';
$output .= $myTabs->endPanel();
//Create 3rd Tab
$output .= $myTabs->startPanel( '3rd', 'tab3' );
$output  .= '<p>This is the third tab</p>';
$output .= $myTabs->endPanel();
//End Pane
$output .= $myTabs->endPane();
//Output to web
echo $output;
?>
Once the tabs have been created it is important to create styles for the id's specified in the code. It can be created in the template's template.css file.

Example styles will look as follows:
dl.tabs {
float: left;
margin: 10px 0 -1px 0;
z-index: 50;
}

dl.tabs dt {
float: left;
padding: 4px 10px;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
border-top: 1px solid #ccc;
margin-left: 3px;
background: #f0f0f0;
color: #666;
}

dl.tabs dt.open {
background: #F9F9F9;
border-bottom: 1px solid #F9F9F9;
z-index: 100;
color: #000;
}

div.current { 
clear: both; 
border: 1px solid #ccc; 
padding: 10px 10px;
}

div.current dd 
{ 
padding: 0; 
margin: 0;
}
And that is it.

Fun thing to do: Create sliders from the above code.

Tuesday, December 16, 2008

Joomla! 1.5 How to create a Select tag through the Joomla Framework

Today I needed to display a list of towns from a database table. Nowhere on the Joomla! documentation could I find an example on how to achieve that, so I had to go figure it out once again.

I have a database table that contains all my towns. First I had to create a query to retrieve the towns and put them in an object list.

The code for that follows.
<?php
$db =& JFactory::getDBO();

$query = 'SELECT `id`,`name` FROM #__town';
$db->setQuery( $query );
$townlist = $db->loadObjectList();
?>

Ok now that I have a list of towns I simply have to use JHTMLSelect.genericlist() to output the HTML code for my select.

The code to do that follows:

<?php
echo JHTML::_('Select.genericlist',$townlist,"town","","id","name",1);
?>


In the code I am simply creating a select from the town list with the options values set as id and the texts set as name (as per the query) . The 1 at the end indicates the selected option. 
If I for example get the selected option from a post/get request, the code will look as follows.

<?php
$selectedId = JRequest::getVar( 'id',0);
echo JHTML::_('Select.genericlist',$townlist,"town","","id","name",$selectedId);
?>

Thursday, December 4, 2008

Joomla! 1.5 How to cloak an email through code.

This one I figured out for myself and I am sure you will be able to do the sime... right. :)

Say for example you have a custom field in the database that stores an email and you want to display that email on a page somewhere. You don't really want to allow every spam or virus harvest bot out there to come and grab the email. That is why Joomla! created the cloak function. It just makes sense to protect ppl's emails.

To cloak the email simply enter the following code.


<?php
$email = 'some.email@some.domain';
$emcloaked = JHTML::_('Email.cloak',$email);
echo $emcloaked;
?>


And that is it.

Wednesday, October 22, 2008

Joomla! 1.5 Getting the modal dialog to work

Today I quickly want to show you how to get the Joomla! 1.5 Modal Dialog (SqueezeBox - Expandable Lightbox) to work in any of your components or modules. It's actually quite easy to do since the Joomla! team did such a great job at making mootools part of the solution. JHTML to the rescue! :)

In this example I'm going to make a normal anchor open up a Modal dialog. Are you ready?

Within your php code tags enter the following:

<?php
JHTML::_('behavior.modal', 'a.modal-button');
?>

Add an anchor and the following to the anchor:


<a href="http://www.google.com/" class="modal-button" rel="{handler: 'iframe', size: {x: 725, y: 520}}">MyMooModal</a>


And that is all there is to it. You can specify ANY link and it will open in the Modal Dialog

Friday, July 25, 2008

How to get the active component in Joomla! 1.5.x

For some time now I have been struggling to figure out how to get the active component in Joomla! 1.5.x without going through the GET/POST Requests. This problem was specifically for the frontpage.

What happens on the frontpage is that it is not possible to echo JRequest::getVar('option') and have the code tell you that you are on the frontpage. Instead the text that will be displayed will simply display com_content, which is the component for articles.

After searching throught the Joomla! Document Wiki I stumbled upon the solution; totally by chance I might add.

How to determine if the user is viewing the front page

The above document shows this piece of code:
$menu = & JSite::getMenu();
if ($menu->getActive() == $menu->getDefault())
{

echo 'This is the front page';
}
But what does the code actually do?

JSite::getMenu() returns a JMenu class called $menu that is inherited from the JObject class.

$menu->getActive() and $menu->getDefault() returns objects of the active and default menu items (by id).

If the active object is the same as the default object then naturally it must be the front page.

For other components the simplest option will still be to use the JRequest object to get the component name.

e.g. JRequest::getVar('option')
Note: I have not tested the above code with Search Engine Friendly URLs yet since they may, or may not, make a difference.