Thursday, June 21, 2012

Joomla path variables (JPATH)


Following is the list of JPATH variables that you can use while component development in Joomla, do let me know if you find any other.

JURI::Root() = http://localhost/myJoomla or http://www.test.com
JPATH_COMPONENT_ADMINISTRATOR = /var/www/html/myjoomla/administrator/components/com_mycomp
JPATH_COMPONENT_SITE= /var/www/html/myjoomla/components/com_mycomp
JPATH_ROOT = /var/www/html/myjoomla
JPATH_SITE = /var/www/html/myjoomla
JPATH_ADMINISTRATOR = /var/www/html/myjoomla/administrator

JPATH_BASE is the root path for the current requested application, so if you are in the administrator application, JPATH_BASE == JPATH_ADMINISTRATOR... if you are in the site application JPATH_BASE == JPATH_SITE... if you are in the installation application JPATH_BASE == JPATH_INSTALLATION.



Wednesday, June 20, 2012

Joomla 2.5 extend jgrid.published column in custom component


Ever wanted to have a column/field like published in articles for your own component? Well here is how you can make one.
If you go to com_content and see how it is done there you will see that for published it uses the following line of code in views/articles/tmpl/default.php

<?php echo JHtml::_('jgrid.published', $item->state, $i, 'articles.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?>


Simplying it you can use it as

<?php echo JHtml::_('jgrid.published', $item->state, $i, 'articles.'); ?>

Which is

<?php echo JHtml::_('jgrid.published', $item->yourFieldName, $i, 'classPrefix.'); ?>

If you just put this one line you will be able to see that your Boolean field will start showing correctly, and especially if you field name is ‘published’ then you are done and you can click on it to toggle between publish and un publish, however if your field name is something other than published e.g. approved or available then you have some work to do.
First you will need to create a new class in com_componentName/helpers/html/className.php
This will be a JFieldClassName class having code like this
<?php
defined('_JEXEC') or die;
abstract class JHtmlClassName
{
                static function approved($value = 0, $i)
                {
                                $states = array(0=> array('disabled.png','tableName.approved','’,'Toggle to approve'),
                    1=> array('tick.png',    'tableName.unapproved', ',        'Toggle to unapprove'), );
                                $state   = JArrayHelper::getValue($states, (int) $value, $states[1]);
                                $html    = JHtml::_('image', 'admin/'.$state[0], JText::_($state[2]), NULL, true);                                                  $html    = '<a href="#" onclick="return listItemTask(\'cb'.$i.'\',\''.$state[1].'\')" title="'.JText::_($state[3]).'">'. $html.'</a>';
                }
                return $html;
                }
} ?>
Now to call this you will have to first register it for both approved/unapproved in controller’s contruct
com_componentName/controllers/classNames.php
                public function __construct($config = array())
                {
                                parent::__construct($config);
                                $this->registerTask('unapproved', 'approved');
                }
Followed by the function which will call the model to update the status:
        function approved()
        {
                                $ids        = JRequest::getVar('cid', array(), '', 'array');
                                $values = array('approved' => 1, 'unapproved' => 0);
                                $task     = $this->getTask();
                                $value   = JArrayHelper::getValue($values, $task, 0, 'int');
                                $model = $this->getModel();
                                if (!$model->approved($ids, $value)) {
                                                                JError::raiseWarning(500, $model->getError());
                                }
                                $this->setRedirect(url to get you back to the same page);
        }
Now its all up are ready and you can just create a function in your respective model which takes the value (which will be 0 or 1) and id and update the status , and finally you can use it by writing the following line in default.php
<?php echo JHtml::_('className.approved', $item->approved, $i, 'tableName.'); ?>
You will need to include the html helper file you created first by putting the following like at the top of default.php
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');