Thursday, March 27, 2014

SQL Group By single character

SELECT LEFT(pcode, 2) AS pcode2 FROM addy GROUP BY pcode2

Wednesday, March 19, 2014

Removing an empty div using Jquery

$('.selector:empty').remove();

Another Way

$.expr[':'].blank = function(obj){ return obj.innerHTML.trim().length === 0; }; $('.selector:empty').remove();

Friday, February 7, 2014

Capitalize First character in a Javascript String

var capitalized = capitalizeMe.substr(0, 1).toUpperCase() + capitalizeMe.substr(1);

var capitalized = capitalizeMe[0].toUpperCase() + capitalizeMe.substring(1);

var capitalized = capitalizeMe.charAt(0).toUpperCase() + capitalizeMe.substring(1);

var capitalized = capitalizeMe.slice(0,1).toUpperCase() + capitalizeMe.slice(1, capitalizeMe.length);

var capitalized = capitalizeMe.substring(0, 1).toUpperCase() + capitalizeMe.substring(1);

Custom image for facebook share - Open in popup

<a class="facebook" target="_blank" onclick="return !window.open(this.href, 'Facebook', 'width=640,height=300')" href="http://www.facebook.com/sharer/sharer.php?u=YOUR_URL">Facebook</a>

Thursday, February 6, 2014

JQuery URL parser function

// Parse URL Queries Method
(function($){
    $.getQuery = function( query ) {
        query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var expr = "[\\?&]"+query+"=([^&#]*)";
        var regex = new RegExp( expr );
        var results = regex.exec( window.location.href );
        if( results !== null ) {
            return results[1];
            return decodeURIComponent(results[1].replace(/\+/g, " "));
        } else {
            return false;
        }
    };
})(jQuery);

// Document load
$(function(){
    var test_query = $.getQuery('test');
    alert(test_query); // For the URL /?test=yes, the value would be "yes"
});
Reference:

http://www.kevinleary.net/jquery-parse-url/

Change URL parameter value of a variable in a Javascript Regular Expression (RegEx)

function changeParamByName(href, paramName, newVal) {
var tmpRegex = new RegExp("(" + paramName + "=)[a-z]+", 'ig');
return href.replace(tmpRegex, '$1'+newVal);
}
 
var href = changeParamByName("http://domain.com?var=thisIsOld&bca=something&b=2&c=3","bca","theNewValue");
console.log(href);



Reference:

http://blog.adrianlawley.com/jquery-change-url-parameter-value/

Changing the URL of parent

In some cases to make the parent window to load a different URL ( from IFRAME and pop etc.,), use

window.top.location.href = "http://www.site.com"; 
To just refresh the parent window use

parent.location.reload();

To get the parent URL as a string (in cases to modify parent URL), use this,

parentlocation = document.referrer;


Javascript function to get parent URL

This is usefull in case of finding parent URL from iframe or a popup etc.,

function getParentUrl() {
    var isInIframe = (parent !== window),
        parentUrl = null;

    if (isInIframe) {
        parentUrl = document.referrer;
    }

    return parentUrl;
}

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; ?>