Thursday, January 23, 2014

Setting up a global state variable Joomla

Sometimes it is necessary to maintain a state variable across the site.
For example, to check whether Jquery is already loaded in another custom built component or module.

Setting up a state variable

$app = JFactory::getApplication();
$app->setUserState( 'myvar', $myvarvalue );
$app->setUserState( 'com_yourcomponent.data', $yourvalue );

Retrieving a variable saved

$app = JFactory::getApplication();
$variable = $app->getUserStateFromRequest( "com_yourcomponent.data" );

Tuesday, January 21, 2014

List documents in Joomla media manager

By normal, Joomla media manager does not default display the documents manager.

In some cases we need to show them in media manager to link to document.


  1. In administrator/com_media/views/imagelist.php, add the following function
          # Test to list PDF files
function setDocument($index = 0)
{
if (isset($this->documents[$index]))
{
$this->_tmp_document = &$this->documents[$index];
}
else
{
$this->_tmp_document = new JObject;
}
}

Add this in in the apt place in display function
# Test to list PDF files
$documents = $this->get('documents');
$this->documents = &$documents;

      2.  In administrator/com_media/views/tmpl/default.php, add this snippet next to image block
     
       <?php for ($i = 0, $n = count($this->documents); $i < $n; $i++) :
$this->setDocument($i);
echo $this->loadTemplate('document');
endfor; ?>   

      3.  Create a file 'default_document.php' inside administrator/com_media/views/tmpl/, and add this code

       <?php
       defined('_JEXEC') or die;
        
        $params = new JRegistry;
        $dispatcher = JEventDispatcher::getInstance();
        $dispatcher->trigger('onContentBeforeDisplay', array('com_media.file', &$this->_tmp_document, &$params));
        ?>
        <li class="imgOutline thumbnail height-80 width-80 center">
        <a class="img-preview" href="javascript:ImageManager.populateFields('<?php echo $this->_tmp_document->path_relative; ?>')" title="<?php echo $this->_tmp_document->name; ?>" >
        <div class="height-50">
        <!--<?php echo JHtml::_('image', $this->baseURL . '/' . $this->_tmp_document->path_relative, JText::sprintf('COM_MEDIA_IMAGE_TITLE', $this->_tmp_document->title, JHtml::_('number.bytes', $this->_tmp_document->size)), array('width' => $this->_tmp_document->width_60, 'height' => $this->_tmp_document->height_60)); ?>-->
        <?php echo JHtml::_('image', $this->_tmp_document->icon_32, $this->_tmp_document->name, null, true, true) ? JHtml::_('image', $this->_tmp_document->icon_32, $this->_tmp_document->title, null, true) : JHtml::_('image', 'media/con_info.png', $this->_tmp_document->name, null, true); ?></a>
        </div>
        <div class="small">
        <?php echo JHtml::_('string.truncate', $this->_tmp_document->name, 10, false); ?>
        </div>
        </a>
        </li>
        <?php
        $dispatcher->trigger('onContentAfterDisplay', array('com_media.file', &$this->_tmp_document, &$params));
        ?>




Saturday, January 11, 2014

CakePHP base URL

To access base URL in controller use
Router::url('/', true);

To access base URL in view use
<?php echo $this->webroot; ?>